Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to package prebuilt shared library inside an APK

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?

like image 299
User7723337 Avatar asked Jan 12 '12 13:01

User7723337


1 Answers

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.

like image 154
vitakot Avatar answered Sep 28 '22 15:09

vitakot