Skip to content

[Xamarin.Android.Build.Tasks] Add support for Proguard mapping.txt file #5304

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Documentation/guides/building-apps/build-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,17 @@ with build environments that have FIPS compliance enforced.

Added in Xamarin.Android 10.1.

## AndroidProguardMappingFile

Specifies the `-printmapping` proguard rule for `r8`. This will
mean the `mapping.txt` file will be produced in the `$(OutputPath)`
folder. This file can then be used when uploading packages to the
Google Play Store.

Default value is `$(OutputPath)mapping.txt`

Added in Xamarin.Android 11.2

## AndroidR8IgnoreWarnings

Specifies
Expand Down
9 changes: 9 additions & 0 deletions Documentation/release-notes/5304.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Build and deployment performance

* [GitHub PR 5304](https://github.com/xamarin/xamarin-android/pull/5304):
Add support for producing a proguard `mapping.txt` file to the
build system. This file can be used by users to remove this warning

"This App Bundle contains Java/Kotlin code, which might be obfuscated."

when uploading packages to the Google Play Store.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

-dontobfuscate

# required for publishing proguard mapping file
-keepattributes SourceFile
-keepattributes LineNumberTable

-keep class android.support.multidex.MultiDexApplication { <init>(); }
-keep class com.xamarin.java_interop.** { *; <init>(); }
-keep class mono.MonoRuntimeProvider* { *; <init>(...); }
Expand Down
18 changes: 11 additions & 7 deletions src/Xamarin.Android.Build.Tasks/Tasks/Proguard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ public class Proguard : AndroidToolTask
public string ProguardGeneratedReferenceConfiguration { get; set; }
public string ProguardGeneratedApplicationConfiguration { get; set; }
public string ProguardCommonXamarinConfiguration { get; set; }
public string ProguardMappingFileOutput { get; set; }

[Required]
public string [] ProguardConfigurationFiles { get; set; }

public ITaskItem[] JavaLibrariesToEmbed { get; set; }

public ITaskItem[] JavaLibrariesToReference { get; set; }

public bool UseProguard { get; set; }

public string JavaOptions { get; set; }
Expand Down Expand Up @@ -92,11 +93,11 @@ protected override string GenerateCommandLineCommands ()
// Add the JavaOptions if they are not null
// These could be any of the additional options
if (!string.IsNullOrEmpty (JavaOptions)) {
cmd.AppendSwitch (JavaOptions);
cmd.AppendSwitch (JavaOptions);
}

// Add the specific -XmxN to override the default heap size for the JVM
// N can be in the form of Nm or NGB (e.g 100m or 1GB )
// N can be in the form of Nm or NGB (e.g 100m or 1GB )
cmd.AppendSwitchIfNotNull ("-Xmx", JavaMaximumHeapSize);

cmd.AppendSwitchIfNotNull ("-jar ", Path.Combine (ProguardJarPath));
Expand All @@ -118,9 +119,12 @@ protected override string GenerateCommandLineCommands ()
}

if (!string.IsNullOrWhiteSpace (ProguardCommonXamarinConfiguration))
using (var xamcfg = File.Create (ProguardCommonXamarinConfiguration))
GetType ().Assembly.GetManifestResourceStream ("proguard_xamarin.cfg").CopyTo (xamcfg);

using (var xamcfg = File.CreateText (ProguardCommonXamarinConfiguration)) {
GetType ().Assembly.GetManifestResourceStream ("proguard_xamarin.cfg").CopyTo (xamcfg.BaseStream);
if (!string.IsNullOrEmpty (ProguardMappingFileOutput))
xamcfg.WriteLine ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
}

var enclosingChar = OS.IsWindows ? "\"" : string.Empty;

foreach (var file in ProguardConfigurationFiles) {
Expand Down
7 changes: 6 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/R8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class R8 : D8
public string ProguardGeneratedReferenceConfiguration { get; set; }
public string ProguardGeneratedApplicationConfiguration { get; set; }
public string ProguardCommonXamarinConfiguration { get; set; }
public string ProguardMappingFileOutput { get; set; }
public string [] ProguardConfigurationFiles { get; set; }

protected override string MainClass => "com.android.tools.r8.R8";
Expand Down Expand Up @@ -99,6 +100,8 @@ protected override CommandLineBuilder GetCommandLineBuilder ()
if (IgnoreWarnings) {
xamcfg.WriteLine ("-ignorewarnings");
}
if (!string.IsNullOrEmpty (ProguardMappingFileOutput))
xamcfg.WriteLine ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
}
}
} else {
Expand All @@ -115,6 +118,8 @@ protected override CommandLineBuilder GetCommandLineBuilder ()
if (IgnoreWarnings) {
lines.Add ("-ignorewarnings");
}
if (!string.IsNullOrEmpty (ProguardMappingFileOutput))
lines.Add ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
File.WriteAllLines (temp, lines);
tempFiles.Add (temp);
cmd.AppendSwitchIfNotNull ("--pg-conf ", temp);
Expand All @@ -131,5 +136,5 @@ protected override CommandLineBuilder GetCommandLineBuilder ()
return cmd;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ public void CheckManagedSymbolsArchive (bool isRelease, bool monoSymbolArchive,
}
}

[Test]
public void CheckProguardMappingFileExists ()
{
var proj = new XamarinAndroidApplicationProject {
IsRelease = true,
};
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidDexTool, "d8");
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidLinkTool, "r8");
using (var b = CreateApkBuilder ()) {
Assert.IsTrue (b.Build (proj), "build should have succeeded.");
string mappingFile = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, "mapping.txt");
FileAssert.Exists (mappingFile, $"'{mappingFile}' should have been generated.");
}
}

[Test]
public void CheckIncludedAssemblies ()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<AndroidLinkTool Condition=" '$(AndroidLinkTool)' == '' And '$(AndroidEnableProguard)' == 'True' ">proguard</AndroidLinkTool>
<AndroidLinkTool Condition=" '$(AndroidLinkTool)' == 'proguard' And '$(AndroidEnableDesugar)' == 'True' ">r8</AndroidLinkTool>
<AndroidEnableProguard Condition=" '$(AndroidLinkTool)' != '' ">True</AndroidEnableProguard>
<AndroidProguardMappingFile Condition=" '$(AndroidProguardMappingFile)' == '' ">$(OutputPath)mapping.txt</AndroidProguardMappingFile>
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' And ('$(AndroidDexTool)' == 'd8' Or '$(AndroidLinkTool)' == 'r8') ">True</AndroidEnableDesugar>
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' ">False</AndroidEnableDesugar>
<AndroidR8IgnoreWarnings Condition=" '$(AndroidR8IgnoreWarnings)' == '' ">True</AndroidR8IgnoreWarnings>
Expand Down
2 changes: 2 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.D8.targets
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
ProguardCommonXamarinConfiguration="$(IntermediateOutputPath)proguard\proguard_xamarin.cfg"
ProguardGeneratedReferenceConfiguration="$(_ProguardProjectConfiguration)"
ProguardGeneratedApplicationConfiguration="$(IntermediateOutputPath)proguard\proguard_project_primary.cfg"
ProguardMappingFileOutput="$(AndroidProguardMappingFile)"
ProguardConfigurationFiles="@(_ProguardConfiguration)"
EnableShrinking="$(_R8EnableShrinking)"
EnableMultiDex="$(AndroidEnableMultiDex)"
Expand Down Expand Up @@ -96,6 +97,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
<Touch Files="$(_AndroidStampDirectory)_CompileToDalvik.stamp" AlwaysCreate="true" />
<ItemGroup>
<FileWrites Include="$(_AndroidIntermediateDexOutputDirectory)*.dex" />
<FileWrites Include="$(AndroidProguardMappingFile)" />
</ItemGroup>

</Target>
Expand Down
6 changes: 4 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Xamarin.Android.DX.targets
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
<ItemGroup>
<_JarsToProguard Include="@(_JavaLibrariesToCompile)" />
</ItemGroup>

<MakeDir Directories="$(IntermediateOutputPath)proguard" />

<Proguard
Expand All @@ -42,6 +42,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
ProguardCommonXamarinConfiguration="$(IntermediateOutputPath)proguard\proguard_xamarin.cfg"
ProguardGeneratedReferenceConfiguration="$(_ProguardProjectConfiguration)"
ProguardGeneratedApplicationConfiguration="$(IntermediateOutputPath)proguard\proguard_project_primary.cfg"
ProguardMappingFileOutput="$(AndroidProguardMappingFile)"
ProguardConfigurationFiles="@(_ProguardConfiguration)"
JavaLibrariesToEmbed="@(_JarsToProguard);@(_InstantRunJavaReference)"
JavaLibrariesToReference="@(AndroidExternalJavaLibrary)"
Expand Down Expand Up @@ -79,7 +80,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
<Delete Files="@(_DexesToDelete)" />

<!-- Compile java code to dalvik -->
<CompileToDalvik
<CompileToDalvik
DxJarPath="$(DxJarPath)"
DxExtraArguments="$(DxExtraArguments)"
JavaToolPath="$(JavaToolPath)"
Expand All @@ -98,6 +99,7 @@ Copyright (C) 2018 Xamarin. All rights reserved.
<Touch Files="$(_AndroidStampDirectory)_CompileToDalvik.stamp" AlwaysCreate="true" />
<ItemGroup>
<FileWrites Include="$(_AndroidIntermediateDexOutputDirectory)*.dex" />
<FileWrites Include="$(AndroidProguardMappingFile)" />
</ItemGroup>

</Target>
Expand Down