# Cannot find libtorch\_jni\_lite.so on arm64-v8a device

**URL:** <https://discuss.pytorch.org/t/cannot-find-libtorch-jni-lite-so-on-arm64-v8a-device/128944>\
**Category:** Mobile\
**Created:** [August 9, 2021, 9:59am UTC](https://discuss.pytorch.org/t/cannot-find-libtorch-jni-lite-so-on-arm64-v8a-device/128944 "2021-08-09T09:59:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![erict](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/ee7513/32.png) [@erict](https://discuss.pytorch.org/u/erict)\
**Post date:** [August 9, 2021, 9:59am UTC](https://discuss.pytorch.org/t/cannot-find-libtorch-jni-lite-so-on-arm64-v8a-device/128944/1 "2021-08-09T09:59:59Z")

</div>

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](https://pytorch.org/tutorials/recipes/android_native_app_with_custom_op.html). 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.

 ![libtorchEmulator](https://discuss.pytorch.org/uploads/default/original/3X/d/c/dc85b607280307f2ec666c50d44e64504290ed05.png)

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

---

<div class="post-metadata">

**Author:** ![guangy10](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/a9a28c/32.png) [@guangy10](https://discuss.pytorch.org/u/guangy10)\
**Post date:** [August 9, 2021, 7:06pm UTC](https://discuss.pytorch.org/t/cannot-find-libtorch-jni-lite-so-on-arm64-v8a-device/128944/2 "2021-08-09T19:06:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![erict](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/ee7513/32.png) [@erict](https://discuss.pytorch.org/u/erict)\
**Post date:** [August 10, 2021, 8:26am UTC](https://discuss.pytorch.org/t/cannot-find-libtorch-jni-lite-so-on-arm64-v8a-device/128944/3 "2021-08-10T08:26:47Z")

</div>

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

```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})

```

build.gradle

```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'
}

```

native-lib.cpp

```auto
#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

---

<div class="post-metadata">

**Author:** ![guangy10](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/a9a28c/32.png) [@guangy10](https://discuss.pytorch.org/u/guangy10)\
**Post date:** [August 10, 2021, 5:03pm UTC](https://discuss.pytorch.org/t/cannot-find-libtorch-jni-lite-so-on-arm64-v8a-device/128944/4 "2021-08-10T17:03:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![erict](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/e/ee7513/32.png) [@erict](https://discuss.pytorch.org/u/erict)\
**Post date:** [August 27, 2021, 7:26am UTC](https://discuss.pytorch.org/t/cannot-find-libtorch-jni-lite-so-on-arm64-v8a-device/128944/5 "2021-08-27T07:26:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Linbin](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/linbin/32/18676_2.png) [@Linbin](https://discuss.pytorch.org/u/Linbin)\
**Post date:** [August 27, 2021, 4:37pm UTC](https://discuss.pytorch.org/t/cannot-find-libtorch-jni-lite-so-on-arm64-v8a-device/128944/6 "2021-08-27T16:37:22Z")

</div>

Hi eric, did it work if you use libtorch\_jni.so?
