-
Notifications
You must be signed in to change notification settings - Fork 538
/
Copy pathR8.cs
147 lines (134 loc) · 4.88 KB
/
R8.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using System.Collections.Generic;
using System.IO;
using Microsoft.Android.Build.Tasks;
namespace Xamarin.Android.Tasks
{
/// <summary>
/// This task invokes r8 in order to:
/// - Compile to dex format + code shrinking (replacement for proguard)
/// - Enable multi-dex, even if code shrinking is not used
/// </summary>
public class R8 : D8
{
public override string TaskPrefix => "R8S";
[Required]
public string AndroidSdkBuildToolsPath { get; set; }
// multidex
public bool EnableMultiDex { get; set; }
public ITaskItem [] CustomMainDexListFiles { get; set; }
public string MultiDexMainDexListFile { get; set; }
// proguard-like configuration settings
public bool EnableShrinking { get; set; } = true;
public bool IgnoreWarnings { get; set; }
public string AcwMapFile { get; set; }
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";
readonly List<string> tempFiles = new List<string> ();
public override bool RunTask ()
{
try {
return base.RunTask ();
} finally {
foreach (var temp in tempFiles) {
File.Delete (temp);
}
}
}
protected override CommandLineBuilder GetCommandLineBuilder ()
{
var cmd = base.GetCommandLineBuilder ();
if (EnableMultiDex) {
if (MinSdkVersion >= 21) {
if (CustomMainDexListFiles?.Length > 0) {
Log.LogCodedWarning ("XA4306", Properties.Resources.XA4306);
}
} else if (string.IsNullOrEmpty (MultiDexMainDexListFile)) {
Log.LogCodedWarning ("XA4305", Properties.Resources.XA4305);
} else {
var content = new List<string> ();
var temp = Path.GetTempFileName ();
tempFiles.Add (temp);
if (CustomMainDexListFiles != null) {
foreach (var file in CustomMainDexListFiles) {
if (File.Exists (file.ItemSpec)) {
content.Add (File.ReadAllText (file.ItemSpec));
} else {
Log.LogCodedWarning ("XA4309", file.ItemSpec, 0, Properties.Resources.XA4309, file.ItemSpec);
}
}
}
File.WriteAllText (temp, string.Concat (content));
cmd.AppendSwitchIfNotNull ("--main-dex-list ", temp);
cmd.AppendSwitchIfNotNull ("--main-dex-rules ", Path.Combine (AndroidSdkBuildToolsPath, "mainDexClasses.rules"));
cmd.AppendSwitchIfNotNull ("--main-dex-list-output ", MultiDexMainDexListFile);
}
}
if (EnableShrinking) {
if (!string.IsNullOrEmpty (AcwMapFile)) {
var acwLines = File.ReadAllLines (AcwMapFile);
using (var appcfg = File.CreateText (ProguardGeneratedApplicationConfiguration)) {
for (int i = 0; i + 2 < acwLines.Length; i += 3) {
try {
var line = acwLines [i + 2];
var java = line.Substring (line.IndexOf (';') + 1);
appcfg.WriteLine ("-keep class " + java + " { *; }");
} catch {
// skip invalid lines
}
}
}
}
if (!string.IsNullOrWhiteSpace (ProguardCommonXamarinConfiguration)) {
using (var xamcfg = File.CreateText (ProguardCommonXamarinConfiguration)) {
GetType ().Assembly.GetManifestResourceStream ("proguard_xamarin.cfg").CopyTo (xamcfg.BaseStream);
if (IgnoreWarnings) {
xamcfg.WriteLine ("-ignorewarnings");
}
if (!string.IsNullOrEmpty (ProguardMappingFileOutput)) {
xamcfg.WriteLine ("-keepattributes SourceFile");
xamcfg.WriteLine ("-keepattributes LineNumberTable");
xamcfg.WriteLine ($"-printmapping \"{Path.GetFullPath (ProguardMappingFileOutput)}\"");
}
}
}
} else {
//NOTE: we may be calling r8 *only* for multi-dex, and all shrinking is disabled
cmd.AppendSwitch ("--no-tree-shaking");
cmd.AppendSwitch ("--no-minification");
// Rules to turn off optimizations
var temp = Path.GetTempFileName ();
var lines = new List<string> {
"-dontoptimize",
"-dontpreverify",
"-keepattributes **"
};
if (IgnoreWarnings) {
lines.Add ("-ignorewarnings");
}
if (!string.IsNullOrEmpty (ProguardMappingFileOutput)) {
lines.Add ("-keepattributes SourceFile");
lines.Add ("-keepattributes LineNumberTable");
lines.Add ($"-printmapping \"{Path.GetFullPath (ProguardMappingFileOutput)}\"");
}
File.WriteAllLines (temp, lines);
tempFiles.Add (temp);
cmd.AppendSwitchIfNotNull ("--pg-conf ", temp);
}
if (ProguardConfigurationFiles != null) {
foreach (var file in ProguardConfigurationFiles) {
if (File.Exists (file))
cmd.AppendSwitchIfNotNull ("--pg-conf ", file);
else
Log.LogCodedWarning ("XA4304", file, 0, Properties.Resources.XA4304, file);
}
}
return cmd;
}
}
}