How can I add static library to Android NDK using build.gradle file?

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

LOCAL_STATIC_LIBRARIES := android_native_app_glue

      

and then imports it into "main.c" just 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 functionality. So, for example, to replace LOCAL_LDLIBS := ...

with android.mk, I used ldLibs = ...

in build.gradle. What code does gradle replace LOCAL_STATIC_LIBRARIES

?

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

+3


source to share


1 answer


if you really want to make it work without the Makefile, you can copy and paste ndk\sources\android\native_app_glue\android_native_app_glue.(c|h)

into jni folder and add android

to your ldLibs.

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

:

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

      



In this case, you will have to call ndk-build

yourself, but you can also do gradle for this:

// 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
}

      

+2


source







All Articles