Skip to content

Commit

Permalink
[Android] Update Android build scripts & app download tasks (PaddlePa…
Browse files Browse the repository at this point in the history
…ddle#835)

* [Android] Add ERNIE3.0 intent and slot task jni support

* [Bug Fix] fix app sdk download tasks

* [Bug Fix] fix app sdk download tasks

* [JNI] remove empty .cc files

* [Android] Update Android build scripts
  • Loading branch information
DefTruth authored Dec 9, 2022
1 parent 4ccec07 commit 195132a
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cmake/paddlelite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ endif()
if(WIN32 OR APPLE OR IOS)
message(FATAL_ERROR "Doesn't support windows/mac/ios platform with backend Paddle Lite now.")
elseif(ANDROID)
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-android-${ANDROID_ABI}-latest.tgz")
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-android-${ANDROID_ABI}-latest-dev.tgz")
if(ANDROID_ABI MATCHES "arm64-v8a")
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-android-${ANDROID_ABI}-fp16-latest.tgz")
set(PADDLELITE_URL "${PADDLELITE_URL_PREFIX}/lite-android-${ANDROID_ABI}-fp16-latest-dev.tgz")
endif()
else() # Linux
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
Expand Down
24 changes: 14 additions & 10 deletions java/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ task downloadAndExtractModels(type: DefaultTask) {
String[] modelPaths = model.src.split("/")
String modelName = modelPaths[modelPaths.length - 1]
String modelPrefix = modelName.substring(0, modelName.length() - 4)
// Download the target model if not exists
boolean copyFiles = !file("${model.dest}/${modelPrefix}").exists()
if (!file("${cachePath}/${modelName}").exists()) {
println "[INFO] Downloading ${model.src} -> ${cachePath}/${modelName}"
ant.get(src: model.src, dest: file("${cachePath}/${modelName}"))
boolean copyFiles = false
if (!file("${model.dest}/${modelPrefix}").exists()) {
// Download the target model if not exists
if (!file("${cachePath}/${modelName}").exists()) {
println "[INFO] Downloading ${model.src} -> ${cachePath}/${modelName}"
ant.get(src: model.src, dest: file("${cachePath}/${modelName}"))
}
copyFiles = true
}
if (copyFiles) {
Expand Down Expand Up @@ -127,11 +129,13 @@ task downloadAndExtractSDKs(type: DefaultTask) {
FD_JAVA_SDK.eachWithIndex { sdk, index ->
String[] sdkPaths = sdk.src.split("/")
String sdkName = sdkPaths[sdkPaths.length - 1]
// Download the target SDK if not exists
boolean copyFiles = !file("${sdk.dest}/${sdkName}").exists()
if (!file("${cachePath}/${sdkName}").exists()) {
println "[INFO] Downloading ${sdk.src} -> ${cachePath}/${sdkName}"
ant.get(src: sdk.src, dest: file("${cachePath}/${sdkName}"))
boolean copyFiles = false
if (!file("${sdk.dest}/${sdkName}").exists()) {
// Download the target SDK if not exists
if (file("${cachePath}/${sdkName}").exists()) {
println "[INFO] Downloading ${sdk.src} -> ${cachePath}/${sdkName}"
ant.get(src: sdk.src, dest: file("${cachePath}/${sdkName}"))
}
copyFiles = true
}
if (copyFiles) {
Expand Down
10 changes: 6 additions & 4 deletions java/android/fastdeploy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ task downloadAndExtractLibs(type: DefaultTask) {
String[] libPaths = lib.src.split("/")
String libName = libPaths[libPaths.length - 1]
libName = libName.split("\\.")[0]
boolean copyFiles = !file("${lib.dest}/${libName}").exists()
if (!file("${cachePath}/${libName}.tgz").exists()) {
println "[INFO] Downloading ${lib.src} -> ${cachePath}/${libName}.tgz"
ant.get(src: lib.src, dest: file("${cachePath}/${libName}.tgz"))
boolean copyFiles = false
if (!file("${lib.dest}/${libName}").exists()) {
if (!file("${cachePath}/${libName}.tgz").exists()) {
println "[INFO] Downloading ${lib.src} -> ${cachePath}/${libName}.tgz"
ant.get(src: lib.src, dest: file("${cachePath}/${libName}.tgz"))
}
copyFiles = true
}
if (copyFiles) {
Expand Down
9 changes: 2 additions & 7 deletions scripts/android/build_android_aar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
set -e
set +x

PACKAGE_VERSION=$1
FASTDEPLOY_DIR=$(pwd)
BUILT_PACKAGE_DIR=build/Android
CXX_PACKAGE_PREFIX=fastdeploy-android-${PACKAGE_VERSION}-shared
CXX_PACKAGE_PREFIX=fastdeploy-android-latest-dev-shared
CXX_PACKAGE_NAME=${BUILT_PACKAGE_DIR}/${CXX_PACKAGE_PREFIX}
ARMV8_CXX_PACKAGE_NAME=${BUILT_PACKAGE_DIR}/arm64-v8a-api-21/install
ARMV7_CXX_PACKAGE_NAME=${BUILT_PACKAGE_DIR}/armeabi-v7a-api-21/install

# check package name
echo "[INFO] --- FASTDEPLOY_DIR: ${FASTDEPLOY_DIR}"
if [ "$PACKAGE_VERSION" = "dev" ]; then
CXX_PACKAGE_PREFIX=fastdeploy-android-latest-shared-dev
CXX_PACKAGE_NAME=${BUILT_PACKAGE_DIR}/fastdeploy-android-latest-shared-dev
fi

# check arm v7 & v8 c++ sdk
if [ ! -d "${BUILT_PACKAGE_DIR}" ]; then
Expand Down Expand Up @@ -76,4 +71,4 @@ echo "[INFO] --- Built java aar package!"
ls -lh ${JNI_LIB_DIR}/build/outputs/aar/

# Usage:
# ./scripts/android/build_android_aar.sh dev
# ./scripts/android/build_android_aar.sh
6 changes: 3 additions & 3 deletions scripts/android/build_android_app.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# build java aar package
# build FastDeploy app
FASTDEPLOY_DIR=$(pwd)
JAVA_ANDROID_DIR=${FASTDEPLOY_DIR}/java/android
JNI_LIB_DIR=${JAVA_ANDROID_DIR}/fastdeploy
Expand All @@ -7,7 +7,7 @@ APP_DIR=${JAVA_ANDROID_DIR}/app
APP_LIBS_DIR=${APP_DIR}/libs

cd ${JAVA_ANDROID_DIR}
# check aar package
# check aar package first!
echo "[INFO] --- JAVA_ANDROID_DIR: ${JAVA_ANDROID_DIR}"
if [ ! -d "${JNI_LIB_DIR}/build/outputs/aar" ]; then
echo "-- [ERROR] ${JNI_LIB_DIR} not exists, please build aar package first!"
Expand All @@ -28,7 +28,7 @@ if [ -f "${APP_LIBS_DIR}/fastdeploy-android-sdk-latest-dev.aar" ]; then
echo "[INFO] --- Update aar package done!"
fi
# build android app
echo "[INFO] --- Building FastDeploy Android app ..."
echo "[INFO] --- Building FastDeploy Android App ..."
chmod +x gradlew
./gradlew app:assembleDebug
echo "[INFO] --- Built FastDeploy Android app."
Expand Down
2 changes: 1 addition & 1 deletion scripts/android/build_android_cpp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ __build_fastdeploy_android_shared() {
-DENABLE_LITE_BACKEND=ON \
-DENABLE_PADDLE_FRONTEND=OFF \
-DENABLE_FLYCV=ON \
-DENABLE_TEXT=ON \
-DENABLE_TEXT=OFF \
-DENABLE_VISION=ON \
-DENABLE_VISION_VISUALIZE=ON \
-DBUILD_EXAMPLES=ON \
Expand Down
118 changes: 118 additions & 0 deletions scripts/android/build_android_cpp_with_text_api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash
set -e
set +x

# -------------------------------------------------------------------------------
# mutable global variables
# -------------------------------------------------------------------------------
TOOLCHAIN=clang # gcc/clang toolchain

# -------------------------------------------------------------------------------
# readonly global variables
# -------------------------------------------------------------------------------
readonly ROOT_PATH=$(pwd)
readonly ANDROID_ABI=$1
readonly ANDROID_PLATFORM="android-$2"
readonly BUILD_ROOT=build/Android
readonly BUILD_DIR=${BUILD_ROOT}/${ANDROID_ABI}-api-$2

# -------------------------------------------------------------------------------
# tasks
# -------------------------------------------------------------------------------
__make_build_dir() {
if [ ! -d "${BUILD_DIR}" ]; then
echo "-- [INFO] BUILD_DIR: ${BUILD_DIR} not exists, setup manually ..."
if [ ! -d "${BUILD_ROOT}" ]; then
mkdir -p "${BUILD_ROOT}" && echo "-- [INFO] Created ${BUILD_ROOT} !"
fi
mkdir -p "${BUILD_DIR}" && echo "-- [INFO] Created ${BUILD_DIR} !"
else
echo "-- [INFO] Found BUILD_DIR: ${BUILD_DIR}"
fi
}

__check_cxx_envs() {
if [ $LDFLAGS ]; then
echo "-- [INFO] Found LDFLAGS: ${LDFLAGS}, \c"
echo "unset it before crossing compiling ${ANDROID_ABI}"
unset LDFLAGS
fi
if [ $CPPFLAGS ]; then
echo "-- [INFO] Found CPPFLAGS: ${CPPFLAGS}, \c"
echo "unset it before crossing compiling ${ANDROID_ABI}"
unset CPPFLAGS
fi
if [ $CPLUS_INCLUDE_PATH ]; then
echo "-- [INFO] Found CPLUS_INCLUDE_PATH: ${CPLUS_INCLUDE_PATH}, \c"
echo "unset it before crossing compiling ${ANDROID_ABI}"
unset CPLUS_INCLUDE_PATH
fi
if [ $C_INCLUDE_PATH ]; then
echo "-- [INFO] Found C_INCLUDE_PATH: ${C_INCLUDE_PATH}, \c"
echo "unset it before crossing compiling ${ANDROID_ABI}"
unset C_INCLUDE_PATH
fi
}

__set_android_ndk() {
if [ -z $ANDROID_NDK ]; then
echo "-- [INFO] ANDROID_NDK not exists, please setup manually ..."
exit 0
else
echo "-- [INFO] Found ANDROID_NDK: ${ANDROID_NDK}"
fi
if [ "$ANDROID_NDK" ]; then
NDK_VERSION=$(echo $ANDROID_NDK | egrep -o "[0-9]{2}" | head -n 1)
if [ "$NDK_VERSION" -gt 17 ]; then
TOOLCHAIN=clang
fi
echo "-- [INFO] Checked ndk version: ${NDK_VERSION}"
echo "-- [INFO] Selected toolchain: ${TOOLCHAIN}"
fi
}

__build_fastdeploy_android_shared() {

local ANDROID_STL=c++_shared # c++_static
local ANDROID_TOOLCHAIN=${TOOLCHAIN}
local TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake
local FASDEPLOY_INSTALL_DIR="${ROOT_PATH}/${BUILD_DIR}/install"
cd "${BUILD_DIR}" && echo "-- [INFO] Working Dir: ${PWD}"

cmake -DCMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_FILE} \
-DCMAKE_BUILD_TYPE=MinSizeRel \
-DANDROID_ABI=${ANDROID_ABI} \
-DANDROID_NDK=${ANDROID_NDK} \
-DANDROID_PLATFORM=${ANDROID_PLATFORM} \
-DANDROID_STL=${ANDROID_STL} \
-DANDROID_TOOLCHAIN=${ANDROID_TOOLCHAIN} \
-DENABLE_ORT_BACKEND=OFF \
-DENABLE_LITE_BACKEND=ON \
-DENABLE_PADDLE_FRONTEND=OFF \
-DENABLE_FLYCV=ON \
-DENABLE_TEXT=ON \
-DENABLE_VISION=ON \
-DENABLE_VISION_VISUALIZE=ON \
-DBUILD_EXAMPLES=ON \
-DWITH_OPENCV_STATIC=OFF \
-DWITH_LITE_STATIC=OFF \
-DWITH_OPENMP=OFF \
-DCMAKE_INSTALL_PREFIX=${FASDEPLOY_INSTALL_DIR} \
-Wno-dev ../../.. && make -j8 && make install

echo "-- [INFO][built][${ANDROID_ABI}][${BUILD_DIR}/install]"
}

main() {
__make_build_dir
__check_cxx_envs
__set_android_ndk
__build_fastdeploy_android_shared
exit 0
}

main

# Usage:
# ./scripts/android/build_android_cpp_with_text_api.sh arm64-v8a 21
# ./scripts/android/build_android_cpp_with_text_api.sh armeabi-v7a 21

0 comments on commit 195132a

Please sign in to comment.