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