Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error java.lang.ClassNotFoundException: Didn't find class "org.pytorch.executorch.NativePeer" #8636

Closed
adonnini opened this issue Feb 22, 2025 · 4 comments
Assignees
Labels
module: android Issues related to Android code, build, and execution module: user experience Issues related to reducing friction for users

Comments

@adonnini
Copy link

adonnini commented Feb 22, 2025

Hi,

I tried to run the model produced with executorch using the executorch runtime for Android.

In /app/src/main/jniLibs/arm64-v8a for my app I have libexecutorch.so created following the instructions in
https://pytorch.org/executorch/stable/demo-apps-android.html

When I attempt to execute the following command in my application

mModule = Module.load(moduleFileAbsoluteFilePath);

execution fails producing the error log you will find below.

What am I doing wrong? What should I do next? Please let me know if you need additional information.

Thanks

ERROR LOG

02-22 14:00:59.256: I/NeuralNetworkService(26059):  - neuralNetworkloadAndRunPytorch - About to load module --- 
02-22 14:00:59.271: W/libc(26059): Access denied finding property "ro.mediatek.platform"
02-22 14:00:59.271: W/libc(26059): Access denied finding property "ro.chipname"
02-22 14:00:59.271: E/audit(1210): type=1400 audit(1740229259.269:43350): avc:  denied  { read } for  pid=26059 comm="Thread-2" name="u:object_r:media_plat_prop:s0" dev="tmpfs" ino=315 scontext=u:r:untrusted_app_30:s0:c45,c258,c512,c768 tcontext=u:object_r:media_plat_prop:s0 tclass=file permissive=0 SEPF_SM-S908B_12_0001 audit_filtered
02-22 14:00:59.273: D/nativeloader(26059): Load /data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/base.apk!/lib/arm64-v8a/libexecutorch.so using ns clns-5 from class loader (caller=/data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/base.apk): ok
02-22 14:00:59.275: E/log(26059): error java.lang.ClassNotFoundException: Didn't find class "org.pytorch.executorch.NativePeer" on path: DexPathList[[zip file "/data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/base.apk"],nativeLibraryDirectories=[/data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/lib/arm64, /data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/base.apk!/lib/arm64-v8a, /system/lib64]]
02-22 14:00:59.275: A/lNetworkService(26059): java_vm_ext.cc:616] JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception java.lang.ClassNotFoundException: Didn't find class "org.pytorch.executorch.NativePeer" on path: DexPathList[[zip file "/data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/base.apk"],nativeLibraryDirectories=[/data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/lib/arm64, /data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/base.apk!/lib/arm64-v8a, /system/lib64]]
02-22 14:00:59.275: A/lNetworkService(26059): java_vm_ext.cc:616] (Throwable with no stack trace)
02-22 14:00:59.275: A/lNetworkService(26059): java_vm_ext.cc:616] 
02-22 14:00:59.275: A/lNetworkService(26059): java_vm_ext.cc:616]     in call to NewGlobalRef
02-22 14:00:59.275: A/lNetworkService(26059): java_vm_ext.cc:616]     from java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader, java.lang.Class)
02-22 14:00:59.276: E/audit(1210): type=1400 audit(1740229259.269:43351): avc:  denied  { read } for  pid=26059 comm="Thread-2" name="u:object_r:chipname_prop:s0" dev="tmpfs" ino=165 scontext=u:r:untrusted_app_30:s0:c45,c258,c512,c768 tcontext=u:object_r:chipname_prop:s0 tclass=file permissive=0 SEPF_SM-S908B_12_0001 audit_filtered

cc @kirklandsign @cbilgin @mergennachin @byjlw

@adonnini
Copy link
Author

Hi, an update.

I added

includeBuild("/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/executorch/extension/android")

to settings.gradle for my application. It did not make any difference.

If I added

includeBuild("/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/executorch/extension/android/src/main/java/org/pytorch/executorch/")

the build failed, as expected, with the following error:

Project directory '/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/executorch/extension/android/src/main/java/org/pytorch/executorch' is not part of the build defined by settings file '/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/executorch/extension/android/settings.gradle'. If this is an unrelated build, it must have its own settings file.

I could be wrong but I think that using the executorch runtime in an Android application may not work for applications other than the executorch android demo because it is hardwired to look for the necessary modules (e.g. NativePeer) in executorch/extension/android.

I also noticed that AndroidManifest for the executorch Android demo app has:

        <uses-native-library android:name="libexecutorchdemo.so"
            android:required="false"/>

I could be wrong. Shouldn't it be

        <uses-native-library android:name="libexecutorch.so"
            android:required="false"/>

?

Thanks

@adonnini
Copy link
Author

Hi Update #2

I worked around the classNotFound exception by creating a java.org.pytorch.executorch package containing all the source files from
executorch/extension/android/src/main/java/org/pytorch/executorch

Now execution fails producing the error log reported below.

  1. I built libexecutorch.so following the instructions in
    https://pytorch.org/executorch/stable/demo-apps-android.html

  2. Package name is the same as expected (and used in the android demo app), i.e. org.pytorch.executorch

  3. The dependencies are

                implementation 'com.facebook.fbjni:fbjni-java-only:0.2.2'
                implementation 'com.facebook.soloader:nativeloader:0.10.5'

as in the android demo app

At this point, I am at a loss as to what I am doing wrong and what I should try next.

Should I add and android module to my project just as there is one in the android demo app. In the android demo app, the android module contains all the modules necessary to use the executorch runtime (e.g. NativePeer) in the application.

Please let me know if you need additional information.

Thanks

ERROR LOG

02-23 16:41:41.176: W/libc(25151): Access denied finding property "ro.mediatek.platform"
02-23 16:41:41.176: W/libc(25151): Access denied finding property "ro.chipname"
02-23 16:41:41.177: E/audit(1210): type=1400 audit(1740325301.170:52396): avc:  denied  { read } for  pid=25151 comm="Thread-2" name="u:object_r:media_plat_prop:s0" dev="tmpfs" ino=315 scontext=u:r:untrusted_app_30:s0:c45,c258,c512,c768 tcontext=u:object_r:media_plat_prop:s0 tclass=file permissive=0 SEPF_SM-S908B_12_0001 audit_filtered
02-23 16:41:41.177: E/audit(1210): type=1400 audit(1740325301.170:52397): avc:  denied  { read } for  pid=25151 comm="Thread-2" name="u:object_r:chipname_prop:s0" dev="tmpfs" ino=165 scontext=u:r:untrusted_app_30:s0:c45,c258,c512,c768 tcontext=u:object_r:chipname_prop:s0 tclass=file permissive=0 SEPF_SM-S908B_12_0001 audit_filtered
02-23 16:41:41.179: D/nativeloader(25151): Load /data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/base.apk!/lib/arm64-v8a/libexecutorch.so using ns clns-5 from class loader (caller=/data/app/~~CzYQ5jB-bXszoW-cWE7uUg==/com.android.contextq-B_Rwji2_VyK2Jx3NvUwN1A==/base.apk): ok
02-23 16:41:41.179: W/lNetworkService(25151): CheckJNI: method to register "initHybrid" not in the given class. This is slow, consider changing your RegisterNatives calls.
02-23 16:41:41.183: E/lNetworkService(25151): ----- class 'Lorg/pytorch/executorch/NativePeer;' cl=0x140133a8 -----
02-23 16:41:41.183: E/lNetworkService(25151):   objectSize=240 (224 from super)
02-23 16:41:41.183: E/lNetworkService(25151):   access=0x0000.0000
02-23 16:41:41.183: E/lNetworkService(25151):   super='java.lang.Class<java.lang.Object>' (cl=0x0)
02-23 16:41:41.183: E/lNetworkService(25151):   interfaces (1):
02-23 16:41:41.183: E/lNetworkService(25151):      0: java.lang.Class<org.pytorch.executorch.INativePeer> (cl=0x140133a8)
02-23 16:41:41.183: E/lNetworkService(25151):   vtable (2 entries, 11 in super):
02-23 16:41:41.183: E/lNetworkService(25151):      0: org.pytorch.executorch.EValue org.pytorch.executorch.NativePeer.forward(org.pytorch.executorch.EValue[])
02-23 16:41:41.183: E/lNetworkService(25151):      1: void org.pytorch.executorch.NativePeer.resetNative()
02-23 16:41:41.183: E/lNetworkService(25151):   direct methods (3 entries):
02-23 16:41:41.183: E/lNetworkService(25151):      0: void org.pytorch.executorch.NativePeer.<clinit>()
02-23 16:41:41.183: E/lNetworkService(25151):      1: void org.pytorch.executorch.NativePeer.<init>(java.lang.String, java.util.Map)
02-23 16:41:41.183: E/lNetworkService(25151):      2: com.facebook.jni.HybridData org.pytorch.executorch.NativePeer.initHybrid(java.lang.String, java.util.Map)
02-23 16:41:41.183: E/lNetworkService(25151):   instance fields (1 entries):
02-23 16:41:41.183: E/lNetworkService(25151):      0: com.facebook.jni.HybridData org.pytorch.executorch.NativePeer.mHybridData
02-23 16:41:41.183: E/lNetworkService(25151): Failed to register native method org.pytorch.executorch.NativePeer.initHybrid(Ljava/lang/String;I)Lcom/facebook/jni/HybridData; in /data/data/com.android.contextq/code_cache/.overlay/base.apk/classes14.dex
02-23 16:41:41.184: E/log(25151): error java.lang.NoSuchMethodError: no static or non-static method "Lorg/pytorch/executorch/NativePeer;.initHybrid(Ljava/lang/String;I)Lcom/facebook/jni/HybridData;"
02-23 16:41:41.185: A/lNetworkService(25151): java_vm_ext.cc:616] JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception java.lang.NoSuchMethodError: no static or non-static method "Lorg/pytorch/executorch/NativePeer;.initHybrid(Ljava/lang/String;I)Lcom/facebook/jni/HybridData;"
02-23 16:41:41.185: A/lNetworkService(25151): java_vm_ext.cc:616] (Throwable with no stack trace)
02-23 16:41:41.185: A/lNetworkService(25151): java_vm_ext.cc:616] 
02-23 16:41:41.185: A/lNetworkService(25151): java_vm_ext.cc:616]     in call to NewGlobalRef
02-23 16:41:41.185: A/lNetworkService(25151): java_vm_ext.cc:616]     from java.lang.String java.lang.Runtime.nativeLoad(java.lang.String, java.lang.ClassLoader, java.lang.Class)

@swolchok swolchok added the module: android Issues related to Android code, build, and execution label Feb 24, 2025
@mergennachin mergennachin added the module: user experience Issues related to reducing friction for users label Feb 24, 2025
@github-project-automation github-project-automation bot moved this to To triage in ExecuTorch DevX Feb 24, 2025
@kirklandsign
Copy link
Contributor

Hi @adonnini

Just want to clarify, generally for external dependencies for your Android java/kotlin app, you will need an Android library called AAR. An AAR contains java classes (in jar), native libraries (.so), and probably other resources.

For ExecuTorch, the .so file itself is not sufficient. The java packages are not present in the .so file. You need the corresponding java files (basically everything you copied in executorch/extension/android/src/main/java/org/pytorch/executorch). You need to compile these files and make it a jar. Then you will combine the so and jar into the AAR. We have a script build/build_android_llm_demo.sh to do that exactly (sorry about the poor naming).

@kirklandsign kirklandsign self-assigned this Feb 24, 2025
@kirklandsign
Copy link
Contributor

Please build the AAR and use it in your project.

We will overhaul the documentation in #8536, so I will close this specific issue for now.

Please re-open if you have further issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: android Issues related to Android code, build, and execution module: user experience Issues related to reducing friction for users
Projects
Status: Done
Development

No branches or pull requests

4 participants