Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] add Android CI build scripts #805

Merged
merged 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions scripts/android/build_android_aar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash
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_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
echo "[ERROR] --- ${BUILT_PACKAGE_DIR} not exist, please build c++ sdk first!"
exit 0
fi
if [ ! -d "${ARMV8_CXX_PACKAGE_NAME}" ]; then
echo "[ERROR] --- ${ARMV8_CXX_PACKAGE_NAME} not exist, please build c++ sdk first!"
exit 0
fi
if [ ! -d "${ARMV7_CXX_PACKAGE_NAME}" ]; then
echo "[ERROR] --- ${ARMV7_CXX_PACKAGE_NAME} not exist, please build c++ sdk first!"
exit 0
fi

# remove old package
echo "[INFO] --- Packing ${CXX_PACKAGE_NAME} package ..."
if [ -d "${CXX_PACKAGE_NAME}" ]; then
echo "[INFO] --- Removed old package done !"
rm ${CXX_PACKAGE_NAME}.tgz
rm -rf ${CXX_PACKAGE_NAME}
fi

# package latest c++ sdk
mkdir ${CXX_PACKAGE_NAME}
echo "[INFO] --- Collecting package contents ..."
cp -r ${ARMV7_CXX_PACKAGE_NAME}/* ${CXX_PACKAGE_NAME}/
cp -r ${ARMV8_CXX_PACKAGE_NAME}/* ${CXX_PACKAGE_NAME}/
rm -rf ${CXX_PACKAGE_NAME}/examples
echo "[INFO] --- Removed examples files ..."
echo "[INFO] --- Removing static .a files: "
find ${CXX_PACKAGE_NAME}/third_libs/install/ -name "*.a"
rm $(find ${CXX_PACKAGE_NAME}/third_libs/install/ -name "*.a")
echo "[INFO] --- Taring ${CXX_PACKAGE_NAME}.tgz package ..."
tar -zcvf ${CXX_PACKAGE_NAME}.tgz ${CXX_PACKAGE_NAME}/* >> ${BUILT_PACKAGE_DIR}/pkg.log 2>&1
echo "[INFO] --- Package ${CXX_PACKAGE_NAME}.tgz done ! Package size info: "
du -sh ${BUILT_PACKAGE_DIR}/* | grep ${CXX_PACKAGE_PREFIX}

# update c++ sdk to jni lib
echo "[INFO] --- Update c++ sdk for jni lib ..."
JAVA_ANDROID_DIR=${FASTDEPLOY_DIR}/java/android
JNI_LIB_DIR=${JAVA_ANDROID_DIR}/fastdeploy
CXX_LIB_FOR_JNI_DIR=${JNI_LIB_DIR}/libs/${CXX_PACKAGE_PREFIX}
if [ -d "${CXX_LIB_FOR_JNI_DIR}" ]; then
rm -rf ${CXX_LIB_FOR_JNI_DIR}
echo "[INFO] --- Remove old ${CXX_LIB_FOR_JNI_DIR} done!"
fi
cp -r ${CXX_PACKAGE_NAME} ${JNI_LIB_DIR}/libs
echo "[INFO] --- Update ${CXX_LIB_FOR_JNI_DIR} done!"

# build java aar package
cd ${JAVA_ANDROID_DIR}
echo "[INFO] --- JAVA_ANDROID_DIR: ${JAVA_ANDROID_DIR}"
echo "[INFO] --- Building java aar package ... "
chmod +x gradlew
./gradlew fastdeploy:assembleDebug
echo "[INFO] --- Built java aar package!"
ls -lh ${JNI_LIB_DIR}/build/outputs/aar/

# Usage:
# ./scripts/android/build_android_aar.sh dev
37 changes: 37 additions & 0 deletions scripts/android/build_android_app.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# build java aar package
FASTDEPLOY_DIR=$(pwd)
JAVA_ANDROID_DIR=${FASTDEPLOY_DIR}/java/android
JNI_LIB_DIR=${JAVA_ANDROID_DIR}/fastdeploy
AAR_DEBUG_PACKAGE=${JNI_LIB_DIR}/build/outputs/aar/fastdeploy-debug.aar
APP_DIR=${JAVA_ANDROID_DIR}/app
APP_LIBS_DIR=${APP_DIR}/libs

cd ${JAVA_ANDROID_DIR}
# check aar package
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!"
exit 0
fi
ls -lh ${JNI_LIB_DIR}/build/outputs/aar/
if [ ! -d "${APP_LIBS_DIR}" ]; then
mkdir -p "${APP_LIBS_DIR}" && echo "-- [INFO] Created ${APP_LIBS_DIR} !"
fi
# update aar package
echo "[INFO] --- Update aar package ..."
if [ -f "${APP_LIBS_DIR}/fastdeploy-android-sdk-latest-dev.aar" ]; then
rm -f "${APP_LIBS_DIR}/fastdeploy-android-sdk-latest-dev.aar"
echo "[INFO] --- Removed old aar package: ${APP_LIBS_DIR}/fastdeploy-android-sdk-latest-dev.aar"
fi
cp ${AAR_DEBUG_PACKAGE} ${APP_LIBS_DIR}/fastdeploy-android-sdk-latest-dev.aar
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 ..."
chmod +x gradlew
./gradlew app:assembleDebug
echo "[INFO] --- Built FastDeploy Android app."

# Usage:
# ./scripts/android/build_android_app.sh
118 changes: 118 additions & 0 deletions scripts/android/build_android_cpp.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.sh arm64-v8a 21
# ./scripts/android/build_android_cpp.sh armeabi-v7a 21