Android studio CMake 不会 link 预建库到主 .so 文件
Android studio CMake does not link the prebuilt library to the main .so file
我最终为 android 交叉编译了一个 capstone library,但现在我在 link 使用我的主 .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.soor
libcapstone.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 以将自定义预构建库正确地 link 为主共享库?(native-lib.so)
我将 CMakeLists.txt 更改为 link 作为静态库的顶点。
# 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/ )
希望成功了!
我最终为 android 交叉编译了一个 capstone library,但现在我在 link 使用我的主 .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.soor
libcapstone.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 以将自定义预构建库正确地 link 为主共享库?(native-lib.so)
我将 CMakeLists.txt 更改为 link 作为静态库的顶点。
# 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/ )
希望成功了!