-
Notifications
You must be signed in to change notification settings - Fork 528
/
CreateMultiDexMainDexClassList.cs
120 lines (100 loc) · 4.68 KB
/
CreateMultiDexMainDexClassList.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
// Copyright (C) 2011 Xamarin, Inc. All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Collections.Specialized;
using Xamarin.Android.Tools;
namespace Xamarin.Android.Tasks
{
public class CreateMultiDexMainDexClassList : JavaToolTask
{
[Required]
public string ClassesOutputDirectory { get; set; }
[Required]
public string ProguardJarPath { get; set; }
[Required]
public string AndroidSdkBuildToolsPath { get; set; }
[Required]
public ITaskItem[] JavaLibraries { get; set; }
public string MultiDexMainDexListFile { get; set; }
public ITaskItem[] CustomMainDexListFiles { get; set; }
public string ProguardInputJarFilter { get; set; }
public string ExtraArgs { get; set; }
Action<CommandLineBuilder> commandlineAction;
string tempJar;
bool writeOutputToKeepFile = false;
public override bool Execute ()
{
Log.LogDebugMessage ("CreateMultiDexMainDexClassList");
Log.LogDebugMessage (" ClassesOutputDirectory: {0}", ClassesOutputDirectory);
Log.LogDebugTaskItems (" JavaLibraries:", JavaLibraries);
Log.LogDebugMessage (" MultiDexMainDexListFile: {0}", MultiDexMainDexListFile);
Log.LogDebugTaskItems (" CustomMainDexListFiles:", CustomMainDexListFiles);
Log.LogDebugMessage (" ToolExe: {0}", ToolExe);
Log.LogDebugMessage (" ToolPath: {0}", ToolPath);
Log.LogDebugMessage (" ProguardJarPath: {0}", ProguardJarPath);
Log.LogDebugMessage (" ProguardInputJarFilter: {0}", ProguardInputJarFilter);
if (CustomMainDexListFiles != null && CustomMainDexListFiles.Any ()) {
var content = string.Concat (CustomMainDexListFiles.Select (i => File.ReadAllText (i.ItemSpec)));
File.WriteAllText (MultiDexMainDexListFile, content);
return true;
}
tempJar = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName () + ".jar");
commandlineAction = GenerateProguardCommands;
// run proguard first
var retval = base.Execute ();
if (!retval || Log.HasLoggedErrors)
return false;
commandlineAction = GenerateMainDexListBuilderCommands;
// run java second
return base.Execute () && !Log.HasLoggedErrors;
}
protected override string GenerateCommandLineCommands ()
{
var cmd = new CommandLineBuilder ();
commandlineAction (cmd);
return cmd.ToString ();
}
void GenerateProguardCommands (CommandLineBuilder cmd)
{
var enclosingChar = OS.IsWindows ? "\"" : string.Empty;
var jars = JavaLibraries.Select (i => i.ItemSpec).Concat (new string [] { Path.Combine (ClassesOutputDirectory, "..", "classes.zip") });
cmd.AppendSwitchIfNotNull ("-jar ", ProguardJarPath);
cmd.AppendSwitchUnquotedIfNotNull ("-injars ", "\"'" + string.Join ($"'{ProguardInputJarFilter}{Path.PathSeparator}'", jars) + $"'{ProguardInputJarFilter}\"");
cmd.AppendSwitch ("-dontwarn");
cmd.AppendSwitch ("-forceprocessing");
cmd.AppendSwitchIfNotNull ("-outjars ", tempJar);
cmd.AppendSwitchIfNotNull ("-libraryjars ", $"'{Path.Combine (AndroidSdkBuildToolsPath, "lib", "shrinkedAndroid.jar")}'");
cmd.AppendSwitch ("-dontoptimize");
cmd.AppendSwitch ("-dontobfuscate");
cmd.AppendSwitch ("-dontpreverify");
cmd.AppendSwitchUnquotedIfNotNull ("-include ", $"{enclosingChar}'{Path.Combine (AndroidSdkBuildToolsPath, "mainDexClasses.rules")}'{enclosingChar}");
}
void GenerateMainDexListBuilderCommands(CommandLineBuilder cmd)
{
var enclosingDoubleQuote = OS.IsWindows ? "\"" : string.Empty;
var enclosingQuote = OS.IsWindows ? string.Empty : "'";
var jars = JavaLibraries.Select (i => i.ItemSpec).Concat (new string [] { Path.Combine (ClassesOutputDirectory, "..", "classes.zip") });
cmd.AppendSwitchIfNotNull ("-Djava.ext.dirs=", Path.Combine (AndroidSdkBuildToolsPath, "lib"));
cmd.AppendSwitch ("com.android.multidex.MainDexListBuilder");
if (!string.IsNullOrWhiteSpace (ExtraArgs))
cmd.AppendSwitch (ExtraArgs);
cmd.AppendSwitch ($"{enclosingDoubleQuote}{tempJar}{enclosingDoubleQuote}");
cmd.AppendSwitchUnquotedIfNotNull ("", $"{enclosingDoubleQuote}{enclosingQuote}" +
string.Join ($"{enclosingQuote}{Path.PathSeparator}{enclosingQuote}", jars) +
$"{enclosingQuote}{enclosingDoubleQuote}");
writeOutputToKeepFile = true;
}
protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance)
{
var match = CodeErrorRegEx.Match (singleLine);
var exceptionMatch = ExceptionRegEx.Match (singleLine);
if (writeOutputToKeepFile && !match.Success && !exceptionMatch.Success)
File.AppendAllText (MultiDexMainDexListFile, singleLine + "\n");
base.LogEventsFromTextOutput (singleLine, messageImportance);
}
}
}