Cannot find libtorch_jni_lite.so on arm64-v8a device

Hi,

i am trying to use libtorch Native in Android application following this tutorial. Making Native Android Application that uses PyTorch prebuilt libraries — PyTorch Tutorials 2.2.0+cu121 documentation. I have made some modification in order to get it working and now on the Android Studio emulator i am able to print a libtorch tensor in Android.

Unfortunately on arm64-v8a the application crashes with the following error

java.lang.UnsatisfiedLinkError: dlopen failed: library "libpytorch_jni_lite.so" not found: needed by /data/data/com.example.testlib/code_cache/.overlay/base.apk/lib/arm64-v8a/libtestlib.so in namespace classloader-namespace
    at java.lang.Runtime.loadLibrary0(Runtime.java:1087)
    at java.lang.Runtime.loadLibrary0(Runtime.java:1008)
    at java.lang.System.loadLibrary(System.java:1664)
    at com.example.testlib.MainActivity.<clinit>(MainActivity.java:14)
    at java.lang.Class.newInstance(Native Method)
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
    at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3659)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3956)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2295)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)

It seems that libtorch_jni_lite.so is not found. Can someone please help me to understand better how to solve?
Thanks

Hey @erict, would you mind sharing what modification you have made based on the tutorial?
@Linbin may be able to help with that once we get more info about the issue.

Hi @guangy10,
following i am reporting what i have changed. Basically I refactor the example with a more simpler one and update the library with libtorch 1.9.0.

CMakeLists.txt

cmake_minimum_required(VERSION 3.10.2)
project("testlib")
set(LIB_TR /media/aistudios/44c62318-a7de-4fb6-a3e2-01aba49489c5/Develop/Official/DockerContainer/Android/Develop/testLib/pytorch_android_lite-1.9.0.aar)


#link_directories(${LIB_TR})

add_library( 
        testlib
        SHARED
        native-lib.cpp)


add_library( imported-pytorch
        SHARED
        IMPORTED )

set_target_properties( 
        imported-pytorch
        PROPERTIES IMPORTED_LOCATION
        ${LIB_TR}/jni/${ANDROID_ABI}/libpytorch_jni_lite.so )


add_library( imported-jni
        SHARED
        IMPORTED )

set_target_properties( # Specifies the target library.
        imported-jni  
        PROPERTIES IMPORTED_LOCATION
        ${LIB_TR}/jni/${ANDROID_ABI}/libfbjni.so )

find_library( # Sets the name of the path variable.
        log-lib
        log)


target_include_directories(testlib PUBLIC ${LIB_TR}/headers )

target_link_libraries( 
        testlib
        imported-pytorch
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

build.gradle

plugins {
    id 'com.android.application'
}

android {
    compileSdk 30
//    configurations {
//        extractForNativeBuild
//    }
    defaultConfig {
        applicationId "com.example.testlib"
        minSdk 28
        targetSdk 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags '-std=c++17'

            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.10.2'
        }
    }
    buildFeatures {
        viewBinding true
    }


}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    implementation 'org.pytorch:pytorch_android_lite:1.9.0'
//    extractForNativeBuild 'org.pytorch:pytorch_android_lite:1.9.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

native-lib.cpp

#include <jni.h>
#include <string>
#include <torch/script.h>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_testlib_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    torch::Tensor x = torch::randn({4, 8});
    std::ostringstream stream;
    stream << x;
    std::string tensor_string = stream.str();
    return env->NewStringUTF(tensor_string.c_str());
}

As i reported this code is working on x86 but it is crashing on arm64-v8a device. I have followed the tutorial to extract libtorch_jni_lite.so from the aar archive.

If you have any idea on how i can fix this, please let me know. Thanks

@Linbin @IvanKobzarev can you guys help with this issue?

Hi @Linbin @IvanKobzarev @guangy10

could you please give me some info, if possibile, on how to solve this problem. There are some tutorial/code already working?

Do you think i need to compile libtorch from source for Android? In this case could you provide me some resources if available on how to do correctly?

Thanks

Hi eric, did it work if you use libtorch_jni.so?

1 Like