Skip to content

Commit b50becf

Browse files
authored
[Xamarin.Android.Build.Tasks] Make Proguard mapping.txt opt-in (#5318)
Context: #5304 The Proguard mapping.txt PR was not quite ready for prime time and has seemingly caused some new proguard test failures and an .apk size regression in our Xamarin.Forms baseline. Let's disable this by default for now until we can fix these issues.
1 parent 001675f commit b50becf

File tree

6 files changed

+28
-11
lines changed

6 files changed

+28
-11
lines changed

Documentation/release-notes/5304.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,13 @@
66

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

9-
when uploading packages to the Google Play Store.
9+
when uploading packages to the Google Play Store.
10+
11+
In order to opt-in to this behavior, set the `$(AndroidProguardMappingFile)`
12+
property in your project file to the path of the file you want generated:
13+
14+
```xml
15+
<PropertyGroup>
16+
<AndroidProguardMappingFile>$(OutputPath)\mapping.txt</AndroidProguardMappingFile>
17+
</PropertyGroup>
18+
```

src/Xamarin.Android.Build.Tasks/Resources/proguard_xamarin.cfg

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
-dontobfuscate
44

5-
# required for publishing proguard mapping file
6-
-keepattributes SourceFile
7-
-keepattributes LineNumberTable
8-
95
-keep class android.support.multidex.MultiDexApplication { <init>(); }
106
-keep class com.xamarin.java_interop.** { *; <init>(); }
117
-keep class mono.MonoRuntimeProvider* { *; <init>(...); }

src/Xamarin.Android.Build.Tasks/Tasks/Proguard.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,16 @@ protected override string GenerateCommandLineCommands ()
118118
// skip invalid lines
119119
}
120120

121-
if (!string.IsNullOrWhiteSpace (ProguardCommonXamarinConfiguration))
121+
if (!string.IsNullOrWhiteSpace (ProguardCommonXamarinConfiguration)) {
122122
using (var xamcfg = File.CreateText (ProguardCommonXamarinConfiguration)) {
123123
GetType ().Assembly.GetManifestResourceStream ("proguard_xamarin.cfg").CopyTo (xamcfg.BaseStream);
124-
if (!string.IsNullOrEmpty (ProguardMappingFileOutput))
124+
if (!string.IsNullOrEmpty (ProguardMappingFileOutput)) {
125+
xamcfg.WriteLine ("-keepattributes SourceFile");
126+
xamcfg.WriteLine ("-keepattributes LineNumberTable");
125127
xamcfg.WriteLine ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
128+
}
126129
}
130+
}
127131

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

src/Xamarin.Android.Build.Tasks/Tasks/R8.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@ protected override CommandLineBuilder GetCommandLineBuilder ()
100100
if (IgnoreWarnings) {
101101
xamcfg.WriteLine ("-ignorewarnings");
102102
}
103-
if (!string.IsNullOrEmpty (ProguardMappingFileOutput))
103+
if (!string.IsNullOrEmpty (ProguardMappingFileOutput)) {
104+
xamcfg.WriteLine ("-keepattributes SourceFile");
105+
xamcfg.WriteLine ("-keepattributes LineNumberTable");
104106
xamcfg.WriteLine ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
107+
}
105108
}
106109
}
107110
} else {
@@ -118,8 +121,11 @@ protected override CommandLineBuilder GetCommandLineBuilder ()
118121
if (IgnoreWarnings) {
119122
lines.Add ("-ignorewarnings");
120123
}
121-
if (!string.IsNullOrEmpty (ProguardMappingFileOutput))
124+
if (!string.IsNullOrEmpty (ProguardMappingFileOutput)) {
125+
lines.Add ("-keepattributes SourceFile");
126+
lines.Add ("-keepattributes LineNumberTable");
122127
lines.Add ($"-printmapping {Path.GetFullPath (ProguardMappingFileOutput)}");
128+
}
123129
File.WriteAllLines (temp, lines);
124130
tempFiles.Add (temp);
125131
cmd.AppendSwitchIfNotNull ("--pg-conf ", temp);

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/PackagingTest.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ public void CheckProguardMappingFileExists ()
5656
};
5757
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidDexTool, "d8");
5858
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidLinkTool, "r8");
59+
// Projects must provide a $(AndroidProguardMappingFile) value to opt in
60+
proj.SetProperty (proj.ReleaseProperties, "AndroidProguardMappingFile", @"$(OutputPath)\mapping.txt");
61+
5962
using (var b = CreateApkBuilder ()) {
60-
Assert.IsTrue (b.Build (proj), "build should have succeeded.");
6163
string mappingFile = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, "mapping.txt");
64+
Assert.IsTrue (b.Build (proj), "build should have succeeded.");
6265
FileAssert.Exists (mappingFile, $"'{mappingFile}' should have been generated.");
6366
}
6467
}

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,6 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
275275
<AndroidLinkTool Condition=" '$(AndroidLinkTool)' == '' And '$(AndroidEnableProguard)' == 'True' ">proguard</AndroidLinkTool>
276276
<AndroidLinkTool Condition=" '$(AndroidLinkTool)' == 'proguard' And '$(AndroidEnableDesugar)' == 'True' ">r8</AndroidLinkTool>
277277
<AndroidEnableProguard Condition=" '$(AndroidLinkTool)' != '' ">True</AndroidEnableProguard>
278-
<AndroidProguardMappingFile Condition=" '$(AndroidProguardMappingFile)' == '' ">$(OutputPath)mapping.txt</AndroidProguardMappingFile>
279278
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' And ('$(AndroidDexTool)' == 'd8' Or '$(AndroidLinkTool)' == 'r8') ">True</AndroidEnableDesugar>
280279
<AndroidEnableDesugar Condition=" '$(AndroidEnableDesugar)' == '' ">False</AndroidEnableDesugar>
281280
<AndroidR8IgnoreWarnings Condition=" '$(AndroidR8IgnoreWarnings)' == '' ">True</AndroidR8IgnoreWarnings>

0 commit comments

Comments
 (0)