Skip to content

Java.Lang.ClassNotFoundException thrown when creating a java object on a dotnet thread using native aot #10118

@emmauss

Description

@emmauss

Android framework version

net10.0-android (Preview)

Affected platform version

dotnet/android commit 0520997

Description

When constructing an object of a class implementing Java.Lang.Object on a thread other than the main thread, or possible a thread that does not have a looper registered(not possible to test since loopers are java objects and you can't create them on dotnet thread due to this issue), the app crashes with a Java.Lang.ClassNotFoundException exception, showing a message similar to the following;

com.companyname.android_naot         I  Android Exception: Java.Lang.ClassNotFoundException: Didn't find class "crc64644ab4d403a62c42.TestRunnable" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/system/lib64, /system_ext/lib64, /system/lib64, /system_ext/lib64]]
at Java.Interop.JniEnvironment.Types.TryFindClass(String classname, Boolean throwOnError) + 0x6d4
at Java.Interop.JniEnvironment.Types.FindClass(String classname) + 0x44
at Java.Interop.JniType..ctor(String classname) + 0x30
at Java.Interop.JniPeerMembers.JniInstanceMethods..ctor(Type declaringType) + 0x1a0
at Java.Interop.JniPeerMembers.JniInstanceMethods.GetConstructorsForType(Type declaringType) + 0xf8
at Java.Interop.JniPeerMembers.JniInstanceMethods.StartCreateInstance(String constructorSignature, Type declaringType, JniArgumentValue* parameters) + 0xd4
at Java.Lang.Object..ctor() + 0xd8
at android_naot.TestRunnable..ctor(Action action) + 0x28
at android_naot.MainActivity.<OnCreate>b__0_1(Object _) + 0x110

This crash will occur on Timers and other dotnet api that uses threads.

Steps to Reproduce

  1. Clone and checkout failing_repro branch from https://github.com/emmauss/AndroidNativeAOTTest/tree/failing_repro
  2. Build and run with dotnet-local.cmd build -t:Run, with dotnet-local.cmd being the batch file for locally built dotnet runtime
  3. When app runs, tap the "Hello" button. It fills up the visible space in the activity.
  4. App crashes.

Did you find any workaround?

Creating the object on the main thread and passing the reference to the thread you want bypasses the issue, as seen here https://github.com/emmauss/AndroidNativeAOTTest/blob/7de8ab27f77eb2f86670ca30c12620f4040b0c6e/MainActivity.cs#L12. The app runs a timer that output Button Clicked in logcat using the main thread.

Relevant log output

Metadata

Metadata

Assignees

Labels

Area: App RuntimeIssues in `libmonodroid.so`.needs-triageIssues that need to be assigned.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions