【问题标题】:Android studio CMake does not link the prebuilt library to the main .so fileAndroid Studio CMake 不会将预建库链接到主 .so 文件
【发布时间】:2019-08-22 12:58:04
【问题描述】:

我最终为 android 交叉编译了一个 capstone library,但现在我无法将它与我的主要 .so 库(native-lib)链接起来。 我按照official web site中显示的步骤操作。

我的 CMakeList.txt 如下。

# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              log )

# Links your native library against one or more other native libraries.
#target_link_libraries( # Specifies the target library.
#                       native-lib
#
#                      # Links the log library to the target library.
#                       ${log-lib} )

add_library( capstone
        SHARED
        IMPORTED )

set_target_properties( # Specifies the target library.
        capstone

        # Specifies the parameter you want to define.
        PROPERTIES IMPORTED_LOCATION

        # Provides the path to the library you want to import.
        C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone.so.5 )

target_link_libraries( native-lib capstone ${log-lib} ) #app-glue


include_directories( capstone/include/ )

构建的 APK 有 native-lib.so,但没有 'libcapstone.soorlibcapstone.so.5`。因此,当我启动 APK 时,会发生以下错误。

2019-08-22 14:21:01.340 6122-6122/com.kyhsgeekcode.disassembler E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.kyhsgeekcode.disassembler, PID: 6122
    java.lang.UnsatisfiedLinkError: dlopen failed: library "libcapstone.so.5" not found
        at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
        at java.lang.System.loadLibrary(System.java:1669)
        at com.kyhsgeekcode.disassembler.MainActivity.<clinit>(MainActivity.java:169)
        at java.lang.Class.newInstance(Native Method)

我尝试在文件夹中添加jniLibs目录并添加libcapstone.so.5,但是构建的APK仍然不包含libcapstone.so.(5),并且发生了同样的错误。

我应该如何编辑我的 CMakeList.txt 以将自定义预构建库正确链接到主共享库?(native-lib.so)

【问题讨论】:

  • android 不支持 IIRC 版本的动态库,请尝试在 Android Studio 构建之前将 libcapstone.so.5 重命名为 libcapstone.so
  • @RichardCritten 发布答案,以便将其标记为正确 :)
  • @DanAlbert 这是一个 IIRC,有点猜测,我自己无法测试。如果其他人可以验证它,那么可以发布答案。如果不进行测试,我会觉得不够自信。
  • CMake 与 ndk-build 的不同之处在于它不会复制要打包到 APK 或捆绑包中的预构建共享库。我们必须将 C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/ ` 添加到 jniLibs。不过,不支持版本化名称。

标签: android c++ cmake android-ndk shared-libraries


【解决方案1】:

我更改了我的 CMakeLists.txt 以将顶点链接为静态库。

# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              log )

# Links your native library against one or more other native libraries.
#target_link_libraries( # Specifies the target library.
#                       native-lib
#
#                      # Links the log library to the target library.
#                       ${log-lib} )

#add_library( capstone
#        SHARED
#        IMPORTED )

#set_target_properties( # Specifies the target library.
#        capstone
#
#        # Specifies the parameter you want to define.
#        PROPERTIES IMPORTED_LOCATION#
#
#        # Provides the path to the library you want to import.
#        C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone. )

target_link_libraries( native-lib C:/Users/82102/AndroidStudioProjects/Android-Disassembler/capstone/${ANDROID_ABI}/libcapstone.a ${log-lib} ) #app-glue


include_directories( capstone/include/ )

希望它成功了!

【讨论】:

    猜你喜欢
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2019-08-17
    • 1970-01-01
    • 2012-09-01
    • 1970-01-01
    • 2012-12-28
    相关资源
    最近更新 更多