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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ant;
import com.android.sdklib.build.JarListSanitizer;
import com.android.sdklib.build.JarListSanitizer.DifferentLibException;
import com.android.sdklib.build.JarListSanitizer.Sha1Exception;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.ExecTask;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.resources.FileResource;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Custom task to execute dx while handling dependencies.
*/
public class DexExecTask extends SingleDependencyTask {
private String mExecutable;
private String mOutput;
private boolean mVerbose = false;
private boolean mNoLocals = false;
private List<Path> mPathInputs;
private List<FileSet> mFileSetInputs;
/**
* Sets the value of the "executable" attribute.
* @param executable the value.
*/
public void setExecutable(Path executable) {
mExecutable = TaskHelper.checkSinglePath("executable", executable);
}
/**
* Sets the value of the "verbose" attribute.
* @param verbose the value.
*/
public void setVerbose(boolean verbose) {
mVerbose = verbose;
}
/**
* Sets the value of the "output" attribute.
* @param output the value.
*/
public void setOutput(Path output) {
mOutput = TaskHelper.checkSinglePath("output", output);
}
/**
* Sets the value of the "nolocals" attribute.
* @param verbose the value.
*/
public void setNoLocals(boolean nolocals) {
mNoLocals = nolocals;
}
/**
* Returns an object representing a nested <var>path</var> element.
*/
public Object createPath() {
if (mPathInputs == null) {
mPathInputs = new ArrayList<Path>();
}
Path path = new Path(getProject());
mPathInputs.add(path);
return path;
}
/**
* Returns an object representing a nested <var>path</var> element.
*/
public Object createFileSet() {
if (mFileSetInputs == null) {
mFileSetInputs = new ArrayList<FileSet>();
}
FileSet fs = new FileSet();
fs.setProject(getProject());
mFileSetInputs.add(fs);
return fs;
}
@Override
public void execute() throws BuildException {
// get all input paths
List<File> paths = new ArrayList<File>();
if (mPathInputs != null) {
for (Path pathList : mPathInputs) {
for (String path : pathList.list()) {
paths.add(new File(path));
}
}
}
if (mFileSetInputs != null) {
for (FileSet fs : mFileSetInputs) {
Iterator<?> iter = fs.iterator();
while (iter.hasNext()) {
FileResource fr = (FileResource) iter.next();
paths.add(fr.getFile());
}
}
}
// figure out the path to the dependency file.
String depFile = mOutput + ".d";
// get InputPath with no extension restrictions
List<InputPath> inputPaths = getInputPaths(paths, null /*extensionsToCheck*/,
null /*factory*/);
if (initDependencies(depFile, inputPaths) && dependenciesHaveChanged() == false) {
System.out.println(
"No new compiled code. No need to convert bytecode to dalvik format.");
return;
}
System.out.println(String.format(
"Converting compiled files and external libraries into %1$s...", mOutput));
ExecTask task = new ExecTask();
task.setProject(getProject());
task.setOwningTarget(getOwningTarget());
task.setExecutable(mExecutable);
task.setTaskName(getExecTaskName());
task.setFailonerror(true);
task.createArg().setValue("--dex");
if (mNoLocals) {
task.createArg().setValue("--no-locals");
}
if (mVerbose) {
task.createArg().setValue("--verbose");
}
task.createArg().setValue("--output");
task.createArg().setValue(mOutput);
paths = sanitizePaths(paths);
for (File f : paths) {
String absPath = f.getAbsolutePath();
System.out.println("Input: " + absPath);
task.createArg().setValue(absPath);
}
// execute it.
task.execute();
// generate the dependency file.
generateDependencyFile(depFile, inputPaths, mOutput);
}
@Override
protected String getExecTaskName() {
return "dx";
}
// private helper methods.
private List<File> sanitizePaths(List<File> paths) {
// first get the non-files.
List<File> results = new ArrayList<File>();
for (int i = 0 ; i < paths.size() ;) {
File f = paths.get(i);
// TEMP WORKAROUND: ignore classes.jar as all the output of libraries are
// called the same (in Ant) but are not actually the same jar file.
// TODO: Be aware of library output vs. regular jar dependency.
if (f.isFile() && f.getName().equals("classes.jar") == false) {
i++;
} else {
results.add(f);
paths.remove(i);
}
}
File outputFile = new File(mOutput);
JarListSanitizer sanitizer = new JarListSanitizer(outputFile.getParentFile());
try {
results.addAll(sanitizer.sanitize(paths));
} catch (DifferentLibException e) {
throw new BuildException(e.getMessage(), e);
} catch (Sha1Exception e) {
throw new BuildException(
"Failed to compute sha1 for " + e.getJarFile().getAbsolutePath(), e);
}
return results;
}
}
|