NDK - How to use a generated .so library in another project NDK - How to use a generated .so library in another project android android

NDK - How to use a generated .so library in another project


There is a much simpler way to do all of this.

Let's say that your prebuilt library is called "libprebuilt.so"

In the project folder of the new project you want to only include the prebuilt library, do something like:

mkdir -p jni/libprebuiltcp libprebuilt.so jni/libprebuilt

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

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := libprebuiltLOCAL_SRC_FILES := libprebuilt.soinclude $(PREBUILT_SHARED_LIBRARY)

Then when you do ndk-build, it will copy this library in to libs/armeabi/ ... that's all!


I figured out a way to do this, so posting it here in case someone else runs into it.

  1. The code to be shared (including the Java JNI wrapper, native code, .so library), should be in a separate project. Convert this to a Library project by right-click on project name --> properties --> Android properties --> check mark "Is Library". This project cannot be executed now but can be referenced by other projects.

  2. In the project which will use the shared object, add a reference to the Libarray project by right-click on project name --> properties --> Android properties --> Library/"Add". This should show up the Library project created in the previous step. Select it.

Now the .so can be referred to easily between different projects.


There is a package name inside all JNI (Java Native Interface) functions names (like JNIEXPORT void JNICALL Java_com_myapp_myclass_funct). So i guess you should rename these funkcions in library a recompile it. Or maybe use it as external package in your app.