]> git.saurik.com Git - android/aapt.git/blame - Main.cpp
resolved conflicts for merge of ce307f88 to eclair-mr2
[android/aapt.git] / Main.cpp
CommitLineData
a534180c
TAOSP
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
79e5d372
MA
9#include <utils/Log.h>
10#include <utils/threads.h>
11#include <utils/List.h>
12#include <utils/Errors.h>
a534180c
TAOSP
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,
7751daa4 50 " %s d[ump] [--values] WHAT file.{apk} [asset [asset ...]]\n"
a534180c
TAOSP
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"
e942a5c2
DH
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"
fa19db5e 62 " [--app-version-name TEXT] [--custom-package VAL] \\\n"
a534180c 63 " [-I base-package [-I base-package ...]] \\\n"
6648ff78 64 " [-A asset-source-dir] [-G class-list-file] [-P public-definitions-file] \\\n"
a534180c
TAOSP
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"
1e8883fc 102 " -k junk path of file(s) added\n"
a534180c
TAOSP
103 " -m make package directories under location specified by -J\n"
104#if 0
105 " -p pseudolocalize the default configuration\n"
106#endif
107 " -u update existing packages (add new, replace older, remove deleted files)\n"
108 " -v verbose output\n"
109 " -x create extending (non-application) resource IDs\n"
110 " -z require localization of resource attributes marked with\n"
111 " localization=\"suggested\"\n"
112 " -A additional directory in which to find raw asset files\n"
6648ff78 113 " -G A file to output proguard options into.\n"
a534180c
TAOSP
114 " -F specify the apk file to output\n"
115 " -I add an existing package to base include set\n"
116 " -J specify where to output R.java resource constant definitions\n"
117 " -M specify full path to AndroidManifest.xml to include in zip\n"
118 " -P specify where to output public resource definitions\n"
fa19db5e
XD
119 " -S directory in which to find resources. Multiple directories will be scanned\n"
120 " and the first match found (left to right) will take precedence.\n"
15c62a5b 121 " -8 Encode string resources in UTF-8.\n"
a534180c
TAOSP
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"
e942a5c2
DH
124 " compress any files at all.\n"
125 " --min-sdk-version\n"
126 " inserts android:minSdkVersion in to manifest.\n"
127 " --target-sdk-version\n"
128 " inserts android:targetSdkVersion in to manifest.\n"
129 " --max-sdk-version\n"
130 " inserts android:maxSdkVersion in to manifest.\n"
7751daa4
DH
131 " --values\n"
132 " when used with \"dump resources\" also includes resource values.\n"
e942a5c2
DH
133 " --version-code\n"
134 " inserts android:versionCode in to manifest.\n"
135 " --version-name\n"
fa19db5e
XD
136 " inserts android:versionName in to manifest.\n"
137 " --custom-package\n"
138 " generates R.java into a different package.\n");
a534180c
TAOSP
139}
140
141/*
142 * Dispatch the command.
143 */
144int handleCommand(Bundle* bundle)
145{
146 //printf("--- command %d (verbose=%d force=%d):\n",
147 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce());
148 //for (int i = 0; i < bundle->getFileSpecCount(); i++)
149 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i));
150
151 switch (bundle->getCommand()) {
152 case kCommandVersion: return doVersion(bundle);
153 case kCommandList: return doList(bundle);
154 case kCommandDump: return doDump(bundle);
155 case kCommandAdd: return doAdd(bundle);
156 case kCommandRemove: return doRemove(bundle);
157 case kCommandPackage: return doPackage(bundle);
158 default:
159 fprintf(stderr, "%s: requested command not yet supported\n", gProgName);
160 return 1;
161 }
162}
163
164/*
165 * Parse args.
166 */
167int main(int argc, char* const argv[])
168{
169 char *prog = argv[0];
170 Bundle bundle;
171 bool wantUsage = false;
172 int result = 1; // pessimistically assume an error.
173 int tolerance = 0;
174
175 /* default to compression */
176 bundle.setCompressionMethod(ZipEntry::kCompressDeflated);
177
178 if (argc < 2) {
179 wantUsage = true;
180 goto bail;
181 }
182
183 if (argv[1][0] == 'v')
184 bundle.setCommand(kCommandVersion);
185 else if (argv[1][0] == 'd')
186 bundle.setCommand(kCommandDump);
187 else if (argv[1][0] == 'l')
188 bundle.setCommand(kCommandList);
189 else if (argv[1][0] == 'a')
190 bundle.setCommand(kCommandAdd);
191 else if (argv[1][0] == 'r')
192 bundle.setCommand(kCommandRemove);
193 else if (argv[1][0] == 'p')
194 bundle.setCommand(kCommandPackage);
195 else {
196 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]);
197 wantUsage = true;
198 goto bail;
199 }
200 argc -= 2;
201 argv += 2;
202
203 /*
204 * Pull out flags. We support "-fv" and "-f -v".
205 */
206 while (argc && argv[0][0] == '-') {
207 /* flag(s) found */
208 const char* cp = argv[0] +1;
209
210 while (*cp != '\0') {
211 switch (*cp) {
212 case 'v':
213 bundle.setVerbose(true);
214 break;
215 case 'a':
216 bundle.setAndroidList(true);
217 break;
218 case 'c':
219 argc--;
220 argv++;
221 if (!argc) {
222 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n");
223 wantUsage = true;
224 goto bail;
225 }
226 bundle.addConfigurations(argv[0]);
227 break;
228 case 'f':
229 bundle.setForce(true);
230 break;
231 case 'g':
232 argc--;
233 argv++;
234 if (!argc) {
235 fprintf(stderr, "ERROR: No argument supplied for '-g' option\n");
236 wantUsage = true;
237 goto bail;
238 }
239 tolerance = atoi(argv[0]);
240 bundle.setGrayscaleTolerance(tolerance);
241 printf("%s: Images with deviation <= %d will be forced to grayscale.\n", prog, tolerance);
242 break;
1e8883fc
DZ
243 case 'k':
244 bundle.setJunkPath(true);
245 break;
a534180c
TAOSP
246 case 'm':
247 bundle.setMakePackageDirs(true);
248 break;
249#if 0
250 case 'p':
251 bundle.setPseudolocalize(true);
252 break;
253#endif
254 case 'u':
255 bundle.setUpdate(true);
256 break;
257 case 'x':
258 bundle.setExtending(true);
259 break;
260 case 'z':
261 bundle.setRequireLocalization(true);
262 break;
263 case 'j':
264 argc--;
265 argv++;
266 if (!argc) {
267 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n");
268 wantUsage = true;
269 goto bail;
270 }
271 convertPath(argv[0]);
272 bundle.addJarFile(argv[0]);
273 break;
274 case 'A':
275 argc--;
276 argv++;
277 if (!argc) {
278 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n");
279 wantUsage = true;
280 goto bail;
281 }
282 convertPath(argv[0]);
283 bundle.setAssetSourceDir(argv[0]);
284 break;
6648ff78
JO
285 case 'G':
286 argc--;
287 argv++;
288 if (!argc) {
289 fprintf(stderr, "ERROR: No argument supplied for '-G' option\n");
290 wantUsage = true;
291 goto bail;
292 }
293 convertPath(argv[0]);
294 bundle.setProguardFile(argv[0]);
295 break;
a534180c
TAOSP
296 case 'I':
297 argc--;
298 argv++;
299 if (!argc) {
300 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n");
301 wantUsage = true;
302 goto bail;
303 }
304 convertPath(argv[0]);
305 bundle.addPackageInclude(argv[0]);
306 break;
307 case 'F':
308 argc--;
309 argv++;
310 if (!argc) {
311 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n");
312 wantUsage = true;
313 goto bail;
314 }
315 convertPath(argv[0]);
316 bundle.setOutputAPKFile(argv[0]);
317 break;
318 case 'J':
319 argc--;
320 argv++;
321 if (!argc) {
322 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n");
323 wantUsage = true;
324 goto bail;
325 }
326 convertPath(argv[0]);
327 bundle.setRClassDir(argv[0]);
328 break;
329 case 'M':
330 argc--;
331 argv++;
332 if (!argc) {
333 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n");
334 wantUsage = true;
335 goto bail;
336 }
337 convertPath(argv[0]);
338 bundle.setAndroidManifestFile(argv[0]);
339 break;
340 case 'P':
341 argc--;
342 argv++;
343 if (!argc) {
344 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n");
345 wantUsage = true;
346 goto bail;
347 }
348 convertPath(argv[0]);
349 bundle.setPublicOutputFile(argv[0]);
350 break;
351 case 'S':
352 argc--;
353 argv++;
354 if (!argc) {
355 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n");
356 wantUsage = true;
357 goto bail;
358 }
359 convertPath(argv[0]);
360 bundle.addResourceSourceDir(argv[0]);
361 break;
362 case '0':
363 argc--;
364 argv++;
365 if (!argc) {
366 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n");
367 wantUsage = true;
368 goto bail;
369 }
370 if (argv[0][0] != 0) {
371 bundle.addNoCompressExtension(argv[0]);
372 } else {
373 bundle.setCompressionMethod(ZipEntry::kCompressStored);
374 }
375 break;
15c62a5b
KR
376 case '8':
377 bundle.setUTF8(true);
378 break;
e942a5c2
DH
379 case '-':
380 if (strcmp(cp, "-min-sdk-version") == 0) {
381 argc--;
382 argv++;
383 if (!argc) {
384 fprintf(stderr, "ERROR: No argument supplied for '--min-sdk-version' option\n");
385 wantUsage = true;
386 goto bail;
387 }
388 bundle.setMinSdkVersion(argv[0]);
389 } else if (strcmp(cp, "-target-sdk-version") == 0) {
390 argc--;
391 argv++;
392 if (!argc) {
393 fprintf(stderr, "ERROR: No argument supplied for '--target-sdk-version' option\n");
394 wantUsage = true;
395 goto bail;
396 }
397 bundle.setTargetSdkVersion(argv[0]);
398 } else if (strcmp(cp, "-max-sdk-version") == 0) {
399 argc--;
400 argv++;
401 if (!argc) {
402 fprintf(stderr, "ERROR: No argument supplied for '--max-sdk-version' option\n");
403 wantUsage = true;
404 goto bail;
405 }
406 bundle.setMaxSdkVersion(argv[0]);
407 } else if (strcmp(cp, "-version-code") == 0) {
408 argc--;
409 argv++;
410 if (!argc) {
411 fprintf(stderr, "ERROR: No argument supplied for '--version-code' option\n");
412 wantUsage = true;
413 goto bail;
414 }
415 bundle.setVersionCode(argv[0]);
416 } else if (strcmp(cp, "-version-name") == 0) {
417 argc--;
418 argv++;
419 if (!argc) {
420 fprintf(stderr, "ERROR: No argument supplied for '--version-name' option\n");
421 wantUsage = true;
422 goto bail;
423 }
424 bundle.setVersionName(argv[0]);
7751daa4
DH
425 } else if (strcmp(cp, "-values") == 0) {
426 bundle.setValues(true);
fa19db5e
XD
427 } else if (strcmp(cp, "-custom-package") == 0) {
428 argc--;
429 argv++;
430 if (!argc) {
431 fprintf(stderr, "ERROR: No argument supplied for '--custom-package' option\n");
432 wantUsage = true;
433 goto bail;
434 }
435 bundle.setCustomPackage(argv[0]);
e942a5c2
DH
436 } else {
437 fprintf(stderr, "ERROR: Unknown option '-%s'\n", cp);
438 wantUsage = true;
439 goto bail;
440 }
441 cp += strlen(cp) - 1;
442 break;
a534180c
TAOSP
443 default:
444 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp);
445 wantUsage = true;
446 goto bail;
447 }
448
449 cp++;
450 }
451 argc--;
452 argv++;
453 }
454
455 /*
456 * We're past the flags. The rest all goes straight in.
457 */
458 bundle.setFileSpec(argv, argc);
459
460 result = handleCommand(&bundle);
461
462bail:
463 if (wantUsage) {
464 usage();
465 result = 2;
466 }
467
468 //printf("--> returning %d\n", result);
469 return result;
470}