Main.cpp revision 681c4f45bf373f84c5b80a4c8dda871ae6fcfcbd
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] [--include-meta-data] 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 " --include-meta-data\n" 142 " when used with \"dump badging\" also includes meta-data tags.\n" 143 " --min-sdk-version\n" 144 " inserts android:minSdkVersion in to manifest. If the version is 7 or\n" 145 " higher, the default encoding for resources will be in UTF-8.\n" 146 " --target-sdk-version\n" 147 " inserts android:targetSdkVersion in to manifest.\n" 148 " --max-res-version\n" 149 " ignores versioned resource directories above the given value.\n" 150 " --values\n" 151 " when used with \"dump resources\" also includes resource values.\n" 152 " --version-code\n" 153 " inserts android:versionCode in to manifest.\n" 154 " --version-name\n" 155 " inserts android:versionName in to manifest.\n" 156 " --custom-package\n" 157 " generates R.java into a different package.\n" 158 " --extra-packages\n" 159 " generate R.java for libraries. Separate libraries with ':'.\n" 160 " --generate-dependencies\n" 161 " generate dependency files in the same directories for R.java and resource package\n" 162 " --auto-add-overlay\n" 163 " Automatically add resources that are only in overlays.\n" 164 " --preferred-configurations\n" 165 " Like the -c option for filtering out unneeded configurations, but\n" 166 " only expresses a preference. If there is no resource available with\n" 167 " the preferred configuration then it will not be stripped.\n" 168 " --rename-manifest-package\n" 169 " Rewrite the manifest so that its package name is the package name\n" 170 " given here. Relative class names (for example .Foo) will be\n" 171 " changed to absolute names with the old package so that the code\n" 172 " does not need to change.\n" 173 " --rename-instrumentation-target-package\n" 174 " Rewrite the manifest so that all of its instrumentation\n" 175 " components target the given package. Useful when used in\n" 176 " conjunction with --rename-manifest-package to fix tests against\n" 177 " a package that has been renamed.\n" 178 " --product\n" 179 " Specifies which variant to choose for strings that have\n" 180 " product variants\n" 181 " --utf16\n" 182 " changes default encoding for resources to UTF-16. Only useful when API\n" 183 " level is set to 7 or higher where the default encoding is UTF-8.\n" 184 " --non-constant-id\n" 185 " Make the resources ID non constant. This is required to make an R java class\n" 186 " that does not contain the final value but is used to make reusable compiled\n" 187 " libraries that need to access resources.\n" 188 " --error-on-failed-insert\n" 189 " Forces aapt to return an error if it fails to insert values into the manifest\n" 190 " with --debug-mode, --min-sdk-version, --target-sdk-version --version-code\n" 191 " and --version-name.\n" 192 " Insertion typically fails if the manifest already defines the attribute.\n" 193 " --output-text-symbols\n" 194 " Generates a text file containing the resource symbols of the R class in the\n" 195 " specified folder.\n" 196 " --ignore-assets\n" 197 " Assets to be ignored. Default pattern is:\n" 198 " %s\n", 199 gDefaultIgnoreAssets); 200} 201 202/* 203 * Dispatch the command. 204 */ 205int handleCommand(Bundle* bundle) 206{ 207 //printf("--- command %d (verbose=%d force=%d):\n", 208 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce()); 209 //for (int i = 0; i < bundle->getFileSpecCount(); i++) 210 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i)); 211 212 switch (bundle->getCommand()) { 213 case kCommandVersion: return doVersion(bundle); 214 case kCommandList: return doList(bundle); 215 case kCommandDump: return doDump(bundle); 216 case kCommandAdd: return doAdd(bundle); 217 case kCommandRemove: return doRemove(bundle); 218 case kCommandPackage: return doPackage(bundle); 219 case kCommandCrunch: return doCrunch(bundle); 220 case kCommandSingleCrunch: return doSingleCrunch(bundle); 221 default: 222 fprintf(stderr, "%s: requested command not yet supported\n", gProgName); 223 return 1; 224 } 225} 226 227/* 228 * Parse args. 229 */ 230int main(int argc, char* const argv[]) 231{ 232 char *prog = argv[0]; 233 Bundle bundle; 234 bool wantUsage = false; 235 int result = 1; // pessimistically assume an error. 236 int tolerance = 0; 237 238 /* default to compression */ 239 bundle.setCompressionMethod(ZipEntry::kCompressDeflated); 240 241 if (argc < 2) { 242 wantUsage = true; 243 goto bail; 244 } 245 246 if (argv[1][0] == 'v') 247 bundle.setCommand(kCommandVersion); 248 else if (argv[1][0] == 'd') 249 bundle.setCommand(kCommandDump); 250 else if (argv[1][0] == 'l') 251 bundle.setCommand(kCommandList); 252 else if (argv[1][0] == 'a') 253 bundle.setCommand(kCommandAdd); 254 else if (argv[1][0] == 'r') 255 bundle.setCommand(kCommandRemove); 256 else if (argv[1][0] == 'p') 257 bundle.setCommand(kCommandPackage); 258 else if (argv[1][0] == 'c') 259 bundle.setCommand(kCommandCrunch); 260 else if (argv[1][0] == 's') 261 bundle.setCommand(kCommandSingleCrunch); 262 else { 263 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]); 264 wantUsage = true; 265 goto bail; 266 } 267 argc -= 2; 268 argv += 2; 269 270 /* 271 * Pull out flags. We support "-fv" and "-f -v". 272 */ 273 while (argc && argv[0][0] == '-') { 274 /* flag(s) found */ 275 const char* cp = argv[0] +1; 276 277 while (*cp != '\0') { 278 switch (*cp) { 279 case 'v': 280 bundle.setVerbose(true); 281 break; 282 case 'a': 283 bundle.setAndroidList(true); 284 break; 285 case 'c': 286 argc--; 287 argv++; 288 if (!argc) { 289 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n"); 290 wantUsage = true; 291 goto bail; 292 } 293 bundle.addConfigurations(argv[0]); 294 break; 295 case 'f': 296 bundle.setForce(true); 297 break; 298 case 'g': 299 argc--; 300 argv++; 301 if (!argc) { 302 fprintf(stderr, "ERROR: No argument supplied for '-g' option\n"); 303 wantUsage = true; 304 goto bail; 305 } 306 tolerance = atoi(argv[0]); 307 bundle.setGrayscaleTolerance(tolerance); 308 printf("%s: Images with deviation <= %d will be forced to grayscale.\n", prog, tolerance); 309 break; 310 case 'k': 311 bundle.setJunkPath(true); 312 break; 313 case 'm': 314 bundle.setMakePackageDirs(true); 315 break; 316#if 0 317 case 'p': 318 bundle.setPseudolocalize(true); 319 break; 320#endif 321 case 'u': 322 bundle.setUpdate(true); 323 break; 324 case 'x': 325 bundle.setExtending(true); 326 break; 327 case 'z': 328 bundle.setRequireLocalization(true); 329 break; 330 case 'j': 331 argc--; 332 argv++; 333 if (!argc) { 334 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n"); 335 wantUsage = true; 336 goto bail; 337 } 338 convertPath(argv[0]); 339 bundle.addJarFile(argv[0]); 340 break; 341 case 'A': 342 argc--; 343 argv++; 344 if (!argc) { 345 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n"); 346 wantUsage = true; 347 goto bail; 348 } 349 convertPath(argv[0]); 350 bundle.setAssetSourceDir(argv[0]); 351 break; 352 case 'G': 353 argc--; 354 argv++; 355 if (!argc) { 356 fprintf(stderr, "ERROR: No argument supplied for '-G' option\n"); 357 wantUsage = true; 358 goto bail; 359 } 360 convertPath(argv[0]); 361 bundle.setProguardFile(argv[0]); 362 break; 363 case 'I': 364 argc--; 365 argv++; 366 if (!argc) { 367 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n"); 368 wantUsage = true; 369 goto bail; 370 } 371 convertPath(argv[0]); 372 bundle.addPackageInclude(argv[0]); 373 break; 374 case 'F': 375 argc--; 376 argv++; 377 if (!argc) { 378 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n"); 379 wantUsage = true; 380 goto bail; 381 } 382 convertPath(argv[0]); 383 bundle.setOutputAPKFile(argv[0]); 384 break; 385 case 'J': 386 argc--; 387 argv++; 388 if (!argc) { 389 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n"); 390 wantUsage = true; 391 goto bail; 392 } 393 convertPath(argv[0]); 394 bundle.setRClassDir(argv[0]); 395 break; 396 case 'M': 397 argc--; 398 argv++; 399 if (!argc) { 400 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n"); 401 wantUsage = true; 402 goto bail; 403 } 404 convertPath(argv[0]); 405 bundle.setAndroidManifestFile(argv[0]); 406 break; 407 case 'P': 408 argc--; 409 argv++; 410 if (!argc) { 411 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n"); 412 wantUsage = true; 413 goto bail; 414 } 415 convertPath(argv[0]); 416 bundle.setPublicOutputFile(argv[0]); 417 break; 418 case 'S': 419 argc--; 420 argv++; 421 if (!argc) { 422 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n"); 423 wantUsage = true; 424 goto bail; 425 } 426 convertPath(argv[0]); 427 bundle.addResourceSourceDir(argv[0]); 428 break; 429 case 'C': 430 argc--; 431 argv++; 432 if (!argc) { 433 fprintf(stderr, "ERROR: No argument supplied for '-C' option\n"); 434 wantUsage = true; 435 goto bail; 436 } 437 convertPath(argv[0]); 438 bundle.setCrunchedOutputDir(argv[0]); 439 break; 440 case 'i': 441 argc--; 442 argv++; 443 if (!argc) { 444 fprintf(stderr, "ERROR: No argument supplied for '-i' option\n"); 445 wantUsage = true; 446 goto bail; 447 } 448 convertPath(argv[0]); 449 bundle.setSingleCrunchInputFile(argv[0]); 450 break; 451 case 'o': 452 argc--; 453 argv++; 454 if (!argc) { 455 fprintf(stderr, "ERROR: No argument supplied for '-o' option\n"); 456 wantUsage = true; 457 goto bail; 458 } 459 convertPath(argv[0]); 460 bundle.setSingleCrunchOutputFile(argv[0]); 461 break; 462 case '0': 463 argc--; 464 argv++; 465 if (!argc) { 466 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n"); 467 wantUsage = true; 468 goto bail; 469 } 470 if (argv[0][0] != 0) { 471 bundle.addNoCompressExtension(argv[0]); 472 } else { 473 bundle.setCompressionMethod(ZipEntry::kCompressStored); 474 } 475 break; 476 case '-': 477 if (strcmp(cp, "-debug-mode") == 0) { 478 bundle.setDebugMode(true); 479 } else if (strcmp(cp, "-min-sdk-version") == 0) { 480 argc--; 481 argv++; 482 if (!argc) { 483 fprintf(stderr, "ERROR: No argument supplied for '--min-sdk-version' option\n"); 484 wantUsage = true; 485 goto bail; 486 } 487 bundle.setMinSdkVersion(argv[0]); 488 } else if (strcmp(cp, "-target-sdk-version") == 0) { 489 argc--; 490 argv++; 491 if (!argc) { 492 fprintf(stderr, "ERROR: No argument supplied for '--target-sdk-version' option\n"); 493 wantUsage = true; 494 goto bail; 495 } 496 bundle.setTargetSdkVersion(argv[0]); 497 } else if (strcmp(cp, "-max-sdk-version") == 0) { 498 argc--; 499 argv++; 500 if (!argc) { 501 fprintf(stderr, "ERROR: No argument supplied for '--max-sdk-version' option\n"); 502 wantUsage = true; 503 goto bail; 504 } 505 bundle.setMaxSdkVersion(argv[0]); 506 } else if (strcmp(cp, "-max-res-version") == 0) { 507 argc--; 508 argv++; 509 if (!argc) { 510 fprintf(stderr, "ERROR: No argument supplied for '--max-res-version' option\n"); 511 wantUsage = true; 512 goto bail; 513 } 514 bundle.setMaxResVersion(argv[0]); 515 } else if (strcmp(cp, "-version-code") == 0) { 516 argc--; 517 argv++; 518 if (!argc) { 519 fprintf(stderr, "ERROR: No argument supplied for '--version-code' option\n"); 520 wantUsage = true; 521 goto bail; 522 } 523 bundle.setVersionCode(argv[0]); 524 } else if (strcmp(cp, "-version-name") == 0) { 525 argc--; 526 argv++; 527 if (!argc) { 528 fprintf(stderr, "ERROR: No argument supplied for '--version-name' option\n"); 529 wantUsage = true; 530 goto bail; 531 } 532 bundle.setVersionName(argv[0]); 533 } else if (strcmp(cp, "-values") == 0) { 534 bundle.setValues(true); 535 } else if (strcmp(cp, "-include-meta-data") == 0) { 536 bundle.setIncludeMetaData(true); 537 } else if (strcmp(cp, "-custom-package") == 0) { 538 argc--; 539 argv++; 540 if (!argc) { 541 fprintf(stderr, "ERROR: No argument supplied for '--custom-package' option\n"); 542 wantUsage = true; 543 goto bail; 544 } 545 bundle.setCustomPackage(argv[0]); 546 } else if (strcmp(cp, "-extra-packages") == 0) { 547 argc--; 548 argv++; 549 if (!argc) { 550 fprintf(stderr, "ERROR: No argument supplied for '--extra-packages' option\n"); 551 wantUsage = true; 552 goto bail; 553 } 554 bundle.setExtraPackages(argv[0]); 555 } else if (strcmp(cp, "-generate-dependencies") == 0) { 556 bundle.setGenDependencies(true); 557 } else if (strcmp(cp, "-utf16") == 0) { 558 bundle.setWantUTF16(true); 559 } else if (strcmp(cp, "-preferred-configurations") == 0) { 560 argc--; 561 argv++; 562 if (!argc) { 563 fprintf(stderr, "ERROR: No argument supplied for '--preferred-configurations' option\n"); 564 wantUsage = true; 565 goto bail; 566 } 567 bundle.addPreferredConfigurations(argv[0]); 568 } else if (strcmp(cp, "-rename-manifest-package") == 0) { 569 argc--; 570 argv++; 571 if (!argc) { 572 fprintf(stderr, "ERROR: No argument supplied for '--rename-manifest-package' option\n"); 573 wantUsage = true; 574 goto bail; 575 } 576 bundle.setManifestPackageNameOverride(argv[0]); 577 } else if (strcmp(cp, "-rename-instrumentation-target-package") == 0) { 578 argc--; 579 argv++; 580 if (!argc) { 581 fprintf(stderr, "ERROR: No argument supplied for '--rename-instrumentation-target-package' option\n"); 582 wantUsage = true; 583 goto bail; 584 } 585 bundle.setInstrumentationPackageNameOverride(argv[0]); 586 } else if (strcmp(cp, "-auto-add-overlay") == 0) { 587 bundle.setAutoAddOverlay(true); 588 } else if (strcmp(cp, "-error-on-failed-insert") == 0) { 589 bundle.setErrorOnFailedInsert(true); 590 } else if (strcmp(cp, "-output-text-symbols") == 0) { 591 argc--; 592 argv++; 593 if (!argc) { 594 fprintf(stderr, "ERROR: No argument supplied for '-output-text-symbols' option\n"); 595 wantUsage = true; 596 goto bail; 597 } 598 bundle.setOutputTextSymbols(argv[0]); 599 } else if (strcmp(cp, "-product") == 0) { 600 argc--; 601 argv++; 602 if (!argc) { 603 fprintf(stderr, "ERROR: No argument supplied for '--product' option\n"); 604 wantUsage = true; 605 goto bail; 606 } 607 bundle.setProduct(argv[0]); 608 } else if (strcmp(cp, "-non-constant-id") == 0) { 609 bundle.setNonConstantId(true); 610 } else if (strcmp(cp, "-no-crunch") == 0) { 611 bundle.setUseCrunchCache(true); 612 } else if (strcmp(cp, "-ignore-assets") == 0) { 613 argc--; 614 argv++; 615 if (!argc) { 616 fprintf(stderr, "ERROR: No argument supplied for '--ignore-assets' option\n"); 617 wantUsage = true; 618 goto bail; 619 } 620 gUserIgnoreAssets = argv[0]; 621 } else { 622 fprintf(stderr, "ERROR: Unknown option '-%s'\n", cp); 623 wantUsage = true; 624 goto bail; 625 } 626 cp += strlen(cp) - 1; 627 break; 628 default: 629 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp); 630 wantUsage = true; 631 goto bail; 632 } 633 634 cp++; 635 } 636 argc--; 637 argv++; 638 } 639 640 /* 641 * We're past the flags. The rest all goes straight in. 642 */ 643 bundle.setFileSpec(argv, argc); 644 645 result = handleCommand(&bundle); 646 647bail: 648 if (wantUsage) { 649 usage(); 650 result = 2; 651 } 652 653 //printf("--> returning %d\n", result); 654 return result; 655} 656