Main.cpp revision 1553c82e084ac5763fb393857224145506771b99
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 " [--min-sdk-version VAL] [--target-sdk-version VAL] \\\n" 61 " [--max-sdk-version VAL] [--app-version VAL] \\\n" 62 " [--app-version-name TEXT]\\\n" 63 " [-I base-package [-I base-package ...]] \\\n" 64 " [-A asset-source-dir] [-G class-list-file] [-P public-definitions-file] \\\n" 65 " [-S resource-sources [-S resource-sources ...]] " 66 " [-F apk-file] [-J R-file-dir] \\\n" 67 " [raw-files-dir [raw-files-dir] ...]\n" 68 "\n" 69 " Package the android resources. It will read assets and resources that are\n" 70 " supplied with the -M -A -S or raw-files-dir arguments. The -J -P -F and -R\n" 71 " options control which files are output.\n\n" 72 , gProgName); 73 fprintf(stderr, 74 " %s r[emove] [-v] file.{zip,jar,apk} file1 [file2 ...]\n" 75 " Delete specified files from Zip-compatible archive.\n\n", 76 gProgName); 77 fprintf(stderr, 78 " %s a[dd] [-v] file.{zip,jar,apk} file1 [file2 ...]\n" 79 " Add specified files to Zip-compatible archive.\n\n", gProgName); 80 fprintf(stderr, 81 " %s v[ersion]\n" 82 " Print program version.\n\n", gProgName); 83 fprintf(stderr, 84 " Modifiers:\n" 85 " -a print Android-specific data (resources, manifest) when listing\n" 86 " -c specify which configurations to include. The default is all\n" 87 " configurations. The value of the parameter should be a comma\n" 88 " separated list of configuration values. Locales should be specified\n" 89 " as either a language or language-region pair. Some examples:\n" 90 " en\n" 91 " port,en\n" 92 " port,land,en_US\n" 93 " If you put the special locale, zz_ZZ on the list, it will perform\n" 94 " pseudolocalization on the default locale, modifying all of the\n" 95 " strings so you can look for strings that missed the\n" 96 " internationalization process. For example:\n" 97 " port,land,zz_ZZ\n" 98 " -d one or more device assets to include, separated by commas\n" 99 " -f force overwrite of existing files\n" 100 " -g specify a pixel tolerance to force images to grayscale, default 0\n" 101 " -j specify a jar or zip file containing classes to include\n" 102 " -m make package directories under location specified by -J\n" 103#if 0 104 " -p pseudolocalize the default configuration\n" 105#endif 106 " -u update existing packages (add new, replace older, remove deleted files)\n" 107 " -v verbose output\n" 108 " -x create extending (non-application) resource IDs\n" 109 " -z require localization of resource attributes marked with\n" 110 " localization=\"suggested\"\n" 111 " -A additional directory in which to find raw asset files\n" 112 " -G A file to output proguard options into.\n" 113 " -F specify the apk file to output\n" 114 " -I add an existing package to base include set\n" 115 " -J specify where to output R.java resource constant definitions\n" 116 " -M specify full path to AndroidManifest.xml to include in zip\n" 117 " -P specify where to output public resource definitions\n" 118 " -S directory in which to find resources. Multiple directories will be scanned" 119 " and the first match found (left to right) will take precedence." 120 " -0 specifies an additional extension for which such files will not\n" 121 " be stored compressed in the .apk. An empty string means to not\n" 122 " compress any files at all.\n" 123 " --min-sdk-version\n" 124 " inserts android:minSdkVersion in to manifest.\n" 125 " --target-sdk-version\n" 126 " inserts android:targetSdkVersion in to manifest.\n" 127 " --max-sdk-version\n" 128 " inserts android:maxSdkVersion in to manifest.\n" 129 " --values\n" 130 " when used with \"dump resources\" also includes resource values.\n" 131 " --version-code\n" 132 " inserts android:versionCode in to manifest.\n" 133 " --version-name\n" 134 " inserts android:versionName in to manifest.\n"); 135} 136 137/* 138 * Dispatch the command. 139 */ 140int handleCommand(Bundle* bundle) 141{ 142 //printf("--- command %d (verbose=%d force=%d):\n", 143 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce()); 144 //for (int i = 0; i < bundle->getFileSpecCount(); i++) 145 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i)); 146 147 switch (bundle->getCommand()) { 148 case kCommandVersion: return doVersion(bundle); 149 case kCommandList: return doList(bundle); 150 case kCommandDump: return doDump(bundle); 151 case kCommandAdd: return doAdd(bundle); 152 case kCommandRemove: return doRemove(bundle); 153 case kCommandPackage: return doPackage(bundle); 154 default: 155 fprintf(stderr, "%s: requested command not yet supported\n", gProgName); 156 return 1; 157 } 158} 159 160/* 161 * Parse args. 162 */ 163int main(int argc, char* const argv[]) 164{ 165 char *prog = argv[0]; 166 Bundle bundle; 167 bool wantUsage = false; 168 int result = 1; // pessimistically assume an error. 169 int tolerance = 0; 170 171 /* default to compression */ 172 bundle.setCompressionMethod(ZipEntry::kCompressDeflated); 173 174 if (argc < 2) { 175 wantUsage = true; 176 goto bail; 177 } 178 179 if (argv[1][0] == 'v') 180 bundle.setCommand(kCommandVersion); 181 else if (argv[1][0] == 'd') 182 bundle.setCommand(kCommandDump); 183 else if (argv[1][0] == 'l') 184 bundle.setCommand(kCommandList); 185 else if (argv[1][0] == 'a') 186 bundle.setCommand(kCommandAdd); 187 else if (argv[1][0] == 'r') 188 bundle.setCommand(kCommandRemove); 189 else if (argv[1][0] == 'p') 190 bundle.setCommand(kCommandPackage); 191 else { 192 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]); 193 wantUsage = true; 194 goto bail; 195 } 196 argc -= 2; 197 argv += 2; 198 199 /* 200 * Pull out flags. We support "-fv" and "-f -v". 201 */ 202 while (argc && argv[0][0] == '-') { 203 /* flag(s) found */ 204 const char* cp = argv[0] +1; 205 206 while (*cp != '\0') { 207 switch (*cp) { 208 case 'v': 209 bundle.setVerbose(true); 210 break; 211 case 'a': 212 bundle.setAndroidList(true); 213 break; 214 case 'c': 215 argc--; 216 argv++; 217 if (!argc) { 218 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n"); 219 wantUsage = true; 220 goto bail; 221 } 222 bundle.addConfigurations(argv[0]); 223 break; 224 case 'f': 225 bundle.setForce(true); 226 break; 227 case 'g': 228 argc--; 229 argv++; 230 if (!argc) { 231 fprintf(stderr, "ERROR: No argument supplied for '-g' option\n"); 232 wantUsage = true; 233 goto bail; 234 } 235 tolerance = atoi(argv[0]); 236 bundle.setGrayscaleTolerance(tolerance); 237 printf("%s: Images with deviation <= %d will be forced to grayscale.\n", prog, tolerance); 238 break; 239 case 'm': 240 bundle.setMakePackageDirs(true); 241 break; 242#if 0 243 case 'p': 244 bundle.setPseudolocalize(true); 245 break; 246#endif 247 case 'u': 248 bundle.setUpdate(true); 249 break; 250 case 'x': 251 bundle.setExtending(true); 252 break; 253 case 'z': 254 bundle.setRequireLocalization(true); 255 break; 256 case 'j': 257 argc--; 258 argv++; 259 if (!argc) { 260 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n"); 261 wantUsage = true; 262 goto bail; 263 } 264 convertPath(argv[0]); 265 bundle.addJarFile(argv[0]); 266 break; 267 case 'A': 268 argc--; 269 argv++; 270 if (!argc) { 271 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n"); 272 wantUsage = true; 273 goto bail; 274 } 275 convertPath(argv[0]); 276 bundle.setAssetSourceDir(argv[0]); 277 break; 278 case 'G': 279 argc--; 280 argv++; 281 if (!argc) { 282 fprintf(stderr, "ERROR: No argument supplied for '-G' option\n"); 283 wantUsage = true; 284 goto bail; 285 } 286 convertPath(argv[0]); 287 bundle.setProguardFile(argv[0]); 288 break; 289 case 'I': 290 argc--; 291 argv++; 292 if (!argc) { 293 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n"); 294 wantUsage = true; 295 goto bail; 296 } 297 convertPath(argv[0]); 298 bundle.addPackageInclude(argv[0]); 299 break; 300 case 'F': 301 argc--; 302 argv++; 303 if (!argc) { 304 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n"); 305 wantUsage = true; 306 goto bail; 307 } 308 convertPath(argv[0]); 309 bundle.setOutputAPKFile(argv[0]); 310 break; 311 case 'J': 312 argc--; 313 argv++; 314 if (!argc) { 315 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n"); 316 wantUsage = true; 317 goto bail; 318 } 319 convertPath(argv[0]); 320 bundle.setRClassDir(argv[0]); 321 break; 322 case 'M': 323 argc--; 324 argv++; 325 if (!argc) { 326 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n"); 327 wantUsage = true; 328 goto bail; 329 } 330 convertPath(argv[0]); 331 bundle.setAndroidManifestFile(argv[0]); 332 break; 333 case 'P': 334 argc--; 335 argv++; 336 if (!argc) { 337 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n"); 338 wantUsage = true; 339 goto bail; 340 } 341 convertPath(argv[0]); 342 bundle.setPublicOutputFile(argv[0]); 343 break; 344 case 'S': 345 argc--; 346 argv++; 347 if (!argc) { 348 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n"); 349 wantUsage = true; 350 goto bail; 351 } 352 convertPath(argv[0]); 353 bundle.addResourceSourceDir(argv[0]); 354 break; 355 case '0': 356 argc--; 357 argv++; 358 if (!argc) { 359 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n"); 360 wantUsage = true; 361 goto bail; 362 } 363 if (argv[0][0] != 0) { 364 bundle.addNoCompressExtension(argv[0]); 365 } else { 366 bundle.setCompressionMethod(ZipEntry::kCompressStored); 367 } 368 break; 369 case '-': 370 if (strcmp(cp, "-min-sdk-version") == 0) { 371 argc--; 372 argv++; 373 if (!argc) { 374 fprintf(stderr, "ERROR: No argument supplied for '--min-sdk-version' option\n"); 375 wantUsage = true; 376 goto bail; 377 } 378 bundle.setMinSdkVersion(argv[0]); 379 } else if (strcmp(cp, "-target-sdk-version") == 0) { 380 argc--; 381 argv++; 382 if (!argc) { 383 fprintf(stderr, "ERROR: No argument supplied for '--target-sdk-version' option\n"); 384 wantUsage = true; 385 goto bail; 386 } 387 bundle.setTargetSdkVersion(argv[0]); 388 } else if (strcmp(cp, "-max-sdk-version") == 0) { 389 argc--; 390 argv++; 391 if (!argc) { 392 fprintf(stderr, "ERROR: No argument supplied for '--max-sdk-version' option\n"); 393 wantUsage = true; 394 goto bail; 395 } 396 bundle.setMaxSdkVersion(argv[0]); 397 } else if (strcmp(cp, "-version-code") == 0) { 398 argc--; 399 argv++; 400 if (!argc) { 401 fprintf(stderr, "ERROR: No argument supplied for '--version-code' option\n"); 402 wantUsage = true; 403 goto bail; 404 } 405 bundle.setVersionCode(argv[0]); 406 } else if (strcmp(cp, "-version-name") == 0) { 407 argc--; 408 argv++; 409 if (!argc) { 410 fprintf(stderr, "ERROR: No argument supplied for '--version-name' option\n"); 411 wantUsage = true; 412 goto bail; 413 } 414 bundle.setVersionName(argv[0]); 415 } else if (strcmp(cp, "-values") == 0) { 416 bundle.setValues(true); 417 } else { 418 fprintf(stderr, "ERROR: Unknown option '-%s'\n", cp); 419 wantUsage = true; 420 goto bail; 421 } 422 cp += strlen(cp) - 1; 423 break; 424 default: 425 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp); 426 wantUsage = true; 427 goto bail; 428 } 429 430 cp++; 431 } 432 argc--; 433 argv++; 434 } 435 436 /* 437 * We're past the flags. The rest all goes straight in. 438 */ 439 bundle.setFileSpec(argv, argc); 440 441 result = handleCommand(&bundle); 442 443bail: 444 if (wantUsage) { 445 usage(); 446 result = 2; 447 } 448 449 //printf("--> returning %d\n", result); 450 return result; 451} 452