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