]> git.saurik.com Git - android/aapt.git/blob - Main.cpp
auto import from //branches/cupcake/...@125939
[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.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"
49 " badging Print the label and icon for the app declared in APK.\n"
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,
56 " %s p[ackage] [-f][-u][-m][-v][-x][-M AndroidManifest.xml] \\\n"
57 " [-0 extension [-0 extension ...]] \\\n"
58 " [-I base-package [-I base-package ...]] \\\n"
59 " [-A asset-source-dir] [-P public-definitions-file] \\\n"
60 " [-S resource-sources] [-F apk-file] [-J R-file-dir] \\\n"
61 " [raw-files-dir [raw-files-dir] ...]\n"
62 "\n"
63 " Package the android resources. It will read assets and resources that are\n"
64 " supplied with the -M -A -S or raw-files-dir arguments. The -J -P -F and -R\n"
65 " options control which files are output.\n\n"
66 , gProgName);
67 fprintf(stderr,
68 " %s r[emove] [-v] file.{zip,jar,apk} file1 [file2 ...]\n"
69 " Delete specified files from Zip-compatible archive.\n\n",
70 gProgName);
71 fprintf(stderr,
72 " %s a[dd] [-v] file.{zip,jar,apk} file1 [file2 ...]\n"
73 " Add specified files to Zip-compatible archive.\n\n", gProgName);
74 fprintf(stderr,
75 " %s v[ersion]\n"
76 " Print program version.\n\n", gProgName);
77 fprintf(stderr,
78 " Modifiers:\n"
79 " -a print Android-specific data (resources, manifest) when listing\n"
80 " -c specify which configurations to include. The default is all\n"
81 " configurations. The value of the parameter should be a comma\n"
82 " separated list of configuration values. Locales should be specified\n"
83 " as either a language or language-region pair. Some examples:\n"
84 " en\n"
85 " port,en\n"
86 " port,land,en_US\n"
87 " If you put the special locale, zz_ZZ on the list, it will perform\n"
88 " pseudolocalization on the default locale, modifying all of the\n"
89 " strings so you can look for strings that missed the\n"
90 " internationalization process. For example:\n"
91 " port,land,zz_ZZ\n"
92 " -d one or more device assets to include, separated by commas\n"
93 " -f force overwrite of existing files\n"
94 " -j specify a jar or zip file containing classes to include\n"
95 " -m make package directories under location specified by -J\n"
96 #if 0
97 " -p pseudolocalize the default configuration\n"
98 #endif
99 " -u update existing packages (add new, replace older, remove deleted files)\n"
100 " -v verbose output\n"
101 " -x create extending (non-application) resource IDs\n"
102 " -z require localization of resource attributes marked with\n"
103 " localization=\"suggested\"\n"
104 " -A additional directory in which to find raw asset files\n"
105 " -F specify the apk file to output\n"
106 " -I add an existing package to base include set\n"
107 " -J specify where to output R.java resource constant definitions\n"
108 " -M specify full path to AndroidManifest.xml to include in zip\n"
109 " -P specify where to output public resource definitions\n"
110 " -S directory in which to find resources\n"
111 " -0 specifies an additional extension for which such files will not\n"
112 " be stored compressed in the .apk. An empty string means to not\n"
113 " compress any files at all.\n");
114 }
115
116 /*
117 * Dispatch the command.
118 */
119 int handleCommand(Bundle* bundle)
120 {
121 //printf("--- command %d (verbose=%d force=%d):\n",
122 // bundle->getCommand(), bundle->getVerbose(), bundle->getForce());
123 //for (int i = 0; i < bundle->getFileSpecCount(); i++)
124 // printf(" %d: '%s'\n", i, bundle->getFileSpecEntry(i));
125
126 switch (bundle->getCommand()) {
127 case kCommandVersion: return doVersion(bundle);
128 case kCommandList: return doList(bundle);
129 case kCommandDump: return doDump(bundle);
130 case kCommandAdd: return doAdd(bundle);
131 case kCommandRemove: return doRemove(bundle);
132 case kCommandPackage: return doPackage(bundle);
133 default:
134 fprintf(stderr, "%s: requested command not yet supported\n", gProgName);
135 return 1;
136 }
137 }
138
139 /*
140 * Parse args.
141 */
142 int main(int argc, char* const argv[])
143 {
144 Bundle bundle;
145 bool wantUsage = false;
146 int result = 1; // pessimistically assume an error.
147
148 /* default to compression */
149 bundle.setCompressionMethod(ZipEntry::kCompressDeflated);
150
151 if (argc < 2) {
152 wantUsage = true;
153 goto bail;
154 }
155
156 if (argv[1][0] == 'v')
157 bundle.setCommand(kCommandVersion);
158 else if (argv[1][0] == 'd')
159 bundle.setCommand(kCommandDump);
160 else if (argv[1][0] == 'l')
161 bundle.setCommand(kCommandList);
162 else if (argv[1][0] == 'a')
163 bundle.setCommand(kCommandAdd);
164 else if (argv[1][0] == 'r')
165 bundle.setCommand(kCommandRemove);
166 else if (argv[1][0] == 'p')
167 bundle.setCommand(kCommandPackage);
168 else {
169 fprintf(stderr, "ERROR: Unknown command '%s'\n", argv[1]);
170 wantUsage = true;
171 goto bail;
172 }
173 argc -= 2;
174 argv += 2;
175
176 /*
177 * Pull out flags. We support "-fv" and "-f -v".
178 */
179 while (argc && argv[0][0] == '-') {
180 /* flag(s) found */
181 const char* cp = argv[0] +1;
182
183 while (*cp != '\0') {
184 switch (*cp) {
185 case 'v':
186 bundle.setVerbose(true);
187 break;
188 case 'a':
189 bundle.setAndroidList(true);
190 break;
191 case 'c':
192 argc--;
193 argv++;
194 if (!argc) {
195 fprintf(stderr, "ERROR: No argument supplied for '-c' option\n");
196 wantUsage = true;
197 goto bail;
198 }
199 bundle.addConfigurations(argv[0]);
200 break;
201 case 'f':
202 bundle.setForce(true);
203 break;
204 case 'm':
205 bundle.setMakePackageDirs(true);
206 break;
207 #if 0
208 case 'p':
209 bundle.setPseudolocalize(true);
210 break;
211 #endif
212 case 'u':
213 bundle.setUpdate(true);
214 break;
215 case 'x':
216 bundle.setExtending(true);
217 break;
218 case 'z':
219 bundle.setRequireLocalization(true);
220 break;
221 case 'j':
222 argc--;
223 argv++;
224 if (!argc) {
225 fprintf(stderr, "ERROR: No argument supplied for '-j' option\n");
226 wantUsage = true;
227 goto bail;
228 }
229 convertPath(argv[0]);
230 bundle.addJarFile(argv[0]);
231 break;
232 case 'A':
233 argc--;
234 argv++;
235 if (!argc) {
236 fprintf(stderr, "ERROR: No argument supplied for '-A' option\n");
237 wantUsage = true;
238 goto bail;
239 }
240 convertPath(argv[0]);
241 bundle.setAssetSourceDir(argv[0]);
242 break;
243 case 'I':
244 argc--;
245 argv++;
246 if (!argc) {
247 fprintf(stderr, "ERROR: No argument supplied for '-I' option\n");
248 wantUsage = true;
249 goto bail;
250 }
251 convertPath(argv[0]);
252 bundle.addPackageInclude(argv[0]);
253 break;
254 case 'F':
255 argc--;
256 argv++;
257 if (!argc) {
258 fprintf(stderr, "ERROR: No argument supplied for '-F' option\n");
259 wantUsage = true;
260 goto bail;
261 }
262 convertPath(argv[0]);
263 bundle.setOutputAPKFile(argv[0]);
264 break;
265 case 'J':
266 argc--;
267 argv++;
268 if (!argc) {
269 fprintf(stderr, "ERROR: No argument supplied for '-J' option\n");
270 wantUsage = true;
271 goto bail;
272 }
273 convertPath(argv[0]);
274 bundle.setRClassDir(argv[0]);
275 break;
276 case 'M':
277 argc--;
278 argv++;
279 if (!argc) {
280 fprintf(stderr, "ERROR: No argument supplied for '-M' option\n");
281 wantUsage = true;
282 goto bail;
283 }
284 convertPath(argv[0]);
285 bundle.setAndroidManifestFile(argv[0]);
286 break;
287 case 'P':
288 argc--;
289 argv++;
290 if (!argc) {
291 fprintf(stderr, "ERROR: No argument supplied for '-P' option\n");
292 wantUsage = true;
293 goto bail;
294 }
295 convertPath(argv[0]);
296 bundle.setPublicOutputFile(argv[0]);
297 break;
298 case 'S':
299 argc--;
300 argv++;
301 if (!argc) {
302 fprintf(stderr, "ERROR: No argument supplied for '-S' option\n");
303 wantUsage = true;
304 goto bail;
305 }
306 convertPath(argv[0]);
307 bundle.setResourceSourceDir(argv[0]);
308 break;
309 case '0':
310 argc--;
311 argv++;
312 if (!argc) {
313 fprintf(stderr, "ERROR: No argument supplied for '-e' option\n");
314 wantUsage = true;
315 goto bail;
316 }
317 if (argv[0][0] != 0) {
318 bundle.addNoCompressExtension(argv[0]);
319 } else {
320 bundle.setCompressionMethod(ZipEntry::kCompressStored);
321 }
322 break;
323 default:
324 fprintf(stderr, "ERROR: Unknown flag '-%c'\n", *cp);
325 wantUsage = true;
326 goto bail;
327 }
328
329 cp++;
330 }
331 argc--;
332 argv++;
333 }
334
335 /*
336 * We're past the flags. The rest all goes straight in.
337 */
338 bundle.setFileSpec(argv, argc);
339
340 result = handleCommand(&bundle);
341
342 bail:
343 if (wantUsage) {
344 usage();
345 result = 2;
346 }
347
348 //printf("--> returning %d\n", result);
349 return result;
350 }