Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link a prebuilt shared Library to an Android NDK project?

Tags:

Here I used this Android.mk file in jni/ folder.

LOCAL_PATH := $(call my-dir)  include $(CLEAR_VARS)  LOCAL_PATH := $(call my-dir)  include $(CLEAR_VARS)  # Here we give our module name and source file(s) LOCAL_MODULE    := offlineDownload LOCAL_SRC_FILES := offline_download.c  LOCAL_SHARED_LIBRARIES :=../lib/libpackext.so.1.0 LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog  include $(BUILD_SHARED_LIBRARY) 

And make one lib folder in project directory and put my prebuilt .so library and make one Android.mk file which contains following

LOCAL_PATH := $(call my-dir)  include $(CLEAR_VARS)  LOCAL_MODULE    := packext LOCAL_SRC_FILES := libpackext.so.1.0 LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include  include $(PREBUILT_SHARED_LIBRARY) 

And when i use ndk-build -B command than i got undefined reference to packageExtraction. Here I use my prebuilt library functions means I can't link my prebuilt shared library to my offlinedownload library.

So any body please help me to solved out this issue.

like image 450
user1089679 Avatar asked Mar 26 '12 10:03

user1089679


People also ask

What is an Android shared library?

Similar to the traditional Linux model, shared libraries in Android are relocatable ELF files that map to the address space of the process when loaded. To save memory and avoid code duplication, all shared objects shipped with Android are dynamically linked against the Bionic libc library [23].

How do I install native library on Android?

Or: you could try putting your library into /res in the project and use System. load() instead of System. loadLibrary() to load it.

Can be used for Android app development using the Android Native Developmentkit NDK?

NDK or Native Development Kit is a toolset that is provided by Android to use C or C++ code in our Android application. So, if you are using Android Studio version 2.2 or higher then you can use C or C++ in your Android application. But for Android development, Java and Kotlin are the recommended languages.


2 Answers

Here is a complete Android.mk file for using a 3rd party shared library. The library (libffmpeg.so) is placed in the jni folder. Its "LOCAL_EXPORT_C_INCLUDES" specifies where the header files are kept for the library.

LOCAL_PATH := $(call my-dir)  include $(CLEAR_VARS) LOCAL_MODULE := ffmpeg LOCAL_SRC_FILES := libffmpeg.so LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../ffmpeg/libs/arm-linux-androideabi4.7_1/include include $(PREBUILT_SHARED_LIBRARY)   include $(CLEAR_VARS) LOCAL_MODULE    := ffmpegandroid LOCAL_SRC_FILES := ffmpegandroid.c LOCAL_SHARED_LIBRARIES := ffmpeg include $(BUILD_SHARED_LIBRARY) 

If you wanted to support multiple architectures then you could specify:

APP_ABI := armeabi armeabi-v7a x86 mips 

in your jni/Application.mk and change the LOCAL_SRC_FILES to something like:

LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libffmpeg.so 

and place a libffmpeg.so at jni/armeabi/libffmpeg.so, jni/armeabi-v7a/libffmpeg.so etc ..

like image 57
Dan Brough Avatar answered Oct 22 '22 10:10

Dan Brough


Android NDK official hello-libs CMake example

https://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs

Just worked for me on Ubuntu 17.10 host, Android Studio 3, Android SDK 26, NDK 15.2. so I strongly recommend that you base your project on it.

The shared library is called libgperf, the key code parts are:

  • hello-libs/app/src/main/cpp/CMakeLists.txt:

    // -L add_library(lib_gperf SHARED IMPORTED) set_target_properties(lib_gperf PROPERTIES IMPORTED_LOCATION           ${distribution_DIR}/gperf/lib/${ANDROID_ABI}/libgperf.so)  // -I target_include_directories(hello-libs PRIVATE                            ${distribution_DIR}/gperf/include) // -lgperf target_link_libraries(hello-libs                       lib_gperf) 
  • on C++ code, use: #include <gperf.h>

  • header location: hello-libs/distribution/gperf/include/gperf.h

  • lib location: distribution/gperf/lib/arm64-v8a/libgperf.so

  • app/build.gradle:

    android {     sourceSets {         main {             // let gradle pack the shared library into apk             jniLibs.srcDirs = ['../distribution/gperf/lib'] 

    Then, if you look under /data/app on the device, libgperf.so will be there as well.

  • If you only support some architectures, see: Gradle Build NDK target only ARM

The example git tracks the prebuilt shared libraries, but it also contains the build system to actually build them as well: https://github.com/googlesamples/android-ndk/tree/840858984e1bb8a7fab37c1b7c571efbe7d6eb75/hello-libs/gen-libs