Skip to content

[Microsoft.Android.Build.Tasks] Move R8 remapping task to modern assembly - #12844

Open
simonrozsival wants to merge 1 commit into
simonrozsival-fix-r8-obfuscation-policyfrom
simonrozsival-move-r8-remapping-tasks
Open

simonrozsival wants to merge 1 commit into
simonrozsival-fix-r8-obfuscation-policyfrom
simonrozsival-move-r8-remapping-tasks

Conversation

@simonrozsival

@simonrozsival simonrozsival commented Sep 21, 2026

Copy link
Copy Markdown
Member

Summary

This PR is stacked on and depends on #12692. That parent is built on the focused lower chain #12846, #12847, and #12848.

This final layer is only a task-assembly boundary change for the runtime-R8 remapping producer. It moves GenerateR8JniRemapping and the .NET-only helpers it directly requires into Microsoft.Android.Build.Tasks:

  • JniRemappingAssemblyScanner
  • R8Mapping
  • JniDescriptorText
  • NativeAotJniRetention

The generic runtime table producer remains in Xamarin.Android.Build.Tasks: GenerateJniRemappingNativeCode and JniRemappingNativeCodeGenerator are unchanged and continue to serve both MAM and R8 remapping.

The relocation also:

  • loads only GenerateR8JniRemapping from the modern assembly using the existing Full/Core UsingTask pattern
  • tracks both modern and legacy task assemblies in mixed incremental targets
  • links only the metadata helpers required by the read-only scanner
  • packages ELFSharp.dll beside the modern task assembly under tools/net
  • keeps net10 MSBuildDeviceIntegration compatible by linking R8Mapping.cs instead of referencing the net11 task project
  • preserves a test-only aliased legacy task reference to validate generated XML through MergeRemapXml and GenerateJniRemappingNativeCode

This PR does not restore or relocate the removed managed assembly rewriter, rewrite planners/rebuilders, rewrite-only tests, or XA4325/XA4326 behavior.

Validation

  • Microsoft.Android.Build.Tasks build
  • Xamarin.Android.Build.Tasks build
  • 86 focused modern R8 remapping tests
  • 41 trimmable typemap integration tests
  • MSBuildDeviceIntegration project build
  • verified packaged tools/net/ELFSharp.dll
  • final read-only review found no blocking issues

Copilot AI lite review requested due to automatic review settings September 21, 2026 12:40
@simonrozsival
simonrozsival added this pull request to stack #12845 September 21, 2026 12:41

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.

Copilot review overview

🟡 Changes recommended

A concrete correctness gap in MetadataRawColumns.GetExportedTypeDefinitionId() (missing row-number validation) should be fixed before merging to avoid confusing failures on malformed metadata.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Low severity

Open (1)
What changed in this PR

This PR relocates the R8 mapping + JNI rewrite subsystem (and related tests) into Microsoft.Android.Build.Tasks, wiring the SDK targets to load GenerateR8JniRemapping from the modern task assembly while keeping behavior consistent with the parent PR.

Changes:

  • Switched Microsoft.Android.Sdk.R8JniRemapping.targets to load Microsoft.Android.Tasks.GenerateR8JniRemapping from Microsoft.Android.Build.Tasks.dll using the existing Full/Core UsingTask pattern.
  • Added/ported JNI remapping + assembly-rewrite utilities (including NativeAOT object scanning via ELF) into src/Microsoft.Android.Build.Tasks/Utilities/JniRemapping and updated/added focused unit tests.
  • Updated build/test projects and packaging to reference/copy the modern task assembly dependencies (including ELFSharp.dll).
File Description
tests/​MSBuildDeviceIntegration/​MSBuildDeviceIntegration.csproj Links R8Mapping.cs for net10-compatible device integration build usage.
tests/​Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests/​Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests.csproj Adds a project reference to Microsoft.Android.Build.Tasks for integration coverage.
src/​Xamarin.Android.Build.Tasks/​Microsoft.Android.Sdk/​targets/​Microsoft.Android.Sdk.R8JniRemapping.targets Loads GenerateR8JniRemapping from the modern task assembly and updates task assembly path normalization.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​R8Mapping.cs Minor lookup/nullable adjustments in mapping logic.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​NativeResourceSectionCopier.cs Adds PE Win32 resource-section relocation helper for rebuilt assemblies.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​NativeAotJniRetention.cs Adds NativeAOT ELF-based retention scanning to select required mapping entries.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​MetadataRawColumns.cs Adds raw metadata-table column readers for columns not exposed by MetadataReader.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​MetadataEncoding.cs Adds ECMA-335 compressed integer encode/decode helpers for blob rewriting.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​LdstrRewriter.cs Adds ldstr classification/rewriting for JNI-bearing literal patterns.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​JniRewritePlanner.cs Adds “plan” pass to discover exact metadata/IL changes needed for rewrite.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​JniRewritePlan.cs Adds storage for planned CA/US/FieldRVA replacements keyed by use site.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​JniRewriteException.cs Adds dedicated exception type for rewrite failures/malformed inputs.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​JniDescriptorText.cs Adds JNI descriptor parsing/rewriting helpers (source ↔ token conversions).
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​JniAssemblyRewriter.cs Adds top-level rewrite/scan entrypoints for rebuilding managed assemblies.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​IlOpcodeTable.cs Adds minimal IL operand-size table used by the IL scanner.
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​FieldRvaTable.cs Adds FieldRVA reader/decoder for mapped field data (incl. UTF-8 JNI data).
src/​Microsoft.Android.Build.Tasks/​Utilities/​JniRemapping/​CustomAttributeStringRewriter.cs Adds targeted fixed-string rewriting for custom attribute value blobs.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Utilities/​JniRemapping/​R8MappingTests.cs Adds unit tests validating mapping parsing/lookup behavior.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Utilities/​JniRemapping/​NativeResourceSectionCopierTests.cs Adds unit tests validating resource-section relocation and corruption handling.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Utilities/​JniRemapping/​LdstrRewriterTests.cs Adds unit tests for ldstr rewriting across supported literal forms.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Utilities/​JniRemapping/​JniFixtureBuilder.cs Adds PE fixture builder used by end-to-end rewrite tests.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Utilities/​JniRemapping/​JniDescriptorTextTests.cs Adds unit tests for descriptor parsing/validation/conversion helpers.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Utilities/​JniRemapping/​AssemblyRebuilderTests.cs Adds end-to-end assembly rebuild/edge-case tests (resources, FieldRVA, SN).
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Tasks/​RewriteJniNamesForR8Tests.cs Updates tests to reference Microsoft.Android.Tasks task namespace.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​Tasks/​GenerateR8JniRemappingTests.cs Updates tests for task move and keeps aliased coverage for legacy tasks.
src/​Microsoft.Android.Build.Tasks/​Tests/​Microsoft.Android.Build.Tasks.Tests/​JniAssemblyRewriterTypeMapTests.cs Updates tests to use non-aliased Xamarin.Android.Tasks.JniRemapping types.
src/​Microsoft.Android.Build.Tasks/​Tasks/​RewriteJniNamesForR8.cs Moves task to Microsoft.Android.Tasks namespace and uses invariant formatting for errors.
src/​Microsoft.Android.Build.Tasks/​Tasks/​GenerateR8JniRemapping.cs Moves task to Microsoft.Android.Tasks namespace and normalizes string.Format culture usage.
src/​Microsoft.Android.Build.Tasks/​Microsoft.Android.Build.Tasks.csproj Adds ELFSharp dependency, enables unsafe blocks, and links shared localized resources.
build-tools/​installers/​create-installers.targets Ensures ELFSharp.dll is included in installer MSBuild payload.

Comment on lines 12 to 16
<TargetFramework>$(DotNetTargetFramework)</TargetFramework>
<RootNamespace>Microsoft.Android.Tasks</RootNamespace>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<WarningsAsErrors>Nullable</WarningsAsErrors>
@simonrozsival
simonrozsival force-pushed the simonrozsival-move-r8-remapping-tasks branch from 12ec1e8 to 4eca0b9 Compare September 21, 2026 14:26
@simonrozsival simonrozsival changed the title [Microsoft.Android.Build.Tasks] Move R8 remapping tasks to modern assembly [Microsoft.Android.Build.Tasks] Move R8 remapping task to modern assembly Sep 21, 2026
@simonrozsival
simonrozsival force-pushed the simonrozsival-move-r8-remapping-tasks branch from 4eca0b9 to 9d74331 Compare September 21, 2026 14:29
@simonrozsival
simonrozsival removed this pull request from stack #12845 September 21, 2026 14:31
@simonrozsival
simonrozsival added this pull request to stack #12850 September 21, 2026 14:31
simonrozsival added a commit that referenced this pull request Sep 21, 2026
## Summary

Remove the experimental build-time managed-assembly rewriting approach for R8 so any future assembly-rewriting design can start from a clean foundation.

This cleanup is related to #12535 and the original prototype in #12575.

## What this PR undoes

This removes the managed rewriting implementation developed across the R8 obfuscation stack:

- #12629 added the PE metadata rebuild substrate.
- #12630 added managed JNI metadata rewriting from R8 mappings.
- #12631 added rewriting for generated trimmable type-map assemblies.
- #12632 and #12634 integrated and tested that rewriting approach for CoreCLR and NativeAOT.

Concretely, this PR removes the rewrite task, rewrite-only metadata/IL utilities, dedicated tests and fixtures, and the XA4325/XA4326 resources and documentation. The intent is to abandon this implementation rather than preserve an unused rewriting stack that a future design would need to work around.

## What remains in place

- The generic `R8Mapping` parser introduced in #12628 remains, along with its tests and the `MSBuildDeviceIntegration` consumer. It is independently useful for reading R8 mapping files.
- The ordinary R8 configuration and `private-members` obfuscation/optimization policy from #12668 remain unchanged. This PR does **not** disable R8 or remove private-member obfuscation.
- Existing D8/R8 packaging, ProGuard rule handling, and non-rewriting build behavior remain unchanged.
- Runtime remapping remains active as the stacked follow-up work in #12847, #12848, #12692, and #12844. Those PRs implement the alternative opt-in strategy without managed assembly rewriting and are not part of this cleanup diff.

We may revisit managed assembly rewriting in .NET 12 based on customer feedback and performance data, but with a fresh design rather than this implementation.

## Validation

- Built `Xamarin.Android.Build.Tasks`
- Ran `Microsoft.Android.Build.Tasks.Tests`
- Ran `Microsoft.Android.Sdk.TrimmableTypeMap.IntegrationTests`
- Built `MSBuildDeviceIntegration`
- Ran focused `R8MappingTests`
@simonrozsival
simonrozsival force-pushed the simonrozsival-move-r8-remapping-tasks branch from 9d74331 to 3a222f8 Compare September 21, 2026 20:49
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@simonrozsival
simonrozsival force-pushed the simonrozsival-move-r8-remapping-tasks branch from 3a222f8 to 1fe751e Compare September 22, 2026 13:22

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants