I am using dynamic delivery feature of Android because openCV make the app size too large. The problem is When I try to load the opencv by using SplitInstallHelper.loadLibrary(this, "opencv_java4")
. I gives me this error.
FATAL EXCEPTION: main E AndroidRuntime: Process: com.example.com, PID: 7470 E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: library "libc++_shared.so" not found E AndroidRuntime: at java.lang.Runtime.loadLibrary0(Runtime.java:1016) E AndroidRuntime: at java.lang.System.loadLibrary(System.java:1669) E AndroidRuntime: at com.google.android.play.core.splitinstall.SplitInstallHelper.loadLibrary(Unknown Source:0) E AndroidRuntime: at org.opencv.com.example.com.TempActivity.onResume(TempActivity.kt:615) E AndroidRuntime: at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1434) E AndroidRuntime: at android.app.Activity.performResume(Activity.java:7304) E AndroidRuntime: at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3993) E AndroidRuntime: at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4033) E AndroidRuntime: at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:51) E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:145) E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1977) E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106) E AndroidRuntime: at android.os.Looper.loop(Looper.java:193) E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6923) E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:870)
My onCreate looks like this
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
try {
SplitInstallHelper.loadLibrary(this, "opencv_java4")
} catch (exception: Exception) {
exception.printStackTrace()
}
Then loading the openCV library inside onResume
override fun onResume() {
super.onResume()
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback)
} else {
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS)
}
}
This is my defaultConfig in gradle for openCV module.
defaultConfig {
externalNativeBuild {
cmake {
cppFlags ""
arguments "-DANDROID_ARM_NEON=TRUE",'-DANDROID_STL=c++_shared'
targets "opencv_jni_shared"
}
}
ndk {
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
}
}
Some resource asked to make changes to CMakeList.txt & dummy.cpp file's
CmakeList.txt
cmake_minimum_required(VERSION 3.6)
# dummy target to bring libc++_shared.so into packages
add_library(opencv_jni_shared STATIC dummy.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_link_libraries( # Specifies the target library.
opencv_jni_shared
# Links the target library to the log library
# included in the NDK.
${log-lib})
dummy.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
whatever(
JNIEnv *env,
jobject /* this */){
std::string hello = "Hello";
return env->NewStringUTF(hello.c_str());
};
Okay, so I was desperately in search of a solution when I stumbled upon this Github issue. So apparently Android was asking me to load libc++_shared.so file before I try to load opencv_java4.
This is the change I made to my Activity which was loading opencv
And it worked!