Now we can generate the so file.
First of all, add these code into build.gradle file of your project:
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn CopyToJniLibs
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkDir = properties.getProperty('ndk.dir')
commandLine "$ndkDir/ndk-build.cmd", "NDK_PROJECT_PATH=$projectDir.absolutePath\\build", "APP_BUILD_SCRIPT=$projectDir.absolutePath\\src\\main\\jni\\Android.mk"
}
task CopyToJniLibs(type: Copy, dependsOn: 'ndkBuild', description: 'copy the native libs to jniLibs') {
from fileTree(dir: file(buildDir.absolutePath + '/libs'), include: '**/*.so')
into file('src/main/jniLibs')
}
In the terminal prompt in Android Studio, we can simply enter following command to generate the so file:
gradlew ndkBuild
gradlew CopyToJniLibs
Finally, we can find so file in src/main/jniLibs