问题
I use ndk-build to build a static library and compile to a *.so successfully, but Runtime error is thrown when runs in android simulator. The error is
E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.example.kotlin.mixed, PID: 31185
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_ZNSt6__ndk17codecvtIcc9mbstate_tE2idE" referenced by "/data/app/org.example.kotlin.mixed-2/lib/x86/libtest.so"...
at java.lang.Runtime.loadLibrary(Runtime.java:372)
at java.lang.System.loadLibrary(System.java:1076)
at com.bytedance.lark.sdk.Sdk.<init>(Sdk.kt:15)
at org.example.kotlin.mixed.MyApplication.onCreate(MyApplication.kt:12)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
And I find symbol _ZNSt6__ndk17codecvtIcc9mbstate_tE2idE appears on the static library I build. I can not find reference of ndk in my source code. I am not sure if the ndk-build add this symbol to the static lib.
the ndk-build config goes here.
Application.mk
APP_ABI := x86
APP_PLATFORM := android-21 // I change this to android-14, also not work
APP_STL:=c++_static
APP_CPPFLAGS:=-std=c++11 -fexceptions -frtti -DANDROID -DDEBUG
NDK_TOOLCHAIN_VERSION := clang
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include ../uuid/Android.mk
include mylib.mk
mylib.mk
LOCAL_PATH := $(call my-dir)
MYLIB_CSOURCES := \
// my source code
MYLIB_INCLUDES := \
$(LOCAL_PATH)/../uuid/include \
$(LOCAL_PATH)/../../../../../lib/rapidjson/include \
$(LOCAL_PATH)/../../../../../src
###
### Build mylib.a
###
include $(CLEAR_VARS)
LOCAL_MODULE := mylibc++
LOCAL_SRC_FILES := \
$(addprefix ../../../../../src/,$(MYLIB_CSOURCES))
LOCAL_C_INCLUDES := $(MYLIB_INCLUDES)
LOCAL_CFLAGS += -DANDROID -DDEBUG -D__ANDROID__
LOCAL_WHOLE_STATIC_LIBRARIES := uuid
include $(BUILD_STATIC_LIBRARY)
android build config:
android {
compileSdkVersion 23
buildToolsVersion '26.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
lintOptions {
abortOnError false
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir 'libs'
}
}
回答1:
The symbol _ZNSt6__ndk17codecvtIcc9mbstate_tE2idE (which means std::__ndk1::codecvt::id) is available in libc++_shared.so.
If your runtime environment (emulator) is lower than API 21, you must explicitly load this library from Java, before you load libtest.so.
libc++_shared.so should also be packed into the APK together with libtest.so. Make sure that it is present in libs/x86 and also for other relevant ABIs.
In Android Studio, you can let gradle build the NDK libraries for you, and it will take care of the necessary dependencies.
来源:https://stackoverflow.com/questions/47047664/java-lang-unsatisfiedlinkerror-dlopen-failed-cannot-locate-symbol-znst6-ndk