我试图在我的Android应用中加载openssl .So文件,但它抛出了不舒服的linkerror。
mycode:
public class MainActivity extends AppCompatActivity {
static {
System.loadLibrary("ssl");
System.loadLibrary("crypto");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}}
例外:
12-28 10:53:41.753 12254-12254/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ndk.test.app, PID: 12254
java.lang.UnsatisfiedLinkError: dlopen failed: library "/system/lib/libssl.so" needed or dlopened by "/system/lib/libnativeloader.so" is not accessible for the namespace "classloader-namespace"
at java.lang.Runtime.loadLibrary0(Runtime.java:977)
at java.lang.System.loadLibrary(System.java:1567)
at com.ndk.test.app.MainActivity.<clinit>(MainActivity.java:11)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1086)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2839)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
cmakelist.txt
cmake_minimum_required(VERSION 3.4.1)
# configure import libs
set(distribution_DIR ${PROJECT_SOURCE_DIR}/src/main/cpp)
add_library(lib_crypto SHARED IMPORTED)
set_target_properties(lib_crypto PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libs/libcrypto.so)
add_library(lib_ssl SHARED IMPORTED)
set_target_properties(lib_ssl PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/libs/libssl.so)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
add_library( # Sets 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( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
target_include_directories(native-lib PRIVATE
${distribution_DIR}/include
${distribution_DIR}/include)
target_link_libraries( # Specifies the target library.
native-lib
android
lib_crypto
lib_ssl
# Links the target library to the log library
# included in the NDK.
${log-lib} )
这在我的代码和异常之上。我也在 cmakelist.txt 中导入.so(s),但我无法找到问题的确切问题,因为我也将.SO(s)放在Libs文件夹中。
请建议我一些解决方案。
我不好,我通过添加 $ {android_abi} 解决了它,我忘了将.so(s)放在ABI文件夹中。
set(distribution_DIR ${PROJECT_SOURCE_DIR}/src/main/cpp/${ANDROID_ABI})