[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,

  1. You don’t want to distribute your source code, but, want to distribute your libraries to third-party NDK developers.
  2. You want to speed up your project build via providing the prebuilt library.

To achieve these requirements, you can follow these steps to add the prebuilt library into your application project.
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:

Note:

發佈留言