43

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.

shizhen
  • 12,251
  • 9
  • 52
  • 88
OceanBlue
  • 9,142
  • 21
  • 62
  • 84

3 Answers3

44

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!

gnychis
  • 7,289
  • 18
  • 75
  • 113
  • 3
    +1 . Thanks. I revisited this question & saw your answer. How would you include the .h files in that case? I mean src files in ProjectA include .h files from ProjectB. When I use your method, ndk-build cannot find them. – OceanBlue Jul 06 '11 at 19:10
  • 2
    @OceanBlue: just put it in jni/ rather than jni/libprebuilt. Generally, you could just copy all the .so .a files into the new project keeping the same hierarchy, /libs/armeabi and /obj/local/armeabi. Actually, the above android.mk just does the same copy. – Jichao Aug 19 '13 at 09:03
  • Nobody has a problem using shared library? My Android.mk works fine with shared libraries in debug, but in release copy them with less information (that original release in prebuilt folder) and doesnt work. – vgonisanz May 28 '14 at 13:26
  • Hi, @gnychis. As answer of Komi I see that the answer of OceanBlue is correct but it is complex. About your solution, Could you explain about "Java_com_myapp_myclass_funct" in your solution ? – mdtuyen Aug 09 '16 at 07:10
  • @gnychis i am facing the same problem i have tried your solution but it doesnot solve my problem save issue the native methods not found error.I have put .so file in JniLibs Folder under armeabi-v7a an x86 – Mudassir Khan Oct 31 '17 at 10:10
21

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.

OceanBlue
  • 9,142
  • 21
  • 62
  • 84
  • 1
    Don' forget to wrapper all your calls to the native with Java code otherwise you will have to recompile the method names for all your projects. I usually stick them in one or a few different classes then call those classes. – JPM Oct 24 '11 at 19:42
  • Is there a non eclipse specific solution? – zeitgeist Jan 23 '23 at 08:08
7

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.

Komi
  • 330
  • 3
  • 13
  • Thanks, I'd forgotten about the package_name_in_jni_method_names. Of course, that wouldn't work. Can you please elaborate your second suggestion... using it as external package... if I try "import" option, it does not let me import .so types, only JARs & such. – OceanBlue Apr 15 '11 at 14:50
  • I´m not sure here, but cannot you create another package with the name you need in your current project? Just guessing, never tryed.. – Komi Apr 15 '11 at 17:42
  • @Komi, yes. easy option is to rename functions in library based on the jni naming convention and then copy .so files over to appropriate location. this will avoid importing as external package and all that comes with it. – blueether May 06 '16 at 07:27