How to pack a pre-built shared library inside the APK

This question seems to have been asked a lot, but everyone tried 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 () via Java) using the Android build system, that is, I want to write an Android.mk file that does this work.

Requirement: 1. Pre-built library: libTest.so 2. Write the Android.mk file that will pack it into libs / armeabi-7 inside apk.

I don't know much about the build system I use, but compilation is done using the 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 cannot find it there and therefore the assembly fails.

Can anyone give any directions on writing an Android.mk file that will pack my shared prebuild library into an APK?

+4
source share
2 answers

To create your own library, you must

  • Create jni folder in project folder
  • Create the libs folder in the project folder
  • Add Adnroid.mk to make the file in the jni folder, it should look 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 source file of the library containing the implementation of its own methods. You can add more sources to the list, separated by spaces.

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

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

Then you will need to create a builder. Refer to this article on how to do this:

After these steps, the creator will create your library in the libs folder, which will be automatically packed in apk when creating the entire application.

Note. Your library name must be lowercase; this is the Linux convention. The prefix "lib" will be automatically added, so the final library name will be libtest.so.

+2
source

Let's say there is one libxxx.so shared library in the libs/armeabi/ project that you want to pack in apk. In Android.mk you need to do 2 things as shown 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 unzip the built-in apk, you will find that libxxx.so will be in libs/armeabi*/ . It is really packaged in apk.

0
source

Source: https://habr.com/ru/post/1390640/


All Articles