[FIXED] How do I add .so file to lib folder in APK?

Issue

If I add the following to my cmakelists.txt file:

add_library( # Sets the name of the library.
        extralib

        # Sets the library as a shared library.
        MODULE

        # Provides a relative path to your source file(s).
        extra.cpp)

and I build my Android application, then the APK will have libextralib.so in its ‘lib’ folder. In the above case, ‘extralib’ is built from a source code file (extra.cpp). How can I use a pre-built .so file as ‘source’ for extralib?

I tried:

add_library( # Sets the name of the library.
        extralib

        # Sets the library as a shared library.
        MODULE

        # Provides a relative path to your source file(s).
        extra.so)

but Android Studio does not like that. I get the following error:

CMake Error: CMake can not determine linker language for target: extralib

I am trying to figure out how I can add extra .so files to the APK lib folder (they will be loaded at dynamically at runtime) where some .so files are built from source code while others are prebuilt .so files. I know I can just create a jniLibs folder and stuff all my .so files into that folder, but I don’t want to do that. I want it all defined in cmakelists.txt instead.

Solution

This method should still work:

 add_library(lib_extra SHARED IMPORTED)
 set_target_properties(lib_extra PROPERTIES IMPORTED_LOCATION
    ${your-extra-lib-location}/${ANDROID_ABI}/libextra.so)
 
 target_link_libraries(${project-lib-name} lib_extra)

Make sure you have a binary(extra.so) for each ABI you plan to include in your app. After build APK, you can check they are there with Studio’s build -> Analyze APK... (or just unzip the APK file).

Answered By – Gerry

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published