3

I'm trying to learn to use the NDK in AndroidStudio, and I'd like to import the "android_native_app_glue" file used in the "native-activity" sample, so that I have a framework for basic functions like the display, touch, etc. In the sample, it loads the library with this line in the android.mk file:

LOCAL_STATIC_LIBRARIES := android_native_app_glue

and then imports it in "main.c" simply with:

#include <android_native_app_glue.h>

But in AndroidStudio, as far as I can tell from experimenting with it, it doesn't use the android.mk file at all and instead uses the build.gradle file to do all the same functions instead. So, for example, to replace LOCAL_LDLIBS := ... in the android.mk, I used ldLibs = ... in the build.gradle. What gradle code replaces LOCAL_STATIC_LIBRARIES?

Is there also a resource somewhere that explains, in general, how to translate from android.mk to build.gradle?

Sam Jaques
  • 291
  • 3
  • 13
  • Hey Sam, if you want to learn to use the NDK, please view my answer to the question posted here (https://stackoverflow.com/questions/16667903/android-studio-gradle-and-ndk/27737154#27737154), and my associated blog article here (http://www.sureshjoshi.com/mobile/android-ndk-in-android-studio-with-swig/) – SJoshi Jun 15 '15 at 02:03

1 Answers1

2

if you really want to make it work without Makefiles, you can copy and paste ndk\sources\android\native_app_glue\android_native_app_glue.(c|h) into your jni folder and add android to your ldLibs.

Else, you can still rely on classic Makefiles, by deactivating the default call to ndk-build, and making gradle use your libs from the libs directory:

sourceSets.main {
    jniLibs.srcDir 'src/main/libs'
    jni.srcDirs = [] //disable automatic ndk-build call
}

In that case you'll have to call ndk-build yourself, but you can also make gradle call it for you:

// call regular ndk-build(.cmd) script from app directory
task ndkBuild(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
    } else {
        commandLine 'ndk-build', '-C', file('src/main').absolutePath
    }
}

tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}
ph0b
  • 14,353
  • 4
  • 43
  • 41
  • I'm not sure what parts of this go where. So I would add "lbLibs="Android", and then where would I put "ndk/sources/.../android_native_app_glue"? For the rest of the code, does it go in the gradle file? I tried adding it there, but it said "method not found:main()". Does "sourceSets.main" need to go within some specific part of the gradle file, e.g., in android{}? – Sam Jaques Jul 04 '15 at 20:36
  • Playing around further, I found that if I add "#include <../../../../../sources/android/native_app_glue/android_native_app_glue.h>" then it will compile the native glue code, but if I try to call any of the functions (e.g., "app_dummy()") it gives an "undefined reference" error. Is there something more I need to add to make sure that it can actually use the functions declared in android_native_app_glue.h? – Sam Jaques Jul 04 '15 at 21:42
  • Yes you would add `ldLibs "android"`. And then you would copy the .c and .h from android_native_app_glue to your jni sources. From your code, reference the header as usual `#include `. sourceSets.main goes within android {}, but put this code only if you want to use your own Makefiles. – ph0b Jul 06 '15 at 08:38