A first difficulty may come if you are trying to build non-trivial native codes under your JNI folder. For example, I am trying to build 3 native C libraries along with my own one, which obviously cannot be automatically built with the built-in support of gradle in AS. As of now, the automatic NDK build is probably for some simpler project with just a few native source files. Without writing the Android.mk for ndk-build, it's not possible to build them correctly.
An recent and excellent article about the new NDK support in latest Android Studio has been helping me a lot here: http://ph0b.com/new-android-studio-ndk-support/. However I can't get everything working if I want to use Android.mk/Application.mk for NDK projects in Android Studio.
Here is how I did it, as of now, through the latest gradle-experimental plugin: com.android.tools.build:gradle-experimental:0.4.0. I am using this on both AS 1.5.1 stable and 2.0 preview.
Instead of setting jni srcDirs to an non-existing folder as in ph0b's article, which has an side effect of not showing the jni folder at all in the project panel in AS, I'm setting it to the correct one:
android.sources {
main {
jni {
source {
//srcDirs = ['src/main/none'] /*non-exiting*/
srcDirs = ['src/main/jni']
}
}
jniLibs {
source {
srcDirs = ['src/main/libs']
}
}
}
}
And then skip the NDK module through the gradle script (Module: app) like this:
/* skip the automatic gradle NDK build */
tasks.all {
task -> if (task.name.contains('LibmylibSharedLibrary')) task.enabled = false
}
provided that your android.ndk setting is something like this:
/* native build settings */
android.ndk {
moduleName = "libmylib"
}
And now the gradle build will simply skip the automatic NDK build. You should follow ph0b's article to include the ndk-build instead:
/* call regular ndk-build script from app directory
see http://ph0b.com/new-android-studio-ndk-support/ for more details */
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main/jni').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main/jni').absolutePath
}
}
Now everything just works fine!
ps. AS's NDK support and gradle-experimental plugin change a lot since it's under active development (?) for now. This is proven to be working in AS 1.5.1 stable and AS 2.0 preview 4, as of now (Dec. 30, 2015).