Issues with Android Studio 2.2 externalNativeBuild

With the release of Android Studio 2.2, there is now a built-in way to execute complex ndk builds using externalNativeBuild.  I have been having trouble getting this to work with my enterprise project which has some java plugins and c++ plugins.

Prior to Android Studio 2.2, I had my project setup to perform the ndk-build for the c++ plugins on the command line which uses an Android.mk and Application.mk, the task in gradle looked like this: 

task ndkBuild(type: Exec) {     commandLine getNdkBuildCmd(),             '-C', file('src/main/jni').absolutePath,             '-j', Runtime.runtime.availableProcessors(),             "NDK\_LIBS\_OUT=$jniLibsDir",             'all',             'NDK\_DEBUG=1'     dependsOn 'generateLuaBytecodes'     doFirst {         println '== ndkBuild ==' } }

GenerateLuaByteCodes is a task that runs lua2c.sh to create the c++ plugin.c file.

After upgrading to 2.2, I tried to use the externalNativeBuild configuration to accomplish the same thing, and this is what I came up with:

defaultConfig {     externalNativeBuild {         ndkBuild {             arguments "NDK\_LIBS\_OUT=$jniLibsDir", "-j$numProcs", "all"             abiFilters "armeabi-v7a"         }     } } externalNativeBuild{     ndkBuild{         path "src/main/jni/Android.mk"     } }

I made it so that the externalNativeBuild task depends on generateLuaByteCodes.

The build order for assembleDebug goes a little something like this:

  1. build java plugins
  2. build c++ plugins
  3. compile lua
  4. certify build
  5. bundle/package/assemble apk per the built-in functionality

Currently, the build is succeeding, but it is crashing on startup with ExceptionInInitializerError, UnsatisfiedLinkError (see attached log for stacktraces).  Given that the app works on 2.2 using the ndk-build command, and does not with externalNativeBuild, it seems like something must be setup wrong.  I want to use externalNativeBuild because it gets all of the c++ into the android studio project for editing and debugging just like in xCode.

Additional Info/Tool versions:

  1. Completely clean build
  2. Corona Enterprise build 2016.2954
  3. Android NDK r12b
  4. SDK target 24, min 16, build tools 24.0.2 (all projects/modules)
  5. Android Studio embedded Java 8
  6. Gradle version 2.14.1
  7. Android Plugin version 2.2.0
  8. c++_static (clang) runtime

If anyone has had success with externalNativeBuild, or can point me in the right direction, I would greatly appreciate it.

First time looking into that and I think I see the problem. According to the documentation “ndkBuild” only builds the module, but not packages it. To build and package you should replace “ndkBuild” with “ndk”. Try it.

Hi Lerg!

I think I see what you’re referring to in the new documentation https://developer.android.com/studio/projects/add-native-code.html#specify-abi.  I tried using the ndk block as specified in the doc with the same results (errors):

defaultConfig { externalNativeBuild { ndkBuild { arguments "NDK\_LIBS\_OUT=$jniLibsDir", "-j$numProcs", "all" } } ndk { abiFilters "armeabi-v7a" } } externalNativeBuild{ ndkBuild{ path 'src/main/jni/Android.mk' } }

First time looking into that and I think I see the problem. According to the documentation “ndkBuild” only builds the module, but not packages it. To build and package you should replace “ndkBuild” with “ndk”. Try it.

Hi Lerg!

I think I see what you’re referring to in the new documentation https://developer.android.com/studio/projects/add-native-code.html#specify-abi.  I tried using the ndk block as specified in the doc with the same results (errors):

defaultConfig { externalNativeBuild { ndkBuild { arguments "NDK\_LIBS\_OUT=$jniLibsDir", "-j$numProcs", "all" } } ndk { abiFilters "armeabi-v7a" } } externalNativeBuild{ ndkBuild{ path 'src/main/jni/Android.mk' } }