Skip to content

[Xamarin.Android.Build.Tasks] use mono.android.jar only for $(AndroidCodegenTarget)=XAJavaInterop1 #9813

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

Merged
merged 1 commit into from
Feb 20, 2025

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Feb 20, 2025

mono.android.jar/dex are compiled with $(AndroidCodegenTarget)=XAJavaInterop1, and so this causes a problem if you build a project with $(AndroidCodegenTarget)=JavaInterop1 as incorrect Java stubs will be used.

For example, dotnet new maui using NativeAOT we can get a crash such as:

02-14 16:23:29.884 19260 19260 E .hellonativeaot: No implementation found for void mono.android.Runtime.register(java.lang.String, java.lang.Class, java.lang.String) (tried Java_mono_android_Runtime_register and Java_mono_android_Runtime_register__Ljava_lang_String_2Ljava_lang_Class_2Ljava_lang_String_2) - is the library loaded, e.g. System.loadLibrary?

Which is caused by mono.android.Runtime.register being called by mono/android/view/View_OnClickListenerImplementor.java which is compiled with $(AndroidCodegenTarget)=XAJavaInterop1.

To fix this:

  • Several places already check if $(_AndroidUseMarshalMethods)=true and skip usage of mono.android.jar.

  • Add an additional check to verify $(AndroidCodegenTarget)=XAJavaInterop1

Previously, $(AndroidCodegenTarget) was also allowed to blank and default to XAJavaInterop1, but it seems better to explicitly set it.

I also updated the NativAOT() test to verify mono/android/views/View_OnClickListenerImplementor.java is generated on disk.

…oidCodegenTarget)=XAJavaInterop1`

`mono.android.jar/dex` are compiled with
`$(AndroidCodegenTarget)=XAJavaInterop1`, and so this causes a problem
if you build a project targeting `$(AndroidCodegenTarget)=JavaInterop1`
as incorrect Java stubs will be used.

For example, `dotnet new maui` using NativAOT we can get a crash such as:

    02-14 16:23:29.884 19260 19260 E .hellonativeaot: No implementation found for void mono.android.Runtime.register(java.lang.String, java.lang.Class, java.lang.String) (tried Java_mono_android_Runtime_register and Java_mono_android_Runtime_register__Ljava_lang_String_2Ljava_lang_Class_2Ljava_lang_String_2) - is the library loaded, e.g. System.loadLibrary?

Which is caused by `mono.android.Runtime.register` being called by
`mono/android/view/View_OnClickListenerImplementor.java` which is
compiled with `$(AndroidCodegenTarget)=XAJavaInterop1`.

To fix this:

* Several places already check if `$(_AndroidUseMarshalMethods)=true`
  and skip usage of `mono.android.jar`.

* Add an additional check to verify `$(AndroidCodegenTarget)=XAJavaInterop1`

Previously, `$(AndroidCodegenTarget)` was also allowed to blank and
default to `XAJavaInterop1`, but it seems better to explicitly set it.

I also updated the `NativAOT()` test to verify
`mono/android/views/View_OnClickListenerImplementor.java` is generated
on disk.
@jonathanpeppers jonathanpeppers marked this pull request as ready for review February 20, 2025 17:11
@jonpryor jonpryor merged commit 236893c into main Feb 20, 2025
58 checks passed
@jonpryor jonpryor deleted the dev/peppers/MonoAndroidJavaStubs branch February 20, 2025 18:23
grendello pushed a commit that referenced this pull request Feb 21, 2025
…oidCodegenTarget)=XAJavaInterop1` (#9813)

`mono.android.jar` and `mono.android.dex` are compiled with
`$(AndroidCodegenTarget)=XAJavaInterop1`.  This causes problems if
you build a project with `$(AndroidCodegenTarget)=JavaInterop1`
as the wrong set of Java stubs will be used.

For example, `dotnet new maui` using NativAOT we can get a crash such as:

	E .hellonativeaot: No implementation found for void mono.android.Runtime.register(java.lang.String, java.lang.Class, java.lang.String) (tried Java_mono_android_Runtime_register and Java_mono_android_Runtime_register__Ljava_lang_String_2Ljava_lang_Class_2Ljava_lang_String_2) - is the library loaded, e.g. System.loadLibrary?

Which is caused by `mono.android.Runtime.register()` being called by
`mono/android/view/View_OnClickListenerImplementor.java` which is
compiled with `$(AndroidCodegenTarget)=XAJavaInterop1`.

To fix this:

  * Several places already check if `$(_AndroidUseMarshalMethods)=true`
    and skip usage of `mono.android.jar`.

  * Add an additional check to verify
    `$(AndroidCodegenTarget)=XAJavaInterop1`

Previously, `$(AndroidCodegenTarget)` was also allowed to be blank and
default to `XAJavaInterop1`, but it seems better to explicitly set it.

I also updated `BuildTest2.NativAOT()` to verify that
`mono/android/views/View_OnClickListenerImplementor.java` is
generated on disk.
@github-actions github-actions bot locked and limited conversation to collaborators Mar 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants