Android NDK-JNI的一些使用技巧
如何在现有项目中加入*.so共享库
- 方式一
- 在”app/src/main/“目录中新建”jniLibs”,将共享库复制到此目录
- 在android studio中sync files即可
- 方式二
- 在”app/src/main/“目录中新建”libs”,将共享库复制到此目录
- 在app/build.gradle中添加libs文件夹路径
1 | android { |
如何在现有项目中加入JNI源码
- 方式一:使用android studio添加
鼠标右击app工程,选择Add C++ to Module
选择Link an existing CMakeLists.txt or Android.mk to this module
- 方式二:直接在app/build.gradle中添加代码,其中NDK Version需要更改为android studio设置中指定NDK路径中的版本
1 | android { |
JNI源码如何生成*.so共享库
找到NDK路径中找到”ndk-build.cmd”,比如我的路径为”C:\Users\ASUS\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd”
在Terminal或者DOS控制到中,CD 到JNI源码位置,然后执行”C:\Users\ASUS\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17D:\work\App\Myjni\app\src\main\jni>"C:\Users\ASUS\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd"
fcntl(): Bad file descriptor
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-16.
Android NDK: WARNING: APP_PLATFORM android-16 is higher than android:minSdkVersion 1 in D:/work/App/Myjni/app/src/main/AndroidManifest.xml. NDK binaries will *not* be compatible with devices older than android-16. See https://android.googlesource.com/platform
/ndk/+/master/docs/user/common_problems.md for more information.
Android NDK: WARNING: Unsupported source file extensions in D:/work/App/Myjni/app/src/main/jni/Android.mk for module i2chmi
Android NDK: :
Android NDK: WARNING: Unsupported source file extensions in D:/work/App/Myjni/app/src/main/jni/Android.mk for module i2chmi
Android NDK: :
Android NDK: WARNING: Unsupported source file extensions in D:/work/App/Myjni/app/src/main/jni/Android.mk for module i2chmi
Android NDK: :
Android NDK: WARNING: Unsupported source file extensions in D:/work/App/Myjni/app/src/main/jni/Android.mk for module i2chmi
Android NDK: :
[arm64-v8a] Install : libi2chmi.so => libs/arm64-v8a/libi2chmi.so
[x86_64] Install : libi2chmi.so => libs/x86_64/libi2chmi.so
[armeabi-v7a] Install : libi2chmi.so => libs/armeabi-v7a/libi2chmi.so
[x86] Install : libi2chmi.so => libs/x86/libi2chmi.so