Commit b4f3cd0
[r8] Include build metadata in app bundles (#12646)
## Summary
- pass `--build-metadata-output` to R8
- include the generated metadata at `BUNDLE-METADATA/com.android.tools/r8.json` in app bundles
- verify both R8 metadata files are packaged and incremental builds remain up to date
This metadata lets Google Play report R8 optimization, shrinking, and obfuscation coverage for the upcoming app-quality requirements announced in [App quality: Memory optimization and secure onboarding](https://android-developers.googleblog.com/2026/08/app-quality-memory-optimization-secure-onboarding.html).
Related to #12639.
Related to #12535.
## What .NET 10 customers can try now
Save the following as `Directory.Build.targets` beside the project. It enables R8 for Release builds, downloads R8 9.0.32, generates `r8.json`, and includes it in the app bundle.
The response file is required for this workaround because the shipping .NET 10 task passes `$(AndroidR8ExtraArguments)` directly on the Java command line, with platform-dependent handling of multiple arguments.
```xml
<Project>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<AndroidLinkTool>r8</AndroidLinkTool>
</PropertyGroup>
<Target
Name="IncludeR8BuildMetadata"
BeforeTargets="_CompileToDalvik"
Condition=" '$(AndroidLinkTool)' == 'r8' ">
<PropertyGroup>
<_R8BuildMetadataFile>$(IntermediateOutputPath)r8.json</_R8BuildMetadataFile>
<_R8JarFile>$([System.IO.Path]::GetFullPath('$(IntermediateOutputPath)r8-9.0.32.jar'))</_R8JarFile>
<_R8MetadataRsp>$(IntermediateOutputPath)r8-build-metadata.rsp</_R8MetadataRsp>
<AndroidR8JarPath>$(_R8JarFile)</AndroidR8JarPath>
<AndroidR8ExtraArguments>@$(_R8MetadataRsp)</AndroidR8ExtraArguments>
</PropertyGroup>
<DownloadFile
Condition=" !Exists('$(_R8JarFile)') "
SourceUrl="https://storage.googleapis.com/r8-releases/raw/9.0.32/r8.jar"
DestinationFolder="$(IntermediateOutputPath)"
DestinationFileName="r8-9.0.32.jar"
/>
<WriteLinesToFile
File="$(_R8MetadataRsp)"
Overwrite="true"
Lines="--build-metadata-output;$(_R8BuildMetadataFile)"
/>
<ItemGroup>
<AndroidAppBundleMetaDataFile Include="com.android.tools/r8.json:$(_R8BuildMetadataFile)" />
</ItemGroup>
</Target>
</Project>
```
Build the app bundle with:
```console
dotnet build -c Release -p:AndroidPackageFormat=aab
```
The resulting signed AAB should contain `BUNDLE-METADATA/com.android.tools/r8.json`. This workaround was verified on macOS by @yuseok-kim-edushare and end-to-end on Windows with the .NET 10 Android workload: the metadata reports R8 9.0.32 and its DEX SHA-256 matches the packaged `classes.dex`.
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>1 parent 876c57e commit b4f3cd0
4 files changed
Lines changed: 26 additions & 1 deletion
File tree
- src/Xamarin.Android.Build.Tasks
- Tasks
- Tests/Xamarin.Android.Build.Tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
122 | 123 | | |
123 | 124 | | |
124 | 125 | | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
125 | 131 | | |
126 | 132 | | |
127 | 133 | | |
| |||
Lines changed: 13 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
39 | 51 | | |
40 | 52 | | |
41 | 53 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
230 | 230 | | |
231 | 231 | | |
232 | 232 | | |
| 233 | + | |
233 | 234 | | |
234 | 235 | | |
235 | 236 | | |
| |||
2167 | 2168 | | |
2168 | 2169 | | |
2169 | 2170 | | |
| 2171 | + | |
| 2172 | + | |
| 2173 | + | |
| 2174 | + | |
2170 | 2175 | | |
2171 | 2176 | | |
2172 | 2177 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| 81 | + | |
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| |||
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
| 119 | + | |
118 | 120 | | |
119 | 121 | | |
120 | 122 | | |
| |||
0 commit comments