Skip to content

[Android] ban [Export] in the banned API analyzers - #36773

Merged
kubaflo merged 1 commit into
mainfrom
jonathanpeppers-ban-export-attribute
Jul 25, 2026
Merged

kubaflo merged 1 commit into
mainfrom
jonathanpeppers-ban-export-attribute

Conversation

@jonathanpeppers

Copy link
Copy Markdown
Member

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Context: #24744
Context: #24952

Description of Change

Java.Interop.ExportAttribute is annotated with [RequiresUnreferencedCode], so every use of [Export] produces an IL2026 trim warning and depends on Java.Interop.Export.dll reflection at runtime. #24744 established the alternative: declare an abstract class in src/Core/AndroidNative and override it, which lets the Java Callable Wrapper generator emit the method — and its @android.webkit.JavascriptInterface annotation — statically.

This adds T:Java.Interop.ExportAttribute to eng/BannedSymbols.txt so the pattern is enforced by RS0030 going forward, instead of relying on reviewers to catch it. The message points at HybridJavaScriptInterface.java as the working example:

T:Java.Interop.ExportAttribute;[Export] pulls in Java.Interop.Export dynamic features that are not trimming or AOT friendly. Declare an abstract class in src/Core/AndroidNative and override it instead, see src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/HybridJavaScriptInterface.java

Adding the ban flagged the one remaining offender, RefreshViewWebViewScrollCapture.ScrollCaptureState.SetCanScrollUp():

src/Core/src/Platform/Android/RefreshViewWebViewScrollCapture.cs(209,4): error RS0030:
The symbol 'Java.Interop.ExportAttribute' is banned in this project: [Export] pulls in
Java.Interop.Export dynamic features that are not trimming or AOT friendly. ...

So it is fixed the same way #24744 did: a new RefreshViewJavaScriptInterface abstract class in the maui.aar Java sources, with ScrollCaptureState deriving from it. The [RequiresUnreferencedCode("Java.Interop.Export uses dynamic features.")] suppression on SetCanScrollUp() is no longer needed and is removed.

Note the base type is deliberately an abstract class and not an interface. #24952 reverted HybridJavaScriptInterface from an interface back to a class because Java does not inherit method annotations from interfaces — the generated JCW silently lost @android.webkit.JavascriptInterface and the JavaScript bridge stopped working. The same applies here.

The remaining [Export] usages in the repo are all Foundation.ExportAttribute (Apple), which is a different type and is unaffected by this ban.

Issues Fixed

None; this is a proactive lint rule plus the one cleanup it surfaced.

Testing

Verified locally by building src/Core/src/Core.csproj for net10.0-android36.0:

  • Before the fix — build fails with error RS0030 at RefreshViewWebViewScrollCapture.cs(209,4), confirming the new banned-symbol entry is actually enforced.
  • After the fix — Microsoft.Maui.dll builds clean, including the Gradle step that compiles the new Java file into maui.aar.

No behavior change is expected at runtime: the exported Java method name is still setCanScrollUp, and [JavascriptInterface] remains on the C# override, matching the shipping HybridWebViewHandler pattern.

`Java.Interop.ExportAttribute` is annotated with
`[RequiresUnreferencedCode]`, so every use of `[Export]` produces an IL2026
trim warning and relies on `Java.Interop.Export.dll` reflection at runtime.
#24744 established the alternative: declare an abstract class in
`src/Core/AndroidNative` and `override` it, which lets the Java Callable
Wrapper generator emit the method (and its `@JavascriptInterface`
annotation) statically.

Add `T:Java.Interop.ExportAttribute` to `eng/BannedSymbols.txt` so this is
enforced by RS0030 going forward. The message points at
`HybridJavaScriptInterface.java` as the working example.

This flagged the one remaining offender,
`RefreshViewWebViewScrollCapture.ScrollCaptureState.SetCanScrollUp()`:

    error RS0030: The symbol 'Java.Interop.ExportAttribute' is banned in this project

Fix it the same way #24744 did, by adding a
`RefreshViewJavaScriptInterface` abstract class to the `maui.aar` Java
sources and deriving `ScrollCaptureState` from it. The
`[RequiresUnreferencedCode]` suppression on `SetCanScrollUp()` is no longer
needed.

Note the base type is an abstract class rather than an interface on
purpose: #24952 reverted `HybridJavaScriptInterface` from an
interface back to a class because Java does not inherit method annotations
from interfaces, so the generated JCW silently lost
`@android.webkit.JavascriptInterface` and the JavaScript bridge stopped
working.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7fb11a38-1848-4bb2-8b5c-7853dcad56bb
Copilot AI review requested due to automatic review settings July 24, 2026 19:00
@github-actions

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 36773

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 36773"

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request enforces a trimming/AOT-friendly Android pattern by banning Java.Interop.ExportAttribute via the Banned API analyzers and updating the remaining in-repo Android WebView bridge usage to use the abstract-Java-class + C# override approach (matching the established HybridJavaScriptInterface pattern).

Changes:

  • Add T:Java.Interop.ExportAttribute to eng/BannedSymbols.txt so [Export] usage is enforced as RS0030 with guidance to the AndroidNative abstract-class pattern.
  • Replace the last [Export] usage in RefreshViewWebViewScrollCapture by switching to a bound RefreshViewJavaScriptInterface base class and overriding the method.
  • Add RefreshViewJavaScriptInterface.java to src/Core/AndroidNative so the Java side has a stable @android.webkit.JavascriptInterface-annotated contract.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/Core/src/Platform/Android/RefreshViewWebViewScrollCapture.cs Removes [Export]/RUC suppression and switches the JS bridge to override an AndroidNative abstract base type.
src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/RefreshViewJavaScriptInterface.java Introduces the abstract Java contract with @JavascriptInterface for the scroll-capture bridge method.
eng/BannedSymbols.txt Adds a banned-symbol rule for Java.Interop.ExportAttribute with remediation guidance.

@github-actions github-actions Bot added area-infrastructure CI, Maestro / Coherency, upstream dependencies/versions platform/android labels Jul 24, 2026
@PureWeen

Copy link
Copy Markdown
Member

/azp run maui-pr-uitests, maui-pr-devicetests

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@kubaflo

kubaflo commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Scoped maui-pr review: I found no blocker in this three-file change.

The implementation replaces [Export] with the established AndroidNative abstract-class/managed-override pattern, preserves the JavaScript-visible setCanScrollUp method, and adds analyzer enforcement against reintroducing the AOT-unsafe pattern.

maui-pr build 1525638 is green, including both macOS and Windows NativeAOT integration legs. This is the correct upstream fix for the Android IL2026 blocker seen in #36733. UI-test results are intentionally outside this scoped verdict.

@kubaflo
kubaflo merged commit 04f7219 into main Jul 25, 2026
166 of 170 checks passed
@kubaflo
kubaflo deleted the jonathanpeppers-ban-export-attribute branch July 25, 2026 10:30
@github-actions github-actions Bot added this to the .NET 10 SR10 milestone Jul 25, 2026
kubaflo added a commit that referenced this pull request Jul 25, 2026
<!-- Please let the below note in for people that find this PR -->
> [!NOTE]
> Are you waiting for the changes in this PR to be merged?
> It would be very helpful if you could [test the resulting
artifacts](https://github.com/dotnet/maui/wiki/Testing-PR-Builds) from
this PR and let us know in a comment if this change resolves your issue.
Thank you!

### Description of Change

Merges the latest `main` commits into `merge/main-to-net11.0` so the
existing [`main` → `net11.0` merge PR
#36733](#36733) includes changes
landed after its current merge snapshot.

This specifically brings in
[#36773](#36773), which replaces the
Android `[Export]` bridge with the NativeAOT-safe AndroidNative
implementation and resolves the `IL2026` failures seen in #36733's
`maui-pr` run.

After this PR merges, `maui-pr` should be rerun on #36733.

### Issues Fixed

None.

---------
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 25, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — d9c9ddf3 Deployed Jul 24, 2026 by jonathanpeppers via conclusion #940
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-infrastructure CI, Maestro / Coherency, upstream dependencies/versions platform/android

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants