Android Studio 1.5.1 配置编译NDK参考文档

Android Studio 1.5.1上面对于NDK的编译进一步简化,只需要在工程的defaultConfig设置中增加如下配置就可以了:

ndk {
    moduleName "jni_module"
    ldLibs "log"
    abiFilters "armeabi"
}

新建的工程中的app目录下的build.gradle中的内容如下:

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'
}

修改后的配置文件如下:

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目录就可以了。

如果此时提示:

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,在文件中新建一行,添加如下:

android.useDeprecatedNdk=true

注意,还需要在local.properties设置NDK的路径

ndk.dir=D\:\\Android\\android-ndk-r10e

默认情况下,build.gradle中的代码是不能进行调试的,需要增加两个配置项:

jniDebuggable true
debuggable true

修改后的配置文件如下:

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中增加即可。修改后的结果如下:

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'
}

发布者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注