androidStudio使用自定义Android.mk文件

找了好久,总算找到一个正确的使用Android.mk文件的方式

修改app/build/build.gradle 为以下内容,

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.cc.jniTest"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets.main.jni.srcDirs = [] //禁用自带编译ndk功能
    sourceSets.main.jniLibs.srcDir 'src/main/libs'
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

task ndkBuild(type: Exec) {
    workingDir file('src/main/jni')
    commandLine getNdkBuildCmd()
}

task cleanNative(type: Exec){
    workingDir file('src/main/jni')
    commandLine getNdkBuildCmd(), 'clean'
}

clean.dependsOn cleanNative

def getNdkDir() {
    if (System.env.ANDROID_NDK_ROOT != null)
        return System.env.ANDROID_NDK_ROOT
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def ndkdir = properties.getProperty('ndk.dir', null)
    if (ndkdir == null)
        throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.")
    return ndkdir
}

def getNdkBuildCmd() {
    def ndkbuild = getNdkDir() + "/ndk-build"
    ndkbuild += ".cmd"
    return ndkbuild
}
如果还是不行,直接cmd命令编译so吧,先把ndk目录添加至系统环境变量,到jni目录下执行ndk-build

你可能感兴趣的:(androidStudio使用)