2 // Copyright 2006 The Android Open Source Project
4 // Android Asset Packaging Tool main entry point.
8 #include "ResourceTable.h"
11 #include <utils/Log.h>
12 #include <utils/threads.h>
13 #include <utils/List.h>
14 #include <utils/Errors.h>
19 using namespace android
;
22 * Show version info. All the cool kids do it.
24 int doVersion(Bundle
* bundle
)
26 if (bundle
->getFileSpecCount() != 0)
27 printf("(ignoring extra arguments)\n");
28 printf("Android Asset Packaging Tool, v0.2\n");
35 * Open the file read only. The call fails if the file doesn't exist.
37 * Returns NULL on failure.
39 ZipFile
* openReadOnly(const char* fileName
)
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
);
52 fprintf(stderr
, "ERROR: failed opening '%s' as Zip file\n",
62 * Open the file read-write. The file will be created if it doesn't
63 * already exist and "okayToCreate" is set.
65 * Returns NULL on failure.
67 ZipFile
* openReadWrite(const char* fileName
, bool okayToCreate
)
73 flags
= ZipFile::kOpenReadWrite
;
75 flags
|= ZipFile::kOpenCreate
;
78 result
= zip
->open(fileName
, flags
);
79 if (result
!= NO_ERROR
) {
91 * Return a short string describing the compression method.
93 const char* compressionName(int method
)
95 if (method
== ZipEntry::kCompressStored
)
97 else if (method
== ZipEntry::kCompressDeflated
)
104 * Return the percent reduction in size (0% == no compression).
106 int calcPercent(long uncompressedLen
, long compressedLen
)
108 if (!uncompressedLen
)
111 return (int) (100.0 - (compressedLen
* 100.0) / uncompressedLen
+ 0.5);
115 * Handle the "list" command, which can be a simple file dump or
118 * The verbose listing closely matches the output of the Info-ZIP "unzip"
121 int doList(Bundle
* bundle
)
125 const ZipEntry
* entry
;
126 long totalUncLen
, totalCompLen
;
127 const char* zipFileName
;
129 if (bundle
->getFileSpecCount() != 1) {
130 fprintf(stderr
, "ERROR: specify zip file name (only)\n");
133 zipFileName
= bundle
->getFileSpecEntry(0);
135 zip
= openReadOnly(zipFileName
);
141 if (bundle
->getVerbose()) {
142 printf("Archive: %s\n", zipFileName
);
144 " Length Method Size Ratio Offset Date Time CRC-32 Name\n");
146 "-------- ------ ------- ----- ------- ---- ---- ------ ----\n");
149 totalUncLen
= totalCompLen
= 0;
151 count
= zip
->getNumEntries();
152 for (i
= 0; i
< count
; i
++) {
153 entry
= zip
->getEntryByIndex(i
);
154 if (bundle
->getVerbose()) {
158 when
= entry
->getModWhen();
159 strftime(dateBuf
, sizeof(dateBuf
), "%m-%d-%y %H:%M",
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(),
171 entry
->getFileName());
173 printf("%s\n", entry
->getFileName());
176 totalUncLen
+= entry
->getUncompressedLen();
177 totalCompLen
+= entry
->getCompressedLen();
180 if (bundle
->getVerbose()) {
182 "-------- ------- --- -------\n");
183 printf("%8ld %7ld %2d%% %d files\n",
186 calcPercent(totalUncLen
, totalCompLen
),
187 zip
->getNumEntries());
190 if (bundle
->getAndroidList()) {
192 if (!assets
.addAssetPath(String8(zipFileName
), NULL
)) {
193 fprintf(stderr
, "ERROR: list -a failed because assets could not be loaded\n");
197 const ResTable
& res
= assets
.getResources(false);
199 printf("\nNo resource table found.\n");
201 #ifndef HAVE_ANDROID_OS
202 printf("\nResource table:\n");
207 Asset
* manifestAsset
= assets
.openNonAsset("AndroidManifest.xml",
208 Asset::ACCESS_BUFFER
);
209 if (manifestAsset
== NULL
) {
210 printf("\nNo AndroidManifest.xml found.\n");
212 printf("\nAndroid manifest:\n");
214 tree
.setTo(manifestAsset
->getBuffer(true),
215 manifestAsset
->getLength());
216 printXMLBlock(&tree
);
218 delete manifestAsset
;
228 static ssize_t
indexOfAttribute(const ResXMLTree
& tree
, uint32_t attrRes
)
230 size_t N
= tree
.getAttributeCount();
231 for (size_t i
=0; i
<N
; i
++) {
232 if (tree
.getAttributeNameResID(i
) == attrRes
) {
239 String8
getAttribute(const ResXMLTree
& tree
, const char* ns
,
240 const char* attr
, String8
* outError
)
242 ssize_t idx
= tree
.indexOfAttribute(ns
, attr
);
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";
254 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
255 return str
? String8(str
, len
) : String8();
258 static String8
getAttribute(const ResXMLTree
& tree
, uint32_t attrRes
, String8
* outError
)
260 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
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";
272 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
273 return str
? String8(str
, len
) : String8();
276 static int32_t getIntegerAttribute(const ResXMLTree
& tree
, uint32_t attrRes
,
277 String8
* outError
, int32_t defValue
= -1)
279 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
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";
294 static String8
getResolvedAttribute(const ResTable
* resTable
, const ResXMLTree
& tree
,
295 uint32_t attrRes
, String8
* outError
)
297 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
302 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
303 if (value
.dataType
== Res_value::TYPE_STRING
) {
305 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
306 return str
? String8(str
, len
) : String8();
308 resTable
->resolveReference(&value
, 0);
309 if (value
.dataType
!= Res_value::TYPE_STRING
) {
310 if (outError
!= NULL
) *outError
= "attribute is not a string value";
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();
320 // These are attribute resource constants for the platform, as found
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,
346 const char *getComponentName(String8
&pkgName
, String8
&componentName
) {
347 ssize_t idx
= componentName
.find(".");
348 String8
retStr(pkgName
);
350 retStr
+= componentName
;
351 } else if (idx
< 0) {
353 retStr
+= componentName
;
355 return componentName
.string();
357 return retStr
.string();
361 * Handle the "dump" command, to extract select data from an archive.
363 int doDump(Bundle
* bundle
)
365 status_t result
= UNKNOWN_ERROR
;
368 if (bundle
->getFileSpecCount() < 1) {
369 fprintf(stderr
, "ERROR: no dump option specified\n");
373 if (bundle
->getFileSpecCount() < 2) {
374 fprintf(stderr
, "ERROR: no dump file specified\n");
378 const char* option
= bundle
->getFileSpecEntry(0);
379 const char* filename
= bundle
->getFileSpecEntry(1);
383 if (!assets
.addAssetPath(String8(filename
), &assetsCookie
)) {
384 fprintf(stderr
, "ERROR: dump failed because assets could not be loaded\n");
388 const ResTable
& res
= assets
.getResources(false);
390 fprintf(stderr
, "ERROR: dump failed because no resource table was found\n");
394 if (strcmp("resources", option
) == 0) {
395 #ifndef HAVE_ANDROID_OS
396 res
.print(bundle
->getValues());
398 } else if (strcmp("xmltree", option
) == 0) {
399 if (bundle
->getFileSpecCount() < 3) {
400 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
404 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
405 const char* resname
= bundle
->getFileSpecEntry(i
);
407 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
409 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
413 if (tree
.setTo(asset
->getBuffer(true),
414 asset
->getLength()) != NO_ERROR
) {
415 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
419 printXMLBlock(&tree
);
425 } else if (strcmp("xmlstrings", option
) == 0) {
426 if (bundle
->getFileSpecCount() < 3) {
427 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
431 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
432 const char* resname
= bundle
->getFileSpecEntry(i
);
434 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
436 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
440 if (tree
.setTo(asset
->getBuffer(true),
441 asset
->getLength()) != NO_ERROR
) {
442 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
445 printStringPool(&tree
.getStrings());
452 asset
= assets
.openNonAsset("AndroidManifest.xml",
453 Asset::ACCESS_BUFFER
);
455 fprintf(stderr
, "ERROR: dump failed because no AndroidManifest.xml found\n");
459 if (tree
.setTo(asset
->getBuffer(true),
460 asset
->getLength()) != NO_ERROR
) {
461 fprintf(stderr
, "ERROR: AndroidManifest.xml is corrupt\n");
466 if (strcmp("permissions", option
) == 0) {
468 ResXMLTree::event_code_t code
;
470 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
471 if (code
== ResXMLTree::END_TAG
) {
475 if (code
!= ResXMLTree::START_TAG
) {
479 String8
tag(tree
.getElementName(&len
));
480 //printf("Depth %d tag %s\n", depth, tag.string());
482 if (tag
!= "manifest") {
483 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
486 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
487 printf("package: %s\n", pkg
.string());
488 } else if (depth
== 2 && tag
== "permission") {
490 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
492 fprintf(stderr
, "ERROR: %s\n", error
.string());
495 printf("permission: %s\n", name
.string());
496 } else if (depth
== 2 && tag
== "uses-permission") {
498 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
500 fprintf(stderr
, "ERROR: %s\n", error
.string());
503 printf("uses-permission: %s\n", name
.string());
506 } else if (strcmp("badging", option
) == 0) {
508 ResXMLTree::event_code_t code
;
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;
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;
575 int normalScreen
= 1;
577 int xlargeScreen
= 1;
579 String8 activityName
;
580 String8 activityLabel
;
581 String8 activityIcon
;
582 String8 receiverName
;
584 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
585 if (code
== ResXMLTree::END_TAG
) {
588 withinApplication
= false;
589 } else if (depth
< 3) {
590 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
591 const char *aName
= getComponentName(pkg
, activityName
);
593 printf("launchable activity name='%s'", aName
);
595 printf("label='%s' icon='%s'\n",
596 activityLabel
.string(),
597 activityIcon
.string());
599 if (!hasIntentFilter
) {
600 hasOtherActivities
|= withinActivity
;
601 hasOtherReceivers
|= withinReceiver
;
602 hasOtherServices
|= withinService
;
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
);
623 withinIntentFilter
= false;
627 if (code
!= ResXMLTree::START_TAG
) {
631 String8
tag(tree
.getElementName(&len
));
632 //printf("Depth %d, %s\n", depth, tag.string());
634 if (tag
!= "manifest") {
635 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
638 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
639 printf("package: name='%s' ", pkg
.string());
640 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
642 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
645 if (versionCode
> 0) {
646 printf("versionCode='%d' ", versionCode
);
648 printf("versionCode='' ");
650 String8 versionName
= getResolvedAttribute(&res
, tree
, VERSION_NAME_ATTR
, &error
);
652 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
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
);
662 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
665 printf("application: label='%s' ", label
.string());
666 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
668 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
671 printf("icon='%s'\n", icon
.string());
672 int32_t testOnly
= getIntegerAttribute(tree
, TEST_ONLY_ATTR
, &error
, 0);
674 fprintf(stderr
, "ERROR getting 'android:testOnly' attribute: %s\n", error
.string());
678 printf("testOnly='%d'\n", testOnly
);
680 } else if (tag
== "uses-sdk") {
681 int32_t code
= getIntegerAttribute(tree
, MIN_SDK_VERSION_ATTR
, &error
);
684 String8 name
= getResolvedAttribute(&res
, tree
, MIN_SDK_VERSION_ATTR
, &error
);
686 fprintf(stderr
, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
690 if (name
== "Donut") targetSdk
= 4;
691 printf("sdkVersion:'%s'\n", name
.string());
692 } else if (code
!= -1) {
694 printf("sdkVersion:'%d'\n", code
);
696 code
= getIntegerAttribute(tree
, MAX_SDK_VERSION_ATTR
, NULL
, -1);
698 printf("maxSdkVersion:'%d'\n", code
);
700 code
= getIntegerAttribute(tree
, TARGET_SDK_VERSION_ATTR
, &error
);
703 String8 name
= getResolvedAttribute(&res
, tree
, TARGET_SDK_VERSION_ATTR
, &error
);
705 fprintf(stderr
, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
709 if (name
== "Donut" && targetSdk
< 4) targetSdk
= 4;
710 printf("targetSdkVersion:'%s'\n", name
.string());
711 } else if (code
!= -1) {
712 if (targetSdk
< code
) {
715 printf("targetSdkVersion:'%d'\n", code
);
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
);
732 if (reqKeyboardType
!= 0) {
733 printf(" reqKeyboardType='%d'", reqKeyboardType
);
735 if (reqHardKeyboard
!= 0) {
736 printf(" reqHardKeyboard='%d'", reqHardKeyboard
);
738 if (reqNavigation
!= 0) {
739 printf(" reqNavigation='%d'", reqNavigation
);
741 if (reqFiveWayNav
!= 0) {
742 printf(" reqFiveWayNav='%d'", reqFiveWayNav
);
745 } else if (tag
== "supports-density") {
746 int32_t dens
= getIntegerAttribute(tree
, DENSITY_ATTR
, &error
);
748 fprintf(stderr
, "ERROR getting 'android:density' attribute: %s\n",
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
);
765 if (name
!= "" && error
== "") {
766 int req
= getIntegerAttribute(tree
,
767 REQUIRED_ATTR
, NULL
, 1);
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;
808 printf("uses-feature%s:'%s'\n",
809 req
? "" : "-not-required", name
.string());
811 int vers
= getIntegerAttribute(tree
,
812 GL_ES_VERSION_ATTR
, &error
);
814 printf("uses-gl-es:'0x%x'\n", vers
);
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;
853 printf("uses-permission:'%s'\n", name
.string());
855 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
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());
864 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
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());
873 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
878 } else if (depth
== 3 && withinApplication
) {
879 withinActivity
= false;
880 withinReceiver
= false;
881 withinService
= false;
882 hasIntentFilter
= false;
883 if(tag
== "activity") {
884 withinActivity
= true;
885 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
887 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
891 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
893 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
897 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
899 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
902 } else if (tag
== "uses-library") {
903 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
905 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
908 int req
= getIntegerAttribute(tree
,
909 REQUIRED_ATTR
, NULL
, 1);
910 printf("uses-library%s:'%s'\n",
911 req
? "" : "-not-required", libraryName
.string());
912 } else if (tag
== "receiver") {
913 withinReceiver
= true;
914 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
917 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
920 } else if (tag
== "service") {
921 withinService
= true;
922 serviceName
= getAttribute(tree
, NAME_ATTR
, &error
);
925 fprintf(stderr
, "ERROR getting 'android:name' attribute for service: %s\n", error
.string());
929 } else if ((depth
== 4) && (tag
== "intent-filter")) {
930 hasIntentFilter
= true;
931 withinIntentFilter
= true;
932 actMainActivity
= actWidgetReceivers
= actImeService
= actWallpaperService
= false;
933 } else if ((depth
== 5) && withinIntentFilter
){
935 if (tag
== "action") {
936 action
= getAttribute(tree
, NAME_ATTR
, &error
);
938 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
941 if (withinActivity
) {
942 if (action
== "android.intent.action.MAIN") {
943 isMainActivity
= true;
944 actMainActivity
= true;
946 } else if (withinReceiver
) {
947 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
948 actWidgetReceivers
= true;
950 } else if (withinService
) {
951 if (action
== "android.view.InputMethod") {
952 actImeService
= true;
953 } else if (action
== "android.service.wallpaper.WallpaperService") {
954 actWallpaperService
= true;
957 if (action
== "android.intent.action.SEARCH") {
962 if (tag
== "category") {
963 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
965 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
968 if (withinActivity
) {
969 if (category
== "android.intent.category.LAUNCHER") {
970 isLauncherActivity
= true;
977 /* The following blocks handle printing "inferred" uses-features, based
978 * on whether related features or permissions are used by the app.
979 * Note that the various spec*Feature variables denote whether the
980 * relevant tag was *present* in the AndroidManfest, not that it was
981 * present and set to true.
983 // Camera-related back-compatibility logic
984 if (!specCameraFeature
) {
985 if (reqCameraFlashFeature
|| reqCameraAutofocusFeature
) {
986 // if app requested a sub-feature (autofocus or flash) and didn't
987 // request the base camera feature, we infer that it meant to
988 printf("uses-feature:'android.hardware.camera'\n");
989 } else if (hasCameraPermission
) {
990 // if app wants to use camera but didn't request the feature, we infer
991 // that it meant to, and further that it wants autofocus
992 // (which was the 1.0 - 1.5 behavior)
993 printf("uses-feature:'android.hardware.camera'\n");
994 if (!specCameraAutofocusFeature
) {
995 printf("uses-feature:'android.hardware.camera.autofocus'\n");
1000 // Location-related back-compatibility logic
1001 if (!specLocationFeature
&&
1002 (hasMockLocPermission
|| hasCoarseLocPermission
|| hasGpsPermission
||
1003 hasGeneralLocPermission
|| reqNetworkLocFeature
|| reqGpsFeature
)) {
1004 // if app either takes a location-related permission or requests one of the
1005 // sub-features, we infer that it also meant to request the base location feature
1006 printf("uses-feature:'android.hardware.location'\n");
1008 if (!specGpsFeature
&& hasGpsPermission
) {
1009 // if app takes GPS (FINE location) perm but does not request the GPS
1010 // feature, we infer that it meant to
1011 printf("uses-feature:'android.hardware.location.gps'\n");
1013 if (!specNetworkLocFeature
&& hasCoarseLocPermission
) {
1014 // if app takes Network location (COARSE location) perm but does not request the
1015 // network location feature, we infer that it meant to
1016 printf("uses-feature:'android.hardware.location.network'\n");
1019 // Bluetooth-related compatibility logic
1020 if (!specBluetoothFeature
&& hasBluetoothPermission
&& (targetSdk
> 4)) {
1021 // if app takes a Bluetooth permission but does not request the Bluetooth
1022 // feature, we infer that it meant to
1023 printf("uses-feature:'android.hardware.bluetooth'\n");
1026 // Microphone-related compatibility logic
1027 if (!specMicrophoneFeature
&& hasRecordAudioPermission
) {
1028 // if app takes the record-audio permission but does not request the microphone
1029 // feature, we infer that it meant to
1030 printf("uses-feature:'android.hardware.microphone'\n");
1033 // WiFi-related compatibility logic
1034 if (!specWiFiFeature
&& hasWiFiPermission
) {
1035 // if app takes one of the WiFi permissions but does not request the WiFi
1036 // feature, we infer that it meant to
1037 printf("uses-feature:'android.hardware.wifi'\n");
1040 // Telephony-related compatibility logic
1041 if (!specTelephonyFeature
&& (hasTelephonyPermission
|| reqTelephonySubFeature
)) {
1042 // if app takes one of the telephony permissions or requests a sub-feature but
1043 // does not request the base telephony feature, we infer that it meant to
1044 printf("uses-feature:'android.hardware.telephony'\n");
1047 // Touchscreen-related back-compatibility logic
1048 if (!specTouchscreenFeature
) { // not a typo!
1049 // all apps are presumed to require a touchscreen, unless they explicitly say
1050 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1051 // Note that specTouchscreenFeature is true if the tag is present, regardless
1052 // of whether its value is true or false, so this is safe
1053 printf("uses-feature:'android.hardware.touchscreen'\n");
1055 if (!specMultitouchFeature
&& reqDistinctMultitouchFeature
) {
1056 // if app takes one of the telephony permissions or requests a sub-feature but
1057 // does not request the base telephony feature, we infer that it meant to
1058 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1061 if (hasMainActivity
) {
1064 if (hasWidgetReceivers
) {
1065 printf("app-widget\n");
1067 if (hasImeService
) {
1070 if (hasWallpaperService
) {
1071 printf("wallpaper\n");
1073 if (hasOtherActivities
) {
1074 printf("other-activities\n");
1079 if (hasOtherReceivers
) {
1080 printf("other-receivers\n");
1082 if (hasOtherServices
) {
1083 printf("other-services\n");
1086 // Determine default values for any unspecified screen sizes,
1087 // based on the target SDK of the package. As of 4 (donut)
1088 // the screen size support was introduced, so all default to
1090 if (smallScreen
> 0) {
1091 smallScreen
= targetSdk
>= 4 ? -1 : 0;
1093 if (normalScreen
> 0) {
1096 if (largeScreen
> 0) {
1097 largeScreen
= targetSdk
>= 4 ? -1 : 0;
1099 if (xlargeScreen
> 0) {
1100 // Introduced in Honeycomb.
1101 xlargeScreen
= targetSdk
>= 10 ? -1 : 0;
1103 printf("supports-screens:");
1104 if (smallScreen
!= 0) printf(" 'small'");
1105 if (normalScreen
!= 0) printf(" 'normal'");
1106 if (largeScreen
!= 0) printf(" 'large'");
1107 if (xlargeScreen
!= 0) printf(" 'xlarge'");
1111 Vector
<String8
> locales
;
1112 res
.getLocales(&locales
);
1113 const size_t NL
= locales
.size();
1114 for (size_t i
=0; i
<NL
; i
++) {
1115 const char* localeStr
= locales
[i
].string();
1116 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
1117 localeStr
= "--_--";
1119 printf(" '%s'", localeStr
);
1123 Vector
<ResTable_config
> configs
;
1124 res
.getConfigurations(&configs
);
1125 SortedVector
<int> densities
;
1126 const size_t NC
= configs
.size();
1127 for (size_t i
=0; i
<NC
; i
++) {
1128 int dens
= configs
[i
].density
;
1129 if (dens
== 0) dens
= 160;
1130 densities
.add(dens
);
1133 printf("densities:");
1134 const size_t ND
= densities
.size();
1135 for (size_t i
=0; i
<ND
; i
++) {
1136 printf(" '%d'", densities
[i
]);
1140 AssetDir
* dir
= assets
.openNonAssetDir(assetsCookie
, "lib");
1142 if (dir
->getFileCount() > 0) {
1143 printf("native-code:");
1144 for (size_t i
=0; i
<dir
->getFileCount(); i
++) {
1145 printf(" '%s'", dir
->getFileName(i
).string());
1151 } else if (strcmp("configurations", option
) == 0) {
1152 Vector
<ResTable_config
> configs
;
1153 res
.getConfigurations(&configs
);
1154 const size_t N
= configs
.size();
1155 for (size_t i
=0; i
<N
; i
++) {
1156 printf("%s\n", configs
[i
].toString().string());
1159 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
1170 return (result
!= NO_ERROR
);
1175 * Handle the "add" command, which wants to add files to a new or
1176 * pre-existing archive.
1178 int doAdd(Bundle
* bundle
)
1180 ZipFile
* zip
= NULL
;
1181 status_t result
= UNKNOWN_ERROR
;
1182 const char* zipFileName
;
1184 if (bundle
->getUpdate()) {
1185 /* avoid confusion */
1186 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
1190 if (bundle
->getFileSpecCount() < 1) {
1191 fprintf(stderr
, "ERROR: must specify zip file name\n");
1194 zipFileName
= bundle
->getFileSpecEntry(0);
1196 if (bundle
->getFileSpecCount() < 2) {
1197 fprintf(stderr
, "NOTE: nothing to do\n");
1201 zip
= openReadWrite(zipFileName
, true);
1203 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
1207 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1208 const char* fileName
= bundle
->getFileSpecEntry(i
);
1210 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
1211 printf(" '%s'... (from gzip)\n", fileName
);
1212 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
1214 if (bundle
->getJunkPath()) {
1215 String8 storageName
= String8(fileName
).getPathLeaf();
1216 printf(" '%s' as '%s'...\n", fileName
, storageName
.string());
1217 result
= zip
->add(fileName
, storageName
.string(),
1218 bundle
->getCompressionMethod(), NULL
);
1220 printf(" '%s'...\n", fileName
);
1221 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
1224 if (result
!= NO_ERROR
) {
1225 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
1226 if (result
== NAME_NOT_FOUND
)
1227 fprintf(stderr
, ": file not found\n");
1228 else if (result
== ALREADY_EXISTS
)
1229 fprintf(stderr
, ": already exists in archive\n");
1231 fprintf(stderr
, "\n");
1240 return (result
!= NO_ERROR
);
1245 * Delete files from an existing archive.
1247 int doRemove(Bundle
* bundle
)
1249 ZipFile
* zip
= NULL
;
1250 status_t result
= UNKNOWN_ERROR
;
1251 const char* zipFileName
;
1253 if (bundle
->getFileSpecCount() < 1) {
1254 fprintf(stderr
, "ERROR: must specify zip file name\n");
1257 zipFileName
= bundle
->getFileSpecEntry(0);
1259 if (bundle
->getFileSpecCount() < 2) {
1260 fprintf(stderr
, "NOTE: nothing to do\n");
1264 zip
= openReadWrite(zipFileName
, false);
1266 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
1271 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1272 const char* fileName
= bundle
->getFileSpecEntry(i
);
1275 entry
= zip
->getEntryByName(fileName
);
1276 if (entry
== NULL
) {
1277 printf(" '%s' NOT FOUND\n", fileName
);
1281 result
= zip
->remove(entry
);
1283 if (result
!= NO_ERROR
) {
1284 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
1285 bundle
->getFileSpecEntry(i
), zipFileName
);
1290 /* update the archive */
1295 return (result
!= NO_ERROR
);
1300 * Package up an asset directory and associated application files.
1302 int doPackage(Bundle
* bundle
)
1304 const char* outputAPKFile
;
1307 sp
<AaptAssets
> assets
;
1310 // -c zz_ZZ means do pseudolocalization
1311 ResourceFilter filter
;
1312 err
= filter
.parse(bundle
->getConfigurations());
1313 if (err
!= NO_ERROR
) {
1316 if (filter
.containsPseudo()) {
1317 bundle
->setPseudolocalize(true);
1320 N
= bundle
->getFileSpecCount();
1321 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
1322 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
1323 fprintf(stderr
, "ERROR: no input files\n");
1327 outputAPKFile
= bundle
->getOutputAPKFile();
1329 // Make sure the filenames provided exist and are of the appropriate type.
1330 if (outputAPKFile
) {
1332 type
= getFileType(outputAPKFile
);
1333 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
1335 "ERROR: output file '%s' exists but is not regular file\n",
1342 assets
= new AaptAssets();
1343 err
= assets
->slurpFromArgs(bundle
);
1348 if (bundle
->getVerbose()) {
1352 // If they asked for any files that need to be compiled, do so.
1353 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
1354 err
= buildResources(bundle
, assets
);
1360 // At this point we've read everything and processed everything. From here
1361 // on out it's just writing output files.
1362 if (SourcePos::hasErrors()) {
1366 // Write out R.java constants
1367 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
1368 if (bundle
->getCustomPackage() == NULL
) {
1369 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
1371 const String8
customPkg(bundle
->getCustomPackage());
1372 err
= writeResourceSymbols(bundle
, assets
, customPkg
, true);
1378 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
1382 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
1388 // Write out the ProGuard file
1389 err
= writeProguardFile(bundle
, assets
);
1395 if (outputAPKFile
) {
1396 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
1397 if (err
!= NO_ERROR
) {
1398 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
1405 if (SourcePos::hasErrors()) {
1406 SourcePos::printErrors(stderr
);