JNI Usage method 2 (NDK cross-compile, compile android runnable library)

QQ group: 686809487

If you want to cross-compile an Android program or library, instead of using GCC, you need to use the cross-compile tool chain provided by the Android NDK

Download the Android NDK and configure the environment variable NDK_ROOT to point to the root directory of the NDK

You can then install the cross-compile toolchain with the following command

$NDK_ROOT/build/tools/make-standalone-toolchain.sh \ --platform=android-19 \ - install dir = $HOME/Android/standalone - toolchains/Android - toolchain - arm \ = arm - Linux - androideabi - 4.9 - the toolchain \ --stl=gnustlCopy the code

If you want to change a different CPU, the toolchain parameter specifies the toolchain

Toolchains in NDK directory

Armeabi-v7a can be compiled with -march=armv7-a

arm-linux-androideabi-gcc -march=armv7-a -shared -fPIC test.c -o test.so
Copy the code

Compilation mode 1

$NDK_JNI/android-toolchain-arm/bin/arm-linux-androideabi-g++ -c -fPIC -I ${JAVA_HOME}/include/ -I ${JAVA_HOME}/include/linux/ com_taxiao_cn_apple_jni_NativeMainJNI.cpp -o com_taxiao_cn_apple_jni_NativeMainJNI.o

$NDK_JNI/android-toolchain-arm/bin/arm-linux-androideabi-g++ -march=armv7-a -shared -fPIC -o libnative.so com_taxiao_cn_apple_jni_NativeMainJNI.o -lc
Copy the code

Compilation Mode 2

$NDK_JNI/android-toolchain-arm/bin/arm-linux-androideabi-g++ -shared -fPIC com_taxiao_cn_apple_jni_NativeMainJNI.cpp -o libnative.so
Copy the code

Android studio will run the library “libc++_shared.so” not found

Solutions:

Copy libc++_shared.so from the NDK directory into your Android studio application

/home/build/workspace/hqq/vision/vision-app/ndk/android-ndk-r21b/sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_sha red.so

Wechat official account –>> He Xiao (welcome to join)