blob: 6ff82690105d8876261418d6e740f775515f5f51 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001//
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
Mathias Agopian3b4062e2009-05-31 19:13:00 -07009#include <utils/Log.h>
10#include <utils/threads.h>
11#include <utils/List.h>
12#include <utils/Errors.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013#include <utils/ZipFile.h>
14
15#include <stdlib.h>
16#include <getopt.h>
17#include <assert.h>
18
19using namespace android;
20
21static const char* gProgName = "aapt";
22
23/*
24 * When running under Cygwin on Windows, this will convert slash-based
25 * paths into back-slash-based ones. Otherwise the ApptAssets file comparisons
26 * fail later as they use back-slash separators under Windows.
27 *
28 * This operates in-place on the path string.
29 */
30void convertPath(char *path) {
31 if (path != NULL && OS_PATH_SEPARATOR != '/') {
32 for (; *path; path++) {
33 if (*path == '/') {
34 *path = OS_PATH_SEPARATOR;
35 }
36 }
37 }
38}
39
40/*
41 * Print usage info.
42 */
43void usage(void)
44{
45 fprintf(stderr, "Android Asset Packaging Tool\n\n");
46 fprintf(stderr, "Usage:\n");
47 fprintf(stderr,
48 " %s l[ist] [-v] [-a] file.{zip,jar,apk}\n"
49 " List contents of Zip-compatible archive.\n\n", gProgName);
50 fprintf(stderr,
51 " %s d[ump] WHAT file.{apk} [asset [asset ...]]\n"
52 " badging Print the label and icon for the app declared in APK.\n"
53 " permissions Print the permissions from the APK.\n"
54 " resources Print the resource table from the APK.\n"
55 " configurations Print the configurations in the APK.\n"
56 " xmltree Print the compiled xmls in the given assets.\n"
57 " xmlstrings Print the strings of the given compiled xml assets.\n\n", gProgName);
58 fprintf(stderr,
59 " %s p[ackage] [-d][-f][-m][-u][-v][-x][-z][-M AndroidManifest.xml] \\\n"
Dianne Hackborn62da8462009-05-13 15:06:13 -070060 " [-0 extension [-0 extension ...]] [-g tolerance] [-j jarfile] \\\n"
61 " [--min-sdk-version VAL] [--target-sdk-version VAL] \\\n"
62 " [--max-sdk-version VAL] [--app-version VAL] \\\n"
63 " [--app-version-name TEXT] \\\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 " [-I base-package [-I base-package ...]] \\\n"
65 " [-A asset-source-dir] [-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 " -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"
113 " -F specify the apk file to output\n"
114 " -I add an existing package to base include set\n"
115 " -J specify where to output R.java resource constant definitions\n"
116 " -M specify full path to AndroidManifest.xml to include in zip\n"
117 " -P specify where to output public resource definitions\n"
118 " -S directory in which to find resources. Multiple directories will be scanned"
119 " and the first match found (left to right) will take precedence."
120 " -0 specifies an additional extension for which such files will not\n"
121 " be stored compressed in the .apk. An empty string means to not\n"
Dianne Hackborn62da8462009-05-13 15:06:13 -0700122 " compress any files at all.\n"
123 " --min-sdk-version\n"
124 " inserts android:minSdkVersion in to manifest.\n"
125 " --target-sdk-version\n"
126 " inserts android:targetSdkVersion in to manifest.\n"
127 " --max-sdk-version\n"
128 " inserts android:maxSdkVersion in to manifest.\n"
129 " --version-code\n"
130 " inserts android:versionCode in to manifest.\n"
131 " --version-name\n"
132 " inserts android:versionName in to manifest.\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133}
134
135/*
136 * Dispatch the command.
137 */
138int handleCommand(Bundle* bundle)
139{
140 //printf("--- command %d (verbose=%d force=%d):\n",
141 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce());
142 //for (int i = 0; i < bundle->getFileSpecCount(); i++)
143 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i));
144
145 switch (bundle->getCommand()) {
146 case kCommandVersion: return doVersion(bundle);
147 case kCommandList: return doList(bundle);
148 case kCommandDump: return doDump(bundle);
149 case kCommandAdd: return doAdd(bundle);
150 case kCommandRemove: return doRemove(bundle);
151 case kCommandPackage: return doPackage(bundle);
152 default:
153 fprintf(stderr, "%s: requested command not yet supported\n", gProgName);
154 return 1;
155 }
156}
157
158/*
159 * Parse args.
160 */
161int main(int argc, char* const argv[])
162{
163 char *prog = argv[0];
164 Bundle bundle;
165 bool wantUsage = false;
166 int result = 1; // pessimistically assume an error.
167 int tolerance = 0;
168
169 /* default to compression */
170 bundle.setCompressionMethod(ZipEntry::kCompressDeflated);
171
172 if (argc < 2) {
173 wantUsage = true;
174 goto bail;
175 }
176
177 if (argv[1][0] == 'v')
178 bundle.setCommand(kCommandVersion);
179 else if (argv[1][0] == 'd')
180 bundle.setCommand(kCommandDump);
181 else if (argv[1][0] == 'l')
182 bundle.setCommand(kCommandList);
183 else if (argv[1][0] == 'a')
184 bundle.setCommand(kCommandAdd);
185 else if (argv[1][0] == 'r')
186 bundle.setCommand(kCommandRemove);
187 else if (argv[1][0] == 'p')
188 bundle.setCommand(kCommandPackage);
189 else {
190 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]);
191 wantUsage = true;
192 goto bail;
193 }
194 argc -= 2;
195 argv += 2;
196
197 /*
198 * Pull out flags. We support "-fv" and "-f -v".
199 */
200 while (argc && argv[0][0] == '-') {
201 /* flag(s) found */
202 const char* cp = argv[0] +1;
203
204 while (*cp != '\0') {
205 switch (*cp) {
206 case 'v':
207 bundle.setVerbose(true);
208 break;
209 case 'a':
210 bundle.setAndroidList(true);
211 break;
212 case 'c':
213 argc--;
214 argv++;
215 if (!argc) {
216 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n");
217 wantUsage = true;
218 goto bail;
219 }
220 bundle.addConfigurations(argv[0]);
221 break;
222 case 'f':
223 bundle.setForce(true);
224 break;
225 case 'g':
226 argc--;
227 argv++;
228 if (!argc) {
229 fprintf(stderr, "ERROR: No argument supplied for '-g' option\n");
230 wantUsage = true;
231 goto bail;
232 }
233 tolerance = atoi(argv[0]);
234 bundle.setGrayscaleTolerance(tolerance);
235 printf("%s: Images with deviation <= %d will be forced to grayscale.\n", prog, tolerance);
236 break;
237 case 'm':
238 bundle.setMakePackageDirs(true);
239 break;
240#if 0
241 case 'p':
242 bundle.setPseudolocalize(true);
243 break;
244#endif
245 case 'u':
246 bundle.setUpdate(true);
247 break;
248 case 'x':
249 bundle.setExtending(true);
250 break;
251 case 'z':
252 bundle.setRequireLocalization(true);
253 break;
254 case 'j':
255 argc--;
256 argv++;
257 if (!argc) {
258 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n");
259 wantUsage = true;
260 goto bail;
261 }
262 convertPath(argv[0]);
263 bundle.addJarFile(argv[0]);
264 break;
265 case 'A':
266 argc--;
267 argv++;
268 if (!argc) {
269 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n");
270 wantUsage = true;
271 goto bail;
272 }
273 convertPath(argv[0]);
274 bundle.setAssetSourceDir(argv[0]);
275 break;
276 case 'I':
277 argc--;
278 argv++;
279 if (!argc) {
280 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n");
281 wantUsage = true;
282 goto bail;
283 }
284 convertPath(argv[0]);
285 bundle.addPackageInclude(argv[0]);
286 break;
287 case 'F':
288 argc--;
289 argv++;
290 if (!argc) {
291 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n");
292 wantUsage = true;
293 goto bail;
294 }
295 convertPath(argv[0]);
296 bundle.setOutputAPKFile(argv[0]);
297 break;
298 case 'J':
299 argc--;
300 argv++;
301 if (!argc) {
302 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n");
303 wantUsage = true;
304 goto bail;
305 }
306 convertPath(argv[0]);
307 bundle.setRClassDir(argv[0]);
308 break;
309 case 'M':
310 argc--;
311 argv++;
312 if (!argc) {
313 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n");
314 wantUsage = true;
315 goto bail;
316 }
317 convertPath(argv[0]);
318 bundle.setAndroidManifestFile(argv[0]);
319 break;
320 case 'P':
321 argc--;
322 argv++;
323 if (!argc) {
324 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n");
325 wantUsage = true;
326 goto bail;
327 }
328 convertPath(argv[0]);
329 bundle.setPublicOutputFile(argv[0]);
330 break;
331 case 'S':
332 argc--;
333 argv++;
334 if (!argc) {
335 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n");
336 wantUsage = true;
337 goto bail;
338 }
339 convertPath(argv[0]);
340 bundle.addResourceSourceDir(argv[0]);
341 break;
342 case '0':
343 argc--;
344 argv++;
345 if (!argc) {
346 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n");
347 wantUsage = true;
348 goto bail;
349 }
350 if (argv[0][0] != 0) {
351 bundle.addNoCompressExtension(argv[0]);
352 } else {
353 bundle.setCompressionMethod(ZipEntry::kCompressStored);
354 }
355 break;
Dianne Hackborn62da8462009-05-13 15:06:13 -0700356 case '-':
357 if (strcmp(cp, "-min-sdk-version") == 0) {
358 argc--;
359 argv++;
360 if (!argc) {
361 fprintf(stderr, "ERROR: No argument supplied for '--min-sdk-version' option\n");
362 wantUsage = true;
363 goto bail;
364 }
365 bundle.setMinSdkVersion(argv[0]);
366 } else if (strcmp(cp, "-target-sdk-version") == 0) {
367 argc--;
368 argv++;
369 if (!argc) {
370 fprintf(stderr, "ERROR: No argument supplied for '--target-sdk-version' option\n");
371 wantUsage = true;
372 goto bail;
373 }
374 bundle.setTargetSdkVersion(argv[0]);
375 } else if (strcmp(cp, "-max-sdk-version") == 0) {
376 argc--;
377 argv++;
378 if (!argc) {
379 fprintf(stderr, "ERROR: No argument supplied for '--max-sdk-version' option\n");
380 wantUsage = true;
381 goto bail;
382 }
383 bundle.setMaxSdkVersion(argv[0]);
384 } else if (strcmp(cp, "-version-code") == 0) {
385 argc--;
386 argv++;
387 if (!argc) {
388 fprintf(stderr, "ERROR: No argument supplied for '--version-code' option\n");
389 wantUsage = true;
390 goto bail;
391 }
392 bundle.setVersionCode(argv[0]);
393 } else if (strcmp(cp, "-version-name") == 0) {
394 argc--;
395 argv++;
396 if (!argc) {
397 fprintf(stderr, "ERROR: No argument supplied for '--version-name' option\n");
398 wantUsage = true;
399 goto bail;
400 }
401 bundle.setVersionName(argv[0]);
402 } else {
403 fprintf(stderr, "ERROR: Unknown option '-%s'\n", cp);
404 wantUsage = true;
405 goto bail;
406 }
407 cp += strlen(cp) - 1;
408 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 default:
410 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp);
411 wantUsage = true;
412 goto bail;
413 }
414
415 cp++;
416 }
417 argc--;
418 argv++;
419 }
420
421 /*
422 * We're past the flags. The rest all goes straight in.
423 */
424 bundle.setFileSpec(argv, argc);
425
426 result = handleCommand(&bundle);
427
428bail:
429 if (wantUsage) {
430 usage();
431 result = 2;
432 }
433
434 //printf("--> returning %d\n", result);
435 return result;
436}