]> git.saurik.com Git - android/aapt.git/blob - Main.cpp
Ummm... f*, stop breaking the build.
[android/aapt.git] / Main.cpp
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
18 using namespace android;
19
20 static 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 */
29 void 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 */
42 void 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 " [--debug-mode] [--min-sdk-version VAL] [--target-sdk-version VAL] \\\n"
61 " [--app-version VAL] [--app-version-name TEXT] [--custom-package VAL] \\\n"
62 " [--rename-manifest-package PACKAGE] \\\n"
63 " [--rename-instrumentation-target-package PACKAGE] \\\n"
64 " [--utf16] [--auto-add-overlay] \\\n"
65 " [--max-res-version VAL] \\\n"
66 " [-I base-package [-I base-package ...]] \\\n"
67 " [-A asset-source-dir] [-G class-list-file] [-P public-definitions-file] \\\n"
68 " [-S resource-sources [-S resource-sources ...]] "
69 " [-F apk-file] [-J R-file-dir] \\\n"
70 " [--product product1,product2,...] \\\n"
71 " [raw-files-dir [raw-files-dir] ...]\n"
72 "\n"
73 " Package the android resources. It will read assets and resources that are\n"
74 " supplied with the -M -A -S or raw-files-dir arguments. The -J -P -F and -R\n"
75 " options control which files are output.\n\n"
76 , gProgName);
77 fprintf(stderr,
78 " %s r[emove] [-v] file.{zip,jar,apk} file1 [file2 ...]\n"
79 " Delete specified files from Zip-compatible archive.\n\n",
80 gProgName);
81 fprintf(stderr,
82 " %s a[dd] [-v] file.{zip,jar,apk} file1 [file2 ...]\n"
83 " Add specified files to Zip-compatible archive.\n\n", gProgName);
84 fprintf(stderr,
85 " %s v[ersion]\n"
86 " Print program version.\n\n", gProgName);
87 fprintf(stderr,
88 " Modifiers:\n"
89 " -a print Android-specific data (resources, manifest) when listing\n"
90 " -c specify which configurations to include. The default is all\n"
91 " configurations. The value of the parameter should be a comma\n"
92 " separated list of configuration values. Locales should be specified\n"
93 " as either a language or language-region pair. Some examples:\n"
94 " en\n"
95 " port,en\n"
96 " port,land,en_US\n"
97 " If you put the special locale, zz_ZZ on the list, it will perform\n"
98 " pseudolocalization on the default locale, modifying all of the\n"
99 " strings so you can look for strings that missed the\n"
100 " internationalization process. For example:\n"
101 " port,land,zz_ZZ\n"
102 " -d one or more device assets to include, separated by commas\n"
103 " -f force overwrite of existing files\n"
104 " -g specify a pixel tolerance to force images to grayscale, default 0\n"
105 " -j specify a jar or zip file containing classes to include\n"
106 " -k junk path of file(s) added\n"
107 " -m make package directories under location specified by -J\n"
108 #if 0
109 " -p pseudolocalize the default configuration\n"
110 #endif
111 " -u update existing packages (add new, replace older, remove deleted files)\n"
112 " -v verbose output\n"
113 " -x create extending (non-application) resource IDs\n"
114 " -z require localization of resource attributes marked with\n"
115 " localization=\"suggested\"\n"
116 " -A additional directory in which to find raw asset files\n"
117 " -G A file to output proguard options into.\n"
118 " -F specify the apk file to output\n"
119 " -I add an existing package to base include set\n"
120 " -J specify where to output R.java resource constant definitions\n"
121 " -M specify full path to AndroidManifest.xml to include in zip\n"
122 " -P specify where to output public resource definitions\n"
123 " -S directory in which to find resources. Multiple directories will be scanned\n"
124 " and the first match found (left to right) will take precedence.\n"
125 " -0 specifies an additional extension for which such files will not\n"
126 " be stored compressed in the .apk. An empty string means to not\n"
127 " compress any files at all.\n"
128 " --debug-mode\n"
129 " inserts android:debuggable=\"true\" in to the application node of the\n"
130 " manifest, making the application debuggable even on production devices.\n"
131 " --min-sdk-version\n"
132 " inserts android:minSdkVersion in to manifest. If the version is 7 or\n"
133 " higher, the default encoding for resources will be in UTF-8.\n"
134 " --target-sdk-version\n"
135 " inserts android:targetSdkVersion in to manifest.\n"
136 " --max-res-version\n"
137 " ignores versioned resource directories above the given value.\n"
138 " --values\n"
139 " when used with \"dump resources\" also includes resource values.\n"
140 " --version-code\n"
141 " inserts android:versionCode in to manifest.\n"
142 " --version-name\n"
143 " inserts android:versionName in to manifest.\n"
144 " --custom-package\n"
145 " generates R.java into a different package.\n"
146 " --auto-add-overlay\n"
147 " Automatically add resources that are only in overlays.\n"
148 " --rename-manifest-package\n"
149 " Rewrite the manifest so that its package name is the package name\n"
150 " given here. Relative class names (for example .Foo) will be\n"
151 " changed to absolute names with the old package so that the code\n"
152 " does not need to change.\n"
153 " --rename-instrumentation-target-package\n"
154 " Rewrite the manifest so that all of its instrumentation\n"
155 " components target the given package. Useful when used in\n"
156 " conjunction with --rename-manifest-package to fix tests against\n"
157 " a package that has been renamed.\n"
158 " --product\n"
159 " Specifies which variant to choose for strings that have\n"
160 " product variants\n"
161 " --utf16\n"
162 " changes default encoding for resources to UTF-16. Only useful when API\n"
163 " level is set to 7 or higher where the default encoding is UTF-8.\n"
164 " --non-constant-id\n"
165 " Make the resources ID non constant. This is required to make an R java class\n"
166 " that does not contain the final value but is used to make reusable compiled\n"
167 " libraries that need to access resources.\n");
168 }
169
170 /*
171 * Dispatch the command.
172 */
173 int handleCommand(Bundle* bundle)
174 {
175 //printf("--- command %d (verbose=%d force=%d):\n",
176 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce());
177 //for (int i = 0; i < bundle->getFileSpecCount(); i++)
178 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i));
179
180 switch (bundle->getCommand()) {
181 case kCommandVersion: return doVersion(bundle);
182 case kCommandList: return doList(bundle);
183 case kCommandDump: return doDump(bundle);
184 case kCommandAdd: return doAdd(bundle);
185 case kCommandRemove: return doRemove(bundle);
186 case kCommandPackage: return doPackage(bundle);
187 default:
188 fprintf(stderr, "%s: requested command not yet supported\n", gProgName);
189 return 1;
190 }
191 }
192
193 /*
194 * Parse args.
195 */
196 int main(int argc, char* const argv[])
197 {
198 char *prog = argv[0];
199 Bundle bundle;
200 bool wantUsage = false;
201 int result = 1; // pessimistically assume an error.
202 int tolerance = 0;
203
204 /* default to compression */
205 bundle.setCompressionMethod(ZipEntry::kCompressDeflated);
206
207 if (argc < 2) {
208 wantUsage = true;
209 goto bail;
210 }
211
212 if (argv[1][0] == 'v')
213 bundle.setCommand(kCommandVersion);
214 else if (argv[1][0] == 'd')
215 bundle.setCommand(kCommandDump);
216 else if (argv[1][0] == 'l')
217 bundle.setCommand(kCommandList);
218 else if (argv[1][0] == 'a')
219 bundle.setCommand(kCommandAdd);
220 else if (argv[1][0] == 'r')
221 bundle.setCommand(kCommandRemove);
222 else if (argv[1][0] == 'p')
223 bundle.setCommand(kCommandPackage);
224 else {
225 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]);
226 wantUsage = true;
227 goto bail;
228 }
229 argc -= 2;
230 argv += 2;
231
232 /*
233 * Pull out flags. We support "-fv" and "-f -v".
234 */
235 while (argc && argv[0][0] == '-') {
236 /* flag(s) found */
237 const char* cp = argv[0] +1;
238
239 while (*cp != '\0') {
240 switch (*cp) {
241 case 'v':
242 bundle.setVerbose(true);
243 break;
244 case 'a':
245 bundle.setAndroidList(true);
246 break;
247 case 'c':
248 argc--;
249 argv++;
250 if (!argc) {
251 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n");
252 wantUsage = true;
253 goto bail;
254 }
255 bundle.addConfigurations(argv[0]);
256 break;
257 case 'f':
258 bundle.setForce(true);
259 break;
260 case 'g':
261 argc--;
262 argv++;
263 if (!argc) {
264 fprintf(stderr, "ERROR: No argument supplied for '-g' option\n");
265 wantUsage = true;
266 goto bail;
267 }
268 tolerance = atoi(argv[0]);
269 bundle.setGrayscaleTolerance(tolerance);
270 printf("%s: Images with deviation <= %d will be forced to grayscale.\n", prog, tolerance);
271 break;
272 case 'k':
273 bundle.setJunkPath(true);
274 break;
275 case 'm':
276 bundle.setMakePackageDirs(true);
277 break;
278 #if 0
279 case 'p':
280 bundle.setPseudolocalize(true);
281 break;
282 #endif
283 case 'u':
284 bundle.setUpdate(true);
285 break;
286 case 'x':
287 bundle.setExtending(true);
288 break;
289 case 'z':
290 bundle.setRequireLocalization(true);
291 break;
292 case 'j':
293 argc--;
294 argv++;
295 if (!argc) {
296 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n");
297 wantUsage = true;
298 goto bail;
299 }
300 convertPath(argv[0]);
301 bundle.addJarFile(argv[0]);
302 break;
303 case 'A':
304 argc--;
305 argv++;
306 if (!argc) {
307 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n");
308 wantUsage = true;
309 goto bail;
310 }
311 convertPath(argv[0]);
312 bundle.setAssetSourceDir(argv[0]);
313 break;
314 case 'G':
315 argc--;
316 argv++;
317 if (!argc) {
318 fprintf(stderr, "ERROR: No argument supplied for '-G' option\n");
319 wantUsage = true;
320 goto bail;
321 }
322 convertPath(argv[0]);
323 bundle.setProguardFile(argv[0]);
324 break;
325 case 'I':
326 argc--;
327 argv++;
328 if (!argc) {
329 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n");
330 wantUsage = true;
331 goto bail;
332 }
333 convertPath(argv[0]);
334 bundle.addPackageInclude(argv[0]);
335 break;
336 case 'F':
337 argc--;
338 argv++;
339 if (!argc) {
340 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n");
341 wantUsage = true;
342 goto bail;
343 }
344 convertPath(argv[0]);
345 bundle.setOutputAPKFile(argv[0]);
346 break;
347 case 'J':
348 argc--;
349 argv++;
350 if (!argc) {
351 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n");
352 wantUsage = true;
353 goto bail;
354 }
355 convertPath(argv[0]);
356 bundle.setRClassDir(argv[0]);
357 break;
358 case 'M':
359 argc--;
360 argv++;
361 if (!argc) {
362 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n");
363 wantUsage = true;
364 goto bail;
365 }
366 convertPath(argv[0]);
367 bundle.setAndroidManifestFile(argv[0]);
368 break;
369 case 'P':
370 argc--;
371 argv++;
372 if (!argc) {
373 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n");
374 wantUsage = true;
375 goto bail;
376 }
377 convertPath(argv[0]);
378 bundle.setPublicOutputFile(argv[0]);
379 break;
380 case 'S':
381 argc--;
382 argv++;
383 if (!argc) {
384 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n");
385 wantUsage = true;
386 goto bail;
387 }
388 convertPath(argv[0]);
389 bundle.addResourceSourceDir(argv[0]);
390 break;
391 case '0':
392 argc--;
393 argv++;
394 if (!argc) {
395 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n");
396 wantUsage = true;
397 goto bail;
398 }
399 if (argv[0][0] != 0) {
400 bundle.addNoCompressExtension(argv[0]);
401 } else {
402 bundle.setCompressionMethod(ZipEntry::kCompressStored);
403 }
404 break;
405 case '-':
406 if (strcmp(cp, "-debug-mode") == 0) {
407 bundle.setDebugMode(true);
408 } else if (strcmp(cp, "-min-sdk-version") == 0) {
409 argc--;
410 argv++;
411 if (!argc) {
412 fprintf(stderr, "ERROR: No argument supplied for '--min-sdk-version' option\n");
413 wantUsage = true;
414 goto bail;
415 }
416 bundle.setMinSdkVersion(argv[0]);
417 } else if (strcmp(cp, "-target-sdk-version") == 0) {
418 argc--;
419 argv++;
420 if (!argc) {
421 fprintf(stderr, "ERROR: No argument supplied for '--target-sdk-version' option\n");
422 wantUsage = true;
423 goto bail;
424 }
425 bundle.setTargetSdkVersion(argv[0]);
426 } else if (strcmp(cp, "-max-sdk-version") == 0) {
427 argc--;
428 argv++;
429 if (!argc) {
430 fprintf(stderr, "ERROR: No argument supplied for '--max-sdk-version' option\n");
431 wantUsage = true;
432 goto bail;
433 }
434 bundle.setMaxSdkVersion(argv[0]);
435 } else if (strcmp(cp, "-max-res-version") == 0) {
436 argc--;
437 argv++;
438 if (!argc) {
439 fprintf(stderr, "ERROR: No argument supplied for '--max-res-version' option\n");
440 wantUsage = true;
441 goto bail;
442 }
443 bundle.setMaxResVersion(argv[0]);
444 } else if (strcmp(cp, "-version-code") == 0) {
445 argc--;
446 argv++;
447 if (!argc) {
448 fprintf(stderr, "ERROR: No argument supplied for '--version-code' option\n");
449 wantUsage = true;
450 goto bail;
451 }
452 bundle.setVersionCode(argv[0]);
453 } else if (strcmp(cp, "-version-name") == 0) {
454 argc--;
455 argv++;
456 if (!argc) {
457 fprintf(stderr, "ERROR: No argument supplied for '--version-name' option\n");
458 wantUsage = true;
459 goto bail;
460 }
461 bundle.setVersionName(argv[0]);
462 } else if (strcmp(cp, "-values") == 0) {
463 bundle.setValues(true);
464 } else if (strcmp(cp, "-custom-package") == 0) {
465 argc--;
466 argv++;
467 if (!argc) {
468 fprintf(stderr, "ERROR: No argument supplied for '--custom-package' option\n");
469 wantUsage = true;
470 goto bail;
471 }
472 bundle.setCustomPackage(argv[0]);
473 } else if (strcmp(cp, "-utf16") == 0) {
474 bundle.setWantUTF16(true);
475 } else if (strcmp(cp, "-rename-manifest-package") == 0) {
476 argc--;
477 argv++;
478 if (!argc) {
479 fprintf(stderr, "ERROR: No argument supplied for '--rename-manifest-package' option\n");
480 wantUsage = true;
481 goto bail;
482 }
483 bundle.setManifestPackageNameOverride(argv[0]);
484 } else if (strcmp(cp, "-rename-instrumentation-target-package") == 0) {
485 argc--;
486 argv++;
487 if (!argc) {
488 fprintf(stderr, "ERROR: No argument supplied for '--rename-instrumentation-target-package' option\n");
489 wantUsage = true;
490 goto bail;
491 }
492 bundle.setInstrumentationPackageNameOverride(argv[0]);
493 } else if (strcmp(cp, "-auto-add-overlay") == 0) {
494 bundle.setAutoAddOverlay(true);
495 } else if (strcmp(cp, "-product") == 0) {
496 argc--;
497 argv++;
498 if (!argc) {
499 fprintf(stderr, "ERROR: No argument supplied for '--product' option\n");
500 wantUsage = true;
501 goto bail;
502 }
503 bundle.setProduct(argv[0]);
504 } else if (strcmp(cp, "-non-constant-id") == 0) {
505 bundle.setNonConstantId(true);
506 } else {
507 fprintf(stderr, "ERROR: Unknown option '-%s'\n", cp);
508 wantUsage = true;
509 goto bail;
510 }
511 cp += strlen(cp) - 1;
512 break;
513 default:
514 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp);
515 wantUsage = true;
516 goto bail;
517 }
518
519 cp++;
520 }
521 argc--;
522 argv++;
523 }
524
525 /*
526 * We're past the flags. The rest all goes straight in.
527 */
528 bundle.setFileSpec(argv, argc);
529
530 result = handleCommand(&bundle);
531
532 bail:
533 if (wantUsage) {
534 usage();
535 result = 2;
536 }
537
538 //printf("--> returning %d\n", result);
539 return result;
540 }