Reputation: 2662
I'm currently using a library that I've already compiled into .so
files. I've put the library into jniLibs
and I load it with:
System.loadLibrary("library");
Now, I want to use this library in some of my own native code, so how can I link these .so
files with my own native files from the jni
folder?
Upvotes: 5
Views: 3080
Reputation: 2662
This works now in Android Studio. You can use CFlags -I
and ldFlags -L
when building.
Take a look at this good example.
Upvotes: 2
Reputation: 9492
By "native code", I understand you want to build another C module that uses your first shared library.
You need to use the Android NDK native linking system. You can't compile your second module if it doesn't know how to get the headers of the first one, and that a shared library exists.
Source code required at 2nd module compilation time.
Use LOCAL_C_INCLUDES
, EXPORT_C_INCLUDES
and LOCAL_MODULE = first-module-name
in your first shared library Android.mk
In the second module Android.mk, use LOCAL_SHARED_LIBRARIES = libfirst-module-name
. Don't forget the "lib".
Your second module needs to know the path to the headers of your first library to use it, so you will not avoid to have the code locally when building.
Other possible solution: There is also the $(call import-module,<tag>)
[doc] that I've never used.
All documentation about these compiler flags on this page: http://www.kandroid.org/ndk/docs/ANDROID-MK.html
Only .so and headers required at 2nd module compilation time
This is fully described in Android NDK PREBUILTS documentation.
First shared library:
include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := libfoo.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
Second module using the first:
include $(CLEAR_VARS)
LOCAL_MODULE := foo-user
LOCAL_SRC_FILES := foo-user.c
LOCAL_SHARED_LIBRARIES := foo-prebuilt
include $(BUILD_SHARED_LIBRARY)
Upvotes: 0