Issue
I eventually cross-compiled a capstone library for android, but now I am having trouble linking it with my main .so
library(native-lib).
I followed the steps shown in the official web site.
My CMakeList.txt is as below.
# 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/ )
The APK built has native-lib.so
, but does not have 'libcapstone.soor
libcapstone.so.5`. So when I launch the APK, the error below happens.
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)
I tried adding a jniLibs
directory in the folder and adding the libcapstone.so.5, but the APK built still does not contain libcapstone.so.(5) and the same error happens.
How should I edit my CMakeList.txt to correctly link a custom prebuilt library to a main shared library?(native-lib.so)
Solution
I changed my CMakeLists.txt to link the capstone as a static library.
# 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/ )
Hopefully, it worked!
Answered By - KYHSGeekCode
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.