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

前端 未结 4 1843
醉话见心
醉话见心 2020-11-27 18:28

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

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_PATH := $(call my-dir)
         


        
相关标签:
4条回答
  • 2020-11-27 18:58

    You have to do either one of the following:

    1. Cut and paste everything except LOCAL_PATH := $(call my-dir) from your second Android.mk into your first.

    2. Put the following in the end of your first Android.mk:

      $(call import-module,packext)

      Also make sure that you set your NDK_MODULE_PATH environment variable to a path where the Android.mk-file defining the module packext can be found.

    You also have to change the LOCAL_SHARED_LIBRARIES in the same way mgiza said in the first answer. I suppose the packageExtraction that you got undefined reference to is in your prebuilt library so unless you have other linking problems this should solve the issue.

    0 讨论(0)
  • 2020-11-27 18:59

    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

    0 讨论(0)
  • 2020-11-27 19:05

    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 ..

    0 讨论(0)
  • 2020-11-27 19:05

    Have a look at the ndk documentation for prebuilts:

    android-ndk/docs/PREBUILTS.html

    You have to change

    LOCAL_SHARED_LIBRARIES :=../lib/libpackext.so.1.0
    

    to

    LOCAL_SHARED_LIBRARIES := packext
    

    Be sure that your folder containing the Android.mk for the packext module is named packext and can be found in in your NDK_MODULE_PATH.

    0 讨论(0)
提交回复
热议问题