Search code examples
android-ndkjava-native-interfaceandroid-gradle-plugin

Android Studio 1.4, experimental plugin, path to JNI folder?


I'm using Android Studio 1.4 and the experimental plugin (com.android.tools.build:gradle-experimental:0.2.0) with NDK support.

The project has one C file, and links the rest of the native code via a static library, like this:

android.ndk {
    moduleName = "native"
    ldFlags += ["-g"]
    ldFlags += ["-L/Users/me/Desktop/MyApp/app/src/main/jni", "-lnative"]
}

This works for me, linking libnative.a from the jni folder.

But I'd like to specify the location of the native library relative to the project.
How exactly do I do that?

Thanks.


Solution

  • android.ndk {
        moduleName = "native"
        ldFlags += ["-g"]
        ldLibs += ["-L", file("src/main/jni").absolutePath, "-lnative"]
    }
    

    Note that I use ldLibs, not ldFlags - these are used a bit differently to forge the linker command line.

    Actually, if you want to add just one library, the preferred syntax would be

    ldflags += file("src/main/jni/libnative.a").absolutePath
    

    (there is no need for the linker to look for other libraries in this "src/main/jni" directory).

    file() is relative to the current Module; use File() if you want to specify paths relative to current Project.