Android Studio 1.5.1上面对于NDK的编译进一步简化,只需要在工程的defaultConfig
设置中增加如下配置就可以了:
1 2 3 4 5 |
ndk { moduleName "jni_module" ldLibs "log" abiFilters "armeabi" } |
新建的工程中的app
目录下的build.gradle
中的内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.my.myapplication" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' } |
修改后的配置文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.my.myapplication" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk { moduleName "jni_module" ldLibs "log" abiFilters "armeabi" } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' } |
然后在app->src->main目录下创建jni目录就可以了。
如果此时提示:
1 |
Error: NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin. For details, see http://tools.android.com/tech-docs/new-build-system/gradle-experimental. Set "android.useDeprecatedNdk=true" in gradle.properties to continue using the current NDK integration. |
则在修改工程目录下的gradle.properties
,在文件中新建一行,添加如下:
1 |
android.useDeprecatedNdk=true |
注意,还需要在local.properties
设置NDK
的路径
1 |
ndk.dir=D\:\\Android\\android-ndk-r10e |
默认情况下,build.gradle
中的代码是不能进行调试的,需要增加两个配置项:
1 2 |
jniDebuggable true debuggable true |
修改后的配置文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.my.myapplication" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk { moduleName "jni_module" ldLibs "log" abiFilters "armeabi" } } buildTypes { debug{ jniDebuggable true debuggable true } release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' } |
一般在debug
项中增加提示即可,如果想在release
中也支持Debug
的话,上面两句话在release
中增加即可。修改后的结果如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.my.myapplication" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" ndk { moduleName "jni_module" ldLibs "log" abiFilters "armeabi" } } buildTypes { debug{ jniDebuggable true debuggable true } release { jniDebuggable true debuggable true minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' } |