Repository navigation
[release/11.0.1xx] [R8] Preserve bound Java fields in generated keep rules - #13239
Conversation
Generate field keep rules from Register attributes on managed properties and fields so R8 cannot remove or change fields accessed through JNI. Add the Uno ACTION_COLLAPSE build regression for both obfuscation modes and check the field name and type with dexdump. Fixes #13159 Co-authored-by: Copilot App <[email protected]> Copilot-Session: e1ff07de-4ade-4aa2-8af0-dc4f4ded065a
There was a problem hiding this comment.
🔵 Needs a closer look
The reported instance-field type-narrowing scenario remains unverified by the regression coverage and needs human validation.
1 open issue
What changed in this PR
This servicing fix addresses #13159 by extending generated R8 keep rules to protect Java fields accessed through managed bindings, without broadly disabling class optimization.
Changes:
- Generate field keep rules from
[Register]attributes on managed properties and fields. - Add an AndroidX regression test for both obfuscation modes, plus DEX field lookup support and unit coverage.
- Document preservation of bound Java field names and types.
| File | Description |
|---|---|
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.ProjectTools/Utilities/DexUtils.cs | Share member lookup logic and support field checks. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs | Verify AndroidX field preservation after R8 processing. |
| src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/DexUtilsTests.cs | Test exact field matching by class, name, and type. |
| src/Xamarin.Android.Build.Tasks/Tasks/GenerateProguardConfiguration.cs | Emit keep rules for registered properties and fields. |
| Documentation/docs-mobile/building-apps/build-properties.md | Explain bound-field preservation guarantees. |
Review effort: Balanced
Bind a Java instance int field whose values are limited to 0, 1, and 2. Keep a Java reader live so the field remains present even without generated field rules, then assert the original I descriptor with dexdump. Verified that omitting field keep rules narrows the field to B and fails the test. With the fix restored, all three static and instance field regression cases pass. Co-authored-by: Copilot App <[email protected]> Copilot-Session: e1ff07de-4ade-4aa2-8af0-dc4f4ded065a
|
@dalexsoto review |
1 similar comment
|
@dalexsoto review |
dalexsoto
left a comment
There was a problem hiding this comment.
The servicing field rules preserve bound Java field names and types, and exact DEX assertions address both field removal and retained-int narrowing without requiring broader main-branch typemap changes.
Source-only review of the full current PR and inspected static integration; no runtime or installed-product execution is claimed.
Add the test coverage from #13239 to main. These tests protect managed JNI field access from R8 field removal, renaming, and type narrowing. Related issue: #13159. The static-field test builds a trimmed CoreCLR Release app with Xamarin.AndroidX.Core 1.19.0.1 and checks `ACTION_COLLAPSE` in DEX output with obfuscation disabled and with `private-members`. The instance-field test binds a Java `int` field with values 0, 1, and 2, keeps its Java reader live, and checks that R8 preserves its `I` descriptor instead of narrowing it to `B`. Add exact field class/name/type matching to the shared DEX test helper and unit coverage for that matching. Main already preserves these fields through its typemap member rules, so this PR contains no production or documentation changes and does not assert the release-specific generated rule. Co-authored-by: Copilot App <[email protected]>

Fixes #13159.
.NET 11 RC2 can remove Java fields that managed code accesses through bindings when
AndroidLinkTool=r8andAndroidR8ObfuscationMode=private-members. Uno'sAccessibilityActionCompat.ActionCollapse?.Id ?? 0reproduces the problem: the Java class remains, but itsACTION_COLLAPSEfield is removed.GenerateProguardConfigurationpreviously generated member keep rules only from registered methods. Generate field keep rules from[Register]attributes on managed properties and fields as well. These rules preserve the Java field name and type without disabling optimization for the rest of the class.This is one commit on
release/11.0.1xx. It does not include the larger retained-typemap changes frommain.Add a CoreCLR Release build test using
Xamarin.AndroidX.Coreand Uno's field-access pattern. Check the generated rule and use the existingdexdumputility to assert the field's declaring class, name, and type for bothprivate-membersanddisabled. Add unit coverage for the DEX field lookup.Validation on Windows, with
MSBUILDDISABLENODEREUSE=1,DOTNET_ROLL_FORWARD=Major, andTEST_ANDROID_JDK_PATH=D:\android-toolchain\jdk-25:.\dotnet-local.cmd build Microsoft.Android.slnx -c Debug -nodeReuse:false -v:quiet— passed..\dotnet-local.cmd build Microsoft.Android.slnx -t:PackDotNet -c Debug -nodeReuse:false -v:minimal— passed..\dotnet-local.cmd build src\Xamarin.Android.Build.Tasks\Tests\Xamarin.Android.Build.Tests\Xamarin.Android.Build.Tests.csproj -c Debug -nodeReuse:false -v:quiet— passed..\dotnet-local.cmd test bin\TestDebug\net10.0\Xamarin.Android.Build.Tests.dll --filter Name~R8PreservesAccessibilityActionField— 2 passed..\dotnet-local.cmd test bin\TestDebug\net10.0\Xamarin.Android.Build.Tests.dll --filter FullyQualifiedName~DexUtilsTests— 14 passed..\dotnet-local.cmd test bin\TestDebug\net10.0\Xamarin.Android.Build.Tests.dll --filter FullyQualifiedName~R8Tests— 26 passed.Pull Request
title and
description
should follow the
commit-messages.mdworkflow documentation, and in particular should include: