build-android-arm64-v8a.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env bash
  2. set -ex
  3. dir=build-android-arm64-v8a
  4. mkdir -p $dir
  5. cd $dir
  6. # Note from https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-android
  7. # (optional) remove the hardcoded debug flag in Android NDK android-ndk
  8. # issue: https://github.com/android/ndk/issues/243
  9. #
  10. # open $ANDROID_NDK/build/cmake/android.toolchain.cmake for ndk < r23
  11. # or $ANDROID_NDK/build/cmake/android-legacy.toolchain.cmake for ndk >= r23
  12. #
  13. # delete "-g" line
  14. #
  15. # list(APPEND ANDROID_COMPILER_FLAGS
  16. # -g
  17. # -DANDROID
  18. if [ -z $ANDROID_NDK ]; then
  19. ANDROID_NDK=/ceph-fj/fangjun/software/android-sdk/ndk/21.0.6113669
  20. # or use
  21. # ANDROID_NDK=/ceph-fj/fangjun/software/android-ndk
  22. #
  23. # Inside the $ANDROID_NDK directory, you can find a binary ndk-build
  24. # and some other files like the file "build/cmake/android.toolchain.cmake"
  25. if [ ! -d $ANDROID_NDK ]; then
  26. # For macOS, I have installed Android Studio, select the menu
  27. # Tools -> SDK manager -> Android SDK
  28. # and set "Android SDK location" to /Users/fangjun/software/my-android
  29. ANDROID_NDK=/Users/fangjun/software/my-android/ndk/22.1.7171670
  30. fi
  31. fi
  32. if [ ! -d $ANDROID_NDK ]; then
  33. echo Please set the environment variable ANDROID_NDK before you run this script
  34. exit 1
  35. fi
  36. echo "ANDROID_NDK: $ANDROID_NDK"
  37. sleep 1
  38. cmake -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
  39. -DCMAKE_BUILD_TYPE=Release \
  40. -DBUILD_SHARED_LIBS=ON \
  41. -DSHERPA_NCNN_ENABLE_PORTAUDIO=OFF \
  42. -DSHERPA_NCNN_ENABLE_BINARY=OFF \
  43. -DSHERPA_NCNN_ENABLE_TEST=OFF \
  44. -DSHERPA_NCNN_ENABLE_C_API=OFF \
  45. -DSHERPA_NCNN_ENABLE_GENERATE_INT8_SCALE_TABLE=OFF \
  46. -DCMAKE_INSTALL_PREFIX=./install \
  47. -DANDROID_ABI="arm64-v8a" \
  48. -DANDROID_PLATFORM=android-21 ..
  49. make VERBOSE=1 -j4
  50. make install/strip
  51. # Note: If you set SHERPA_NCNN_ENABLE_BINARY=ON, it will
  52. # generate a binary sherpa-ncnn in $dir/install/bin, which can be run on Android
  53. # with the following commands:
  54. #
  55. # 1. Copy sherpa-ncnn and its dependencies to Android
  56. #
  57. # cd build-android-arm64-v8a/install/lib
  58. # adb push ./lib*.so /data/local/tmp
  59. # cd ../bin
  60. # adb push ./sherpa-ncnn /data/local/tmp
  61. #
  62. # 2. Login into Android
  63. #
  64. # adb shell
  65. # cd /data/local/tmp
  66. # ./sherpa-ncnn
  67. #
  68. # which shows the following error log:
  69. #
  70. # CANNOT LINK EXECUTABLE "./sherpa-ncnn": library "libsherpa-ncnn-core.so" not found: needed by main executable
  71. #
  72. # Please run:
  73. #
  74. # export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
  75. #
  76. # and then you can run:
  77. #
  78. # ./sherpa-ncnn
  79. #
  80. # It should show the help message of sherpa-ncnn.