Repository navigation
[Android] ban [Export] in the banned API analyzers - #36773
Conversation
`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
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 36773Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 36773" |
|
Azure Pipelines: Successfully started running 1 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
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.ExportAttributetoeng/BannedSymbols.txtso[Export]usage is enforced as RS0030 with guidance to the AndroidNative abstract-class pattern. - Replace the last
[Export]usage inRefreshViewWebViewScrollCaptureby switching to a boundRefreshViewJavaScriptInterfacebase class and overriding the method. - Add
RefreshViewJavaScriptInterface.javatosrc/Core/AndroidNativeso 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. |
|
/azp run maui-pr-uitests, maui-pr-devicetests |
|
Azure Pipelines: Successfully started running 2 pipeline(s). |
|
Scoped The implementation replaces
|
<!-- 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. ---------
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.ExportAttributeis annotated with[RequiresUnreferencedCode], so every use of[Export]produces an IL2026 trim warning and depends onJava.Interop.Export.dllreflection at runtime. #24744 established the alternative: declare an abstract class insrc/Core/AndroidNativeandoverrideit, which lets the Java Callable Wrapper generator emit the method — and its@android.webkit.JavascriptInterfaceannotation — statically.This adds
T:Java.Interop.ExportAttributetoeng/BannedSymbols.txtso the pattern is enforced by RS0030 going forward, instead of relying on reviewers to catch it. The message points atHybridJavaScriptInterface.javaas the working example:Adding the ban flagged the one remaining offender,
RefreshViewWebViewScrollCapture.ScrollCaptureState.SetCanScrollUp():So it is fixed the same way #24744 did: a new
RefreshViewJavaScriptInterfaceabstract class in themaui.aarJava sources, withScrollCaptureStatederiving from it. The[RequiresUnreferencedCode("Java.Interop.Export uses dynamic features.")]suppression onSetCanScrollUp()is no longer needed and is removed.Note the base type is deliberately an abstract class and not an interface. #24952 reverted
HybridJavaScriptInterfacefrom an interface back to a class because Java does not inherit method annotations from interfaces — the generated JCW silently lost@android.webkit.JavascriptInterfaceand the JavaScript bridge stopped working. The same applies here.The remaining
[Export]usages in the repo are allFoundation.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.csprojfornet10.0-android36.0:error RS0030atRefreshViewWebViewScrollCapture.cs(209,4), confirming the new banned-symbol entry is actually enforced.Microsoft.Maui.dllbuilds clean, including the Gradle step that compiles the new Java file intomaui.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 shippingHybridWebViewHandlerpattern.