3

I have a library called somelibrary/lib/c++/armeabi-v7a/libsomelibrary.a. How do I add it to the Gradle files in my Android project?

Obviously the library also contains the headers under somelibrary/include/somelibrary/*.h but that's the easy part because I can just add a line cppFlags.addAll(['-I' + file('somepath/somelibrary/include')]) to my build.gradle.

But how do I also add the .a file to the project so that the linking works on all binary formats, not only armeabi-v7a but also arm64-v8a, armeabi and x86? The library contains versions of the .a file for all those binary formats. And in addition to that, it also contains versions for two different c++ standard libraries: "c++" and "gnustl". I don't have the source code of the library.

2
  • discuss.gradle.org/t/… .lib and .a are same thing across platform windows and linux Commented Sep 14, 2016 at 13:45
  • I am not sure if this can help you since it uses the gradle experimental plugin. In the gradle experimental you can directly add a dependency on a static or shared prebuilt library: tools.android.com/tech-docs/new-build-system/… Commented Sep 15, 2016 at 17:35

1 Answer 1

2

It will depend on what gradle version are using, in case of build:gradle-experimental:0.7.3'

In app build.gradle file:

  • Firstly define your library repository

    repositories { libs(PrebuiltLibraries) { your_lib { headers.srcDir "${your_lib_path}/include" binaries.withType(StaticLibraryBinary) { staticLibraryFile = file("${your_lib_path}/your_library.a") } } } } 
  • Define your ndk-module

     android.ndk { moduleName = "your_ndk_module" platformVersion = 9 toolchain "gcc" debuggable true cppFlags.add("-fexceptions") cppFlags.add("-std=c++11") stl = "gnustl_static" } 
  • define library sources

    android.sources { main { jni { dependencies { library "your_lib" linkage "static" } } } } 
  • define your product flavours

     android.productFlavors { create("arm") { ndk.abiFilters.add("armeabi") ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } create("arm7") { ndk.abiFilters.add("armeabi-v7a") ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi-v7a')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } create("arm8") { ndk.abiFilters.add("arm64-v8a") ndk.ldFlags.add("-L${file('src/main/jniLibs/arm64-v8a')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } } 

Whole code:

apply plugin: 'com.android.model.application' def your_lib_path = file(project(':app').projectDir).absolutePath + "/your_lib_path" model { repositories { libs(PrebuiltLibraries) { your_lib { headers.srcDir "${your_lib_path}/include" binaries.withType(StaticLibraryBinary) { staticLibraryFile = file("${your_lib_path}/your_library.a") } } } } //define your ndk-module: android.ndk { moduleName = "your_ndk_module" platformVersion = 9 toolchain "gcc" debuggable true cppFlags.add("-fexceptions") cppFlags.add("-std=c++11") stl = "gnustl_static" // Which STL library to use: gnustl or stlport } //define your android sources android.sources { main { jni { dependencies { library "your_lib" linkage "static" } } } } //define your product flavours: android.productFlavors { // for detailed abiFilter descriptions, refer to "Supported ABIs" @ // https://developer.android.com/ndk/guides/abis.html#sa create("arm") { ndk.abiFilters.add("armeabi") ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } create("arm7") { ndk.abiFilters.add("armeabi-v7a") ndk.ldFlags.add("-L${file('src/main/jniLibs/armeabi-v7a')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } create("arm8") { ndk.abiFilters.add("arm64-v8a") ndk.ldFlags.add("-L${file('src/main/jniLibs/arm64-v8a')}".toString()) ndk.ldLibs.addAll(["your_lib"]) } } } 

You will have to modify your gradle file according to your gradle version.

Hope, this helps.

Cheers.

Unai.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that probably is a perfect answer. The only problem is just that Android Studio just switched to CMake with c++ and this doesn't help anymore...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.