Main.cpp revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
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.h> 10#include <utils/ZipFile.h> 11 12#include <stdlib.h> 13#include <getopt.h> 14#include <assert.h> 15 16using namespace android; 17 18static const char* gProgName = "aapt"; 19 20/* 21 * When running under Cygwin on Windows, this will convert slash-based 22 * paths into back-slash-based ones. Otherwise the ApptAssets file comparisons 23 * fail later as they use back-slash separators under Windows. 24 * 25 * This operates in-place on the path string. 26 */ 27void convertPath(char *path) { 28 if (path != NULL && OS_PATH_SEPARATOR != '/') { 29 for (; *path; path++) { 30 if (*path == '/') { 31 *path = OS_PATH_SEPARATOR; 32 } 33 } 34 } 35} 36 37/* 38 * Print usage info. 39 */ 40void usage(void) 41{ 42 fprintf(stderr, "Android Asset Packaging Tool\n\n"); 43 fprintf(stderr, "Usage:\n"); 44 fprintf(stderr, 45 " %s l[ist] [-v] [-a] file.{zip,jar,apk}\n" 46 " List contents of Zip-compatible archive.\n\n", gProgName); 47 fprintf(stderr, 48 " %s d[ump] WHAT file.{apk} [asset [asset ...]]\n" 49 " badging Print the label and icon for the app declared in APK.\n" 50 " permissions Print the permissions from the APK.\n" 51 " resources Print the resource table from the APK.\n" 52 " configurations Print the configurations in the APK.\n" 53 " xmltree Print the compiled xmls in the given assets.\n" 54 " xmlstrings Print the strings of the given compiled xml assets.\n\n", gProgName); 55 fprintf(stderr, 56 " %s p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \\\n" 57 " [-0 extension [-0 extension ...]] \\\n" 58 " [-g tolerance] \\\n" 59 " [-j jarfile] \\\n" 60 " [-I base-package [-I base-package ...]] \\\n" 61 " [-A asset-source-dir] [-P public-definitions-file] \\\n" 62 " [-S resource-sources [-S resource-sources ...]] " 63 " [-F apk-file] [-J R-file-dir] \\\n" 64 " [raw-files-dir [raw-files-dir] ...]\n" 65 "\n" 66 " Package the android resources. It will read assets and resources that are\n" 67 " supplied with the -M -A -S or raw-files-dir arguments. The -J -P -F and -R\n" 68 " options control which files are output.\n\n" 69 , gProgName); 70 fprintf(stderr, 71 " %s r[emove] [-v] file.{zip,jar,apk} file1 [file2 ...]\n" 72 " Delete specified files from Zip-compatible archive.\n\n", 73 gProgName); 74 fprintf(stderr, 75 " %s a[dd] [-v] file.{zip,jar,apk} file1 [file2 ...]\n" 76 " Add specified files to Zip-compatible archive.\n\n", gProgName); 77 fprintf(stderr, 78 " %s v[ersion]\n" 79 " Print program version.\n\n", gProgName); 80 fprintf(stderr, 81 " Modifiers:\n" 82 " -a print Android-specific data (resources, manifest) when listing\n" 83 " -c specify which configurations to include. The default is all\n" 84 " configurations. The value of the parameter should be a comma\n" 85 " separated list of configuration values. Locales should be specified\n" 86 " as either a language or language-region pair. Some examples:\n" 87 " en\n" 88 " port,en\n" 89 " port,land,en_US\n" 90 " If you put the special locale, zz_ZZ on the list, it will perform\n" 91 " pseudolocalization on the default locale, modifying all of the\n" 92 " strings so you can look for strings that missed the\n" 93 " internationalization process. For example:\n" 94 " port,land,zz_ZZ\n" 95 " -d one or more device assets to include, separated by commas\n" 96 " -f force overwrite of existing files\n" 97 " -g specify a pixel tolerance to force images to grayscale, default 0\n" 98 " -j specify a jar or zip file containing classes to include\n" 99 " -m make package directories under location specified by -J\n" 100#if 0 101 " -p pseudolocalize the default configuration\n" 102#endif 103 " -u update existing packages (add new, replace older, remove deleted files)\n" 104 " -v verbose output\n" 105 " -x create extending (non-application) resource IDs\n" 106 " -z require localization of resource attributes marked with\n" 107 " localization=\"suggested\"\n" 108 " -A additional directory in which to find raw asset files\n" 109 " -F specify the apk file to output\n" 110 " -I add an existing package to base include set\n" 111 " -J specify where to output R.java resource constant definitions\n" 112 " -M specify full path to AndroidManifest.xml to include in zip\n" 113 " -P specify where to output public resource definitions\n" 114 " -S directory in which to find resources. Multiple directories will be scanned" 115 " and the first match found (left to right) will take precedence." 116 " -0 specifies an additional extension for which such files will not\n" 117 " be stored compressed in the .apk. An empty string means to not\n" 118 " compress any files at all.\n"); 119} 120 121/* 122 * Dispatch the command. 123 */ 124int handleCommand(Bundle* bundle) 125{ 126 //printf("--- command %d (verbose=%d force=%d):\n", 127 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce()); 128 //for (int i = 0; i < bundle->getFileSpecCount(); i++) 129 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i)); 130 131 switch (bundle->getCommand()) { 132 case kCommandVersion: return doVersion(bundle); 133 case kCommandList: return doList(bundle); 134 case kCommandDump: return doDump(bundle); 135 case kCommandAdd: return doAdd(bundle); 136 case kCommandRemove: return doRemove(bundle); 137 case kCommandPackage: return doPackage(bundle); 138 default: 139 fprintf(stderr, "%s: requested command not yet supported\n", gProgName); 140 return 1; 141 } 142} 143 144/* 145 * Parse args. 146 */ 147int main(int argc, char* const argv[]) 148{ 149 char *prog = argv[0]; 150 Bundle bundle; 151 bool wantUsage = false; 152 int result = 1; // pessimistically assume an error. 153 int tolerance = 0; 154 155 /* default to compression */ 156 bundle.setCompressionMethod(ZipEntry::kCompressDeflated); 157 158 if (argc < 2) { 159 wantUsage = true; 160 goto bail; 161 } 162 163 if (argv[1][0] == 'v') 164 bundle.setCommand(kCommandVersion); 165 else if (argv[1][0] == 'd') 166 bundle.setCommand(kCommandDump); 167 else if (argv[1][0] == 'l') 168 bundle.setCommand(kCommandList); 169 else if (argv[1][0] == 'a') 170 bundle.setCommand(kCommandAdd); 171 else if (argv[1][0] == 'r') 172 bundle.setCommand(kCommandRemove); 173 else if (argv[1][0] == 'p') 174 bundle.setCommand(kCommandPackage); 175 else { 176 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]); 177 wantUsage = true; 178 goto bail; 179 } 180 argc -= 2; 181 argv += 2; 182 183 /* 184 * Pull out flags. We support "-fv" and "-f -v". 185 */ 186 while (argc && argv[0][0] == '-') { 187 /* flag(s) found */ 188 const char* cp = argv[0] +1; 189 190 while (*cp != '\0') { 191 switch (*cp) { 192 case 'v': 193 bundle.setVerbose(true); 194 break; 195 case 'a': 196 bundle.setAndroidList(true); 197 break; 198 case 'c': 199 argc--; 200 argv++; 201 if (!argc) { 202 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n"); 203 wantUsage = true; 204 goto bail; 205 } 206 bundle.addConfigurations(argv[0]); 207 break; 208 case 'f': 209 bundle.setForce(true); 210 break; 211 case 'g': 212 argc--; 213 argv++; 214 if (!argc) { 215 fprintf(stderr, "ERROR: No argument supplied for '-g' option\n"); 216 wantUsage = true; 217 goto bail; 218 } 219 tolerance = atoi(argv[0]); 220 bundle.setGrayscaleTolerance(tolerance); 221 printf("%s: Images with deviation <= %d will be forced to grayscale.\n", prog, tolerance); 222 break; 223 case 'm': 224 bundle.setMakePackageDirs(true); 225 break; 226#if 0 227 case 'p': 228 bundle.setPseudolocalize(true); 229 break; 230#endif 231 case 'u': 232 bundle.setUpdate(true); 233 break; 234 case 'x': 235 bundle.setExtending(true); 236 break; 237 case 'z': 238 bundle.setRequireLocalization(true); 239 break; 240 case 'j': 241 argc--; 242 argv++; 243 if (!argc) { 244 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n"); 245 wantUsage = true; 246 goto bail; 247 } 248 convertPath(argv[0]); 249 bundle.addJarFile(argv[0]); 250 break; 251 case 'A': 252 argc--; 253 argv++; 254 if (!argc) { 255 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n"); 256 wantUsage = true; 257 goto bail; 258 } 259 convertPath(argv[0]); 260 bundle.setAssetSourceDir(argv[0]); 261 break; 262 case 'I': 263 argc--; 264 argv++; 265 if (!argc) { 266 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n"); 267 wantUsage = true; 268 goto bail; 269 } 270 convertPath(argv[0]); 271 bundle.addPackageInclude(argv[0]); 272 break; 273 case 'F': 274 argc--; 275 argv++; 276 if (!argc) { 277 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n"); 278 wantUsage = true; 279 goto bail; 280 } 281 convertPath(argv[0]); 282 bundle.setOutputAPKFile(argv[0]); 283 break; 284 case 'J': 285 argc--; 286 argv++; 287 if (!argc) { 288 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n"); 289 wantUsage = true; 290 goto bail; 291 } 292 convertPath(argv[0]); 293 bundle.setRClassDir(argv[0]); 294 break; 295 case 'M': 296 argc--; 297 argv++; 298 if (!argc) { 299 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n"); 300 wantUsage = true; 301 goto bail; 302 } 303 convertPath(argv[0]); 304 bundle.setAndroidManifestFile(argv[0]); 305 break; 306 case 'P': 307 argc--; 308 argv++; 309 if (!argc) { 310 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n"); 311 wantUsage = true; 312 goto bail; 313 } 314 convertPath(argv[0]); 315 bundle.setPublicOutputFile(argv[0]); 316 break; 317 case 'S': 318 argc--; 319 argv++; 320 if (!argc) { 321 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n"); 322 wantUsage = true; 323 goto bail; 324 } 325 convertPath(argv[0]); 326 bundle.addResourceSourceDir(argv[0]); 327 break; 328 case '0': 329 argc--; 330 argv++; 331 if (!argc) { 332 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n"); 333 wantUsage = true; 334 goto bail; 335 } 336 if (argv[0][0] != 0) { 337 bundle.addNoCompressExtension(argv[0]); 338 } else { 339 bundle.setCompressionMethod(ZipEntry::kCompressStored); 340 } 341 break; 342 default: 343 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp); 344 wantUsage = true; 345 goto bail; 346 } 347 348 cp++; 349 } 350 argc--; 351 argv++; 352 } 353 354 /* 355 * We're past the flags. The rest all goes straight in. 356 */ 357 bundle.setFileSpec(argv, argc); 358 359 result = handleCommand(&bundle); 360 361bail: 362 if (wantUsage) { 363 usage(); 364 result = 2; 365 } 366 367 //printf("--> returning %d\n", result); 368 return result; 369} 370