Skip to content

Java Callable Wrappers under Native AOT no longer forward C# attributes to Java annotations #12542

Description

@jonpryor

Android framework version

net11.0-android (Preview)

Affected platform version

.NET 11 Preview 7

Description

dotnet/android has long supported forwarding C# custom attributes to Java annotations within generated Java Callable Wrappers. This also worked when targeting (experimental!) Native AOT:

# net 10!
dotnet new android -n android-jsiface
cd android-jsiface
cat <<'EOF' | git applydiff --git a/MyWebViewHandler.cs b/MyWebViewHandler.csnew file mode 100644index 0000000..a3061b9--- /dev/null+++ b/MyWebViewHandler.cs@@ -0,0 +1,10 @@+using Android.Webkit;++namespace impl;++public class MyWebViewHandler : Example.WebViewHandlerBase {+ [JavascriptInterface]+ public override void PostMessage(string? message)+ {+ }+}diff --git a/WebViewHandlerBase.java b/WebViewHandlerBase.javanew file mode 100644index 0000000..a249256--- /dev/null+++ b/WebViewHandlerBase.java@@ -0,0 +1,4 @@+package example;+public abstract class WebViewHandlerBase {+	public abstract void postMessage(String message);+}EOF
dotnet publish -r android-arm64 -p:PublishAot=true

The above would produce obj/Release/net10.0-android/android-arm64/android/src/crc64560613145aa5a4d6/MyWebViewHandler.java, which contained:

@android.webkit.JavascriptInterfacepublicvoidpostMessage (java.lang.Stringp0)
{
n_postMessage (p0);
}

The C# [JavascriptInterface] custom attribute became the Java @JavascriptInterface annotation.

See: android-jsiface.zip

This worked through .NET 11 Preview 6.

Enter .NET 11 Preview 7, which introduces a modified Java Callable Wrapper generator with scrc64… package names for Native AOT; see 3f54298 and others.

.NEt 11 Preview 7 broke Native AOT support out of the box, but we can work around that by using Directory.Build.targets:

cat <<'EOF' | git applydiff --git a/Directory.Build.targets b/Directory.Build.targetsnew file mode 100644index 0000000..4af58f5--- /dev/null+++ b/Directory.Build.targets@@ -0,0 +1,14 @@+<Project>+	<!--+ Android+NativeAOT fails with .NET 11 Preview 7 with error NU1102:+ Unable to find package Microsoft.NETCore.App.Runtime.NativeAOT.android-arm64 with version (= 11.0.0-preview.7.26378.119)+ …because there is no published 11.0.0-preview.7.26378.119 package; it should be 11.0.0-preview.7.26381.103.++ This is the upstream fix: https://github.com/dotnet/android/pull/12356++ For now, Update `$(MicrosoftNETCoreAppRefPackageVersion)` to the correct version so that NuGet versions are correct.+ -->+	<PropertyGroup Condition=" '$(BundledNETCoreAppPackageVersion)' == '11.0.0-preview.7.26381.103' ">+ <MicrosoftNETCoreAppRefPackageVersion>11.0.0-preview.7.26381.103</MicrosoftNETCoreAppRefPackageVersion>+	</PropertyGroup>+</Project>EOF

Next, update $(TargetFramework) to target net11.0-android:

cat <<'EOF' | git applydiff --git a/android-jsiface.csproj b/android-jsiface.csprojindex 974a617..7834ed0 100644--- a/android-jsiface.csproj+++ b/android-jsiface.csproj@@ -1,6 +1,6 @@ <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup>- <TargetFramework>net10.0-android</TargetFramework>+ <TargetFramework>net11.0-android</TargetFramework> <SupportedOSPlatformVersion>24</SupportedOSPlatformVersion> <RootNamespace>android_jsiface</RootNamespace> <OutputType>Exe</OutputType>EOF

Resulting project: android-jsiface+net11.zip

Build for Native AOT using .NET 11 Preview 7:

dotnet publish -r android-arm64 -p:PublishAot=true

Now, investigate the Java Callable Wrapper for MyWebViewHandler, in obj/Release/net11.0-android/android-arm64/typemap/java/scrc64ea0c4e731f7fd0e6/MyWebViewHandler.java (note the /scrc64…, not /crc64…, which contains:

@OverridepublicvoidpostMessage (java.lang.Stringp0)
{
n_PostMessage_Ljava_lang_String_ (p0);
}

Note that @android.webkit.JavascriptInterface is not present.


Why does this matter? It breaks JavaScript bridging under Native AOT, which in turn means Uno unit tests fail, e.g. When_WebMessageReceived() (which is a timeout, and not really meaningful to anyone), but the cause is the missing @JavascriptInterface annotation within the Java Callable Wrapper.

Steps to Reproduce

Did you find any workaround?

No.

Relevant log output

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: App+Library BuildIssues when building Library projects or Application projects.Area: NativeAOTIssues that only occur when using NativeAOT.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions