我使用ndk-build构建一个静态库并成功编译成* .so,但在android模拟器中运行时会抛出运行时错误。错误是
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)
我发现符号_ZNSt6__ndk17codecvtIcc9mbstate_tE2idE出现在我构建的静态库中。我在源代码中找不到ndk的引用。我不确定ndk-build是否将此符号添加到静态库。
ndk-build配置就在这里。
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'
}
}
答案 0 :(得分:0)
符号 _ZNSt6__ndk17codecvtIcc9mbstate_tE2idE (表示 std :: __ ndk1 :: codecvt :: id )可在 libc ++ _ shared.so 中找到。
如果运行时环境(模拟器)低于API 21,则 必须 在加载 libtest.so之前从Java显式加载此库强>
libc ++ _ shared.so 也应与 libtest.so 一起打包到APK中。确保它存在于 libs / x86 中以及其他相关的ABI中。
在Android Studio中,您可以让gradle为您构建NDK库,它将处理必要的依赖项。