如何将预构建的共享库链接到Android NDK项目?

时间:2021-04-29 15:29:57

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

这里我用了这个Android。在jni/文件夹中的mkfile。

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    := offlineDownloadLOCAL_SRC_FILES := offline_download.cLOCAL_SHARED_LIBRARIES :=../lib/libpackext.so.1.0LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -lloginclude $(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

在项目目录中创建一个lib文件夹,并将我预先构建的。so库和一个Android。mkfile包含以下内容

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := packextLOCAL_SRC_FILES := libpackext.so.1.0LOCAL_C_INCLUDES := $(LOCAL_PATH)/../includeinclude $(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.

当我使用ndk-build -B命令时,我得到了未定义的packageExtraction引用。在这里,我使用预构建的库函数意味着我不能将预构建的共享库链接到我的offlinedownload库。

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

所以请大家帮助我解决这个问题。

4 个解决方案

#1


33  

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.

这是一个完整的Android系统。用于使用第三方共享库的mkfile。库(libffmpe .so)放在jni文件夹中。它的“local_export_c_include”指定库的头文件保存在哪里。

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := ffmpegLOCAL_SRC_FILES := libffmpeg.soLOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../ffmpeg/libs/arm-linux-androideabi4.7_1/includeinclude $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE    := ffmpegandroidLOCAL_SRC_FILES := ffmpegandroid.cLOCAL_SHARED_LIBRARIES := ffmpeginclude $(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:

在你的jni /应用程序。然后将LOCAL_SRC_FILES更改为以下内容:

LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libffmpeg.so

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

和地点libffmpeg。所以在jni / armeabi / libffmpeg。所以,jni / armeabi-v7a / libffmpeg。所以等。

#2


2  

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.

    剪切并粘贴除LOCAL_PATH:= $(调用my-dir)之外的所有内容。可在你的第一。

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

    在你的第一个机器人的结尾写上以下内容。

    $(call import-module,packext)

    $(叫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.

    还要确保将NDK_MODULE_PATH环境变量设置为Android所在的路径。可以找到定义模块packext的mk-file。

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.

您还必须像mgiza在第一个答案中那样更改LOCAL_SHARED_LIBRARIES。我认为您得到的未定义引用的packageExtraction是在您的预构建库中,所以除非您有其他的链接问题,否则应该可以解决这个问题。

#3


0  

Have a look at the ndk documentation for prebuilts:

看看ndk文件的预制:

android-ndk/docs/PREBUILTS.html

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.

确保你的文件夹包含Android。packext模块的mk,可以在NDK_MODULE_PATH中找到。

#4


0  

Android NDK official hello-libs CMake example

Android NDK官方hello-libs CMake的例子

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

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.

刚为我在Ubuntu 17.10主机,Android Studio 3, Android SDK 26, NDK 15.2上工作过。所以我强烈建议你把你的项目建立在它的基础上。

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

共享库称为libgperf,关键代码部分为:

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

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

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

    在c++代码上,使用:#include

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

    头位置:hello-libs /分销/ gperf / include / gperf.h

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

    *位置:分布/ gperf / lib / arm64-v8a / libgperf.so

  • app/build.gradle:

    应用程序/ 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.

    然后,如果你在设备上查看/数据/应用程序,libgperf。也会有。

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

    如果您只支持某些架构,请参见: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

这个示例git跟踪预构建的共享库,但是它也包含构建系统来实际构建它们:https://github.com/googlesamples/android- ndk/tree/84085894e1bb8a7fab37c1b7b71efbe7d6eb75 /hello- libs/libs

#1


33  

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.

这是一个完整的Android系统。用于使用第三方共享库的mkfile。库(libffmpe .so)放在jni文件夹中。它的“local_export_c_include”指定库的头文件保存在哪里。

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := ffmpegLOCAL_SRC_FILES := libffmpeg.soLOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../ffmpeg/libs/arm-linux-androideabi4.7_1/includeinclude $(PREBUILT_SHARED_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE    := ffmpegandroidLOCAL_SRC_FILES := ffmpegandroid.cLOCAL_SHARED_LIBRARIES := ffmpeginclude $(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:

在你的jni /应用程序。然后将LOCAL_SRC_FILES更改为以下内容:

LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libffmpeg.so

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

和地点libffmpeg。所以在jni / armeabi / libffmpeg。所以,jni / armeabi-v7a / libffmpeg。所以等。

#2


2  

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.

    剪切并粘贴除LOCAL_PATH:= $(调用my-dir)之外的所有内容。可在你的第一。

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

    在你的第一个机器人的结尾写上以下内容。

    $(call import-module,packext)

    $(叫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.

    还要确保将NDK_MODULE_PATH环境变量设置为Android所在的路径。可以找到定义模块packext的mk-file。

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.

您还必须像mgiza在第一个答案中那样更改LOCAL_SHARED_LIBRARIES。我认为您得到的未定义引用的packageExtraction是在您的预构建库中,所以除非您有其他的链接问题,否则应该可以解决这个问题。

#3


0  

Have a look at the ndk documentation for prebuilts:

看看ndk文件的预制:

android-ndk/docs/PREBUILTS.html

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.

确保你的文件夹包含Android。packext模块的mk,可以在NDK_MODULE_PATH中找到。

#4


0  

Android NDK official hello-libs CMake example

Android NDK官方hello-libs CMake的例子

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

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.

刚为我在Ubuntu 17.10主机,Android Studio 3, Android SDK 26, NDK 15.2上工作过。所以我强烈建议你把你的项目建立在它的基础上。

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

共享库称为libgperf,关键代码部分为:

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

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

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

    在c++代码上,使用:#include

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

    头位置:hello-libs /分销/ gperf / include / gperf.h

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

    *位置:分布/ gperf / lib / arm64-v8a / libgperf.so

  • app/build.gradle:

    应用程序/ 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.

    然后,如果你在设备上查看/数据/应用程序,libgperf。也会有。

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

    如果您只支持某些架构,请参见: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

这个示例git跟踪预构建的共享库,但是它也包含构建系统来实际构建它们:https://github.com/googlesamples/android- ndk/tree/84085894e1bb8a7fab37c1b7b71efbe7d6eb75 /hello- libs/libs