Check failed: lib_handle_ != nullptr: Failed to load dynamic shared library

My .so file can’t be loaded in Android Studio. I compiled it by following code :slight_smile:

def to_tvm(mode="x86"):
    if not os.path.exists(EAST_ONNX_PATH):
        raise Exception("ONNX model not exists")

    if mode == "arm":
        target = (
            "llvm -target=arm-linux-androideabi -mfloat-abi=soft"
        )  # for armeabi-v7a
    elif mode == "x86":
        target = "llvm -mcpu=haswell"  # 酷睿四代
        # target = "llvm -mcpu=broadwell" # 酷睿五代

    input_name = "input.1"
    model = onnx.load(EAST_ONNX_PATH)
    x = torch.randn((1, 3, 512, 512)).data.numpy()
    shape_dict = {input_name: x.shape}
    mod, params = relay.frontend.from_onnx(model, shape_dict)

    with relay.build_config(opt_level=3):
        graph, lib, params = relay.build_module.build(
            mod, target, params=params
        )
    dtype = "float32"
    if mode == "arm":
        lib.export_library(
            EAST_TVM_ARM_LIB_PATH,
            cc="/home/dai/android-toolchain-arm/bin/arm-linux-androideabi-clang++",
        )

        with open(EAST_TVM_ARM_GRAPH_PATH, "w") as f:
            f.write(graph)

        with open(EAST_TVM_ARM_PARAMS_PATH, "wb") as f:
            f.write(relay.save_param_dict(params))

        return 0

But when I’m loading it in Android Studio like this:

tvm::runtime::Module mod = tvm::runtime::Module::LoadFromFile("./east_arm.so");

I got the following error:

03/10 09:29:09: Launching app
$ adb install-multiple -r -t -p com.example.tvm_android_deploy D:\scripts\tvm_android_deploy\app\build\intermediates\instant-run-apk\debug\app-debug.apk 
Split APKs installed in 1 s 495 ms
$ adb shell am start -n "com.example.tvm_android_deploy/com.example.tvm_android_deploy.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 6943 on device huawei-duk_al20-127.0.0.1:21503
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/InstantRun: starting instant run server: is main process
D/houdini: [6943] Added shared library /data/app/com.example.tvm_android_deploy-2/lib/arm/libnative-lib.so for ClassLoader by Native Bridge.
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
    Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
A/libc: /buildbot/src/googleplex-android/ndk-release-r20/external/libcxx/../../external/libcxxabi/src/abort_message.cpp:73: abort_message: assertion "terminating with uncaught exception of type dmlc::Error: [09:29:10] D:\scripts\tvm_android_deploy\app\src\main\cpp\src\runtime\dso_library.cc:84: Check failed: lib_handle_ != nullptr: Failed to load dynamic shared library D:\scripts\tvm_android_deploy\app\src\main\cpp\east_arm.so Cannot load library: " failed
    Fatal signal 6 (SIGABRT), code -6 in tid 6943 (_android_deploy)
Application terminated.

Is there anything wrong with my .so file? Looking forward to your reply.

Hi! I met this bug too. I try to crosscompile the C++ file and LOAD .so file, but on risc-v. Did you solve your problem now?