Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

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

I have to use two native libraries: one is my own and the other one is 3rd-party. As long as I used them in separate projects, everything was ok. But now I'm getting the Exception Ljava/lang/UnsatisfiedLinkError.

I'm using Eclipse.

I found out that if I place the existing library in libs/armeabi, Eclipse begins compilation of the native code and it fails. If I rebuild the JNI part from the command line, compilation succeeds but the 3rd party library disappears. Really stupid.

So how do I tell Eclipse to use an existing .so library along with a library that must be built? The libraries are independent.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...