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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* Copyright (C) 2012 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.SdkConstants;
import com.android.ant.DependencyHelper.JarProcessor;
import com.android.io.FileWrapper;
import com.android.sdklib.internal.project.IPropertySource;
import com.android.xml.AndroidManifest;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Path.PathElement;
import java.io.File;
import java.util.List;
/**
* Computes the dependency of the current project.
*
* Out params:
* <code>libraryResFolderPathOut</code>: the Path object containing the res folder for all the
* library projects in the order needed by aapt.
*
* <code>libraryPackagesOut</code>: a simple property containing ;-separated package name from
* the library projects.
*
* <code>jarLibraryPathOut</code>: the Path object containing all the 3rd party jar files.
*
* <code>libraryNativeFolderPathOut</code>: the Path with all the native folder for the library
* projects.
*
*
* In params:
* <code>targetApi</code>: the compilation target api.
* <code>verbose</code>: whether the build is verbose.
*
*/
public class ComputeDependencyTask extends GetLibraryPathTask {
private String mLibraryManifestFilePathOut;
private String mLibraryResFolderPathOut;
private String mLibraryPackagesOut;
private String mJarLibraryPathOut;
private String mLibraryNativeFolderPathOut;
private String mLibraryBinAidlFolderPathOut;
private String mLibraryRFilePathOut;
private int mTargetApi = -1;
private boolean mVerbose = false;
public void setLibraryManifestFilePathOut(String libraryManifestFilePathOut) {
mLibraryManifestFilePathOut = libraryManifestFilePathOut;
}
public void setLibraryResFolderPathOut(String libraryResFolderPathOut) {
mLibraryResFolderPathOut = libraryResFolderPathOut;
}
public void setLibraryPackagesOut(String libraryPackagesOut) {
mLibraryPackagesOut = libraryPackagesOut;
}
public void setJarLibraryPathOut(String jarLibraryPathOut) {
mJarLibraryPathOut = jarLibraryPathOut;
}
public void setLibraryBinAidlFolderPathOut(String libraryBinAidlFolderPathOut) {
mLibraryBinAidlFolderPathOut = libraryBinAidlFolderPathOut;
}
public void setLibraryRFilePathOut(String libraryRFilePathOut) {
mLibraryRFilePathOut = libraryRFilePathOut;
}
public void setLibraryNativeFolderPathOut(String libraryNativeFolderPathOut) {
mLibraryNativeFolderPathOut = libraryNativeFolderPathOut;
}
public void setTargetApi(int targetApi) {
mTargetApi = targetApi;
}
@Override
public void execute() throws BuildException {
if (mLibraryManifestFilePathOut == null) {
throw new BuildException("Missing attribute libraryManifestFilePathOut");
}
if (mLibraryResFolderPathOut == null) {
throw new BuildException("Missing attribute libraryResFolderPathOut");
}
if (mLibraryPackagesOut == null) {
throw new BuildException("Missing attribute libraryPackagesOut");
}
if (mJarLibraryPathOut == null) {
throw new BuildException("Missing attribute jarLibraryPathOut");
}
if (mLibraryNativeFolderPathOut == null) {
throw new BuildException("Missing attribute libraryNativeFolderPathOut");
}
if (mLibraryBinAidlFolderPathOut == null) {
throw new BuildException("Missing attribute libraryBinFolderPathOut");
}
if (mLibraryRFilePathOut == null) {
throw new BuildException("Missing attribute libraryRFilePathOut");
}
if (mTargetApi == -1) {
throw new BuildException("Missing attribute targetApi");
}
final Project antProject = getProject();
// get the SDK location
File sdkDir = TaskHelper.getSdkLocation(antProject);
// prepare several paths for future tasks
final Path manifestFilePath = new Path(antProject);
final Path resFolderPath = new Path(antProject);
final Path nativeFolderPath = new Path(antProject);
final Path binAidlFolderPath = new Path(antProject);
final Path rFilePath = new Path(antProject);
final StringBuilder packageStrBuilder = new StringBuilder();
// custom jar processor doing a bit more than just collecting the jar files
JarProcessor processor = new JarProcessor() {
@Override
public void processLibrary(String libRootPath, IPropertySource properties) {
// let the super class handle the jar files
super.processLibrary(libRootPath, properties);
// get the AndroidManifest.xml path.
// FIXME: support renamed location.
PathElement element = manifestFilePath.createPathElement();
element.setPath(libRootPath + '/' + SdkConstants.FN_ANDROID_MANIFEST_XML);
// get the res path. $PROJECT/res as well as the crunch cache.
// FIXME: support renamed folders.
element = resFolderPath.createPathElement();
element.setPath(libRootPath + '/' + SdkConstants.FD_OUTPUT +
'/' + SdkConstants.FD_RES);
element = resFolderPath.createPathElement();
element.setPath(libRootPath + '/' + SdkConstants.FD_RESOURCES);
// get the folder for the native libraries. Always $PROJECT/libs
// FIXME: support renamed folder and/or move libs to bin/libs/
element = nativeFolderPath.createPathElement();
element.setPath(libRootPath + '/' + SdkConstants.FD_NATIVE_LIBS);
// get the bin/aidl folder. $PROJECT/bin/aidl for now
// FIXME: support renamed folder.
element = binAidlFolderPath.createPathElement();
element.setPath(libRootPath + '/' + SdkConstants.FD_OUTPUT +
'/' + SdkConstants.FD_AIDL);
// get the package from the manifest.
FileWrapper manifest = new FileWrapper(libRootPath,
SdkConstants.FN_ANDROID_MANIFEST_XML);
try {
String value = AndroidManifest.getPackage(manifest);
if (value != null) { // aapt will complain if it's missing.
packageStrBuilder.append(';');
packageStrBuilder.append(value);
// get the text R file. $PROJECT/bin/R.txt for now
// This must be in sync with the package list.
// FIXME: support renamed folder.
element = rFilePath.createPathElement();
element.setPath(libRootPath + "/" + SdkConstants.FD_OUTPUT +
"/" + "R.txt");
}
} catch (Exception e) {
throw new BuildException(e);
}
}
};
// list of all the jars that are on the classpath. This will receive the
// project's libs/*.jar files, the Library Projects output and their own libs/*.jar
List<File> jars = processor.getJars();
// in case clean has been called before a build type target, the list of
// libraries has already been computed so we don't need to compute it again.
Path libraryFolderPath = (Path) antProject.getReference(getLibraryFolderPathOut());
if (libraryFolderPath == null) {
execute(processor);
} else {
// this contains the list of library folder in reverse order (compilation order).
// We need to process it in the normal order (res order).
System.out.println("Ordered libraries:");
String[] libraries = libraryFolderPath.list();
for (int i = libraries.length - 1 ; i >= 0 ; i--) {
String libRootPath = libraries[i];
System.out.println(libRootPath);
processor.processLibrary(libRootPath);
}
}
boolean hasLibraries = jars.size() > 0;
if (mTargetApi <= 15) {
System.out.println("\n------------------");
System.out.println("API<=15: Adding annotations.jar to the classpath.");
jars.add(new File(sdkDir, SdkConstants.FD_TOOLS +
'/' + SdkConstants.FD_SUPPORT +
'/' + SdkConstants.FN_ANNOTATIONS_JAR));
}
// even with no libraries, always setup these so that various tasks in Ant don't complain
// (the task themselves can handle a ref to an empty Path)
antProject.addReference(mLibraryNativeFolderPathOut, nativeFolderPath);
antProject.addReference(mLibraryManifestFilePathOut, manifestFilePath);
antProject.addReference(mLibraryBinAidlFolderPathOut, binAidlFolderPath);
// the rest is done only if there's a library.
if (hasLibraries) {
antProject.addReference(mLibraryResFolderPathOut, resFolderPath);
antProject.setProperty(mLibraryPackagesOut, packageStrBuilder.toString());
antProject.addReference(mLibraryRFilePathOut, rFilePath);
}
File projectFolder = antProject.getBaseDir();
// add the project's own content of libs/*.jar
File libsFolder = new File(projectFolder, SdkConstants.FD_NATIVE_LIBS);
File[] jarFiles = libsFolder.listFiles(processor.getFilter());
if (jarFiles != null) {
for (File jarFile : jarFiles) {
jars.add(jarFile);
}
}
// now sanitize the path to remove dups
jars = DependencyHelper.sanitizePaths(projectFolder, new IPropertySource() {
@Override
public String getProperty(String name) {
return antProject.getProperty(name);
}
@Override
public void debugPrint() {
}
}, jars);
// and create a Path object for them
Path jarsPath = new Path(antProject);
if (mVerbose) {
System.out.println("\n------------------\nSanitized jar list:");
}
for (File f : jars) {
if (mVerbose) {
System.out.println("- " + f.getAbsolutePath());
}
PathElement element = jarsPath.createPathElement();
element.setPath(f.getAbsolutePath());
}
antProject.addReference(mJarLibraryPathOut, jarsPath);
if (mVerbose) {
System.out.println();
}
}
}
|