Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you link third party library in Android cmake external build system?

Android Studio 2.2 introduces cmake external build system. The problem is that documentation is really lacking and I do not know how should I link third party libraries? I've tried cmake directive target_link_libraries:

target_link_libraries(native-lib libs/libSomething.so)

And it "works" in that app compiles but then I get dlopen error at runtime because libSomething.so has not been packaged with application. The libs directory is under "app" if that changes anything and I've started with default JNI project generated by Android Studio 2.2...

[Update]

I've tried putting libSomething.so under app/src/main/jniLibs/armeabi-v7a but now the "main" (native-lib) library is not packaged.

[Update2]

I've added source set config that includes cmake output dir and this works but is ugly as hell and is not really a permanent solution...

sourceSet
{
    main
    {
        jniLibs.srcDirs = [ "libs", ".externalNativeBuild/cmake/debug/obj"]
    }
}
like image 770
UfoXp Avatar asked Aug 29 '16 09:08

UfoXp


People also ask

What does target link libraries do in CMake?

Specify libraries or flags to use when linking a given target and/or its dependents. Usage requirements from linked library targets will be propagated. Usage requirements of a target's dependencies affect compilation of its own sources.

What is NDK build?

To compile and debug native code for your app, you need the following components: The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android. CMake: an external build tool that works alongside Gradle to build your native library.


2 Answers

For now I ended up copying libSomething.so to cmake library output directory in a post build step. This works because it turns out that Android Studio copies into apk EVERYTHING that is in that directory.

Command in cmake is the following:

add_custom_command(TARGET native-lib POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy
        ${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libSomething.so
        ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libSomething.so
    )
like image 189
UfoXp Avatar answered Sep 22 '22 15:09

UfoXp


For now, you could also put your shared libs into directory, and configure jniLibs to point to that directory, that will pack it. One sample is here: https://github.com/googlesamples/android-ndk/tree/master/hello-libs, follow gperf see if that helps. This way app not depending on the generated build folders. When android studio added packing libs, the jniLibs workaround is not necessary anymore

like image 45
Gerry Avatar answered Sep 26 '22 15:09

Gerry