The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| 16 | using namespace android; |
| 17 | |
| 18 | static 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 | */ |
| 27 | void 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 | */ |
| 40 | void 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" |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 49 | " badging Print the label and icon for the app declared in APK.\n" |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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, |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 56 | " %s p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \\\n" |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 57 | " [-0 extension [-0 extension ...]] \\\n" |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 58 | " [-g tolerance] \\\n" |
| 59 | " [-j jarfile] \\\n" |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 | " [-I base-package [-I base-package ...]] \\\n" |
| 61 | " [-A asset-source-dir] [-P public-definitions-file] \\\n" |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 62 | " [-S resource-sources [-S resource-sources ...]] " |
| 63 | " [-F apk-file] [-J R-file-dir] \\\n" |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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" |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 97 | " -g specify a pixel tolerance to force images to grayscale, default 0\n" |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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" |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 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." |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 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"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Dispatch the command. |
| 123 | */ |
| 124 | int 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 | */ |
| 147 | int main(int argc, char* const argv[]) |
| 148 | { |
| 149 | Bundle bundle; |
| 150 | bool wantUsage = false; |
| 151 | int result = 1; // pessimistically assume an error. |
| 152 | |
| 153 | /* default to compression */ |
| 154 | bundle.setCompressionMethod(ZipEntry::kCompressDeflated); |
| 155 | |
| 156 | if (argc < 2) { |
| 157 | wantUsage = true; |
| 158 | goto bail; |
| 159 | } |
| 160 | |
| 161 | if (argv[1][0] == 'v') |
| 162 | bundle.setCommand(kCommandVersion); |
| 163 | else if (argv[1][0] == 'd') |
| 164 | bundle.setCommand(kCommandDump); |
| 165 | else if (argv[1][0] == 'l') |
| 166 | bundle.setCommand(kCommandList); |
| 167 | else if (argv[1][0] == 'a') |
| 168 | bundle.setCommand(kCommandAdd); |
| 169 | else if (argv[1][0] == 'r') |
| 170 | bundle.setCommand(kCommandRemove); |
| 171 | else if (argv[1][0] == 'p') |
| 172 | bundle.setCommand(kCommandPackage); |
| 173 | else { |
| 174 | fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]); |
| 175 | wantUsage = true; |
| 176 | goto bail; |
| 177 | } |
| 178 | argc -= 2; |
| 179 | argv += 2; |
| 180 | |
| 181 | /* |
| 182 | * Pull out flags. We support "-fv" and "-f -v". |
| 183 | */ |
| 184 | while (argc && argv[0][0] == '-') { |
| 185 | /* flag(s) found */ |
| 186 | const char* cp = argv[0] +1; |
| 187 | |
| 188 | while (*cp != '\0') { |
| 189 | switch (*cp) { |
| 190 | case 'v': |
| 191 | bundle.setVerbose(true); |
| 192 | break; |
| 193 | case 'a': |
| 194 | bundle.setAndroidList(true); |
| 195 | break; |
| 196 | case 'c': |
| 197 | argc--; |
| 198 | argv++; |
| 199 | if (!argc) { |
| 200 | fprintf(stderr, "ERROR: No argument supplied for '-c' option\n"); |
| 201 | wantUsage = true; |
| 202 | goto bail; |
| 203 | } |
| 204 | bundle.addConfigurations(argv[0]); |
| 205 | break; |
| 206 | case 'f': |
| 207 | bundle.setForce(true); |
| 208 | break; |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 209 | case 'g': |
| 210 | argc--; |
| 211 | argv++; |
| 212 | if (!argc) { |
| 213 | fprintf(stderr, "ERROR: No argument supplied for '-g' option\n"); |
| 214 | wantUsage = true; |
| 215 | goto bail; |
| 216 | } |
| 217 | bundle.setGrayscaleTolerance(atoi(argv[0])); |
| 218 | break; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 219 | case 'm': |
| 220 | bundle.setMakePackageDirs(true); |
| 221 | break; |
| 222 | #if 0 |
| 223 | case 'p': |
| 224 | bundle.setPseudolocalize(true); |
| 225 | break; |
| 226 | #endif |
| 227 | case 'u': |
| 228 | bundle.setUpdate(true); |
| 229 | break; |
| 230 | case 'x': |
| 231 | bundle.setExtending(true); |
| 232 | break; |
| 233 | case 'z': |
| 234 | bundle.setRequireLocalization(true); |
| 235 | break; |
| 236 | case 'j': |
| 237 | argc--; |
| 238 | argv++; |
| 239 | if (!argc) { |
| 240 | fprintf(stderr, "ERROR: No argument supplied for '-j' option\n"); |
| 241 | wantUsage = true; |
| 242 | goto bail; |
| 243 | } |
| 244 | convertPath(argv[0]); |
| 245 | bundle.addJarFile(argv[0]); |
| 246 | break; |
| 247 | case 'A': |
| 248 | argc--; |
| 249 | argv++; |
| 250 | if (!argc) { |
| 251 | fprintf(stderr, "ERROR: No argument supplied for '-A' option\n"); |
| 252 | wantUsage = true; |
| 253 | goto bail; |
| 254 | } |
| 255 | convertPath(argv[0]); |
| 256 | bundle.setAssetSourceDir(argv[0]); |
| 257 | break; |
| 258 | case 'I': |
| 259 | argc--; |
| 260 | argv++; |
| 261 | if (!argc) { |
| 262 | fprintf(stderr, "ERROR: No argument supplied for '-I' option\n"); |
| 263 | wantUsage = true; |
| 264 | goto bail; |
| 265 | } |
| 266 | convertPath(argv[0]); |
| 267 | bundle.addPackageInclude(argv[0]); |
| 268 | break; |
| 269 | case 'F': |
| 270 | argc--; |
| 271 | argv++; |
| 272 | if (!argc) { |
| 273 | fprintf(stderr, "ERROR: No argument supplied for '-F' option\n"); |
| 274 | wantUsage = true; |
| 275 | goto bail; |
| 276 | } |
| 277 | convertPath(argv[0]); |
| 278 | bundle.setOutputAPKFile(argv[0]); |
| 279 | break; |
| 280 | case 'J': |
| 281 | argc--; |
| 282 | argv++; |
| 283 | if (!argc) { |
| 284 | fprintf(stderr, "ERROR: No argument supplied for '-J' option\n"); |
| 285 | wantUsage = true; |
| 286 | goto bail; |
| 287 | } |
| 288 | convertPath(argv[0]); |
| 289 | bundle.setRClassDir(argv[0]); |
| 290 | break; |
| 291 | case 'M': |
| 292 | argc--; |
| 293 | argv++; |
| 294 | if (!argc) { |
| 295 | fprintf(stderr, "ERROR: No argument supplied for '-M' option\n"); |
| 296 | wantUsage = true; |
| 297 | goto bail; |
| 298 | } |
| 299 | convertPath(argv[0]); |
| 300 | bundle.setAndroidManifestFile(argv[0]); |
| 301 | break; |
| 302 | case 'P': |
| 303 | argc--; |
| 304 | argv++; |
| 305 | if (!argc) { |
| 306 | fprintf(stderr, "ERROR: No argument supplied for '-P' option\n"); |
| 307 | wantUsage = true; |
| 308 | goto bail; |
| 309 | } |
| 310 | convertPath(argv[0]); |
| 311 | bundle.setPublicOutputFile(argv[0]); |
| 312 | break; |
| 313 | case 'S': |
| 314 | argc--; |
| 315 | argv++; |
| 316 | if (!argc) { |
| 317 | fprintf(stderr, "ERROR: No argument supplied for '-S' option\n"); |
| 318 | wantUsage = true; |
| 319 | goto bail; |
| 320 | } |
| 321 | convertPath(argv[0]); |
The Android Open Source Project | 9266c55 | 2009-01-15 16:12:10 -0800 | [diff] [blame] | 322 | bundle.addResourceSourceDir(argv[0]); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 323 | break; |
| 324 | case '0': |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 325 | argc--; |
| 326 | argv++; |
| 327 | if (!argc) { |
| 328 | fprintf(stderr, "ERROR: No argument supplied for '-e' option\n"); |
| 329 | wantUsage = true; |
| 330 | goto bail; |
| 331 | } |
| 332 | if (argv[0][0] != 0) { |
| 333 | bundle.addNoCompressExtension(argv[0]); |
| 334 | } else { |
| 335 | bundle.setCompressionMethod(ZipEntry::kCompressStored); |
| 336 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 337 | break; |
| 338 | default: |
| 339 | fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp); |
| 340 | wantUsage = true; |
| 341 | goto bail; |
| 342 | } |
| 343 | |
| 344 | cp++; |
| 345 | } |
| 346 | argc--; |
| 347 | argv++; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * We're past the flags. The rest all goes straight in. |
| 352 | */ |
| 353 | bundle.setFileSpec(argv, argc); |
| 354 | |
| 355 | result = handleCommand(&bundle); |
| 356 | |
| 357 | bail: |
| 358 | if (wantUsage) { |
| 359 | usage(); |
| 360 | result = 2; |
| 361 | } |
| 362 | |
| 363 | //printf("--> returning %d\n", result); |
| 364 | return result; |
| 365 | } |