build-android-arm64-v8a.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. -DCMAKE_INSTALL_PREFIX=./install \
  44. -DANDROID_ABI="arm64-v8a" \
  45. -DANDROID_PLATFORM=android-21 ..
  46. make VERBOSE=1 -j4
  47. make install/strip
  48. # Note: If you set SHERPA_NCNN_ENABLE_BINARY=ON, it will
  49. # generate a binary sherpa-ncnn in $dir/install/bin, which can be run on Android
  50. # with the following commands:
  51. #
  52. # 1. Copy sherpa-ncnn and its dependencies to Android
  53. #
  54. # cd build-android-arm64-v8a/install/lib
  55. # adb push ./lib*.so /data/local/tmp
  56. # cd ../bin
  57. # adb push ./sherpa-ncnn /data/local/tmp
  58. #
  59. # 2. Login into Android
  60. #
  61. # adb shell
  62. # cd /data/local/tmp
  63. # ./sherpa-ncnn
  64. #
  65. # which shows the following error log:
  66. #
  67. # CANNOT LINK EXECUTABLE "./sherpa-ncnn": library "libsherpa-ncnn-core.so" not found: needed by main executable
  68. #
  69. # Please run:
  70. #
  71. # export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
  72. #
  73. # and then you can run:
  74. #
  75. # ./sherpa-ncnn
  76. #
  77. # It should show the help message of sherpa-ncnn.