-
Notifications
You must be signed in to change notification settings - Fork 538
/
Copy pathCompileToDalvik.cs
141 lines (110 loc) · 4.59 KB
/
CompileToDalvik.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
// 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 Xamarin.Android.Tools;
namespace Xamarin.Android.Tasks
{
public class CompileToDalvik : JavaToolTask
{
public ITaskItem[] AdditionalJavaLibraryReferences { get; set; }
[Required]
public string ClassesOutputDirectory { get; set; }
public string DxJarPath { get; set; }
public string DxExtraArguments { get; set; }
public string JavaToolPath { get; set; }
[Required]
public ITaskItem[] JavaLibrariesToCompile { get; set; }
public ITaskItem [] AlternativeJarFiles { get; set; }
public bool UseDx { get; set; }
public bool MultiDexEnabled { get; set; }
public string MultiDexMainDexListFile { get; set; }
[Output]
public string [] DexOutputs { get; set; }
protected override string ToolName {
get {
if (UseDx)
return OS.IsWindows ? "dx.bat" : "dx";
return OS.IsWindows ? "java.exe" : "java";
}
}
public override bool Execute ()
{
Log.LogDebugMessage ("CompileToDalvik");
Log.LogDebugMessage (" JavaOptions: {0}", JavaOptions);
Log.LogDebugMessage (" JavaMaximumHeapSize: {0}", JavaMaximumHeapSize);
Log.LogDebugMessage (" ClassesOutputDirectory: {0}", ClassesOutputDirectory);
Log.LogDebugMessage (" JavaToolPath: {0}", JavaToolPath);
Log.LogDebugMessage (" DxJarPath: {0}", DxJarPath);
Log.LogDebugMessage (" ToolExe: {0}", ToolExe);
Log.LogDebugMessage (" ToolPath: {0}", ToolPath);
Log.LogDebugMessage (" UseDx: {0}", UseDx);
Log.LogDebugMessage (" DxExtraArguments: {0}", DxExtraArguments);
Log.LogDebugMessage (" MultiDexEnabled: {0}", MultiDexEnabled);
Log.LogDebugMessage (" MultiDexMainDexListFile: {0}", MultiDexMainDexListFile);
Log.LogDebugTaskItems (" JavaLibrariesToCompile:", JavaLibrariesToCompile);
Log.LogDebugTaskItems (" AlternativeJarFiles:", AlternativeJarFiles);
if (!Directory.Exists (ClassesOutputDirectory))
Directory.CreateDirectory (ClassesOutputDirectory);
bool ret = false;
try {
ret = base.Execute ();
DexOutputs = Directory.GetFiles (Path.GetDirectoryName (ClassesOutputDirectory), "*.dex", SearchOption.TopDirectoryOnly);
Log.LogDebugTaskItems (" DexOutputs: ", DexOutputs);
} catch (FileNotFoundException ex) {
Log.LogErrorFromException (ex);
}
return ret && !Log.HasLoggedErrors;
}
protected override string GenerateCommandLineCommands ()
{
// Running command: C:\Program Files\Java\jdk1.6.0_25\bin\java.exe -jar
// C:\Program Files (x86)\Android\android-sdk\platform-tools\lib\dx.jar --dex
// --output=C:\Users\jeff\Documents\Visual Studio 2010\Projects\<project>\...\android\bin\classes.dex
// C:\Users\jeff\Documents\Visual Studio 2010\Projects\<project>\...\android\bin\classes
// C:\Users\jeff\Documents\Visual Studio 2010\Projects\<project>\...\android\bin\mono.android.jar
var cmd = new CommandLineBuilder ();
if (!UseDx) {
// 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 (DxJarPath));
}
cmd.AppendSwitch (DxExtraArguments);
if (MultiDexEnabled) {
cmd.AppendSwitch ("--multi-dex");
cmd.AppendSwitchIfNotNull ("--main-dex-list=", MultiDexMainDexListFile);
}
cmd.AppendSwitchIfNotNull ("--output ", Path.GetDirectoryName (ClassesOutputDirectory));
// .jar files
if (AlternativeJarFiles != null && AlternativeJarFiles.Any ()) {
Log.LogDebugMessage (" processing AlternativeJarFiles...");
cmd.AppendFileNamesIfNotNull (AlternativeJarFiles, " ");
} else {
Log.LogDebugMessage (" processing ClassesOutputDirectory...");
var zip = Path.GetFullPath (Path.Combine (ClassesOutputDirectory, "..", "classes.zip"));
if (!File.Exists (zip)) {
throw new FileNotFoundException ($"'{zip}' does not exist. Please rebuild the project.");
}
cmd.AppendFileNameIfNotNull (zip);
foreach (var jar in JavaLibrariesToCompile)
cmd.AppendFileNameIfNotNull (jar.ItemSpec);
}
return cmd.ToString ();
}
protected override string GenerateFullPathToTool ()
{
if (UseDx)
return Path.Combine (ToolPath, ToolExe);
return Path.Combine (JavaToolPath, ToolName);
}
}
}