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

I have used ndk successfully to build & use a .so file in one project. I need to use this library in another project. I would rather not copy the source there, but just use the library.

Trying to copy & paste the whole libs/armeabi/libcommon.so to the project root does not work, I think because libs/armeabi is an android generated path.

So what would be the best way to do it?

I am using Eclipse-Galileo & ndk5.


Solution 1:

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/libprebuilt
cp libprebuilt.so jni/libprebuilt

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

LOCAL_PATH := $(call my-dir)

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

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

Solution 2:

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.

Solution 3:

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.