]> git.saurik.com Git - android/aapt.git/blob - Command.cpp
4469e68420902622cc504711f35c6c8ea845a744
[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 Offset 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%% %8zd %s %08lx %s\n",
163 (long) entry->getUncompressedLen(),
164 compressionName(entry->getCompressionMethod()),
165 (long) entry->getCompressedLen(),
166 calcPercent(entry->getUncompressedLen(),
167 entry->getCompressedLen()),
168 (size_t) entry->getLFHOffset(),
169 dateBuf,
170 entry->getCRC32(),
171 entry->getFileName());
172 } else {
173 printf("%s\n", entry->getFileName());
174 }
175
176 totalUncLen += entry->getUncompressedLen();
177 totalCompLen += entry->getCompressedLen();
178 }
179
180 if (bundle->getVerbose()) {
181 printf(
182 "-------- ------- --- -------\n");
183 printf("%8ld %7ld %2d%% %d files\n",
184 totalUncLen,
185 totalCompLen,
186 calcPercent(totalUncLen, totalCompLen),
187 zip->getNumEntries());
188 }
189
190 if (bundle->getAndroidList()) {
191 AssetManager assets;
192 if (!assets.addAssetPath(String8(zipFileName), NULL)) {
193 fprintf(stderr, "ERROR: list -a failed because assets could not be loaded\n");
194 goto bail;
195 }
196
197 const ResTable& res = assets.getResources(false);
198 if (&res == NULL) {
199 printf("\nNo resource table found.\n");
200 } else {
201 #ifndef HAVE_ANDROID_OS
202 printf("\nResource table:\n");
203 res.print(false);
204 #endif
205 }
206
207 Asset* manifestAsset = assets.openNonAsset("AndroidManifest.xml",
208 Asset::ACCESS_BUFFER);
209 if (manifestAsset == NULL) {
210 printf("\nNo AndroidManifest.xml found.\n");
211 } else {
212 printf("\nAndroid manifest:\n");
213 ResXMLTree tree;
214 tree.setTo(manifestAsset->getBuffer(true),
215 manifestAsset->getLength());
216 printXMLBlock(&tree);
217 }
218 delete manifestAsset;
219 }
220
221 result = 0;
222
223 bail:
224 delete zip;
225 return result;
226 }
227
228 static ssize_t indexOfAttribute(const ResXMLTree& tree, uint32_t attrRes)
229 {
230 size_t N = tree.getAttributeCount();
231 for (size_t i=0; i<N; i++) {
232 if (tree.getAttributeNameResID(i) == attrRes) {
233 return (ssize_t)i;
234 }
235 }
236 return -1;
237 }
238
239 String8 getAttribute(const ResXMLTree& tree, const char* ns,
240 const char* attr, String8* outError)
241 {
242 ssize_t idx = tree.indexOfAttribute(ns, attr);
243 if (idx < 0) {
244 return String8();
245 }
246 Res_value value;
247 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
248 if (value.dataType != Res_value::TYPE_STRING) {
249 if (outError != NULL) *outError = "attribute is not a string value";
250 return String8();
251 }
252 }
253 size_t len;
254 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
255 return str ? String8(str, len) : String8();
256 }
257
258 static String8 getAttribute(const ResXMLTree& tree, uint32_t attrRes, String8* outError)
259 {
260 ssize_t idx = indexOfAttribute(tree, attrRes);
261 if (idx < 0) {
262 return String8();
263 }
264 Res_value value;
265 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
266 if (value.dataType != Res_value::TYPE_STRING) {
267 if (outError != NULL) *outError = "attribute is not a string value";
268 return String8();
269 }
270 }
271 size_t len;
272 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
273 return str ? String8(str, len) : String8();
274 }
275
276 static int32_t getIntegerAttribute(const ResXMLTree& tree, uint32_t attrRes,
277 String8* outError, int32_t defValue = -1)
278 {
279 ssize_t idx = indexOfAttribute(tree, attrRes);
280 if (idx < 0) {
281 return defValue;
282 }
283 Res_value value;
284 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
285 if (value.dataType < Res_value::TYPE_FIRST_INT
286 || value.dataType > Res_value::TYPE_LAST_INT) {
287 if (outError != NULL) *outError = "attribute is not an integer value";
288 return defValue;
289 }
290 }
291 return value.data;
292 }
293
294 static String8 getResolvedAttribute(const ResTable* resTable, const ResXMLTree& tree,
295 uint32_t attrRes, String8* outError)
296 {
297 ssize_t idx = indexOfAttribute(tree, attrRes);
298 if (idx < 0) {
299 return String8();
300 }
301 Res_value value;
302 if (tree.getAttributeValue(idx, &value) != NO_ERROR) {
303 if (value.dataType == Res_value::TYPE_STRING) {
304 size_t len;
305 const uint16_t* str = tree.getAttributeStringValue(idx, &len);
306 return str ? String8(str, len) : String8();
307 }
308 resTable->resolveReference(&value, 0);
309 if (value.dataType != Res_value::TYPE_STRING) {
310 if (outError != NULL) *outError = "attribute is not a string value";
311 return String8();
312 }
313 }
314 size_t len;
315 const Res_value* value2 = &value;
316 const char16_t* str = const_cast<ResTable*>(resTable)->valueToString(value2, 0, NULL, &len);
317 return str ? String8(str, len) : String8();
318 }
319
320 // These are attribute resource constants for the platform, as found
321 // in android.R.attr
322 enum {
323 NAME_ATTR = 0x01010003,
324 VERSION_CODE_ATTR = 0x0101021b,
325 VERSION_NAME_ATTR = 0x0101021c,
326 LABEL_ATTR = 0x01010001,
327 ICON_ATTR = 0x01010002,
328 MIN_SDK_VERSION_ATTR = 0x0101020c,
329 MAX_SDK_VERSION_ATTR = 0x01010271,
330 REQ_TOUCH_SCREEN_ATTR = 0x01010227,
331 REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
332 REQ_HARD_KEYBOARD_ATTR = 0x01010229,
333 REQ_NAVIGATION_ATTR = 0x0101022a,
334 REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
335 TARGET_SDK_VERSION_ATTR = 0x01010270,
336 TEST_ONLY_ATTR = 0x01010272,
337 DENSITY_ATTR = 0x0101026c,
338 GL_ES_VERSION_ATTR = 0x01010281,
339 SMALL_SCREEN_ATTR = 0x01010284,
340 NORMAL_SCREEN_ATTR = 0x01010285,
341 LARGE_SCREEN_ATTR = 0x01010286,
342 XLARGE_SCREEN_ATTR = 0x010102bf,
343 REQUIRED_ATTR = 0x0101028e,
344 };
345
346 const char *getComponentName(String8 &pkgName, String8 &componentName) {
347 ssize_t idx = componentName.find(".");
348 String8 retStr(pkgName);
349 if (idx == 0) {
350 retStr += componentName;
351 } else if (idx < 0) {
352 retStr += ".";
353 retStr += componentName;
354 } else {
355 return componentName.string();
356 }
357 return retStr.string();
358 }
359
360 /*
361 * Handle the "dump" command, to extract select data from an archive.
362 */
363 int doDump(Bundle* bundle)
364 {
365 status_t result = UNKNOWN_ERROR;
366 Asset* asset = NULL;
367
368 if (bundle->getFileSpecCount() < 1) {
369 fprintf(stderr, "ERROR: no dump option specified\n");
370 return 1;
371 }
372
373 if (bundle->getFileSpecCount() < 2) {
374 fprintf(stderr, "ERROR: no dump file specified\n");
375 return 1;
376 }
377
378 const char* option = bundle->getFileSpecEntry(0);
379 const char* filename = bundle->getFileSpecEntry(1);
380
381 AssetManager assets;
382 void* assetsCookie;
383 if (!assets.addAssetPath(String8(filename), &assetsCookie)) {
384 fprintf(stderr, "ERROR: dump failed because assets could not be loaded\n");
385 return 1;
386 }
387
388 const ResTable& res = assets.getResources(false);
389 if (&res == NULL) {
390 fprintf(stderr, "ERROR: dump failed because no resource table was found\n");
391 goto bail;
392 }
393
394 if (strcmp("resources", option) == 0) {
395 #ifndef HAVE_ANDROID_OS
396 res.print(bundle->getValues());
397 #endif
398 } else if (strcmp("xmltree", option) == 0) {
399 if (bundle->getFileSpecCount() < 3) {
400 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
401 goto bail;
402 }
403
404 for (int i=2; i<bundle->getFileSpecCount(); i++) {
405 const char* resname = bundle->getFileSpecEntry(i);
406 ResXMLTree tree;
407 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
408 if (asset == NULL) {
409 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
410 goto bail;
411 }
412
413 if (tree.setTo(asset->getBuffer(true),
414 asset->getLength()) != NO_ERROR) {
415 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
416 goto bail;
417 }
418 tree.restart();
419 printXMLBlock(&tree);
420 tree.uninit();
421 delete asset;
422 asset = NULL;
423 }
424
425 } else if (strcmp("xmlstrings", option) == 0) {
426 if (bundle->getFileSpecCount() < 3) {
427 fprintf(stderr, "ERROR: no dump xmltree resource file specified\n");
428 goto bail;
429 }
430
431 for (int i=2; i<bundle->getFileSpecCount(); i++) {
432 const char* resname = bundle->getFileSpecEntry(i);
433 ResXMLTree tree;
434 asset = assets.openNonAsset(resname, Asset::ACCESS_BUFFER);
435 if (asset == NULL) {
436 fprintf(stderr, "ERROR: dump failed because resource %s found\n", resname);
437 goto bail;
438 }
439
440 if (tree.setTo(asset->getBuffer(true),
441 asset->getLength()) != NO_ERROR) {
442 fprintf(stderr, "ERROR: Resource %s is corrupt\n", resname);
443 goto bail;
444 }
445 printStringPool(&tree.getStrings());
446 delete asset;
447 asset = NULL;
448 }
449
450 } else {
451 ResXMLTree tree;
452 asset = assets.openNonAsset("AndroidManifest.xml",
453 Asset::ACCESS_BUFFER);
454 if (asset == NULL) {
455 fprintf(stderr, "ERROR: dump failed because no AndroidManifest.xml found\n");
456 goto bail;
457 }
458
459 if (tree.setTo(asset->getBuffer(true),
460 asset->getLength()) != NO_ERROR) {
461 fprintf(stderr, "ERROR: AndroidManifest.xml is corrupt\n");
462 goto bail;
463 }
464 tree.restart();
465
466 if (strcmp("permissions", option) == 0) {
467 size_t len;
468 ResXMLTree::event_code_t code;
469 int depth = 0;
470 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
471 if (code == ResXMLTree::END_TAG) {
472 depth--;
473 continue;
474 }
475 if (code != ResXMLTree::START_TAG) {
476 continue;
477 }
478 depth++;
479 String8 tag(tree.getElementName(&len));
480 //printf("Depth %d tag %s\n", depth, tag.string());
481 if (depth == 1) {
482 if (tag != "manifest") {
483 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
484 goto bail;
485 }
486 String8 pkg = getAttribute(tree, NULL, "package", NULL);
487 printf("package: %s\n", pkg.string());
488 } else if (depth == 2 && tag == "permission") {
489 String8 error;
490 String8 name = getAttribute(tree, NAME_ATTR, &error);
491 if (error != "") {
492 fprintf(stderr, "ERROR: %s\n", error.string());
493 goto bail;
494 }
495 printf("permission: %s\n", name.string());
496 } else if (depth == 2 && tag == "uses-permission") {
497 String8 error;
498 String8 name = getAttribute(tree, NAME_ATTR, &error);
499 if (error != "") {
500 fprintf(stderr, "ERROR: %s\n", error.string());
501 goto bail;
502 }
503 printf("uses-permission: %s\n", name.string());
504 }
505 }
506 } else if (strcmp("badging", option) == 0) {
507 size_t len;
508 ResXMLTree::event_code_t code;
509 int depth = 0;
510 String8 error;
511 bool withinActivity = false;
512 bool isMainActivity = false;
513 bool isLauncherActivity = false;
514 bool isSearchable = false;
515 bool withinApplication = false;
516 bool withinReceiver = false;
517 bool withinService = false;
518 bool withinIntentFilter = false;
519 bool hasMainActivity = false;
520 bool hasOtherActivities = false;
521 bool hasOtherReceivers = false;
522 bool hasOtherServices = false;
523 bool hasWallpaperService = false;
524 bool hasImeService = false;
525 bool hasWidgetReceivers = false;
526 bool hasIntentFilter = false;
527 bool actMainActivity = false;
528 bool actWidgetReceivers = false;
529 bool actImeService = false;
530 bool actWallpaperService = false;
531
532 // This next group of variables is used to implement a group of
533 // backward-compatibility heuristics necessitated by the addition of
534 // some new uses-feature constants in 2.1 and 2.2. In most cases, the
535 // heuristic is "if an app requests a permission but doesn't explicitly
536 // request the corresponding <uses-feature>, presume it's there anyway".
537 bool specCameraFeature = false; // camera-related
538 bool specCameraAutofocusFeature = false;
539 bool reqCameraAutofocusFeature = false;
540 bool reqCameraFlashFeature = false;
541 bool hasCameraPermission = false;
542 bool specLocationFeature = false; // location-related
543 bool specNetworkLocFeature = false;
544 bool reqNetworkLocFeature = false;
545 bool specGpsFeature = false;
546 bool reqGpsFeature = false;
547 bool hasMockLocPermission = false;
548 bool hasCoarseLocPermission = false;
549 bool hasGpsPermission = false;
550 bool hasGeneralLocPermission = false;
551 bool specBluetoothFeature = false; // Bluetooth API-related
552 bool hasBluetoothPermission = false;
553 bool specMicrophoneFeature = false; // microphone-related
554 bool hasRecordAudioPermission = false;
555 bool specWiFiFeature = false;
556 bool hasWiFiPermission = false;
557 bool specTelephonyFeature = false; // telephony-related
558 bool reqTelephonySubFeature = false;
559 bool hasTelephonyPermission = false;
560 bool specTouchscreenFeature = false; // touchscreen-related
561 bool specMultitouchFeature = false;
562 bool reqDistinctMultitouchFeature = false;
563 // 2.2 also added some other features that apps can request, but that
564 // have no corresponding permission, so we cannot implement any
565 // back-compatibility heuristic for them. The below are thus unnecessary
566 // (but are retained here for documentary purposes.)
567 //bool specCompassFeature = false;
568 //bool specAccelerometerFeature = false;
569 //bool specProximityFeature = false;
570 //bool specAmbientLightFeature = false;
571 //bool specLiveWallpaperFeature = false;
572
573 int targetSdk = 0;
574 int smallScreen = 1;
575 int normalScreen = 1;
576 int largeScreen = 1;
577 int xlargeScreen = 1;
578 String8 pkg;
579 String8 activityName;
580 String8 activityLabel;
581 String8 activityIcon;
582 String8 receiverName;
583 String8 serviceName;
584 while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) {
585 if (code == ResXMLTree::END_TAG) {
586 depth--;
587 if (depth < 2) {
588 withinApplication = false;
589 } else if (depth < 3) {
590 if (withinActivity && isMainActivity && isLauncherActivity) {
591 const char *aName = getComponentName(pkg, activityName);
592 if (aName != NULL) {
593 printf("launchable activity name='%s'", aName);
594 }
595 printf("label='%s' icon='%s'\n",
596 activityLabel.string(),
597 activityIcon.string());
598 }
599 if (!hasIntentFilter) {
600 hasOtherActivities |= withinActivity;
601 hasOtherReceivers |= withinReceiver;
602 hasOtherServices |= withinService;
603 }
604 withinActivity = false;
605 withinService = false;
606 withinReceiver = false;
607 hasIntentFilter = false;
608 isMainActivity = isLauncherActivity = false;
609 } else if (depth < 4) {
610 if (withinIntentFilter) {
611 if (withinActivity) {
612 hasMainActivity |= actMainActivity;
613 hasOtherActivities |= !actMainActivity;
614 } else if (withinReceiver) {
615 hasWidgetReceivers |= actWidgetReceivers;
616 hasOtherReceivers |= !actWidgetReceivers;
617 } else if (withinService) {
618 hasImeService |= actImeService;
619 hasWallpaperService |= actWallpaperService;
620 hasOtherServices |= (!actImeService && !actWallpaperService);
621 }
622 }
623 withinIntentFilter = false;
624 }
625 continue;
626 }
627 if (code != ResXMLTree::START_TAG) {
628 continue;
629 }
630 depth++;
631 String8 tag(tree.getElementName(&len));
632 //printf("Depth %d, %s\n", depth, tag.string());
633 if (depth == 1) {
634 if (tag != "manifest") {
635 fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n");
636 goto bail;
637 }
638 pkg = getAttribute(tree, NULL, "package", NULL);
639 printf("package: name='%s' ", pkg.string());
640 int32_t versionCode = getIntegerAttribute(tree, VERSION_CODE_ATTR, &error);
641 if (error != "") {
642 fprintf(stderr, "ERROR getting 'android:versionCode' attribute: %s\n", error.string());
643 goto bail;
644 }
645 if (versionCode > 0) {
646 printf("versionCode='%d' ", versionCode);
647 } else {
648 printf("versionCode='' ");
649 }
650 String8 versionName = getResolvedAttribute(&res, tree, VERSION_NAME_ATTR, &error);
651 if (error != "") {
652 fprintf(stderr, "ERROR getting 'android:versionName' attribute: %s\n", error.string());
653 goto bail;
654 }
655 printf("versionName='%s'\n", versionName.string());
656 } else if (depth == 2) {
657 withinApplication = false;
658 if (tag == "application") {
659 withinApplication = true;
660 String8 label = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
661 if (error != "") {
662 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
663 goto bail;
664 }
665 printf("application: label='%s' ", label.string());
666 String8 icon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
667 if (error != "") {
668 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
669 goto bail;
670 }
671 printf("icon='%s'\n", icon.string());
672 int32_t testOnly = getIntegerAttribute(tree, TEST_ONLY_ATTR, &error, 0);
673 if (error != "") {
674 fprintf(stderr, "ERROR getting 'android:testOnly' attribute: %s\n", error.string());
675 goto bail;
676 }
677 if (testOnly != 0) {
678 printf("testOnly='%d'\n", testOnly);
679 }
680 } else if (tag == "uses-sdk") {
681 int32_t code = getIntegerAttribute(tree, MIN_SDK_VERSION_ATTR, &error);
682 if (error != "") {
683 error = "";
684 String8 name = getResolvedAttribute(&res, tree, MIN_SDK_VERSION_ATTR, &error);
685 if (error != "") {
686 fprintf(stderr, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
687 error.string());
688 goto bail;
689 }
690 if (name == "Donut") targetSdk = 4;
691 printf("sdkVersion:'%s'\n", name.string());
692 } else if (code != -1) {
693 targetSdk = code;
694 printf("sdkVersion:'%d'\n", code);
695 }
696 code = getIntegerAttribute(tree, MAX_SDK_VERSION_ATTR, NULL, -1);
697 if (code != -1) {
698 printf("maxSdkVersion:'%d'\n", code);
699 }
700 code = getIntegerAttribute(tree, TARGET_SDK_VERSION_ATTR, &error);
701 if (error != "") {
702 error = "";
703 String8 name = getResolvedAttribute(&res, tree, TARGET_SDK_VERSION_ATTR, &error);
704 if (error != "") {
705 fprintf(stderr, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
706 error.string());
707 goto bail;
708 }
709 if (name == "Donut" && targetSdk < 4) targetSdk = 4;
710 printf("targetSdkVersion:'%s'\n", name.string());
711 } else if (code != -1) {
712 if (targetSdk < code) {
713 targetSdk = code;
714 }
715 printf("targetSdkVersion:'%d'\n", code);
716 }
717 } else if (tag == "uses-configuration") {
718 int32_t reqTouchScreen = getIntegerAttribute(tree,
719 REQ_TOUCH_SCREEN_ATTR, NULL, 0);
720 int32_t reqKeyboardType = getIntegerAttribute(tree,
721 REQ_KEYBOARD_TYPE_ATTR, NULL, 0);
722 int32_t reqHardKeyboard = getIntegerAttribute(tree,
723 REQ_HARD_KEYBOARD_ATTR, NULL, 0);
724 int32_t reqNavigation = getIntegerAttribute(tree,
725 REQ_NAVIGATION_ATTR, NULL, 0);
726 int32_t reqFiveWayNav = getIntegerAttribute(tree,
727 REQ_FIVE_WAY_NAV_ATTR, NULL, 0);
728 printf("uses-configuration:");
729 if (reqTouchScreen != 0) {
730 printf(" reqTouchScreen='%d'", reqTouchScreen);
731 }
732 if (reqKeyboardType != 0) {
733 printf(" reqKeyboardType='%d'", reqKeyboardType);
734 }
735 if (reqHardKeyboard != 0) {
736 printf(" reqHardKeyboard='%d'", reqHardKeyboard);
737 }
738 if (reqNavigation != 0) {
739 printf(" reqNavigation='%d'", reqNavigation);
740 }
741 if (reqFiveWayNav != 0) {
742 printf(" reqFiveWayNav='%d'", reqFiveWayNav);
743 }
744 printf("\n");
745 } else if (tag == "supports-density") {
746 int32_t dens = getIntegerAttribute(tree, DENSITY_ATTR, &error);
747 if (error != "") {
748 fprintf(stderr, "ERROR getting 'android:density' attribute: %s\n",
749 error.string());
750 goto bail;
751 }
752 printf("supports-density:'%d'\n", dens);
753 } else if (tag == "supports-screens") {
754 smallScreen = getIntegerAttribute(tree,
755 SMALL_SCREEN_ATTR, NULL, 1);
756 normalScreen = getIntegerAttribute(tree,
757 NORMAL_SCREEN_ATTR, NULL, 1);
758 largeScreen = getIntegerAttribute(tree,
759 LARGE_SCREEN_ATTR, NULL, 1);
760 xlargeScreen = getIntegerAttribute(tree,
761 XLARGE_SCREEN_ATTR, NULL, 1);
762 } else if (tag == "uses-feature") {
763 String8 name = getAttribute(tree, NAME_ATTR, &error);
764
765 if (name != "" && error == "") {
766 int req = getIntegerAttribute(tree,
767 REQUIRED_ATTR, NULL, 1);
768
769 if (name == "android.hardware.camera") {
770 specCameraFeature = true;
771 } else if (name == "android.hardware.camera.autofocus") {
772 // these have no corresponding permission to check for,
773 // but should imply the foundational camera permission
774 reqCameraAutofocusFeature = reqCameraAutofocusFeature || req;
775 specCameraAutofocusFeature = true;
776 } else if (req && (name == "android.hardware.camera.flash")) {
777 // these have no corresponding permission to check for,
778 // but should imply the foundational camera permission
779 reqCameraFlashFeature = true;
780 } else if (name == "android.hardware.location") {
781 specLocationFeature = true;
782 } else if (name == "android.hardware.location.network") {
783 specNetworkLocFeature = true;
784 reqNetworkLocFeature = reqNetworkLocFeature || req;
785 } else if (name == "android.hardware.location.gps") {
786 specGpsFeature = true;
787 reqGpsFeature = reqGpsFeature || req;
788 } else if (name == "android.hardware.bluetooth") {
789 specBluetoothFeature = true;
790 } else if (name == "android.hardware.touchscreen") {
791 specTouchscreenFeature = true;
792 } else if (name == "android.hardware.touchscreen.multitouch") {
793 specMultitouchFeature = true;
794 } else if (name == "android.hardware.touchscreen.multitouch.distinct") {
795 reqDistinctMultitouchFeature = reqDistinctMultitouchFeature || req;
796 } else if (name == "android.hardware.microphone") {
797 specMicrophoneFeature = true;
798 } else if (name == "android.hardware.wifi") {
799 specWiFiFeature = true;
800 } else if (name == "android.hardware.telephony") {
801 specTelephonyFeature = true;
802 } else if (req && (name == "android.hardware.telephony.gsm" ||
803 name == "android.hardware.telephony.cdma")) {
804 // these have no corresponding permission to check for,
805 // but should imply the foundational telephony permission
806 reqTelephonySubFeature = true;
807 }
808 printf("uses-feature%s:'%s'\n",
809 req ? "" : "-not-required", name.string());
810 } else {
811 int vers = getIntegerAttribute(tree,
812 GL_ES_VERSION_ATTR, &error);
813 if (error == "") {
814 printf("uses-gl-es:'0x%x'\n", vers);
815 }
816 }
817 } else if (tag == "uses-permission") {
818 String8 name = getAttribute(tree, NAME_ATTR, &error);
819 if (name != "" && error == "") {
820 if (name == "android.permission.CAMERA") {
821 hasCameraPermission = true;
822 } else if (name == "android.permission.ACCESS_FINE_LOCATION") {
823 hasGpsPermission = true;
824 } else if (name == "android.permission.ACCESS_MOCK_LOCATION") {
825 hasMockLocPermission = true;
826 } else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
827 hasCoarseLocPermission = true;
828 } else if (name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
829 name == "android.permission.INSTALL_LOCATION_PROVIDER") {
830 hasGeneralLocPermission = true;
831 } else if (name == "android.permission.BLUETOOTH" ||
832 name == "android.permission.BLUETOOTH_ADMIN") {
833 hasBluetoothPermission = true;
834 } else if (name == "android.permission.RECORD_AUDIO") {
835 hasRecordAudioPermission = true;
836 } else if (name == "android.permission.ACCESS_WIFI_STATE" ||
837 name == "android.permission.CHANGE_WIFI_STATE" ||
838 name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
839 hasWiFiPermission = true;
840 } else if (name == "android.permission.CALL_PHONE" ||
841 name == "android.permission.CALL_PRIVILEGED" ||
842 name == "android.permission.MODIFY_PHONE_STATE" ||
843 name == "android.permission.PROCESS_OUTGOING_CALLS" ||
844 name == "android.permission.READ_SMS" ||
845 name == "android.permission.RECEIVE_SMS" ||
846 name == "android.permission.RECEIVE_MMS" ||
847 name == "android.permission.RECEIVE_WAP_PUSH" ||
848 name == "android.permission.SEND_SMS" ||
849 name == "android.permission.WRITE_APN_SETTINGS" ||
850 name == "android.permission.WRITE_SMS") {
851 hasTelephonyPermission = true;
852 }
853 printf("uses-permission:'%s'\n", name.string());
854 } else {
855 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
856 error.string());
857 goto bail;
858 }
859 } else if (tag == "uses-package") {
860 String8 name = getAttribute(tree, NAME_ATTR, &error);
861 if (name != "" && error == "") {
862 printf("uses-package:'%s'\n", name.string());
863 } else {
864 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
865 error.string());
866 goto bail;
867 }
868 } else if (tag == "original-package") {
869 String8 name = getAttribute(tree, NAME_ATTR, &error);
870 if (name != "" && error == "") {
871 printf("original-package:'%s'\n", name.string());
872 } else {
873 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
874 error.string());
875 goto bail;
876 }
877 } else if (tag == "uses-gl-texture") {
878 String8 name = getAttribute(tree, NAME_ATTR, &error);
879 if (name != "" && error == "") {
880 printf("uses-gl-texture:'%s'\n", name.string());
881 } else {
882 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n",
883 error.string());
884 goto bail;
885 }
886 }
887 } else if (depth == 3 && withinApplication) {
888 withinActivity = false;
889 withinReceiver = false;
890 withinService = false;
891 hasIntentFilter = false;
892 if(tag == "activity") {
893 withinActivity = true;
894 activityName = getAttribute(tree, NAME_ATTR, &error);
895 if (error != "") {
896 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
897 goto bail;
898 }
899
900 activityLabel = getResolvedAttribute(&res, tree, LABEL_ATTR, &error);
901 if (error != "") {
902 fprintf(stderr, "ERROR getting 'android:label' attribute: %s\n", error.string());
903 goto bail;
904 }
905
906 activityIcon = getResolvedAttribute(&res, tree, ICON_ATTR, &error);
907 if (error != "") {
908 fprintf(stderr, "ERROR getting 'android:icon' attribute: %s\n", error.string());
909 goto bail;
910 }
911 } else if (tag == "uses-library") {
912 String8 libraryName = getAttribute(tree, NAME_ATTR, &error);
913 if (error != "") {
914 fprintf(stderr, "ERROR getting 'android:name' attribute for uses-library: %s\n", error.string());
915 goto bail;
916 }
917 int req = getIntegerAttribute(tree,
918 REQUIRED_ATTR, NULL, 1);
919 printf("uses-library%s:'%s'\n",
920 req ? "" : "-not-required", libraryName.string());
921 } else if (tag == "receiver") {
922 withinReceiver = true;
923 receiverName = getAttribute(tree, NAME_ATTR, &error);
924
925 if (error != "") {
926 fprintf(stderr, "ERROR getting 'android:name' attribute for receiver: %s\n", error.string());
927 goto bail;
928 }
929 } else if (tag == "service") {
930 withinService = true;
931 serviceName = getAttribute(tree, NAME_ATTR, &error);
932
933 if (error != "") {
934 fprintf(stderr, "ERROR getting 'android:name' attribute for service: %s\n", error.string());
935 goto bail;
936 }
937 }
938 } else if ((depth == 4) && (tag == "intent-filter")) {
939 hasIntentFilter = true;
940 withinIntentFilter = true;
941 actMainActivity = actWidgetReceivers = actImeService = actWallpaperService = false;
942 } else if ((depth == 5) && withinIntentFilter){
943 String8 action;
944 if (tag == "action") {
945 action = getAttribute(tree, NAME_ATTR, &error);
946 if (error != "") {
947 fprintf(stderr, "ERROR getting 'android:name' attribute: %s\n", error.string());
948 goto bail;
949 }
950 if (withinActivity) {
951 if (action == "android.intent.action.MAIN") {
952 isMainActivity = true;
953 actMainActivity = true;
954 }
955 } else if (withinReceiver) {
956 if (action == "android.appwidget.action.APPWIDGET_UPDATE") {
957 actWidgetReceivers = true;
958 }
959 } else if (withinService) {
960 if (action == "android.view.InputMethod") {
961 actImeService = true;
962 } else if (action == "android.service.wallpaper.WallpaperService") {
963 actWallpaperService = true;
964 }
965 }
966 if (action == "android.intent.action.SEARCH") {
967 isSearchable = true;
968 }
969 }
970
971 if (tag == "category") {
972 String8 category = getAttribute(tree, NAME_ATTR, &error);
973 if (error != "") {
974 fprintf(stderr, "ERROR getting 'name' attribute: %s\n", error.string());
975 goto bail;
976 }
977 if (withinActivity) {
978 if (category == "android.intent.category.LAUNCHER") {
979 isLauncherActivity = true;
980 }
981 }
982 }
983 }
984 }
985
986 /* The following blocks handle printing "inferred" uses-features, based
987 * on whether related features or permissions are used by the app.
988 * Note that the various spec*Feature variables denote whether the
989 * relevant tag was *present* in the AndroidManfest, not that it was
990 * present and set to true.
991 */
992 // Camera-related back-compatibility logic
993 if (!specCameraFeature) {
994 if (reqCameraFlashFeature || reqCameraAutofocusFeature) {
995 // if app requested a sub-feature (autofocus or flash) and didn't
996 // request the base camera feature, we infer that it meant to
997 printf("uses-feature:'android.hardware.camera'\n");
998 } else if (hasCameraPermission) {
999 // if app wants to use camera but didn't request the feature, we infer
1000 // that it meant to, and further that it wants autofocus
1001 // (which was the 1.0 - 1.5 behavior)
1002 printf("uses-feature:'android.hardware.camera'\n");
1003 if (!specCameraAutofocusFeature) {
1004 printf("uses-feature:'android.hardware.camera.autofocus'\n");
1005 }
1006 }
1007 }
1008
1009 // Location-related back-compatibility logic
1010 if (!specLocationFeature &&
1011 (hasMockLocPermission || hasCoarseLocPermission || hasGpsPermission ||
1012 hasGeneralLocPermission || reqNetworkLocFeature || reqGpsFeature)) {
1013 // if app either takes a location-related permission or requests one of the
1014 // sub-features, we infer that it also meant to request the base location feature
1015 printf("uses-feature:'android.hardware.location'\n");
1016 }
1017 if (!specGpsFeature && hasGpsPermission) {
1018 // if app takes GPS (FINE location) perm but does not request the GPS
1019 // feature, we infer that it meant to
1020 printf("uses-feature:'android.hardware.location.gps'\n");
1021 }
1022 if (!specNetworkLocFeature && hasCoarseLocPermission) {
1023 // if app takes Network location (COARSE location) perm but does not request the
1024 // network location feature, we infer that it meant to
1025 printf("uses-feature:'android.hardware.location.network'\n");
1026 }
1027
1028 // Bluetooth-related compatibility logic
1029 if (!specBluetoothFeature && hasBluetoothPermission && (targetSdk > 4)) {
1030 // if app takes a Bluetooth permission but does not request the Bluetooth
1031 // feature, we infer that it meant to
1032 printf("uses-feature:'android.hardware.bluetooth'\n");
1033 }
1034
1035 // Microphone-related compatibility logic
1036 if (!specMicrophoneFeature && hasRecordAudioPermission) {
1037 // if app takes the record-audio permission but does not request the microphone
1038 // feature, we infer that it meant to
1039 printf("uses-feature:'android.hardware.microphone'\n");
1040 }
1041
1042 // WiFi-related compatibility logic
1043 if (!specWiFiFeature && hasWiFiPermission) {
1044 // if app takes one of the WiFi permissions but does not request the WiFi
1045 // feature, we infer that it meant to
1046 printf("uses-feature:'android.hardware.wifi'\n");
1047 }
1048
1049 // Telephony-related compatibility logic
1050 if (!specTelephonyFeature && (hasTelephonyPermission || reqTelephonySubFeature)) {
1051 // if app takes one of the telephony permissions or requests a sub-feature but
1052 // does not request the base telephony feature, we infer that it meant to
1053 printf("uses-feature:'android.hardware.telephony'\n");
1054 }
1055
1056 // Touchscreen-related back-compatibility logic
1057 if (!specTouchscreenFeature) { // not a typo!
1058 // all apps are presumed to require a touchscreen, unless they explicitly say
1059 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1060 // Note that specTouchscreenFeature is true if the tag is present, regardless
1061 // of whether its value is true or false, so this is safe
1062 printf("uses-feature:'android.hardware.touchscreen'\n");
1063 }
1064 if (!specMultitouchFeature && reqDistinctMultitouchFeature) {
1065 // if app takes one of the telephony permissions or requests a sub-feature but
1066 // does not request the base telephony feature, we infer that it meant to
1067 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1068 }
1069
1070 if (hasMainActivity) {
1071 printf("main\n");
1072 }
1073 if (hasWidgetReceivers) {
1074 printf("app-widget\n");
1075 }
1076 if (hasImeService) {
1077 printf("ime\n");
1078 }
1079 if (hasWallpaperService) {
1080 printf("wallpaper\n");
1081 }
1082 if (hasOtherActivities) {
1083 printf("other-activities\n");
1084 }
1085 if (isSearchable) {
1086 printf("search\n");
1087 }
1088 if (hasOtherReceivers) {
1089 printf("other-receivers\n");
1090 }
1091 if (hasOtherServices) {
1092 printf("other-services\n");
1093 }
1094
1095 // Determine default values for any unspecified screen sizes,
1096 // based on the target SDK of the package. As of 4 (donut)
1097 // the screen size support was introduced, so all default to
1098 // enabled.
1099 if (smallScreen > 0) {
1100 smallScreen = targetSdk >= 4 ? -1 : 0;
1101 }
1102 if (normalScreen > 0) {
1103 normalScreen = -1;
1104 }
1105 if (largeScreen > 0) {
1106 largeScreen = targetSdk >= 4 ? -1 : 0;
1107 }
1108 if (xlargeScreen > 0) {
1109 // Introduced in Honeycomb.
1110 xlargeScreen = targetSdk >= 10 ? -1 : 0;
1111 }
1112 printf("supports-screens:");
1113 if (smallScreen != 0) printf(" 'small'");
1114 if (normalScreen != 0) printf(" 'normal'");
1115 if (largeScreen != 0) printf(" 'large'");
1116 if (xlargeScreen != 0) printf(" 'xlarge'");
1117 printf("\n");
1118
1119 printf("locales:");
1120 Vector<String8> locales;
1121 res.getLocales(&locales);
1122 const size_t NL = locales.size();
1123 for (size_t i=0; i<NL; i++) {
1124 const char* localeStr = locales[i].string();
1125 if (localeStr == NULL || strlen(localeStr) == 0) {
1126 localeStr = "--_--";
1127 }
1128 printf(" '%s'", localeStr);
1129 }
1130 printf("\n");
1131
1132 Vector<ResTable_config> configs;
1133 res.getConfigurations(&configs);
1134 SortedVector<int> densities;
1135 const size_t NC = configs.size();
1136 for (size_t i=0; i<NC; i++) {
1137 int dens = configs[i].density;
1138 if (dens == 0) dens = 160;
1139 densities.add(dens);
1140 }
1141
1142 printf("densities:");
1143 const size_t ND = densities.size();
1144 for (size_t i=0; i<ND; i++) {
1145 printf(" '%d'", densities[i]);
1146 }
1147 printf("\n");
1148
1149 AssetDir* dir = assets.openNonAssetDir(assetsCookie, "lib");
1150 if (dir != NULL) {
1151 if (dir->getFileCount() > 0) {
1152 printf("native-code:");
1153 for (size_t i=0; i<dir->getFileCount(); i++) {
1154 printf(" '%s'", dir->getFileName(i).string());
1155 }
1156 printf("\n");
1157 }
1158 delete dir;
1159 }
1160 } else if (strcmp("configurations", option) == 0) {
1161 Vector<ResTable_config> configs;
1162 res.getConfigurations(&configs);
1163 const size_t N = configs.size();
1164 for (size_t i=0; i<N; i++) {
1165 printf("%s\n", configs[i].toString().string());
1166 }
1167 } else {
1168 fprintf(stderr, "ERROR: unknown dump option '%s'\n", option);
1169 goto bail;
1170 }
1171 }
1172
1173 result = NO_ERROR;
1174
1175 bail:
1176 if (asset) {
1177 delete asset;
1178 }
1179 return (result != NO_ERROR);
1180 }
1181
1182
1183 /*
1184 * Handle the "add" command, which wants to add files to a new or
1185 * pre-existing archive.
1186 */
1187 int doAdd(Bundle* bundle)
1188 {
1189 ZipFile* zip = NULL;
1190 status_t result = UNKNOWN_ERROR;
1191 const char* zipFileName;
1192
1193 if (bundle->getUpdate()) {
1194 /* avoid confusion */
1195 fprintf(stderr, "ERROR: can't use '-u' with add\n");
1196 goto bail;
1197 }
1198
1199 if (bundle->getFileSpecCount() < 1) {
1200 fprintf(stderr, "ERROR: must specify zip file name\n");
1201 goto bail;
1202 }
1203 zipFileName = bundle->getFileSpecEntry(0);
1204
1205 if (bundle->getFileSpecCount() < 2) {
1206 fprintf(stderr, "NOTE: nothing to do\n");
1207 goto bail;
1208 }
1209
1210 zip = openReadWrite(zipFileName, true);
1211 if (zip == NULL) {
1212 fprintf(stderr, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName);
1213 goto bail;
1214 }
1215
1216 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1217 const char* fileName = bundle->getFileSpecEntry(i);
1218
1219 if (strcasecmp(String8(fileName).getPathExtension().string(), ".gz") == 0) {
1220 printf(" '%s'... (from gzip)\n", fileName);
1221 result = zip->addGzip(fileName, String8(fileName).getBasePath().string(), NULL);
1222 } else {
1223 if (bundle->getJunkPath()) {
1224 String8 storageName = String8(fileName).getPathLeaf();
1225 printf(" '%s' as '%s'...\n", fileName, storageName.string());
1226 result = zip->add(fileName, storageName.string(),
1227 bundle->getCompressionMethod(), NULL);
1228 } else {
1229 printf(" '%s'...\n", fileName);
1230 result = zip->add(fileName, bundle->getCompressionMethod(), NULL);
1231 }
1232 }
1233 if (result != NO_ERROR) {
1234 fprintf(stderr, "Unable to add '%s' to '%s'", bundle->getFileSpecEntry(i), zipFileName);
1235 if (result == NAME_NOT_FOUND)
1236 fprintf(stderr, ": file not found\n");
1237 else if (result == ALREADY_EXISTS)
1238 fprintf(stderr, ": already exists in archive\n");
1239 else
1240 fprintf(stderr, "\n");
1241 goto bail;
1242 }
1243 }
1244
1245 result = NO_ERROR;
1246
1247 bail:
1248 delete zip;
1249 return (result != NO_ERROR);
1250 }
1251
1252
1253 /*
1254 * Delete files from an existing archive.
1255 */
1256 int doRemove(Bundle* bundle)
1257 {
1258 ZipFile* zip = NULL;
1259 status_t result = UNKNOWN_ERROR;
1260 const char* zipFileName;
1261
1262 if (bundle->getFileSpecCount() < 1) {
1263 fprintf(stderr, "ERROR: must specify zip file name\n");
1264 goto bail;
1265 }
1266 zipFileName = bundle->getFileSpecEntry(0);
1267
1268 if (bundle->getFileSpecCount() < 2) {
1269 fprintf(stderr, "NOTE: nothing to do\n");
1270 goto bail;
1271 }
1272
1273 zip = openReadWrite(zipFileName, false);
1274 if (zip == NULL) {
1275 fprintf(stderr, "ERROR: failed opening Zip archive '%s'\n",
1276 zipFileName);
1277 goto bail;
1278 }
1279
1280 for (int i = 1; i < bundle->getFileSpecCount(); i++) {
1281 const char* fileName = bundle->getFileSpecEntry(i);
1282 ZipEntry* entry;
1283
1284 entry = zip->getEntryByName(fileName);
1285 if (entry == NULL) {
1286 printf(" '%s' NOT FOUND\n", fileName);
1287 continue;
1288 }
1289
1290 result = zip->remove(entry);
1291
1292 if (result != NO_ERROR) {
1293 fprintf(stderr, "Unable to delete '%s' from '%s'\n",
1294 bundle->getFileSpecEntry(i), zipFileName);
1295 goto bail;
1296 }
1297 }
1298
1299 /* update the archive */
1300 zip->flush();
1301
1302 bail:
1303 delete zip;
1304 return (result != NO_ERROR);
1305 }
1306
1307
1308 /*
1309 * Package up an asset directory and associated application files.
1310 */
1311 int doPackage(Bundle* bundle)
1312 {
1313 const char* outputAPKFile;
1314 int retVal = 1;
1315 status_t err;
1316 sp<AaptAssets> assets;
1317 int N;
1318
1319 // -c zz_ZZ means do pseudolocalization
1320 ResourceFilter filter;
1321 err = filter.parse(bundle->getConfigurations());
1322 if (err != NO_ERROR) {
1323 goto bail;
1324 }
1325 if (filter.containsPseudo()) {
1326 bundle->setPseudolocalize(true);
1327 }
1328
1329 N = bundle->getFileSpecCount();
1330 if (N < 1 && bundle->getResourceSourceDirs().size() == 0 && bundle->getJarFiles().size() == 0
1331 && bundle->getAndroidManifestFile() == NULL && bundle->getAssetSourceDir() == NULL) {
1332 fprintf(stderr, "ERROR: no input files\n");
1333 goto bail;
1334 }
1335
1336 outputAPKFile = bundle->getOutputAPKFile();
1337
1338 // Make sure the filenames provided exist and are of the appropriate type.
1339 if (outputAPKFile) {
1340 FileType type;
1341 type = getFileType(outputAPKFile);
1342 if (type != kFileTypeNonexistent && type != kFileTypeRegular) {
1343 fprintf(stderr,
1344 "ERROR: output file '%s' exists but is not regular file\n",
1345 outputAPKFile);
1346 goto bail;
1347 }
1348 }
1349
1350 // Load the assets.
1351 assets = new AaptAssets();
1352 err = assets->slurpFromArgs(bundle);
1353 if (err < 0) {
1354 goto bail;
1355 }
1356
1357 if (bundle->getVerbose()) {
1358 assets->print();
1359 }
1360
1361 // If they asked for any files that need to be compiled, do so.
1362 if (bundle->getResourceSourceDirs().size() || bundle->getAndroidManifestFile()) {
1363 err = buildResources(bundle, assets);
1364 if (err != 0) {
1365 goto bail;
1366 }
1367 }
1368
1369 // At this point we've read everything and processed everything. From here
1370 // on out it's just writing output files.
1371 if (SourcePos::hasErrors()) {
1372 goto bail;
1373 }
1374
1375 // Write out R.java constants
1376 if (assets->getPackage() == assets->getSymbolsPrivatePackage()) {
1377 if (bundle->getCustomPackage() == NULL) {
1378 err = writeResourceSymbols(bundle, assets, assets->getPackage(), true);
1379 } else {
1380 const String8 customPkg(bundle->getCustomPackage());
1381 err = writeResourceSymbols(bundle, assets, customPkg, true);
1382 }
1383 if (err < 0) {
1384 goto bail;
1385 }
1386 } else {
1387 err = writeResourceSymbols(bundle, assets, assets->getPackage(), false);
1388 if (err < 0) {
1389 goto bail;
1390 }
1391 err = writeResourceSymbols(bundle, assets, assets->getSymbolsPrivatePackage(), true);
1392 if (err < 0) {
1393 goto bail;
1394 }
1395 }
1396
1397 // Write out the ProGuard file
1398 err = writeProguardFile(bundle, assets);
1399 if (err < 0) {
1400 goto bail;
1401 }
1402
1403 // Write the apk
1404 if (outputAPKFile) {
1405 err = writeAPK(bundle, assets, String8(outputAPKFile));
1406 if (err != NO_ERROR) {
1407 fprintf(stderr, "ERROR: packaging of '%s' failed\n", outputAPKFile);
1408 goto bail;
1409 }
1410 }
1411
1412 retVal = 0;
1413 bail:
1414 if (SourcePos::hasErrors()) {
1415 SourcePos::printErrors(stderr);
1416 }
1417 return retVal;
1418 }