From a51adc527f6cfcbd7f50139592fc5a28c420589f Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 1 Sep 2026 23:24:49 +0200 Subject: [PATCH 1/4] [r8-obfuscation] Enable R8 JNI name obfuscation for NativeAOT Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../building-apps/build-properties.md | 4 +- ...id.Sdk.TypeMap.Trimmable.NativeAOT.targets | 69 ++++++++++++++- .../proguard_trimmable_nativeaot.cfg | 2 + .../GenerateNativeAotProguardConfiguration.cs | 85 +++++++++++++++++- .../Tasks/GenerateProguardConfiguration.cs | 19 ++-- .../Tasks/GenerateTrimmableTypeMapTests.cs | 86 +++++++++++++++++-- .../TrimmableTypeMapBuildTests.cs | 25 ++++-- .../Xamarin.Android.Common.targets | 3 +- 8 files changed, 265 insertions(+), 28 deletions(-) diff --git a/Documentation/docs-mobile/building-apps/build-properties.md b/Documentation/docs-mobile/building-apps/build-properties.md index a5fc5014f04..da823acb300 100644 --- a/Documentation/docs-mobile/building-apps/build-properties.md +++ b/Documentation/docs-mobile/building-apps/build-properties.md @@ -472,11 +472,11 @@ This property is `False` by default. A boolean property that enables R8 obfuscation of Java type, method, and field names referenced by managed JNI metadata. The build uses an R8-generated mapping to rewrite -managed assemblies before trimming, then applies the same +managed assemblies before trimming or NativeAOT compilation, then applies the same mapping during the final R8 invocation. This property requires `AndroidLinkTool=r8`, -`AndroidTypeMapImplementation=trimmable`, the CoreCLR runtime, and +`AndroidTypeMapImplementation=trimmable`, and either NativeAOT or CoreCLR with `PublishTrimmed=true`. The default value is `False`. diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.NativeAOT.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.NativeAOT.targets index a485ef744fe..4bce3a2501d 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.NativeAOT.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.TypeMap.Trimmable.NativeAOT.targets @@ -19,7 +19,6 @@ <_AndroidBuildRuntimeIdentifiersInParallel Condition=" ('$(_AndroidTrimmableTypemapTrimJavaCode)' == 'true' or '$(IlcGenerateDgmlFile)' == 'true') and '$(_AndroidBuildRuntimeIdentifiersInParallel)' == '' ">false <_TrimmableRuntimeProviderJavaName Condition=" '$(_TrimmableRuntimeProviderJavaName)' == '' ">net.dot.jni.nativeaot.NativeAotRuntimeProvider - r8 d8 True True @@ -73,6 +72,64 @@ + + + <_AndroidR8JniRewrittenAssemblyDirectory>$(IntermediateOutputPath)r8-jni-rewritten/ + <_AndroidR8JniRewriteStamp>$(_AndroidStampDirectory)_AndroidRewriteJniNamesBeforeIlc.stamp + + + <_AndroidR8JniIlcAssembly Remove="@(_AndroidR8JniIlcAssembly)" /> + <_AndroidR8JniHashedIlcAssembly Remove="@(_AndroidR8JniHashedIlcAssembly)" /> + <_AndroidR8JniRewrittenIlcAssembly Remove="@(_AndroidR8JniRewrittenIlcAssembly)" /> + <_AndroidR8JniExpectedRewriteOutput Remove="@(_AndroidR8JniExpectedRewriteOutput)" /> + <_AndroidR8JniMissingRewriteOutput Remove="@(_AndroidR8JniMissingRewriteOutput)" /> + <_AndroidR8JniIlcAssembly Include="@(ManagedBinary)"> + ManagedBinary + + <_AndroidR8JniIlcAssembly Include="@(IlcReference)"> + IlcReference + + <_AndroidR8JniIlcAssembly Remove="@(_AndroidR8JniIlcAssembly)" Condition=" '%(Extension)' != '.dll' " /> + + + + + + <_AndroidR8JniRewrittenIlcAssembly Include="@(_AndroidR8JniHashedIlcAssembly->'$(_AndroidR8JniRewrittenAssemblyDirectory)%(Hash)/%(Filename)%(Extension)')" /> + <_AndroidR8JniExpectedRewriteOutput Include="@(_AndroidR8JniRewrittenIlcAssembly);$(_AndroidR8JniRewriteManifest)" /> + <_AndroidR8JniMissingRewriteOutput Include="@(_AndroidR8JniExpectedRewriteOutput)" Condition="!Exists('%(Identity)')" /> + + + + + + + + + + + + + + + + + <_AndroidR8JniExpectedRewriteOutput Remove="@(_AndroidR8JniExpectedRewriteOutput)" /> + <_AndroidR8JniMissingRewriteOutput Remove="@(_AndroidR8JniMissingRewriteOutput)" /> + + +