关于android studio进行NDK编译生成.so文件

在使用android studio进行NDK编译时 在项目的build.gradle的最外层添加以下代码
关于android studio进行NDK编译生成.so文件关于android studio进行NDK编译生成.so文件
代码如下:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}

task ndkBuild(type: Exec) {
workingDir file(‘jni’)
commandLine getNdkBuildCmd()
}

task cleanNative(type: Exec) {
workingDir file(‘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
}