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