]> git.saurik.com Git - android/aapt.git/blob - Command.cpp
am 7fca18d4: am 29355534: Fix aapt to build in Windows SDK under Linux
[android/aapt.git] / Command.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 #include "ResourceTable.h"
9 #include "XMLNode.h"
10
11 #include <utils/Log.h>
12 #include <utils/threads.h>
13 #include <utils/List.h>
14 #include <utils/Errors.h>
15
16 #include <fcntl.h>
17 #include <errno.h>
18
19 using namespace android;
20
21 /*
22 * Show version info. All the cool kids do it.
23 */
24 int doVersion(Bundle* bundle)
25 {
26 if (bundle->getFileSpecCount() != 0)
27 printf("(ignoring extra arguments)\n");
28 printf("Android Asset Packaging Tool, v0.2\n");
29
30 return 0;
31 }
32
33
34 /*
35 * Open the file read only. The call fails if the file doesn't exist.
36 *
37 * Returns NULL on failure.
38 */
39 ZipFile* openReadOnly(const char* fileName)
40 {
41 ZipFile* zip;
42 status_t result;
43
44 zip = new ZipFile;
45 result = zip->open(fileName, ZipFile::kOpenReadOnly);
46 if (result != NO_ERROR) {
47 if (result == NAME_NOT_FOUND)
48 fprintf(stderr, "ERROR: '%s' not found\n", fileName);
49 else if (result == PERMISSION_DENIED)
50 fprintf(stderr, "ERROR: '%s' access denied\n", fileName);
51 else
52 fprintf(stderr, "ERROR: failed opening '%s' as Zip file\n",
53 fileName);
54 delete zip;
55 return NULL;
56 }
57
58 return zip;
59 }
60
61 /*
62 * Open the file read-write. The file will be created if it doesn't
63 * already exist and "okayToCreate" is set.
64 *
65 * Returns NULL on failure.
66 */
67 ZipFile* openReadWrite(const char* fileName, bool okayToCreate)
68 {
69 ZipFile* zip = NULL;
70 status_t result;
71 int flags;
72
73 flags = ZipFile::kOpenReadWrite;
74 if (okayToCreate)
75 flags |= ZipFile::kOpenCreate;
76
77 zip = new ZipFile;
78 result = zip->open(fileName, flags);
79 if (result != NO_ERROR) {
80 delete zip;
81 zip = NULL;
82 goto bail;
83 }
84
85 bail:
86 return zip;
87 }
88
89
90 /*
91 * Return a short string describing the compression method.
92 */
93 const char* compressionName(int method)
94 {
95 if (method == ZipEntry::kCompressStored)
96 return "Stored";
97 else if (method == ZipEntry::kCompressDeflated)
98 return "Deflated";
99 else
100 return "Unknown";
101 }
102
103 /*
104 * Return the percent reduction in size (0% == no compression).
105 */
106 int calcPercent(long uncompressedLen, long compressedLen)
107 {
108 if (!uncompressedLen)
109 return 0;
110 else
111 return (int) (100.0 - (compressedLen * 100.0) / uncompressedLen + 0.5);
112 }
113
114 /*
115 * Handle the "list" command, which can be a simple file dump or
116 * a verbose listing.
117 *
118 * The verbose listing closely matches the output of the Info-ZIP "unzip"
119 * command.
120 */
121 int doList(Bundle* bundle)
122 {
123 int result = 1;
124 ZipFile* zip = NULL;
125 const ZipEntry* entry;
126 long totalUncLen, totalCompLen;
127 const char* zipFileName;
128
129 if (bundle->getFileSpecCount() != 1) {
130 fprintf(stderr, "ERROR: specify zip file name (only)\n");
131 goto bail;
132 }
133 zipFileName = bundle->getFileSpecEntry(0);
134
135 zip = openReadOnly(zipFileName);
136 if (zip == NULL)
137 goto bail;
138
139 int count, i;
140
141 if (bundle->getVerbose()) {
142 printf("Archive: %s\n", zipFileName);
143 printf(
144 " Length Method Size Ratio Date Time CRC-32 Name\n");
145 printf(
146 "-------- ------ ------- ----- ---- ---- ------ ----\n");
147 }
148
149 totalUncLen = totalCompLen = 0;
150
151 count = zip->getNumEntries();
152 for (i = 0; i < count; i++) {
153 entry = zip->getEntryByIndex(i);
154 if (bundle->getVerbose()) {
155 char dateBuf[32];
156 time_t when;
157
158 when = entry->getModWhen();
159 strftime(dateBuf, sizeof(dateBuf), "%m-%d-%y %H:%M",
160 localtime(&when));
161
162 printf("%8ld %-7.7s %7ld %3d%% %s %08lx %s\n",
163 (long) entry->getUncompressedLen(),
164 compressionName(entry->getCompressionMethod()),
165 (long) entry->getCompressedLen(),
166 calcPercent(entry->getUncompressedLen(),
167 entry->getCompressedLen()),
168 dateBuf,
169 entry->getCRC32(),
170 entry->getFileName());
171 } else {
172 printf("%s\n", entry->getFileName());
173 }
174
175 totalUncLen += entry->getUncompressedLen();
176 totalCompLen += entry->getCompressedLen();
177 }
178
179 if (bundle->getVerbose()) {
180 printf(
181 "-------- ------- --- -------\n");
182 printf("%8ld %7ld %2d%% %d files\n",
183 totalUncLen,
184 totalCompLen,
185 calcPercent(totalUncLen, totalCompLen),
186 zip->getNumEntries());
187 }
188
189 if (bundle->getAndroidList()) {
190 AssetManager assets;
191 if (!assets.addAssetPath(String8(zipFileName), NULL)) {
192 fprintf(stderr, "ERROR: list -a failed because assets could not be loaded\n");
193 goto bail;
194 }
195
196 const ResTable& res = assets.getResources(false);
197 if (&res == NULL) {
198 printf("\nNo resource table found.\n");
199 } else {
200 printf("\nResource table:\n");
201 res.print(false);
202 }
203
204 Asset* manifestAsset = assets.openNonAsset("AndroidManifest.xml",
205 Asset::ACCESS_BUFFER);
206 if (manifestAsset == NULL) {
207 printf("\nNo AndroidManifest.xml found.\n");
208 } else {
209 printf("\nAndroid manifest:\n");
210 ResXMLTree tree;
211 tree.setTo(manifestAsset->getBuffer(true),
212 manifestAsset->getLength());
213 printXMLBlock(&tree);
214 }
215 delete manifestAsset;
216 }
217
218 result = 0;
219
220 bail:
221 delete zip;
222 return result;
223 }
224
225 static ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes)
226 {
227 size_t N = tree.getAttributeCount();
228 for (size_t i=0; i<N; i++) {
229 if (tree.getAttributeNameResID(i) == attrRes) {
230 return (ssize_t)i;
231 }
232 }
233 return -1;
234 }
235
236 String8 getAttribute(const ResXMLTree& tree, const char* ns,
237 const char* attr, String8* outError)
238 {
239 ssize_t idx = tree.indexOfAttribute(ns, attr);
240 if (idx < 0) {
241 return String8();
242 }
243 Res_value value;
244 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
245 if (value.dataType != Res_value::TYPE_STRING) {
246 if (outError != NULL) *outError = "attribute is not a string value";
247 return String8();
248 }
249 }
250 size_t len;
251 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
252 return str ? String8(str, len) : String8();
253 }
254
255 static String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError)
256 {
257 ssize_t idx = indexOfAttribute(tree, attrRes);
258 if (idx < 0) {
259 return String8();
260 }
261 Res_value value;
262 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
263 if (value.dataType != Res_value::TYPE_STRING) {
264 if (outError != NULL) *outError = "attribute is not a string value";
265 return String8();
266 }
267 }
268 size_t len;
269 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
270 return str ? String8(str, len) : String8();
271 }
272
273 static int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes,
274 String8* outError, int32_t defValue = -1)
275 {
276 ssize_t idx = indexOfAttribute(tree, attrRes);
277 if (idx < 0) {
278 return defValue;
279 }
280 Res_value value;
281 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
282 if (value.dataType < Res_value::TYPE_FIRST_INT
283 || value.dataType > Res_value::TYPE_LAST_INT) {
284 if (outError != NULL) *outError = "attribute is not an integer value";
285 return defValue;
286 }
287 }
288 return value.data;
289 }
290
291 static String8 getResolvedAttribute(const ResTable* resTable, const ResXMLTree& tree,
292 uint32_t attrRes, String8* outError)
293 {
294 ssize_t idx = indexOfAttribute(tree, attrRes);
295 if (idx < 0) {
296 return String8();
297 }
298 Res_value value;
299 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
300 if (value.dataType == Res_value::TYPE_STRING) {
301 size_t len;
302 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
303 return str ? String8(str, len) : String8();
304 }
305 resTable->resolveReference(&value, 0);
306 if (value.dataType != Res_value::TYPE_STRING) {
307 if (outError != NULL) *outError = "attribute is not a string value";
308 return String8();
309 }
310 }
311 size_t len;
312 const Res_value* value2 = &value;
313 const char16_t* str = const_cast<ResTable*>(resTable)->valueToString(value2, 0, NULL, &len);
314 return str ? String8(str, len) : String8();
315 }
316
317 // These are attribute resource constants for the platform, as found
318 // in android.R.attr
319 enum {
320 NAME_ATTR = 0x01010003,
321 VERSION_CODE_ATTR = 0x0101021b,
322 VERSION_NAME_ATTR = 0x0101021c,
323 LABEL_ATTR = 0x01010001,
324 ICON_ATTR = 0x01010002,
325 MIN_SDK_VERSION_ATTR = 0x0101020c,
326 MAX_SDK_VERSION_ATTR = 0x01010271,
327 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
328 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
329 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
330 REQ_NAVIGATION_ATTR = 0x0101022a,
331 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
332 TARGET_SDK_VERSION_ATTR = 0x01010270,
333 TEST_ONLY_ATTR = 0x01010272,
334 DENSITY_ATTR = 0x0101026c,
335 GL_ES_VERSION_ATTR = 0x01010281,
336 SMALL_SCREEN_ATTR = 0x01010284,
337 NORMAL_SCREEN_ATTR = 0x01010285,
338 LARGE_SCREEN_ATTR = 0x01010286,
339 REQUIRED_ATTR = 0x0101028e,
340 };
341
342 const char *getComponentName(String8 &pkgName, String8 &componentName) {
343 ssize_t idx = componentName.find(".");
344 String8 retStr(pkgName);
345 if (idx == 0) {
346 retStr += componentName;
347 } else if (idx < 0) {
348 retStr += ".";
349 retStr += componentName;
350 } else {
351 return componentName.string();
352 }
353 return retStr.string();
354 }
355
356 /*
357 * Handle the "dump" command, to extract select data from an archive.
358 */
359 int doDump(Bundle* bundle)
360 {
361 status_t result = UNKNOWN_ERROR;
362 Asset* asset = NULL;
363
364 if (bundle->getFileSpecCount() < 1) {
365 fprintf(stderr, "ERROR: no dump option specified\n");
366 return 1;
367 }
368
369 if (bundle->getFileSpecCount() < 2) {
370 fprintf(stderr, "ERROR: no dump file specified\n");
371 return 1;
372 }
373
374 const char* option = bundle->getFileSpecEntry(0);
375 const char* filename = bundle->getFileSpecEntry(1);
376
377 AssetManager assets;
378 void* assetsCookie;
379 if (!assets.addAssetPath(String8(filename), &assetsCookie)) {
380 fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n");
381 return 1;
382 }
383
384 const ResTable& res = assets.getResources(false);
385 if (&res == NULL) {
386 fprintf(stderr, "ERROR: dump failed because no resource table was found\n");
387 goto bail;
388 }
389
390 if (strcmp("resources", option) == 0) {
391 res.print(bundle->getValues());
392
393 } else if (strcmp("xmltree", option) == 0) {
394 if (bundle->getFileSpecCount() < 3) {
395 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
396 goto bail;
397 }
398
399 for (int i=2; i<bundle->getFileSpecCount(); i++) {
400 const char* resname = bundle->getFileSpecEntry(i);
401 ResXMLTree tree;
402 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
403 if (asset == NULL) {
404 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
405 goto bail;
406 }
407
408 if (tree.setTo(asset->getBuffer(true),
409 asset->getLength()) != NO_ERROR) {
410 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
411 goto bail;
412 }
413 tree.restart();
414 printXMLBlock(&tree);
415 tree.uninit();
416 delete asset;
417 asset = NULL;
418 }
419
420 } else if (strcmp("xmlstrings", option) == 0) {
421 if (bundle->getFileSpecCount() < 3) {
422 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
423 goto bail;
424 }
425
426 for (int i=2; i<bundle->getFileSpecCount(); i++) {
427 const char* resname = bundle->getFileSpecEntry(i);
428 ResXMLTree tree;
429 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
430 if (asset == NULL) {
431 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
432 goto bail;
433 }
434
435 if (tree.setTo(asset->getBuffer(true),
436 asset->getLength()) != NO_ERROR) {
437 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
438 goto bail;
439 }
440 printStringPool(&tree.getStrings());
441 delete asset;
442 asset = NULL;
443 }
444
445 } else {
446 ResXMLTree tree;
447 asset = assets.openNonAsset("AndroidManifest.xml",
448 Asset::ACCESS_BUFFER);
449 if (asset == NULL) {
450 fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n");
451 goto bail;
452 }
453
454 if (tree.setTo(asset->getBuffer(true),
455 asset->getLength()) != NO_ERROR) {
456 fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n");
457 goto bail;
458 }
459 tree.restart();
460
461 if (strcmp("permissions", option) == 0) {
462 size_t len;
463 ResXMLTree::event_code_t code;
464 int depth = 0;
465 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
466 if (code == ResXMLTree::END_TAG) {
467 depth--;
468 continue;
469 }
470 if (code != ResXMLTree::START_TAG) {
471 continue;
472 }
473 depth++;
474 String8 tag(tree.getElementName(&len));
475 //printf("Depth %d tag %s\n", depth, tag.string());
476 if (depth == 1) {
477 if (tag != "manifest") {
478 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
479 goto bail;
480 }
481 String8 pkg = getAttribute(tree, NULL, "package", NULL);
482 printf("package: %s\n", pkg.string());
483 } else if (depth == 2 && tag == "permission") {
484 String8 error;
485 String8 name = getAttribute(tree, NAME_ATTR, &error);
486 if (error != "") {
487 fprintf(stderr, "ERROR: %s\n", error.string());
488 goto bail;
489 }
490 printf("permission: %s\n", name.string());
491 } else if (depth == 2 && tag == "uses-permission") {
492 String8 error;
493 String8 name = getAttribute(tree, NAME_ATTR, &error);
494 if (error != "") {
495 fprintf(stderr, "ERROR: %s\n", error.string());
496 goto bail;
497 }
498 printf("uses-permission: %s\n", name.string());
499 }
500 }
501 } else if (strcmp("badging", option) == 0) {
502 size_t len;
503 ResXMLTree::event_code_t code;
504 int depth = 0;
505 String8 error;
506 bool withinActivity = false;
507 bool isMainActivity = false;
508 bool isLauncherActivity = false;
509 bool isSearchable = false;
510 bool withinApplication = false;
511 bool withinReceiver = false;
512 bool withinService = false;
513 bool withinIntentFilter = false;
514 bool hasMainActivity = false;
515 bool hasOtherActivities = false;
516 bool hasOtherReceivers = false;
517 bool hasOtherServices = false;
518 bool hasWallpaperService = false;
519 bool hasImeService = false;
520 bool hasWidgetReceivers = false;
521 bool hasIntentFilter = false;
522 bool actMainActivity = false;
523 bool actWidgetReceivers = false;
524 bool actImeService = false;
525 bool actWallpaperService = false;
526 bool specCameraFeature = false;
527 bool hasCameraPermission = false;
528 bool specGpsFeature = false;
529 bool hasGpsPermission = false;
530 int targetSdk = 0;
531 int smallScreen = 1;
532 int normalScreen = 1;
533 int largeScreen = 1;
534 String8 pkg;
535 String8 activityName;
536 String8 activityLabel;
537 String8 activityIcon;
538 String8 receiverName;
539 String8 serviceName;
540 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
541 if (code == ResXMLTree::END_TAG) {
542 depth--;
543 if (depth < 2) {
544 withinApplication = false;
545 } else if (depth < 3) {
546 if (withinActivity && isMainActivity && isLauncherActivity) {
547 const char *aName = getComponentName(pkg, activityName);
548 if (aName != NULL) {
549 printf("launchable activity name='%s'", aName);
550 }
551 printf("label='%s' icon='%s'\n",
552 activityLabel.string(),
553 activityIcon.string());
554 }
555 if (!hasIntentFilter) {
556 hasOtherActivities |= withinActivity;
557 hasOtherReceivers |= withinReceiver;
558 hasOtherServices |= withinService;
559 }
560 withinActivity = false;
561 withinService = false;
562 withinReceiver = false;
563 hasIntentFilter = false;
564 isMainActivity = isLauncherActivity = false;
565 } else if (depth < 4) {
566 if (withinIntentFilter) {
567 if (withinActivity) {
568 hasMainActivity |= actMainActivity;
569 hasOtherActivities |= !actMainActivity;
570 } else if (withinReceiver) {
571 hasWidgetReceivers |= actWidgetReceivers;
572 hasOtherReceivers |= !actWidgetReceivers;
573 } else if (withinService) {
574 hasImeService |= actImeService;
575 hasWallpaperService |= actWallpaperService;
576 hasOtherServices |= (!actImeService && !actWallpaperService);
577 }
578 }
579 withinIntentFilter = false;
580 }
581 continue;
582 }
583 if (code != ResXMLTree::START_TAG) {
584 continue;
585 }
586 depth++;
587 String8 tag(tree.getElementName(&len));
588 //printf("Depth %d, %s\n", depth, tag.string());
589 if (depth == 1) {
590 if (tag != "manifest") {
591 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
592 goto bail;
593 }
594 pkg = getAttribute(tree, NULL, "package", NULL);
595 printf("package: name='%s' ", pkg.string());
596 int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error);
597 if (error != "") {
598 fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string());
599 goto bail;
600 }
601 if (versionCode > 0) {
602 printf("versionCode='%d' ", versionCode);
603 } else {
604 printf("versionCode='' ");
605 }
606 String8 versionName = getResolvedAttribute(&res, tree, VERSION_NAME_ATTR, &error);
607 if (error != "") {
608 fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string());
609 goto bail;
610 }
611 printf("versionName='%s'\n", versionName.string());
612 } else if (depth == 2) {
613 withinApplication = false;
614 if (tag == "application") {
615 withinApplication = true;
616 String8 label = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
617 if (error != "") {
618 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
619 goto bail;
620 }
621 printf("application: label='%s' ", label.string());
622 String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
623 if (error != "") {
624 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
625 goto bail;
626 }
627 printf("icon='%s'\n", icon.string());
628 int32_t testOnly = getIntegerAttribute(tree, TEST_ONLY_ATTR, &error, 0);
629 if (error != "") {
630 fprintf(stderr, "ERROR getting 'android:testOnly' attribute: %s\n", error.string());
631 goto bail;
632 }
633 if (testOnly != 0) {
634 printf("testOnly='%d'\n", testOnly);
635 }
636 } else if (tag == "uses-sdk") {
637 int32_t code = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error);
638 if (error != "") {
639 error = "";
640 String8 name = getResolvedAttribute(&res, tree, MIN_SDK_VERSION_ATTR, &error);
641 if (error != "") {
642 fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
643 error.string());
644 goto bail;
645 }
646 if (name == "Donut") targetSdk = 4;
647 printf("sdkVersion:'%s'\n", name.string());
648 } else if (code != -1) {
649 targetSdk = code;
650 printf("sdkVersion:'%d'\n", code);
651 }
652 code = getIntegerAttribute(tree, MAX_SDK_VERSION_ATTR, NULL, -1);
653 if (code != -1) {
654 printf("maxSdkVersion:'%d'\n", code);
655 }
656 code = getIntegerAttribute(tree, TARGET_SDK_VERSION_ATTR, &error);
657 if (error != "") {
658 error = "";
659 String8 name = getResolvedAttribute(&res, tree, TARGET_SDK_VERSION_ATTR, &error);
660 if (error != "") {
661 fprintf(stderr, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
662 error.string());
663 goto bail;
664 }
665 if (name == "Donut" && targetSdk < 4) targetSdk = 4;
666 printf("targetSdkVersion:'%s'\n", name.string());
667 } else if (code != -1) {
668 if (targetSdk < code) {
669 targetSdk = code;
670 }
671 printf("targetSdkVersion:'%d'\n", code);
672 }
673 } else if (tag == "uses-configuration") {
674 int32_t reqTouchScreen = getIntegerAttribute(tree,
675 REQ_TOUCH_SCREEN_ATTR, NULL, 0);
676 int32_t reqKeyboardType = getIntegerAttribute(tree,
677 REQ_KEYBOARD_TYPE_ATTR, NULL, 0);
678 int32_t reqHardKeyboard = getIntegerAttribute(tree,
679 REQ_HARD_KEYBOARD_ATTR, NULL, 0);
680 int32_t reqNavigation = getIntegerAttribute(tree,
681 REQ_NAVIGATION_ATTR, NULL, 0);
682 int32_t reqFiveWayNav = getIntegerAttribute(tree,
683 REQ_FIVE_WAY_NAV_ATTR, NULL, 0);
684 printf("uses-configuration:");
685 if (reqTouchScreen != 0) {
686 printf(" reqTouchScreen='%d'", reqTouchScreen);
687 }
688 if (reqKeyboardType != 0) {
689 printf(" reqKeyboardType='%d'", reqKeyboardType);
690 }
691 if (reqHardKeyboard != 0) {
692 printf(" reqHardKeyboard='%d'", reqHardKeyboard);
693 }
694 if (reqNavigation != 0) {
695 printf(" reqNavigation='%d'", reqNavigation);
696 }
697 if (reqFiveWayNav != 0) {
698 printf(" reqFiveWayNav='%d'", reqFiveWayNav);
699 }
700 printf("\n");
701 } else if (tag == "supports-density") {
702 int32_t dens = getIntegerAttribute(tree, DENSITY_ATTR, &error);
703 if (error != "") {
704 fprintf(stderr, "ERROR getting 'android:density' attribute: %s\n",
705 error.string());
706 goto bail;
707 }
708 printf("supports-density:'%d'\n", dens);
709 } else if (tag == "supports-screens") {
710 smallScreen = getIntegerAttribute(tree,
711 SMALL_SCREEN_ATTR, NULL, 1);
712 normalScreen = getIntegerAttribute(tree,
713 NORMAL_SCREEN_ATTR, NULL, 1);
714 largeScreen = getIntegerAttribute(tree,
715 LARGE_SCREEN_ATTR, NULL, 1);
716 } else if (tag == "uses-feature") {
717 String8 name = getAttribute(tree, NAME_ATTR, &error);
718
719 if (name != "" && error == "") {
720 int req = getIntegerAttribute(tree,
721 REQUIRED_ATTR, NULL, 1);
722 if (name == "android.hardware.camera") {
723 specCameraFeature = true;
724 } else if (name == "android.hardware.location.gps") {
725 specGpsFeature = true;
726 }
727 printf("uses-feature%s:'%s'\n",
728 req ? "" : "-not-required", name.string());
729 } else {
730 int vers = getIntegerAttribute(tree,
731 GL_ES_VERSION_ATTR, &error);
732 if (error == "") {
733 printf("uses-gl-es:'0x%x'\n", vers);
734 }
735 }
736 } else if (tag == "uses-permission") {
737 String8 name = getAttribute(tree, NAME_ATTR, &error);
738 if (name != "" && error == "") {
739 if (name == "android.permission.CAMERA") {
740 hasCameraPermission = true;
741 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
742 hasGpsPermission = true;
743 }
744 printf("uses-permission:'%s'\n", name.string());
745 } else {
746 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
747 error.string());
748 goto bail;
749 }
750 } else if (tag == "original-package") {
751 String8 name = getAttribute(tree, NAME_ATTR, &error);
752 if (name != "" && error == "") {
753 printf("original-package:'%s'\n", name.string());
754 } else {
755 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
756 error.string());
757 goto bail;
758 }
759 }
760 } else if (depth == 3 && withinApplication) {
761 withinActivity = false;
762 withinReceiver = false;
763 withinService = false;
764 hasIntentFilter = false;
765 if(tag == "activity") {
766 withinActivity = true;
767 activityName = getAttribute(tree, NAME_ATTR, &error);
768 if (error != "") {
769 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
770 goto bail;
771 }
772
773 activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
774 if (error != "") {
775 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
776 goto bail;
777 }
778
779 activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
780 if (error != "") {
781 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
782 goto bail;
783 }
784 } else if (tag == "uses-library") {
785 String8 libraryName = getAttribute(tree, NAME_ATTR, &error);
786 if (error != "") {
787 fprintf(stderr, "ERROR getting 'android:name' attribute for uses-library: %s\n", error.string());
788 goto bail;
789 }
790 int req = getIntegerAttribute(tree,
791 REQUIRED_ATTR, NULL, 1);
792 printf("uses-library%s:'%s'\n",
793 req ? "" : "-not-required", libraryName.string());
794 } else if (tag == "receiver") {
795 withinReceiver = true;
796 receiverName = getAttribute(tree, NAME_ATTR, &error);
797
798 if (error != "") {
799 fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string());
800 goto bail;
801 }
802 } else if (tag == "service") {
803 withinService = true;
804 serviceName = getAttribute(tree, NAME_ATTR, &error);
805
806 if (error != "") {
807 fprintf(stderr, "ERROR getting 'android:name' attribute for service: %s\n", error.string());
808 goto bail;
809 }
810 }
811 } else if ((depth == 4) && (tag == "intent-filter")) {
812 hasIntentFilter = true;
813 withinIntentFilter = true;
814 actMainActivity = actWidgetReceivers = actImeService = actWallpaperService = false;
815 } else if ((depth == 5) && withinIntentFilter){
816 String8 action;
817 if (tag == "action") {
818 action = getAttribute(tree, NAME_ATTR, &error);
819 if (error != "") {
820 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
821 goto bail;
822 }
823 if (withinActivity) {
824 if (action == "android.intent.action.MAIN") {
825 isMainActivity = true;
826 actMainActivity = true;
827 }
828 } else if (withinReceiver) {
829 if (action == "android.appwidget.action.APPWIDGET_UPDATE") {
830 actWidgetReceivers = true;
831 }
832 } else if (withinService) {
833 if (action == "android.view.InputMethod") {
834 actImeService = true;
835 } else if (action == "android.service.wallpaper.WallpaperService") {
836 actWallpaperService = true;
837 }
838 }
839 if (action == "android.intent.action.SEARCH") {
840 isSearchable = true;
841 }
842 }
843
844 if (tag == "category") {
845 String8 category = getAttribute(tree, NAME_ATTR, &error);
846 if (error != "") {
847 fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string());
848 goto bail;
849 }
850 if (withinActivity) {
851 if (category == "android.intent.category.LAUNCHER") {
852 isLauncherActivity = true;
853 }
854 }
855 }
856 }
857 }
858
859 if (!specCameraFeature && hasCameraPermission) {
860 // For applications that have not explicitly stated their
861 // camera feature requirements, but have requested the camera
862 // permission, we are going to give them compatibility treatment
863 // of requiring the equivalent to original android devices.
864 printf("uses-feature:'android.hardware.camera'\n");
865 printf("uses-feature:'android.hardware.camera.autofocus'\n");
866 }
867
868 if (!specGpsFeature && hasGpsPermission) {
869 // For applications that have not explicitly stated their
870 // GPS feature requirements, but have requested the "fine" (GPS)
871 // permission, we are going to give them compatibility treatment
872 // of requiring the equivalent to original android devices.
873 printf("uses-feature:'android.hardware.location.gps'\n");
874 }
875
876 if (hasMainActivity) {
877 printf("main\n");
878 }
879 if (hasWidgetReceivers) {
880 printf("app-widget\n");
881 }
882 if (hasImeService) {
883 printf("ime\n");
884 }
885 if (hasWallpaperService) {
886 printf("wallpaper\n");
887 }
888 if (hasOtherActivities) {
889 printf("other-activities\n");
890 }
891 if (isSearchable) {
892 printf("search\n");
893 }
894 if (hasOtherReceivers) {
895 printf("other-receivers\n");
896 }
897 if (hasOtherServices) {
898 printf("other-services\n");
899 }
900
901 // Determine default values for any unspecified screen sizes,
902 // based on the target SDK of the package. As of 4 (donut)
903 // the screen size support was introduced, so all default to
904 // enabled.
905 if (smallScreen > 0) {
906 smallScreen = targetSdk >= 4 ? -1 : 0;
907 }
908 if (normalScreen > 0) {
909 normalScreen = -1;
910 }
911 if (largeScreen > 0) {
912 largeScreen = targetSdk >= 4 ? -1 : 0;
913 }
914 printf("supports-screens:");
915 if (smallScreen != 0) printf(" 'small'");
916 if (normalScreen != 0) printf(" 'normal'");
917 if (largeScreen != 0) printf(" 'large'");
918 printf("\n");
919
920 printf("locales:");
921 Vector<String8> locales;
922 res.getLocales(&locales);
923 const size_t NL = locales.size();
924 for (size_t i=0; i<NL; i++) {
925 const char* localeStr = locales[i].string();
926 if (localeStr == NULL || strlen(localeStr) == 0) {
927 localeStr = "--_--";
928 }
929 printf(" '%s'", localeStr);
930 }
931 printf("\n");
932
933 Vector<ResTable_config> configs;
934 res.getConfigurations(&configs);
935 SortedVector<int> densities;
936 const size_t NC = configs.size();
937 for (size_t i=0; i<NC; i++) {
938 int dens = configs[i].density;
939 if (dens == 0) dens = 160;
940 densities.add(dens);
941 }
942
943 printf("densities:");
944 const size_t ND = densities.size();
945 for (size_t i=0; i<ND; i++) {
946 printf(" '%d'", densities[i]);
947 }
948 printf("\n");
949
950 AssetDir* dir = assets.openNonAssetDir(assetsCookie, "lib");
951 if (dir != NULL) {
952 if (dir->getFileCount() > 0) {
953 printf("native-code:");
954 for (size_t i=0; i<dir->getFileCount(); i++) {
955 printf(" '%s'", dir->getFileName(i).string());
956 }
957 printf("\n");
958 }
959 delete dir;
960 }
961 } else if (strcmp("configurations", option) == 0) {
962 Vector<ResTable_config> configs;
963 res.getConfigurations(&configs);
964 const size_t N = configs.size();
965 for (size_t i=0; i<N; i++) {
966 printf("%s\n", configs[i].toString().string());
967 }
968 } else {
969 fprintf(stderr, "ERROR: unknown dump option '%s'\n", option);
970 goto bail;
971 }
972 }
973
974 result = NO_ERROR;
975
976 bail:
977 if (asset) {
978 delete asset;
979 }
980 return (result != NO_ERROR);
981 }
982
983
984 /*
985 * Handle the "add" command, which wants to add files to a new or
986 * pre-existing archive.
987 */
988 int doAdd(Bundle* bundle)
989 {
990 ZipFile* zip = NULL;
991 status_t result = UNKNOWN_ERROR;
992 const char* zipFileName;
993
994 if (bundle->getUpdate()) {
995 /* avoid confusion */
996 fprintf(stderr, "ERROR: can't use '-u' with add\n");
997 goto bail;
998 }
999
1000 if (bundle->getFileSpecCount() < 1) {
1001 fprintf(stderr, "ERROR: must specify zip file name\n");
1002 goto bail;
1003 }
1004 zipFileName = bundle->getFileSpecEntry(0);
1005
1006 if (bundle->getFileSpecCount() < 2) {
1007 fprintf(stderr, "NOTE: nothing to do\n");
1008 goto bail;
1009 }
1010
1011 zip = openReadWrite(zipFileName, true);
1012 if (zip == NULL) {
1013 fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName);
1014 goto bail;
1015 }
1016
1017 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1018 const char* fileName = bundle->getFileSpecEntry(i);
1019
1020 if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) {
1021 printf(" '%s'... (from gzip)\n", fileName);
1022 result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL);
1023 } else {
1024 if (bundle->getJunkPath()) {
1025 String8 storageName = String8(fileName).getPathLeaf();
1026 printf(" '%s' as '%s'...\n", fileName, storageName.string());
1027 result = zip->add(fileName, storageName.string(),
1028 bundle->getCompressionMethod(), NULL);
1029 } else {
1030 printf(" '%s'...\n", fileName);
1031 result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
1032 }
1033 }
1034 if (result != NO_ERROR) {
1035 fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName);
1036 if (result == NAME_NOT_FOUND)
1037 fprintf(stderr, ": file not found\n");
1038 else if (result == ALREADY_EXISTS)
1039 fprintf(stderr, ": already exists in archive\n");
1040 else
1041 fprintf(stderr, "\n");
1042 goto bail;
1043 }
1044 }
1045
1046 result = NO_ERROR;
1047
1048 bail:
1049 delete zip;
1050 return (result != NO_ERROR);
1051 }
1052
1053
1054 /*
1055 * Delete files from an existing archive.
1056 */
1057 int doRemove(Bundle* bundle)
1058 {
1059 ZipFile* zip = NULL;
1060 status_t result = UNKNOWN_ERROR;
1061 const char* zipFileName;
1062
1063 if (bundle->getFileSpecCount() < 1) {
1064 fprintf(stderr, "ERROR: must specify zip file name\n");
1065 goto bail;
1066 }
1067 zipFileName = bundle->getFileSpecEntry(0);
1068
1069 if (bundle->getFileSpecCount() < 2) {
1070 fprintf(stderr, "NOTE: nothing to do\n");
1071 goto bail;
1072 }
1073
1074 zip = openReadWrite(zipFileName, false);
1075 if (zip == NULL) {
1076 fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n",
1077 zipFileName);
1078 goto bail;
1079 }
1080
1081 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1082 const char* fileName = bundle->getFileSpecEntry(i);
1083 ZipEntry* entry;
1084
1085 entry = zip->getEntryByName(fileName);
1086 if (entry == NULL) {
1087 printf(" '%s' NOT FOUND\n", fileName);
1088 continue;
1089 }
1090
1091 result = zip->remove(entry);
1092
1093 if (result != NO_ERROR) {
1094 fprintf(stderr, "Unable to delete '%s' from '%s'\n",
1095 bundle->getFileSpecEntry(i), zipFileName);
1096 goto bail;
1097 }
1098 }
1099
1100 /* update the archive */
1101 zip->flush();
1102
1103 bail:
1104 delete zip;
1105 return (result != NO_ERROR);
1106 }
1107
1108
1109 /*
1110 * Package up an asset directory and associated application files.
1111 */
1112 int doPackage(Bundle* bundle)
1113 {
1114 const char* outputAPKFile;
1115 int retVal = 1;
1116 status_t err;
1117 sp<AaptAssets> assets;
1118 int N;
1119
1120 // -c zz_ZZ means do pseudolocalization
1121 ResourceFilter filter;
1122 err = filter.parse(bundle->getConfigurations());
1123 if (err != NO_ERROR) {
1124 goto bail;
1125 }
1126 if (filter.containsPseudo()) {
1127 bundle->setPseudolocalize(true);
1128 }
1129
1130 N = bundle->getFileSpecCount();
1131 if (N < 1 && bundle->getResourceSourceDirs().size() == 0 && bundle->getJarFiles().size() == 0
1132 && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) {
1133 fprintf(stderr, "ERROR: no input files\n");
1134 goto bail;
1135 }
1136
1137 outputAPKFile = bundle->getOutputAPKFile();
1138
1139 // Make sure the filenames provided exist and are of the appropriate type.
1140 if (outputAPKFile) {
1141 FileType type;
1142 type = getFileType(outputAPKFile);
1143 if (type != kFileTypeNonexistent && type != kFileTypeRegular) {
1144 fprintf(stderr,
1145 "ERROR: output file '%s' exists but is not regular file\n",
1146 outputAPKFile);
1147 goto bail;
1148 }
1149 }
1150
1151 // Load the assets.
1152 assets = new AaptAssets();
1153 err = assets->slurpFromArgs(bundle);
1154 if (err < 0) {
1155 goto bail;
1156 }
1157
1158 if (bundle->getVerbose()) {
1159 assets->print();
1160 }
1161
1162 // If they asked for any files that need to be compiled, do so.
1163 if (bundle->getResourceSourceDirs().size() || bundle->getAndroidManifestFile()) {
1164 err = buildResources(bundle, assets);
1165 if (err != 0) {
1166 goto bail;
1167 }
1168 }
1169
1170 // At this point we've read everything and processed everything. From here
1171 // on out it's just writing output files.
1172 if (SourcePos::hasErrors()) {
1173 goto bail;
1174 }
1175
1176 // Write out R.java constants
1177 if (assets->getPackage() == assets->getSymbolsPrivatePackage()) {
1178 if (bundle->getCustomPackage() == NULL) {
1179 err = writeResourceSymbols(bundle, assets, assets->getPackage(), true);
1180 } else {
1181 const String8 customPkg(bundle->getCustomPackage());
1182 err = writeResourceSymbols(bundle, assets, customPkg, true);
1183 }
1184 if (err < 0) {
1185 goto bail;
1186 }
1187 } else {
1188 err = writeResourceSymbols(bundle, assets, assets->getPackage(), false);
1189 if (err < 0) {
1190 goto bail;
1191 }
1192 err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true);
1193 if (err < 0) {
1194 goto bail;
1195 }
1196 }
1197
1198 // Write out the ProGuard file
1199 err = writeProguardFile(bundle, assets);
1200 if (err < 0) {
1201 goto bail;
1202 }
1203
1204 // Write the apk
1205 if (outputAPKFile) {
1206 err = writeAPK(bundle, assets, String8(outputAPKFile));
1207 if (err != NO_ERROR) {
1208 fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile);
1209 goto bail;
1210 }
1211 }
1212
1213 retVal = 0;
1214 bail:
1215 if (SourcePos::hasErrors()) {
1216 SourcePos::printErrors(stderr);
1217 }
1218 return retVal;
1219 }