开发环境,Android studio 2.1.2,SDK环境和NDK环境
SDK不说了,NDK环境配置网上也有很多,这里就简单记录下:
1 下载NDK并安装到电脑 D:\Android\android-ndk-r11c
2 android studio中右键项目进入菜单配置NDK
3 在项目的gradle.properties文件中加上 android.useDeprecatedNdk = true
为了方便生成头文件和so文件,我们可以在Android Studio → External Tools中设置两个命令,分别来生成头文件和生成.so文件
Program: $JDKPath$/bin/javah
Parameters: -encoding UTF-8 -d ../jni -jni $FileClass$
Working directory: $SourcepathEntry$\..\java
![](https://img-blog.csdn.net/20160718153053757?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Program: D:\android\android-ndk-r11c\build\ndk-build.cmd
Parameters: NDK_LIBS_OUT=$ModuleFileDir$/src/main/jniLibs
Working directory: $ModuleFileDir$\src\main
(注意:NDK_LIBS_OUT=$ModuleFileDir$/src/main/jniLibs
这个含义是将生成的so文件放在jniLibs这个文件下,
如果不写这句话,什么都不填,则为默认的文件夹)
写一个测试类
![](https://img-blog.csdn.net/20160718153411110?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
右击这个类选择刚才写的javac命令,就会生成头文件
![](https://img-blog.csdn.net/20160718153635005?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
根据头文件写一个测试的.c文件
![](https://img-blog.csdn.net/20160718153831492?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
注意:打红框的地方要和头文件的名称和方法命名一样
写一个Android.mk文件
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := JniTest LOCAL_SRC_FILES := TestJni.c include $(BUILD_SHARED_LIBRARY)
然后右击jni文件夹选择我们的命令ndk-build
![](https://img-blog.csdn.net/20160718154335013?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
则生成了![](https://img-blog.csdn.net/20160718154410497?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
build.gradle的配置
![](https://img-blog.csdn.net/20160718154716129?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
最后so已经生成,调用
android.useDeprecatedNdk=true
最后加上一句
Android Studio正确添加so库的两种方式
以下两种方式二选一
方法一:
1.在src/main中新建jniLibs文件夹 ,把.so复制进去即可
![]()
方法二:
1.在app/中新建libs文件夹,把.so复制进去
2.在build.gradle中添加以下五行脚本即可(注:以下脚本意思是会把libs文件夹当成jniLibs文件夹,可以直接用so库了)
sourceSets { main { jniLibs.srcDirs = ['libs'] } }贴上完整的build.gradle文件
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.tofu.chat" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main { jniLibs.srcDirs = ['libs'] } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile project(':common') compile files('libs/kandy-1.6.244.jar') }