These days Google has pre-released a new feature that will allow easy reuse of prebuilt native libraries. If you are not ready yet to jump to Android Studio 4.0, and/or to build the necessary AAR for libA, you can use the old ways.
There are three different tracks, depending on what the nature of your Project B is.
If it only loads libA.so from Java/Kotlin, it's enough to specify jniLibs.src
in build.gradle: How can I add .so files to an android library project using gradle 0.7+. Note that all classes that have native methods which are implemented in libA.so must be copied to Project B keeping their fully qualified names intact.
If the second project uses ndk-build, you should include $(PREBUILT_SHARED_LIBRARY)
. An NDK guide has further up-to-date details.
If the project uses CMake, you use add_library(… SHARED IMPORTED)
. An Android Srudio documentation explains different scenarios.
It's important to emphasise a tiny difference between ndk-build and CMake in their treatment of prebuilt .so
libraries. While ndk-build will copy these libraries to the installation directory with all libraries that it builds, CMake does not provide this assistance. Therefore, you will probably add the folder that holds the prebuilt library to jniLibs.src
in build.ghradle (as in the case 1 above).
In all 3 scenarios, make sure that the libraries that you get packed into your APK or AAB are stripped of debug symbols.