-
Notifications
You must be signed in to change notification settings - Fork 538
/
Copy pathProguard.cs
185 lines (143 loc) · 6.15 KB
/
Proguard.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (C) 2012 Xamarin, Inc. All rights reserved.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Text.RegularExpressions;
using Xamarin.Android.Tools;
using System.IO.Compression;
using Xamarin.Tools.Zip;
namespace Xamarin.Android.Tasks
{
// See AOSP/sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/build/BuildHelper.java
public class Proguard : ToolTask
{
public string ProguardJarPath { get; set; }
public string ProguardToolPath { get; set; }
public string JavaToolPath { get; set; }
[Required]
public string JavaPlatformJarPath { get; set; }
[Required]
public string AndroidSdkDirectory { get; set; }
[Required]
public string ClassesOutputDirectory { get; set; }
[Required]
public string AcwMapFile { get; set; }
[Required]
public string ProguardJarOutput { get; set; }
[Required]
public string ProguardGeneratedReferenceConfiguration { get; set; }
[Required]
public string ProguardGeneratedApplicationConfiguration { get; set; }
[Required]
public string ProguardCommonXamarinConfiguration { get; set; }
public string ProguardConfigurationFiles { get; set; }
public ITaskItem[] JavaLibrariesToEmbed { get; set; }
public ITaskItem[] ExternalJavaLibraries { get; set; }
public ITaskItem[] DoNotPackageJavaLibraries { get; set; }
public bool UseProguard { get; set; }
public string JavaOptions { get; set; }
public string JavaMaximumHeapSize { get; set; }
public bool EnableLogging { get; set; }
public string DumpOutput { get; set; }
public string PrintSeedsOutput { get; set; }
public string PrintUsageOutput { get; set; }
public string PrintMappingOutput { get; set; }
public string ProguardInputJarFilter { get; set; }
protected override string ToolName {
get {
if (UseProguard)
return OS.IsWindows ? "proguard.bat" : "proguard";
return OS.IsWindows ? "java.exe" : "java";
}
}
public override bool Execute ()
{
EnvironmentVariables = MonoAndroidHelper.GetProguardEnvironmentVaribles (ProguardHome);
return base.Execute ();
}
protected override string GenerateCommandLineCommands ()
{
var cmd = new CommandLineBuilder ();
if (!UseProguard) {
// Add the JavaOptions if they are not null
// These could be any of the additional options
if (!string.IsNullOrEmpty (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 )
cmd.AppendSwitchIfNotNull ("-Xmx", JavaMaximumHeapSize);
cmd.AppendSwitchIfNotNull ("-jar ", Path.Combine (ProguardJarPath));
}
if (!ClassesOutputDirectory.EndsWith (Path.DirectorySeparatorChar.ToString (), StringComparison.OrdinalIgnoreCase))
ClassesOutputDirectory += Path.DirectorySeparatorChar;
var classesZip = Path.Combine (ClassesOutputDirectory, "..", "classes.zip");
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
}
var injars = new List<string> ();
var libjars = new List<string> ();
injars.Add (classesZip);
if (JavaLibrariesToEmbed != null)
foreach (var jarfile in JavaLibrariesToEmbed)
injars.Add (jarfile.ItemSpec);
using (var xamcfg = File.Create (ProguardCommonXamarinConfiguration))
GetType ().Assembly.GetManifestResourceStream ("proguard_xamarin.cfg").CopyTo (xamcfg);
var configs = ProguardConfigurationFiles
.Replace ("{sdk.dir}", AndroidSdkDirectory + Path.DirectorySeparatorChar)
.Replace ("{intermediate.common.xamarin}", ProguardCommonXamarinConfiguration)
.Replace ("{intermediate.references}", ProguardGeneratedReferenceConfiguration)
.Replace ("{intermediate.application}", ProguardGeneratedApplicationConfiguration)
.Replace ("{project}", string.Empty) // current directory anyways.
.Split (';')
.Select (s => s.Trim ())
.Where (s => !string.IsNullOrWhiteSpace (s));
var enclosingChar = OS.IsWindows ? "\"" : string.Empty;
foreach (var file in configs) {
if (File.Exists (file))
cmd.AppendSwitchUnquotedIfNotNull ("-include ", $"{enclosingChar}'{file}'{enclosingChar}");
else
Log.LogCodedWarning ("XA4304", file, 0, "Proguard configuration file '{0}' was not found.", file);
}
libjars.Add (JavaPlatformJarPath);
if (ExternalJavaLibraries != null)
foreach (var jarfile in ExternalJavaLibraries.Select (p => p.ItemSpec))
libjars.Add (jarfile);
cmd.AppendSwitchUnquotedIfNotNull ("-injars ", "\"'" + string.Join ($"'{ProguardInputJarFilter}{Path.PathSeparator}'", injars.Distinct ()) + $"'{ProguardInputJarFilter}\"");
cmd.AppendSwitchUnquotedIfNotNull ("-libraryjars ", $"{enclosingChar}'" + string.Join ($"'{Path.PathSeparator}'", libjars.Distinct ()) + $"'{enclosingChar}");
cmd.AppendSwitchIfNotNull ("-outjars ", ProguardJarOutput);
if (EnableLogging) {
cmd.AppendSwitchIfNotNull ("-dump ", DumpOutput);
cmd.AppendSwitchIfNotNull ("-printseeds ", PrintSeedsOutput);
cmd.AppendSwitchIfNotNull ("-printusage ", PrintUsageOutput);
cmd.AppendSwitchIfNotNull ("-printmapping ", PrintMappingOutput);
}
// http://stackoverflow.com/questions/5701126/compile-with-proguard-gives-exception-local-variable-type-mismatch#7587680
cmd.AppendSwitch ("-optimizations !code/allocation/variable");
return cmd.ToString ();
}
protected override string GenerateFullPathToTool ()
{
if (UseProguard)
return Path.Combine (ProguardToolPath, "bin", ToolExe);
return Path.Combine (JavaToolPath, ToolName);
}
string ProguardHome {
get { return UseProguard ? ProguardToolPath : Path.GetDirectoryName (Path.GetDirectoryName (ProguardJarPath)); }
}
}
}