]> git.saurik.com Git - android/aapt.git/blob - Main.cpp
am 2be56ccb: Merge "Add --auto-add-overlay option to aapt. DO NOT MERGE" into eclair
[android/aapt.git] / Main.cpp
1 //
2 // Copyright 2006 The Android Open Source Project
3 //
4 // Android Asset Packaging Tool main entry point.
5 //
6 #include "Main.h"
7 #include "Bundle.h"
8
9 #include <utils/Log.h>
10 #include <utils/threads.h>
11 #include <utils/List.h>
12 #include <utils/Errors.h>
13
14 #include <stdlib.h>
15 #include <getopt.h>
16 #include <assert.h>
17
18 using namespace android;
19
20 static const char* gProgName = "aapt";
21
22 /*
23 * When running under Cygwin on Windows, this will convert slash-based
24 * paths into back-slash-based ones. Otherwise the ApptAssets file comparisons
25 * fail later as they use back-slash separators under Windows.
26 *
27 * This operates in-place on the path string.
28 */
29 void convertPath(char *path) {
30 if (path != NULL && OS_PATH_SEPARATOR != '/') {
31 for (; *path; path++) {
32 if (*path == '/') {
33 *path = OS_PATH_SEPARATOR;
34 }
35 }
36 }
37 }
38
39 /*
40 * Print usage info.
41 */
42 void usage(void)
43 {
44 fprintf(stderr, "Android Asset Packaging Tool\n\n");
45 fprintf(stderr, "Usage:\n");
46 fprintf(stderr,
47 " %s l[ist] [-v] [-a] file.{zip,jar,apk}\n"
48 " List contents of Zip-compatible archive.\n\n", gProgName);
49 fprintf(stderr,
50 " %s d[ump] [--values] WHAT file.{apk} [asset [asset ...]]\n"
51 " badging Print the label and icon for the app declared in APK.\n"
52 " permissions Print the permissions from the APK.\n"
53 " resources Print the resource table from the APK.\n"
54 " configurations Print the configurations in the APK.\n"
55 " xmltree Print the compiled xmls in the given assets.\n"
56 " xmlstrings Print the strings of the given compiled xml assets.\n\n", gProgName);
57 fprintf(stderr,
58 " %s p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \\\n"
59 " [-0 extension [-0 extension ...]] [-g tolerance] [-j jarfile] \\\n"
60 " [--min-sdk-version VAL] [--target-sdk-version VAL] \\\n"
61 " [--max-sdk-version VAL] [--app-version VAL] \\\n"
62 " [--app-version-name TEXT] [--custom-package VAL] \\\n"
63 " [--auto-add-overlay] \\\n"
64 " [-I base-package [-I base-package ...]] \\\n"
65 " [-A asset-source-dir] [-G class-list-file] [-P public-definitions-file] \\\n"
66 " [-S resource-sources [-S resource-sources ...]] "
67 " [-F apk-file] [-J R-file-dir] \\\n"
68 " [raw-files-dir [raw-files-dir] ...]\n"
69 "\n"
70 " Package the android resources. It will read assets and resources that are\n"
71 " supplied with the -M -A -S or raw-files-dir arguments. The -J -P -F and -R\n"
72 " options control which files are output.\n\n"
73 , gProgName);
74 fprintf(stderr,
75 " %s r[emove] [-v] file.{zip,jar,apk} file1 [file2 ...]\n"
76 " Delete specified files from Zip-compatible archive.\n\n",
77 gProgName);
78 fprintf(stderr,
79 " %s a[dd] [-v] file.{zip,jar,apk} file1 [file2 ...]\n"
80 " Add specified files to Zip-compatible archive.\n\n", gProgName);
81 fprintf(stderr,
82 " %s v[ersion]\n"
83 " Print program version.\n\n", gProgName);
84 fprintf(stderr,
85 " Modifiers:\n"
86 " -a print Android-specific data (resources, manifest) when listing\n"
87 " -c specify which configurations to include. The default is all\n"
88 " configurations. The value of the parameter should be a comma\n"
89 " separated list of configuration values. Locales should be specified\n"
90 " as either a language or language-region pair. Some examples:\n"
91 " en\n"
92 " port,en\n"
93 " port,land,en_US\n"
94 " If you put the special locale, zz_ZZ on the list, it will perform\n"
95 " pseudolocalization on the default locale, modifying all of the\n"
96 " strings so you can look for strings that missed the\n"
97 " internationalization process. For example:\n"
98 " port,land,zz_ZZ\n"
99 " -d one or more device assets to include, separated by commas\n"
100 " -f force overwrite of existing files\n"
101 " -g specify a pixel tolerance to force images to grayscale, default 0\n"
102 " -j specify a jar or zip file containing classes to include\n"
103 " -k junk path of file(s) added\n"
104 " -m make package directories under location specified by -J\n"
105 #if 0
106 " -p pseudolocalize the default configuration\n"
107 #endif
108 " -u update existing packages (add new, replace older, remove deleted files)\n"
109 " -v verbose output\n"
110 " -x create extending (non-application) resource IDs\n"
111 " -z require localization of resource attributes marked with\n"
112 " localization=\"suggested\"\n"
113 " -A additional directory in which to find raw asset files\n"
114 " -G A file to output proguard options into.\n"
115 " -F specify the apk file to output\n"
116 " -I add an existing package to base include set\n"
117 " -J specify where to output R.java resource constant definitions\n"
118 " -M specify full path to AndroidManifest.xml to include in zip\n"
119 " -P specify where to output public resource definitions\n"
120 " -S directory in which to find resources. Multiple directories will be scanned\n"
121 " and the first match found (left to right) will take precedence.\n"
122 " -0 specifies an additional extension for which such files will not\n"
123 " be stored compressed in the .apk. An empty string means to not\n"
124 " compress any files at all.\n"
125 " --min-sdk-version\n"
126 " inserts android:minSdkVersion in to manifest.\n"
127 " --target-sdk-version\n"
128 " inserts android:targetSdkVersion in to manifest.\n"
129 " --max-sdk-version\n"
130 " inserts android:maxSdkVersion in to manifest.\n"
131 " --values\n"
132 " when used with \"dump resources\" also includes resource values.\n"
133 " --version-code\n"
134 " inserts android:versionCode in to manifest.\n"
135 " --version-name\n"
136 " inserts android:versionName in to manifest.\n"
137 " --custom-package\n"
138 " generates R.java into a different package.\n"
139 " --auto-add-overlay\n"
140 " Automatically add resources that are only in overlays.\n");
141 }
142
143 /*
144 * Dispatch the command.
145 */
146 int handleCommand(Bundle* bundle)
147 {
148 //printf("--- command %d (verbose=%d force=%d):\n",
149 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce());
150 //for (int i = 0; i < bundle->getFileSpecCount(); i++)
151 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i));
152
153 switch (bundle->getCommand()) {
154 case kCommandVersion: return doVersion(bundle);
155 case kCommandList: return doList(bundle);
156 case kCommandDump: return doDump(bundle);
157 case kCommandAdd: return doAdd(bundle);
158 case kCommandRemove: return doRemove(bundle);
159 case kCommandPackage: return doPackage(bundle);
160 default:
161 fprintf(stderr, "%s: requested command not yet supported\n", gProgName);
162 return 1;
163 }
164 }
165
166 /*
167 * Parse args.
168 */
169 int main(int argc, char* const argv[])
170 {
171 char *prog = argv[0];
172 Bundle bundle;
173 bool wantUsage = false;
174 int result = 1; // pessimistically assume an error.
175 int tolerance = 0;
176
177 /* default to compression */
178 bundle.setCompressionMethod(ZipEntry::kCompressDeflated);
179
180 if (argc < 2) {
181 wantUsage = true;
182 goto bail;
183 }
184
185 if (argv[1][0] == 'v')
186 bundle.setCommand(kCommandVersion);
187 else if (argv[1][0] == 'd')
188 bundle.setCommand(kCommandDump);
189 else if (argv[1][0] == 'l')
190 bundle.setCommand(kCommandList);
191 else if (argv[1][0] == 'a')
192 bundle.setCommand(kCommandAdd);
193 else if (argv[1][0] == 'r')
194 bundle.setCommand(kCommandRemove);
195 else if (argv[1][0] == 'p')
196 bundle.setCommand(kCommandPackage);
197 else {
198 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]);
199 wantUsage = true;
200 goto bail;
201 }
202 argc -= 2;
203 argv += 2;
204
205 /*
206 * Pull out flags. We support "-fv" and "-f -v".
207 */
208 while (argc && argv[0][0] == '-') {
209 /* flag(s) found */
210 const char* cp = argv[0] +1;
211
212 while (*cp != '\0') {
213 switch (*cp) {
214 case 'v':
215 bundle.setVerbose(true);
216 break;
217 case 'a':
218 bundle.setAndroidList(true);
219 break;
220 case 'c':
221 argc--;
222 argv++;
223 if (!argc) {
224 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n");
225 wantUsage = true;
226 goto bail;
227 }
228 bundle.addConfigurations(argv[0]);
229 break;
230 case 'f':
231 bundle.setForce(true);
232 break;
233 case 'g':
234 argc--;
235 argv++;
236 if (!argc) {
237 fprintf(stderr, "ERROR: No argument supplied for '-g' option\n");
238 wantUsage = true;
239 goto bail;
240 }
241 tolerance = atoi(argv[0]);
242 bundle.setGrayscaleTolerance(tolerance);
243 printf("%s: Images with deviation <= %d will be forced to grayscale.\n", prog, tolerance);
244 break;
245 case 'k':
246 bundle.setJunkPath(true);
247 break;
248 case 'm':
249 bundle.setMakePackageDirs(true);
250 break;
251 #if 0
252 case 'p':
253 bundle.setPseudolocalize(true);
254 break;
255 #endif
256 case 'u':
257 bundle.setUpdate(true);
258 break;
259 case 'x':
260 bundle.setExtending(true);
261 break;
262 case 'z':
263 bundle.setRequireLocalization(true);
264 break;
265 case 'j':
266 argc--;
267 argv++;
268 if (!argc) {
269 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n");
270 wantUsage = true;
271 goto bail;
272 }
273 convertPath(argv[0]);
274 bundle.addJarFile(argv[0]);
275 break;
276 case 'A':
277 argc--;
278 argv++;
279 if (!argc) {
280 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n");
281 wantUsage = true;
282 goto bail;
283 }
284 convertPath(argv[0]);
285 bundle.setAssetSourceDir(argv[0]);
286 break;
287 case 'G':
288 argc--;
289 argv++;
290 if (!argc) {
291 fprintf(stderr, "ERROR: No argument supplied for '-G' option\n");
292 wantUsage = true;
293 goto bail;
294 }
295 convertPath(argv[0]);
296 bundle.setProguardFile(argv[0]);
297 break;
298 case 'I':
299 argc--;
300 argv++;
301 if (!argc) {
302 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n");
303 wantUsage = true;
304 goto bail;
305 }
306 convertPath(argv[0]);
307 bundle.addPackageInclude(argv[0]);
308 break;
309 case 'F':
310 argc--;
311 argv++;
312 if (!argc) {
313 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n");
314 wantUsage = true;
315 goto bail;
316 }
317 convertPath(argv[0]);
318 bundle.setOutputAPKFile(argv[0]);
319 break;
320 case 'J':
321 argc--;
322 argv++;
323 if (!argc) {
324 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n");
325 wantUsage = true;
326 goto bail;
327 }
328 convertPath(argv[0]);
329 bundle.setRClassDir(argv[0]);
330 break;
331 case 'M':
332 argc--;
333 argv++;
334 if (!argc) {
335 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n");
336 wantUsage = true;
337 goto bail;
338 }
339 convertPath(argv[0]);
340 bundle.setAndroidManifestFile(argv[0]);
341 break;
342 case 'P':
343 argc--;
344 argv++;
345 if (!argc) {
346 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n");
347 wantUsage = true;
348 goto bail;
349 }
350 convertPath(argv[0]);
351 bundle.setPublicOutputFile(argv[0]);
352 break;
353 case 'S':
354 argc--;
355 argv++;
356 if (!argc) {
357 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n");
358 wantUsage = true;
359 goto bail;
360 }
361 convertPath(argv[0]);
362 bundle.addResourceSourceDir(argv[0]);
363 break;
364 case '0':
365 argc--;
366 argv++;
367 if (!argc) {
368 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n");
369 wantUsage = true;
370 goto bail;
371 }
372 if (argv[0][0] != 0) {
373 bundle.addNoCompressExtension(argv[0]);
374 } else {
375 bundle.setCompressionMethod(ZipEntry::kCompressStored);
376 }
377 break;
378 case '-':
379 if (strcmp(cp, "-min-sdk-version") == 0) {
380 argc--;
381 argv++;
382 if (!argc) {
383 fprintf(stderr, "ERROR: No argument supplied for '--min-sdk-version' option\n");
384 wantUsage = true;
385 goto bail;
386 }
387 bundle.setMinSdkVersion(argv[0]);
388 } else if (strcmp(cp, "-target-sdk-version") == 0) {
389 argc--;
390 argv++;
391 if (!argc) {
392 fprintf(stderr, "ERROR: No argument supplied for '--target-sdk-version' option\n");
393 wantUsage = true;
394 goto bail;
395 }
396 bundle.setTargetSdkVersion(argv[0]);
397 } else if (strcmp(cp, "-max-sdk-version") == 0) {
398 argc--;
399 argv++;
400 if (!argc) {
401 fprintf(stderr, "ERROR: No argument supplied for '--max-sdk-version' option\n");
402 wantUsage = true;
403 goto bail;
404 }
405 bundle.setMaxSdkVersion(argv[0]);
406 } else if (strcmp(cp, "-version-code") == 0) {
407 argc--;
408 argv++;
409 if (!argc) {
410 fprintf(stderr, "ERROR: No argument supplied for '--version-code' option\n");
411 wantUsage = true;
412 goto bail;
413 }
414 bundle.setVersionCode(argv[0]);
415 } else if (strcmp(cp, "-version-name") == 0) {
416 argc--;
417 argv++;
418 if (!argc) {
419 fprintf(stderr, "ERROR: No argument supplied for '--version-name' option\n");
420 wantUsage = true;
421 goto bail;
422 }
423 bundle.setVersionName(argv[0]);
424 } else if (strcmp(cp, "-values") == 0) {
425 bundle.setValues(true);
426 } else if (strcmp(cp, "-custom-package") == 0) {
427 argc--;
428 argv++;
429 if (!argc) {
430 fprintf(stderr, "ERROR: No argument supplied for '--custom-package' option\n");
431 wantUsage = true;
432 goto bail;
433 }
434 bundle.setCustomPackage(argv[0]);
435 } else if (strcmp(cp, "-auto-add-overlay") == 0) {
436 bundle.setAutoAddOverlay(true);
437 } else {
438 fprintf(stderr, "ERROR: Unknown option '-%s'\n", cp);
439 wantUsage = true;
440 goto bail;
441 }
442 cp += strlen(cp) - 1;
443 break;
444 default:
445 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp);
446 wantUsage = true;
447 goto bail;
448 }
449
450 cp++;
451 }
452 argc--;
453 argv++;
454 }
455
456 /*
457 * We're past the flags. The rest all goes straight in.
458 */
459 bundle.setFileSpec(argv, argc);
460
461 result = handleCommand(&bundle);
462
463 bail:
464 if (wantUsage) {
465 usage();
466 result = 2;
467 }
468
469 //printf("--> returning %d\n", result);
470 return result;
471 }