Search code examples
androidc++android-studiocmakeandroid-ndk

Android Studio is bundling stubbed library into the APK


I am using Android Studio to build a native C++ project. There is a dependency library (let's call it lib.so) which is required to be linked with the JNI library to resolve few of its symbols. However, I do not want this library to be bundled into the APK as this is a stubbed library and to be used only for the linking purpose. A proper lib.so with proper symbol definitions is already present on the Android device where I want to run the built APK.

I am importing the stubbed library lib.so to be linked with JNI library as:

add_library(lib SHARED IMPORTED) set_target_properties(lib.so PROPERTIES IMPORTED_LOCATION "location_of_lib.so")

add_dependencies(native-jni lib}) target_link_libraries(native-jni lib)

This way the APK compilation is successful but the library is getting bundled into the APK.

I am using AS 4.0.1, NDK r19c, CMake 3.17. The same project used to work as expected, i.e. not bundle the library lib.so with the APK but somehow it started bundling it. Any leads to debug this issue would be appreciated.


Solution

  • Try to use find_library for that. Put your prebuilt lib.so (bad name at least liba.so) to some path and add it to CMAKE_FIND_ROOT_PATH:

    list(APPEND CMAKE_FIND_ROOT_PATH ${PREBUILT_LIBS_DIR}/${ANDROID_PLATFORM}/${ANDROID_ABI}/a)
    

    example how it could look:

    ~/my-proj/prebuilt-libs/android-28/x86/a/lib/liba.so
    

    Then find library and link it:

    find_library(lib-a a)
    target_link_libraries(native-jni lib-a)