multiple (my and 3rd-party) native libraries in Android NDK

The NDK allows for linking with prebuilt user libraries, using the PREBUILT_SHARED_LIBRARY variable.

Assuming that the library you need to link is librandom.so, create a libs folder in jni subfolder of the project folder:

mkdir -p jni/libs
cp librandom.so jni/libs

Then, just create a jni/libs/Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := random
LOCAL_SRC_FILES := librandom.so
include $(PREBUILT_SHARED_LIBRARY)

You can create a section for each prebuilt library, all placed in jni/libs.

Next, you just need to include the above file into your jni/Android.mk to get things to work. In the NDK docs, it is recommended that this be done at the end of the Android.mk, rather than the middle:

include $(LOCAL_PATH)/libs/Android.mk

However, you'll need to do this before the module that requires this library.

For linking, you'll need to add the following into the module section that links to the prebuilt library.

LOCAL_SHARED_LIBRARIES := random

Then when you do ndk-build, it will copy this library into libs/armeabi/ before building the module, and you're good to go.

Note: This does not solve problems with required headers. You'll still need to add the location of the headers for the library into the variable LOCAL_C_INCLUDES in the module that requires it.


This is what I have done for the moment. I will not accept (in stackoverflow sense) my own (this) answer beause it is unsatisfactory.

I have created a new project and copied all java files there. Then, I copied the .so library from the old project and the 3rd party library into libs/armeabi.

That's monstrous. But it works. For the moment. The worst thing is that the version control is torpedoed.