Sankar ganesh Sankar ganesh - 1 year ago 218
Android Question

Android Gradle Adding static library

In old traditional android ndk we will specify the static library to be linked in the file.

PLATFORM_PREFIX := /opt/android-ext/
include $(CLEAR_VARS)
LOCAL_MODULE := library
LOCAL_SRC_FILES := library.a

LOCAL_STATIC_LIBRARIES := android_native_app_glue library

Here is my Question

I am little bit Confused when switching to Gradle experimental plugin for NDK. Share your ideas on how to link Static library in App build.gradle file.

I had followed the latest gradle experimental plug-in documentation given here.

Answer Source

Have a look at this sample.

  1. Tell the compiler where the headers are (in android.ndk{}):

    CFlags += "-I${file("path/to/headers")}".toString() cppFlags += CFlags

  2. Tell the linker where the .a file is (in android.ndk{} or where defining the flavors - make sure to add abiFilter - for example abiFilters += "armeabi-v7")

    ldFlags += "-L${file(path/to/library.a)}".toString() ldLibs += ["nameOfLibrary"]

    Note that the name of the library by convention is the string after "lib" in the .a file name. For example, for a file named libNative.a you should add ldLibs += ["native"] to gradle.

  3. Create a new module and use apply plugin: 'java' to apply java plugin. In the build.gradle write the necessary code to get and place the .a file in the appropriate directory (where you will get it from your module which is using it). Don't forget to add a dependency in the module using the library (compile project(':libraryModule') in dependencies{}) and to include it in the project in settings.gradle file with include ':libraryModule'. If you want to place the module in a specified by you folder (for example where currently your file is), just add project(':libraryModule').projectDir = new File(settingsDir, 'path/to/module').

That should do it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download