How to package prebuilt shared library inside an APK

10,388

Solution 1

In order to prebuild your native library you have to

  1. Create jni folder in your project folder
  2. Create libs folder in your project folder
  3. Add Adnroid.mk make file to the jni folder, it should looks like this:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_LDLIBS    := -llog
    LOCAL_MODULE    := Test
    LOCAL_SRC_FILES := Test.cpp
    
    include $(BUILD_SHARED_LIBRARY)
    

Note 1: Test.cpp is the main library source file containing implementation of native methods. You can add more sources as a space separated list.

Note 2: Do not include headers, they are included automatically.

Note 3: If you need to enable C++ STL, then create another make file - Application.mk, add it to the jni folder and set APP_STL := stlport_static flag in it.

Then you will have to set up a builder. Refer to this article for how to do that:

After these steps, the builder will create your library in the libs folder which will be automatically packed into the apk when building the whole application.

Note: Your library name should be lowercase, it is a Linux convention. The "lib" prefix will be automatically added, so the final library name will be libtest.so.

Solution 2

Let's say there is one libxxx.so shared library under libs/armeabi/ of project which you want to pack into apk. There are 2 things you need to do in the Android.mk as below:

# 1. Copy .so to out/target/product/***/obj/lib
$(shell cp $(wildcard $(LOCAL_PATH)/libs/armeabi/*.so $(TARGET_OUT_INTERMEDIATE_LIBRARIES)) 

# 2. Make libxxx to be packed into apk
LOCAL_JNI_SHARED_LIBRARIES := libs/libxxxx


Then you can use apktool to unpack the built apk, you will find the libxxx.so will be located in libs/armeabi*/. It is indeed packed into the apk.

Solution 3

I had two LOCAL_JNI_SHARED_LIBRARIES that I wanted to build into my APK. I manage to do it by setting the following in my Android.mk:

LOCAL_MODULE_TAGS := samples

After re-compiling the module I was able to find both libraries inside the .apk under /lib/.

Share:
10,388
User7723337
Author by

User7723337

C, C++, Java, Android, AngularJS, JavaScript, HTML and CSS Developer. But mostly interested in Java and Android.

Updated on June 16, 2022

Comments

  • User7723337
    User7723337 almost 2 years

    This question seems to have been asked a lot but all were trying to use eclipse to package the library inside the APK. However, my requirement is to package the library inside the APK (which will later be loaded using System.loadLibrary() through Java) using the Android build system, i.e. i want to write an Android.mk file that does this job.

    Requirement: 1. Prebuilt shared library: libTest.so 2. Write an Android.mk file that will package this to libs/armeabi-7 inside the apk.

    I don't know much about the build system I am using but the compilation is done using "mm" command after exporting the required environment variables.

    When I provide libTest for LOCAL_JNI_SHARED_LIBRARIES, it tries to find it inside its exported paths and fails to find it there and hence build fails.

    Can anyone please give any pointers on writing an Android.mk file that will package my prebuild shared library into the APK?