E/log: error java.lang.ClassNotFoundException: Didn't find class "org.pytorch.PyTorchAndroid"

Hello Pytorch’s community. I looked at the existing topics on this issue, but did not find a suitable solution for myself.

The problem is: I getting

E/log: error java.lang.ClassNotFoundException: Didn’t find class “org.pytorch.PyTorchAndroid” on path: DexPathList[[zip file “/data/app/~~5hf8jgRJFfzqOKLB8ckRxQ==/com.example.ndk_camera_jni-gl6E_Jg9_yyZyXm5mIbJ8A==/base.apk”],nativeLibraryDirectories=[/data/app/~~5hf8jgRJFfzqOKLB8ckRxQ==/com.example.ndk_camera_jni-gl6E_Jg9_yyZyXm5mIbJ8A==/lib/arm64, /data/app/~~5hf8jgRJFfzqOKLB8ckRxQ==/com.example.ndk_camera_jni-gl6E_Jg9_yyZyXm5mIbJ8A==/base.apk!/lib/arm64-v8a, /system/lib64, /system_ext/lib64]]
A/.ndk_camera_jn: java_vm_ext.cc:577] JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception java.lang.ClassNotFoundException: Didn’t find class “org.pytorch.PyTorchAndroid”

I have tried different version: 1.12, 1.9, 1.10. Also, I have tried ‘org.pytorch:pytorch_android_lite:1.9.0’ (lite version), but all time getting the same error.

Just one point need to admit, that I have entry point on NDK layer. It means, that my application started execution from c++ jni. After that it use some method from java layer.

Does somebody face this problem? Maybe someone has a suggestion where should I to find solution

Best regards, Taras

Hi Taras,
I got the same issue at runtime when using native C++ Pytorch API on android and was able to fix it.
I was missing the implementation of pytorch in the app/build.gradle in dependencies,

You need
dependencies {
implementation ‘org.pytorch:pytorch_android_lite:1.10.0’
extractForNativeBuild ‘org.pytorch:pytorch_android_lite:1.10.0’

}

Hope it helps,

Best.

try
target_link_libraries(test fbjni pytorch_jni)
first fbjni and than pytorch_jni
not
target_link_libraries(test pytorch_jni fbjni)