Cannot find libtorch_jni_lite.so on arm64-v8a device

username: erict
cooked: <p>Hi,</p>
<p>i am trying to use libtorch Native in Android application following this tutorial. <a href="https://pytorch.org/tutorials/recipes/android_native_app_with_custom_op.html" class="inline-onebox" rel="noopener nofollow ugc">Making Native Android Application that uses PyTorch prebuilt libraries — PyTorch Tutorials 2.2.0+cu121 documentation</a>. 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.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/c/dc85b607280307f2ec666c50d44e64504290ed05.png" data-download-href="https://discuss.pytorch.org/uploads/default/dc85b607280307f2ec666c50d44e64504290ed05" title="libtorchEmulator"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/c/dc85b607280307f2ec666c50d44e64504290ed05_2_231x500.png" alt="libtorchEmulator" data-base62-sha1="vsPw7yzKyJ9Mh09oXq4CmMATHCt" width="231" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/c/dc85b607280307f2ec666c50d44e64504290ed05_2_231x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/c/dc85b607280307f2ec666c50d44e64504290ed05_2_346x750.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/c/dc85b607280307f2ec666c50d44e64504290ed05_2_462x1000.png 2x" data-dominant-color="D2CCDD"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">libtorchEmulator</span><span class="informations">484×1047 52.3 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Unfortunately on arm64-v8a the application crashes with the following error</p>
<pre><code>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.&lt;clinit&gt;(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)
</code></pre>
<p>It seems that libtorch_jni_lite.so is not found. Can someone please help me to understand better how to solve?<br>
Thanks</p>

username: guangy10
cooked: <p>Hey <a class="mention" href="/u/erict">@erict</a>, would you mind sharing what modification you have made based on the tutorial?<br>
<a class="mention" href="/u/linbin">@Linbin</a> may be able to help with that once we get more info about the issue.</p>

username: erict
cooked: <p>Hi <a class="mention" href="/u/guangy10">@guangy10</a>,<br>
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.</p>
<p>CMakeLists.txt</p>
<pre><code class="lang-auto">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})
</code></pre>
<p>build.gradle</p>
<pre><code class="lang-auto">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'
}
</code></pre>
<p>native-lib.cpp</p>
<pre><code class="lang-auto">#include &lt;jni.h&gt;
#include &lt;string&gt;
#include &lt;torch/script.h&gt;

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 &lt;&lt; x;
    std::string tensor_string = stream.str();
    return env-&gt;NewStringUTF(tensor_string.c_str());
}
</code></pre>
<p>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.</p>
<p>If you have any idea on how i can fix this, please let me know. Thanks</p>

username: guangy10
cooked: <p><a class="mention" href="/u/linbin">@Linbin</a> <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> can you guys help with this issue?</p>

username: erict
cooked: <p>Hi <a class="mention" href="/u/linbin">@Linbin</a>  <a class="mention" href="/u/ivankobzarev">@IvanKobzarev</a> <a class="mention" href="/u/guangy10">@guangy10</a></p>
<p>could you please give me some info, if possibile, on how to solve this problem. There are some tutorial/code already working?</p>
<p>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?</p>
<p>Thanks</p>

username: Linbin
cooked: <p>Hi eric, did it work if you use libtorch_jni.so?</p>