问题
I'm trying to use a C/C++ TensorFlow Lite shared library for my project, and it succeeds.
So I added a few more libraries, but lib_nnapi.so fails to link in runtime(System.loadLibrary
).
If I remove that specific library, everything works fine.
Using fetchelf to rename SONAME failed with cannot locate symbol
error.
Wrote CMakeLists.txt using Official Android ndk example. All library is located in the same directories.
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
option(BUILD_WITH_OPENCV_4 "Build with OpenCV 4.1.0" ON)
option(BUILD_IN_ANDROID "Build the pupil library in android" ON)
option(BUILD_WITH_GUI "Build with gui version of opencv" OFF)
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../distribution)
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/opencv")
add_library(lib_tflite SHARED IMPORTED)
set_target_properties(lib_tflite PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/tflite/lib/${ANDROID_ABI}/libtensorflowlite_c.so)
add_library(lib_tflite_gpu SHARED IMPORTED)
set_target_properties(lib_tflite_gpu PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/tflite/lib/${ANDROID_ABI}/libtensorflowlite_gpu_delegate.so)
add_library(lib_nnapi SHARED IMPORTED)
set_target_properties(lib_nnapi PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/tflite/lib/${ANDROID_ABI}/libnnapi_delegate.so)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
add_library(hello-libs SHARED
hello-libs.cpp)
target_include_directories(hello-libs PRIVATE
${distribution_DIR}/tflite/include
${distribution_DIR}/tflite/include/tensorflow/lite/tools/make/downloads
${distribution_DIR}/tflite/include/tensorflow/lite/tools/make/downloads/flatbuffers/include
)
target_link_libraries(hello-libs
android neuralnetworks log
lib_tflite lib_tflite_gpu
lib_nnapi # <- this library fails to link.
opencv z)
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId = 'com.example.hellolibs'
minSdkVersion 27
targetSdkVersion 29
versionCode = 1
versionName = '1.0'
externalNativeBuild {
cmake {
cppFlags ""
arguments '-DANDROID_STL=c++_shared'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
ndk {
abiFilters "arm64-v8a"
}
}
debug {
ndk {
abiFilters "arm64-v8a"
}
}
}
sourceSets {
main {
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['../distribution/tflite/lib']
}
}
externalNativeBuild {
cmake {
version '3.10.2'
path 'src/main/cpp/CMakeLists.txt'
}
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
}
Error Log
2020-03-30 15:38:18.672 22574-22574/com.example.hellolibs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.hellolibs, PID: 22574
java.lang.UnsatisfiedLinkError: dlopen failed: library "/Users/yonggyulee/Documents/GitHub/AI-Kit/tflite/c/hello-libs/app/src/main/cpp/../../../../distribution/tflite/lib/arm64-v8a/libnnapi_delegate.so" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:1016)
at java.lang.System.loadLibrary(System.java:1669)
at com.example.hellolibs.MainActivity.<clinit>(MainActivity.java:32)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1219)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3040)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3292)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1980)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7168)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
来源:https://stackoverflow.com/questions/60925493/android-c-ndk-some-shared-libraries-refuses-to-link-in-runtime