]> git.saurik.com Git - android/aapt.git/blame - Main.cpp
Add aapt support for generating proguard rules for onClick methods.
[android/aapt.git] / Main.cpp
CommitLineData
a534180c
TAOSP
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
79e5d372
MA
9#include <utils/Log.h>
10#include <utils/threads.h>
11#include <utils/List.h>
12#include <utils/Errors.h>
a534180c
TAOSP
13
14#include <stdlib.h>
15#include <getopt.h>
16#include <assert.h>
17
18using namespace android;
19
20static 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 */
29void 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 */
42void 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,
7751daa4 50 " %s d[ump] [--values] WHAT file.{apk} [asset [asset ...]]\n"
a534180c
TAOSP
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"
e942a5c2 59 " [-0 extension [-0 extension ...]] [-g tolerance] [-j jarfile] \\\n"
f04c74b7 60 " [--debug-mode] [--min-sdk-version VAL] [--target-sdk-version VAL] \\\n"
af945cf3
DH
61 " [--app-version VAL] [--app-version-name TEXT] [--custom-package VAL] \\\n"
62 " [--rename-manifest-package PACKAGE] \\\n"
63 " [--rename-instrumentation-target-package PACKAGE] \\\n"
64 " [--utf16] [--auto-add-overlay] \\\n"
73412e58 65 " [--max-res-version VAL] \\\n"
a534180c 66 " [-I base-package [-I base-package ...]] \\\n"
6648ff78 67 " [-A asset-source-dir] [-G class-list-file] [-P public-definitions-file] \\\n"
e29f4ada 68 " [-S resource-sources [-S resource-sources ...]] \\\n"
a534180c 69 " [-F apk-file] [-J R-file-dir] \\\n"
b8ea3a3f 70 " [--product product1,product2,...] \\\n"
e29f4ada 71 " [-c CONFIGS] [--preferred-configurations CONFIGS] \\\n"
b1bccba1 72 " [-o] \\\n"
a534180c
TAOSP
73 " [raw-files-dir [raw-files-dir] ...]\n"
74 "\n"
75 " Package the android resources. It will read assets and resources that are\n"
76 " supplied with the -M -A -S or raw-files-dir arguments. The -J -P -F and -R\n"
77 " options control which files are output.\n\n"
78 , gProgName);
79 fprintf(stderr,
80 " %s r[emove] [-v] file.{zip,jar,apk} file1 [file2 ...]\n"
81 " Delete specified files from Zip-compatible archive.\n\n",
82 gProgName);
83 fprintf(stderr,
84 " %s a[dd] [-v] file.{zip,jar,apk} file1 [file2 ...]\n"
85 " Add specified files to Zip-compatible archive.\n\n", gProgName);
dddb1fc7
JG
86 fprintf(stderr,
87 " %s c[runch] [-v] -S resource-sources ... -C output-folder ...\n"
88 " Do PNG preprocessing and store the results in output folder.\n\n", gProgName);
a534180c
TAOSP
89 fprintf(stderr,
90 " %s v[ersion]\n"
91 " Print program version.\n\n", gProgName);
92 fprintf(stderr,
93 " Modifiers:\n"
94 " -a print Android-specific data (resources, manifest) when listing\n"
95 " -c specify which configurations to include. The default is all\n"
96 " configurations. The value of the parameter should be a comma\n"
97 " separated list of configuration values. Locales should be specified\n"
98 " as either a language or language-region pair. Some examples:\n"
99 " en\n"
100 " port,en\n"
101 " port,land,en_US\n"
102 " If you put the special locale, zz_ZZ on the list, it will perform\n"
103 " pseudolocalization on the default locale, modifying all of the\n"
104 " strings so you can look for strings that missed the\n"
105 " internationalization process. For example:\n"
106 " port,land,zz_ZZ\n"
107 " -d one or more device assets to include, separated by commas\n"
108 " -f force overwrite of existing files\n"
109 " -g specify a pixel tolerance to force images to grayscale, default 0\n"
110 " -j specify a jar or zip file containing classes to include\n"
1e8883fc 111 " -k junk path of file(s) added\n"
a534180c 112 " -m make package directories under location specified by -J\n"
b1bccba1 113 " -o create overlay package (ie only resources; expects <overlay-package> in manifest)\n"
a534180c
TAOSP
114#if 0
115 " -p pseudolocalize the default configuration\n"
116#endif
117 " -u update existing packages (add new, replace older, remove deleted files)\n"
118 " -v verbose output\n"
119 " -x create extending (non-application) resource IDs\n"
120 " -z require localization of resource attributes marked with\n"
121 " localization=\"suggested\"\n"
122 " -A additional directory in which to find raw asset files\n"
6648ff78 123 " -G A file to output proguard options into.\n"
a534180c
TAOSP
124 " -F specify the apk file to output\n"
125 " -I add an existing package to base include set\n"
126 " -J specify where to output R.java resource constant definitions\n"
127 " -M specify full path to AndroidManifest.xml to include in zip\n"
128 " -P specify where to output public resource definitions\n"
fa19db5e
XD
129 " -S directory in which to find resources. Multiple directories will be scanned\n"
130 " and the first match found (left to right) will take precedence.\n"
a534180c
TAOSP
131 " -0 specifies an additional extension for which such files will not\n"
132 " be stored compressed in the .apk. An empty string means to not\n"
e942a5c2 133 " compress any files at all.\n"
f04c74b7
XD
134 " --debug-mode\n"
135 " inserts android:debuggable=\"true\" in to the application node of the\n"
136 " manifest, making the application debuggable even on production devices.\n"
e942a5c2 137 " --min-sdk-version\n"
28de9b93
KR
138 " inserts android:minSdkVersion in to manifest. If the version is 7 or\n"
139 " higher, the default encoding for resources will be in UTF-8.\n"
e942a5c2
DH
140 " --target-sdk-version\n"
141 " inserts android:targetSdkVersion in to manifest.\n"
73412e58
FK
142 " --max-res-version\n"
143 " ignores versioned resource directories above the given value.\n"
7751daa4
DH
144 " --values\n"
145 " when used with \"dump resources\" also includes resource values.\n"
e942a5c2
DH
146 " --version-code\n"
147 " inserts android:versionCode in to manifest.\n"
148 " --version-name\n"
fa19db5e
XD
149 " inserts android:versionName in to manifest.\n"
150 " --custom-package\n"
28de9b93 151 " generates R.java into a different package.\n"
bc7b4f40 152 " --extra-packages\n"
52ffc169
JG
153 " generate R.java for libraries. Separate libraries with ':'.\n"
154 " --generate-dependencies\n"
b5a473da 155 " generate dependency files in the same directories for R.java and resource package\n"
636d3b70
XD
156 " --auto-add-overlay\n"
157 " Automatically add resources that are only in overlays.\n"
e29f4ada
DH
158 " --preferred-configurations\n"
159 " Like the -c option for filtering out unneeded configurations, but\n"
160 " only expresses a preference. If there is no resource available with\n"
161 " the preferred configuration then it will not be stripped.\n"
af945cf3
DH
162 " --rename-manifest-package\n"
163 " Rewrite the manifest so that its package name is the package name\n"
164 " given here. Relative class names (for example .Foo) will be\n"
165 " changed to absolute names with the old package so that the code\n"
166 " does not need to change.\n"
167 " --rename-instrumentation-target-package\n"
168 " Rewrite the manifest so that all of its instrumentation\n"
169 " components target the given package. Useful when used in\n"
170 " conjunction with --rename-manifest-package to fix tests against\n"
171 " a package that has been renamed.\n"
b8ea3a3f
EF
172 " --product\n"
173 " Specifies which variant to choose for strings that have\n"
174 " product variants\n"
28de9b93
KR
175 " --utf16\n"
176 " changes default encoding for resources to UTF-16. Only useful when API\n"
54f200b0
XD
177 " level is set to 7 or higher where the default encoding is UTF-8.\n"
178 " --non-constant-id\n"
179 " Make the resources ID non constant. This is required to make an R java class\n"
180 " that does not contain the final value but is used to make reusable compiled\n"
181 " libraries that need to access resources.\n");
a534180c
TAOSP
182}
183
184/*
185 * Dispatch the command.
186 */
187int handleCommand(Bundle* bundle)
188{
189 //printf("--- command %d (verbose=%d force=%d):\n",
190 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce());
191 //for (int i = 0; i < bundle->getFileSpecCount(); i++)
192 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i));
193
194 switch (bundle->getCommand()) {
195 case kCommandVersion: return doVersion(bundle);
196 case kCommandList: return doList(bundle);
197 case kCommandDump: return doDump(bundle);
198 case kCommandAdd: return doAdd(bundle);
199 case kCommandRemove: return doRemove(bundle);
200 case kCommandPackage: return doPackage(bundle);
dddb1fc7 201 case kCommandCrunch: return doCrunch(bundle);
a534180c
TAOSP
202 default:
203 fprintf(stderr, "%s: requested command not yet supported\n", gProgName);
204 return 1;
205 }
206}
207
208/*
209 * Parse args.
210 */
211int main(int argc, char* const argv[])
212{
213 char *prog = argv[0];
214 Bundle bundle;
215 bool wantUsage = false;
216 int result = 1; // pessimistically assume an error.
217 int tolerance = 0;
218
219 /* default to compression */
220 bundle.setCompressionMethod(ZipEntry::kCompressDeflated);
221
222 if (argc < 2) {
223 wantUsage = true;
224 goto bail;
225 }
226
227 if (argv[1][0] == 'v')
228 bundle.setCommand(kCommandVersion);
229 else if (argv[1][0] == 'd')
230 bundle.setCommand(kCommandDump);
231 else if (argv[1][0] == 'l')
232 bundle.setCommand(kCommandList);
233 else if (argv[1][0] == 'a')
234 bundle.setCommand(kCommandAdd);
235 else if (argv[1][0] == 'r')
236 bundle.setCommand(kCommandRemove);
237 else if (argv[1][0] == 'p')
238 bundle.setCommand(kCommandPackage);
dddb1fc7
JG
239 else if (argv[1][0] == 'c')
240 bundle.setCommand(kCommandCrunch);
a534180c
TAOSP
241 else {
242 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]);
243 wantUsage = true;
244 goto bail;
245 }
246 argc -= 2;
247 argv += 2;
248
249 /*
250 * Pull out flags. We support "-fv" and "-f -v".
251 */
252 while (argc && argv[0][0] == '-') {
253 /* flag(s) found */
254 const char* cp = argv[0] +1;
255
256 while (*cp != '\0') {
257 switch (*cp) {
258 case 'v':
259 bundle.setVerbose(true);
260 break;
261 case 'a':
262 bundle.setAndroidList(true);
263 break;
264 case 'c':
265 argc--;
266 argv++;
267 if (!argc) {
268 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n");
269 wantUsage = true;
270 goto bail;
271 }
272 bundle.addConfigurations(argv[0]);
273 break;
274 case 'f':
275 bundle.setForce(true);
276 break;
277 case 'g':
278 argc--;
279 argv++;
280 if (!argc) {
281 fprintf(stderr, "ERROR: No argument supplied for '-g' option\n");
282 wantUsage = true;
283 goto bail;
284 }
285 tolerance = atoi(argv[0]);
286 bundle.setGrayscaleTolerance(tolerance);
287 printf("%s: Images with deviation <= %d will be forced to grayscale.\n", prog, tolerance);
288 break;
1e8883fc
DZ
289 case 'k':
290 bundle.setJunkPath(true);
291 break;
a534180c
TAOSP
292 case 'm':
293 bundle.setMakePackageDirs(true);
294 break;
b1bccba1
MK
295 case 'o':
296 bundle.setIsOverlayPackage(true);
297 break;
a534180c
TAOSP
298#if 0
299 case 'p':
300 bundle.setPseudolocalize(true);
301 break;
302#endif
303 case 'u':
304 bundle.setUpdate(true);
305 break;
306 case 'x':
307 bundle.setExtending(true);
308 break;
309 case 'z':
310 bundle.setRequireLocalization(true);
311 break;
312 case 'j':
313 argc--;
314 argv++;
315 if (!argc) {
316 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n");
317 wantUsage = true;
318 goto bail;
319 }
320 convertPath(argv[0]);
321 bundle.addJarFile(argv[0]);
322 break;
323 case 'A':
324 argc--;
325 argv++;
326 if (!argc) {
327 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n");
328 wantUsage = true;
329 goto bail;
330 }
331 convertPath(argv[0]);
332 bundle.setAssetSourceDir(argv[0]);
333 break;
6648ff78
JO
334 case 'G':
335 argc--;
336 argv++;
337 if (!argc) {
338 fprintf(stderr, "ERROR: No argument supplied for '-G' option\n");
339 wantUsage = true;
340 goto bail;
341 }
342 convertPath(argv[0]);
343 bundle.setProguardFile(argv[0]);
344 break;
a534180c
TAOSP
345 case 'I':
346 argc--;
347 argv++;
348 if (!argc) {
349 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n");
350 wantUsage = true;
351 goto bail;
352 }
353 convertPath(argv[0]);
354 bundle.addPackageInclude(argv[0]);
355 break;
356 case 'F':
357 argc--;
358 argv++;
359 if (!argc) {
360 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n");
361 wantUsage = true;
362 goto bail;
363 }
364 convertPath(argv[0]);
365 bundle.setOutputAPKFile(argv[0]);
366 break;
367 case 'J':
368 argc--;
369 argv++;
370 if (!argc) {
371 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n");
372 wantUsage = true;
373 goto bail;
374 }
375 convertPath(argv[0]);
376 bundle.setRClassDir(argv[0]);
377 break;
378 case 'M':
379 argc--;
380 argv++;
381 if (!argc) {
382 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n");
383 wantUsage = true;
384 goto bail;
385 }
386 convertPath(argv[0]);
387 bundle.setAndroidManifestFile(argv[0]);
388 break;
389 case 'P':
390 argc--;
391 argv++;
392 if (!argc) {
393 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n");
394 wantUsage = true;
395 goto bail;
396 }
397 convertPath(argv[0]);
398 bundle.setPublicOutputFile(argv[0]);
399 break;
400 case 'S':
401 argc--;
402 argv++;
403 if (!argc) {
404 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n");
405 wantUsage = true;
406 goto bail;
407 }
408 convertPath(argv[0]);
409 bundle.addResourceSourceDir(argv[0]);
410 break;
dddb1fc7
JG
411 case 'C':
412 argc--;
413 argv++;
414 if (!argc) {
415 fprintf(stderr, "ERROR: No argument supplied for '-C' option\n");
416 wantUsage = true;
417 goto bail;
418 }
419 convertPath(argv[0]);
420 bundle.setCrunchedOutputDir(argv[0]);
421 break;
a534180c
TAOSP
422 case '0':
423 argc--;
424 argv++;
425 if (!argc) {
426 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n");
427 wantUsage = true;
428 goto bail;
429 }
430 if (argv[0][0] != 0) {
431 bundle.addNoCompressExtension(argv[0]);
432 } else {
433 bundle.setCompressionMethod(ZipEntry::kCompressStored);
434 }
435 break;
e942a5c2 436 case '-':
f04c74b7
XD
437 if (strcmp(cp, "-debug-mode") == 0) {
438 bundle.setDebugMode(true);
439 } else if (strcmp(cp, "-min-sdk-version") == 0) {
e942a5c2
DH
440 argc--;
441 argv++;
442 if (!argc) {
443 fprintf(stderr, "ERROR: No argument supplied for '--min-sdk-version' option\n");
444 wantUsage = true;
445 goto bail;
446 }
447 bundle.setMinSdkVersion(argv[0]);
448 } else if (strcmp(cp, "-target-sdk-version") == 0) {
449 argc--;
450 argv++;
451 if (!argc) {
452 fprintf(stderr, "ERROR: No argument supplied for '--target-sdk-version' option\n");
453 wantUsage = true;
454 goto bail;
455 }
456 bundle.setTargetSdkVersion(argv[0]);
457 } else if (strcmp(cp, "-max-sdk-version") == 0) {
458 argc--;
459 argv++;
460 if (!argc) {
461 fprintf(stderr, "ERROR: No argument supplied for '--max-sdk-version' option\n");
462 wantUsage = true;
463 goto bail;
464 }
465 bundle.setMaxSdkVersion(argv[0]);
73412e58
FK
466 } else if (strcmp(cp, "-max-res-version") == 0) {
467 argc--;
468 argv++;
469 if (!argc) {
470 fprintf(stderr, "ERROR: No argument supplied for '--max-res-version' option\n");
471 wantUsage = true;
472 goto bail;
473 }
474 bundle.setMaxResVersion(argv[0]);
e942a5c2
DH
475 } else if (strcmp(cp, "-version-code") == 0) {
476 argc--;
477 argv++;
478 if (!argc) {
479 fprintf(stderr, "ERROR: No argument supplied for '--version-code' option\n");
480 wantUsage = true;
481 goto bail;
482 }
483 bundle.setVersionCode(argv[0]);
484 } else if (strcmp(cp, "-version-name") == 0) {
485 argc--;
486 argv++;
487 if (!argc) {
488 fprintf(stderr, "ERROR: No argument supplied for '--version-name' option\n");
489 wantUsage = true;
490 goto bail;
491 }
492 bundle.setVersionName(argv[0]);
7751daa4
DH
493 } else if (strcmp(cp, "-values") == 0) {
494 bundle.setValues(true);
fa19db5e
XD
495 } else if (strcmp(cp, "-custom-package") == 0) {
496 argc--;
497 argv++;
498 if (!argc) {
499 fprintf(stderr, "ERROR: No argument supplied for '--custom-package' option\n");
500 wantUsage = true;
501 goto bail;
502 }
503 bundle.setCustomPackage(argv[0]);
bc7b4f40
JG
504 } else if (strcmp(cp, "-extra-packages") == 0) {
505 argc--;
506 argv++;
507 if (!argc) {
508 fprintf(stderr, "ERROR: No argument supplied for '--extra-packages' option\n");
509 wantUsage = true;
510 goto bail;
511 }
512 bundle.setExtraPackages(argv[0]);
52ffc169
JG
513 } else if (strcmp(cp, "-generate-dependencies") == 0) {
514 bundle.setGenDependencies(true);
28de9b93 515 } else if (strcmp(cp, "-utf16") == 0) {
5af43148 516 bundle.setWantUTF16(true);
e29f4ada
DH
517 } else if (strcmp(cp, "-preferred-configurations") == 0) {
518 argc--;
519 argv++;
520 if (!argc) {
521 fprintf(stderr, "ERROR: No argument supplied for '--preferred-configurations' option\n");
522 wantUsage = true;
523 goto bail;
524 }
525 bundle.addPreferredConfigurations(argv[0]);
094e8965
JH
526 } else if (strcmp(cp, "-rename-manifest-package") == 0) {
527 argc--;
528 argv++;
529 if (!argc) {
530 fprintf(stderr, "ERROR: No argument supplied for '--rename-manifest-package' option\n");
531 wantUsage = true;
532 goto bail;
533 }
534 bundle.setManifestPackageNameOverride(argv[0]);
af945cf3
DH
535 } else if (strcmp(cp, "-rename-instrumentation-target-package") == 0) {
536 argc--;
537 argv++;
538 if (!argc) {
539 fprintf(stderr, "ERROR: No argument supplied for '--rename-instrumentation-target-package' option\n");
540 wantUsage = true;
541 goto bail;
542 }
543 bundle.setInstrumentationPackageNameOverride(argv[0]);
636d3b70
XD
544 } else if (strcmp(cp, "-auto-add-overlay") == 0) {
545 bundle.setAutoAddOverlay(true);
b8ea3a3f
EF
546 } else if (strcmp(cp, "-product") == 0) {
547 argc--;
548 argv++;
549 if (!argc) {
550 fprintf(stderr, "ERROR: No argument supplied for '--product' option\n");
551 wantUsage = true;
552 goto bail;
553 }
554 bundle.setProduct(argv[0]);
54f200b0
XD
555 } else if (strcmp(cp, "-non-constant-id") == 0) {
556 bundle.setNonConstantId(true);
dddb1fc7
JG
557 } else if (strcmp(cp, "-no-crunch") == 0) {
558 bundle.setUseCrunchCache(true);
559 }else {
e942a5c2
DH
560 fprintf(stderr, "ERROR: Unknown option '-%s'\n", cp);
561 wantUsage = true;
562 goto bail;
563 }
564 cp += strlen(cp) - 1;
565 break;
a534180c
TAOSP
566 default:
567 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp);
568 wantUsage = true;
569 goto bail;
570 }
571
572 cp++;
573 }
574 argc--;
575 argv++;
576 }
577
578 /*
579 * We're past the flags. The rest all goes straight in.
580 */
581 bundle.setFileSpec(argv, argc);
582
583 result = handleCommand(&bundle);
584
585bail:
586 if (wantUsage) {
587 usage();
588 result = 2;
589 }
590
591 //printf("--> returning %d\n", result);
592 return result;
593}