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