Skip to content

Commit d1bc533

Browse files
jonathanpeppersjonpryor
authored andcommitted
[Xamarin.Android.Build.Tasks] ProGuard/MultiDex fix parentheses in path (#1137)
Fixes: #1135 Context: commit 2c2d47c When making changes in 2c2d47c, I added a unit test that builds an application with both spaces and parentheses in its path. However, this was not enough to test everything; the test also needed an `@(AndroidJavaLibrary)` build item with spaces and parentheses in its name! Changes: - Made a `Hello (World).jar` file, that has a single class in it - Added the jar file as a `AndroidJavaLibrary`, in the `BuildApplicationWithSpacesInPath` test - Reverted `Proguard.cs` and `CreateMultiDexMainDexClassList.cs` from what I did in 2c2d47c -- Dean tried to warn me - Move the `(!META-INF/MANIFEST.MF)` filter expression outside the single quotes - For the `-injars` expression to work on *both* macOS and Windows, I had to enclose the entire expression in double-quotes 2c2d47c was a good attempt at trying to cleanup the code, but I could not get the manifest filter to work along with a path including a parentheses.
1 parent e790874 commit d1bc533

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,28 +75,31 @@ protected override string GenerateCommandLineCommands ()
7575

7676
void GenerateProguardCommands (CommandLineBuilder cmd)
7777
{
78-
var enclosingChar = OS.IsWindows ? "\"" : "'";
78+
var enclosingChar = OS.IsWindows ? "\"" : string.Empty;
7979
var jars = JavaLibraries.Select (i => i.ItemSpec).Concat (new string [] { Path.Combine (ClassesOutputDirectory, "classes.zip") });
8080
cmd.AppendSwitchIfNotNull ("-jar ", ProguardJarPath);
81-
cmd.AppendSwitchUnquotedIfNotNull ("-injars ", $"{enclosingChar}" + string.Join ($"{ProguardInputJarFilter}{enclosingChar}{Path.PathSeparator}{enclosingChar}", jars) + $"{ProguardInputJarFilter}{enclosingChar}");
81+
cmd.AppendSwitchUnquotedIfNotNull ("-injars ", "\"'" + string.Join ($"'{ProguardInputJarFilter}{Path.PathSeparator}'", jars) + $"'{ProguardInputJarFilter}\"");
8282
cmd.AppendSwitch ("-dontwarn");
8383
cmd.AppendSwitch ("-forceprocessing");
8484
cmd.AppendSwitchIfNotNull ("-outjars ", tempJar);
85-
cmd.AppendSwitchIfNotNull ("-libraryjars ", $"{enclosingChar}{Path.Combine (AndroidSdkBuildToolsPath, "lib", "shrinkedAndroid.jar")}{enclosingChar}");
85+
cmd.AppendSwitchIfNotNull ("-libraryjars ", $"'{Path.Combine (AndroidSdkBuildToolsPath, "lib", "shrinkedAndroid.jar")}'");
8686
cmd.AppendSwitch ("-dontoptimize");
8787
cmd.AppendSwitch ("-dontobfuscate");
8888
cmd.AppendSwitch ("-dontpreverify");
89-
cmd.AppendSwitchUnquotedIfNotNull ("-include ", $"{enclosingChar}{Path.Combine (AndroidSdkBuildToolsPath, "mainDexClasses.rules")}{enclosingChar}");
89+
cmd.AppendSwitchUnquotedIfNotNull ("-include ", $"{enclosingChar}'{Path.Combine (AndroidSdkBuildToolsPath, "mainDexClasses.rules")}'{enclosingChar}");
9090
}
9191

9292
void GenerateMainDexListBuilderCommands(CommandLineBuilder cmd)
9393
{
94-
var enclosingChar = OS.IsWindows ? "\"" : "'";
94+
var enclosingDoubleQuote = OS.IsWindows ? "\"" : string.Empty;
95+
var enclosingQuote = OS.IsWindows ? string.Empty : "'";
9596
var jars = JavaLibraries.Select (i => i.ItemSpec).Concat (new string [] { Path.Combine (ClassesOutputDirectory, "classes.zip") });
9697
cmd.AppendSwitchIfNotNull ("-Djava.ext.dirs=", Path.Combine (AndroidSdkBuildToolsPath, "lib"));
9798
cmd.AppendSwitch ("com.android.multidex.MainDexListBuilder");
98-
cmd.AppendSwitchUnquotedIfNotNull ("", $"{enclosingChar}{tempJar}{enclosingChar}");
99-
cmd.AppendSwitchUnquotedIfNotNull ("", enclosingChar + string.Join ($"{enclosingChar}{Path.PathSeparator}{enclosingChar}", jars) + enclosingChar);
99+
cmd.AppendSwitch ($"{enclosingDoubleQuote}{tempJar}{enclosingDoubleQuote}");
100+
cmd.AppendSwitchUnquotedIfNotNull ("", $"{enclosingDoubleQuote}{enclosingQuote}" +
101+
string.Join ($"{enclosingQuote}{Path.PathSeparator}{enclosingQuote}", jars) +
102+
$"{enclosingQuote}{enclosingDoubleQuote}");
100103
writeOutputToKeepFile = true;
101104
}
102105

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ protected override string GenerateCommandLineCommands ()
140140

141141
var injars = new List<string> ();
142142
var libjars = new List<string> ();
143-
injars.Add (classesZip + ProguardInputJarFilter);
143+
injars.Add (classesZip);
144144
if (JavaLibrariesToEmbed != null)
145145
foreach (var jarfile in JavaLibrariesToEmbed)
146-
injars.Add (jarfile.ItemSpec + ProguardInputJarFilter);
146+
injars.Add (jarfile.ItemSpec);
147147

148148
using (var xamcfg = File.Create (ProguardCommonXamarinConfiguration))
149149
GetType ().Assembly.GetManifestResourceStream ("proguard_xamarin.cfg").CopyTo (xamcfg);
@@ -158,11 +158,11 @@ protected override string GenerateCommandLineCommands ()
158158
.Select (s => s.Trim ())
159159
.Where (s => !string.IsNullOrWhiteSpace (s));
160160

161-
var enclosingChar = OS.IsWindows ? "\"" : "'";
161+
var enclosingChar = OS.IsWindows ? "\"" : string.Empty;
162162

163163
foreach (var file in configs) {
164164
if (File.Exists (file))
165-
cmd.AppendSwitchUnquotedIfNotNull ("-include ", $"{enclosingChar}{file}{enclosingChar}");
165+
cmd.AppendSwitchUnquotedIfNotNull ("-include ", $"{enclosingChar}'{file}'{enclosingChar}");
166166
else
167167
Log.LogWarning ("Proguard configuration file '{0}' was not found.", file);
168168
}
@@ -172,9 +172,8 @@ protected override string GenerateCommandLineCommands ()
172172
foreach (var jarfile in ExternalJavaLibraries.Select (p => p.ItemSpec))
173173
libjars.Add (jarfile);
174174

175-
string delimiter = $"{enclosingChar}{Path.PathSeparator}{enclosingChar}";
176-
cmd.AppendSwitchUnquotedIfNotNull ("-injars ", enclosingChar + string.Join (delimiter, injars.Distinct ()) + enclosingChar);
177-
cmd.AppendSwitchUnquotedIfNotNull ("-libraryjars ", enclosingChar + string.Join (delimiter, libjars.Distinct ()) + enclosingChar);
175+
cmd.AppendSwitchUnquotedIfNotNull ("-injars ", "\"'" + string.Join ($"'{ProguardInputJarFilter}{Path.PathSeparator}'", injars.Distinct ()) + $"'{ProguardInputJarFilter}\"");
176+
cmd.AppendSwitchUnquotedIfNotNull ("-libraryjars ", $"{enclosingChar}'" + string.Join ($"'{Path.PathSeparator}'", libjars.Distinct ()) + $"'{enclosingChar}");
178177
cmd.AppendSwitchIfNotNull ("-outjars ", ProguardJarOutput);
179178

180179
if (EnableLogging) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,19 @@ public void BuildApplicationWithSpacesInPath ([Values (true, false)] bool isRele
16991699
AotAssemblies = isRelease,
17001700
EnableProguard = enableProguard,
17011701
};
1702+
proj.OtherBuildItems.Add (new BuildItem ("AndroidJavaLibrary", "Hello (World).jar") { BinaryContent = () => Convert.FromBase64String (@"
1703+
UEsDBBQACAgIAMl8lUsAAAAAAAAAAAAAAAAJAAQATUVUQS1JTkYv/soAAAMAUEsHCAAAAAACAAAAA
1704+
AAAAFBLAwQUAAgICADJfJVLAAAAAAAAAAAAAAAAFAAAAE1FVEEtSU5GL01BTklGRVNULk1G803My0
1705+
xLLS7RDUstKs7Mz7NSMNQz4OVyLkpNLElN0XWqBAlY6BnEG5oaKWj4FyUm56QqOOcXFeQXJZYA1Wv
1706+
ycvFyAQBQSwcIbrokAkQAAABFAAAAUEsDBBQACAgIAIJ8lUsAAAAAAAAAAAAAAAASAAAAc2FtcGxl
1707+
L0hlbGxvLmNsYXNzO/Vv1z4GBgYTBkEuBhYGXg4GPnYGfnYGAUYGNpvMvMwSO0YGZg3NMEYGFuf8l
1708+
FRGBn6fzLxUv9LcpNSikMSkHKAIa3l+UU4KI4OIhqZPVmJZon5OYl66fnBJUWZeujUjA1dwfmlRcq
1709+
pbJkgtl0dqTk6+HkgZDwMrAxvQFrCIIiMDT3FibkFOqj6Yz8gggDDKPykrNbmEQZGBGehCEGBiYAR
1710+
pBpLsQJ4skGYE0qxa2xkYNwIZjAwcQJINIggkORm4oEqloUqZhZg2oClkB5LcYLN5AFBLBwjQMrpO
1711+
0wAAABMBAABQSwECFAAUAAgICADJfJVLAAAAAAIAAAAAAAAACQAEAAAAAAAAAAAAAAAAAAAATUVUQ
1712+
S1JTkYv/soAAFBLAQIUABQACAgIAMl8lUtuuiQCRAAAAEUAAAAUAAAAAAAAAAAAAAAAAD0AAABNRV
1713+
RBLUlORi9NQU5JRkVTVC5NRlBLAQIUABQACAgIAIJ8lUvQMrpO0wAAABMBAAASAAAAAAAAAAAAAAA
1714+
AAMMAAABzYW1wbGUvSGVsbG8uY2xhc3NQSwUGAAAAAAMAAwC9AAAA1gEAAAAA") });
17021715
if (enableMultiDex)
17031716
proj.SetProperty ("AndroidEnableMultiDex", "True");
17041717

@@ -1718,6 +1731,7 @@ public void BuildApplicationWithSpacesInPath ([Values (true, false)] bool isRele
17181731
}
17191732
using (var b = CreateApkBuilder (Path.Combine ("temp", $"BuildReleaseAppWithA InIt({isRelease}{enableProguard}{enableMultiDex})"))) {
17201733
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
1734+
Assert.IsFalse (b.LastBuildOutput.ContainsText ("Duplicate zip entry"), "Should not get warning about [META-INF/MANIFEST.MF]");
17211735
}
17221736
}
17231737

0 commit comments

Comments
 (0)