[Android] How to build the JNI with a prebuilt shared library in the Android Application?
Sometimes, we need to develop C/C++ for achieving the performance-sensitive calculation via JNI technology in a Android application. In addition, we also probably to encounter these problems such as,
- You don’t want to distribute your source code, but, want to distribute your libraries to third-party NDK developers.
- You want to speed up your project build via providing the prebuilt library.
First, we need to identify these path for you to help to explain easily.
- Project folder: ${PROJECT}
- JNI folder: JNI=${PROJECT}/jni
- Prebuilt library folder: PREBUILT=${PROJECT}/jni/lib
Here are the steps you need to follow,
1. First, you need to prepare the prebuilt library. You can put it into the folder “${PREBUILT}“.
2. In the “${JNI}“, you need to provide the Android.mk file to build the jni library including the prebuilt library. Here is the example of Android.mk file for you,
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/libfoo.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := jni.cpp
LOCAL_SHARED_LIBRARIES := foo
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/jni/include
LOCAL_MODULE := jni
include $(BUILD_SHARED_LIBRARY)
3. In the “${JNI}“, you also need to provide the Application.mk for ndk-build to configure the platform specific settings. Here is an Application.mk example for you,
APP_OPTM := release
APP_ABI := armeabi-v7a armeabi
APP_PLATFORM := android-10
4. In the “${JNI}” folder, you can execute the ndk-build command to build the JNI source code with the prebuilt library.
Open Source for You:
- You can also reference to the example in my github – example_JNI_with_prebuilt_shared_library.
Note:
- Some developers encounter the “undefined reference” problem which is discussed in the android ndk undefined reference to a method. This is because you may forget to add the “extern “C”” definitions in the header file. You can also reference to this post to know about this issue – extern “C” for C++.
- UPDATE 2013/06/17 – You need to load all the shared libraries into your Android project, since the dependency between the libraries. You can also reference to this post – UnsatisfiedLinkError when trying to load library that is clearly there.
發佈留言
很抱歉,必須登入網站才能發佈留言。