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 printf("\nResource table:\n");
205 Asset
* manifestAsset
= assets
.openNonAsset("AndroidManifest.xml",
206 Asset::ACCESS_BUFFER
);
207 if (manifestAsset
== NULL
) {
208 printf("\nNo AndroidManifest.xml found.\n");
210 printf("\nAndroid manifest:\n");
212 tree
.setTo(manifestAsset
->getBuffer(true),
213 manifestAsset
->getLength());
214 printXMLBlock(&tree
);
216 delete manifestAsset
;
226 static ssize_t
indexOfAttribute(const ResXMLTree
& tree
, uint32_t attrRes
)
228 size_t N
= tree
.getAttributeCount();
229 for (size_t i
=0; i
<N
; i
++) {
230 if (tree
.getAttributeNameResID(i
) == attrRes
) {
237 String8
getAttribute(const ResXMLTree
& tree
, const char* ns
,
238 const char* attr
, String8
* outError
)
240 ssize_t idx
= tree
.indexOfAttribute(ns
, attr
);
245 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
246 if (value
.dataType
!= Res_value::TYPE_STRING
) {
247 if (outError
!= NULL
) *outError
= "attribute is not a string value";
252 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
253 return str
? String8(str
, len
) : String8();
256 static String8
getAttribute(const ResXMLTree
& tree
, uint32_t attrRes
, String8
* outError
)
258 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
263 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
264 if (value
.dataType
!= Res_value::TYPE_STRING
) {
265 if (outError
!= NULL
) *outError
= "attribute is not a string value";
270 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
271 return str
? String8(str
, len
) : String8();
274 static int32_t getIntegerAttribute(const ResXMLTree
& tree
, uint32_t attrRes
,
275 String8
* outError
, int32_t defValue
= -1)
277 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
282 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
283 if (value
.dataType
< Res_value::TYPE_FIRST_INT
284 || value
.dataType
> Res_value::TYPE_LAST_INT
) {
285 if (outError
!= NULL
) *outError
= "attribute is not an integer value";
292 static String8
getResolvedAttribute(const ResTable
* resTable
, const ResXMLTree
& tree
,
293 uint32_t attrRes
, String8
* outError
)
295 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
300 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
301 if (value
.dataType
== Res_value::TYPE_STRING
) {
303 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
304 return str
? String8(str
, len
) : String8();
306 resTable
->resolveReference(&value
, 0);
307 if (value
.dataType
!= Res_value::TYPE_STRING
) {
308 if (outError
!= NULL
) *outError
= "attribute is not a string value";
313 const Res_value
* value2
= &value
;
314 const char16_t* str
= const_cast<ResTable
*>(resTable
)->valueToString(value2
, 0, NULL
, &len
);
315 return str
? String8(str
, len
) : String8();
318 // These are attribute resource constants for the platform, as found
321 NAME_ATTR
= 0x01010003,
322 VERSION_CODE_ATTR
= 0x0101021b,
323 VERSION_NAME_ATTR
= 0x0101021c,
324 LABEL_ATTR
= 0x01010001,
325 ICON_ATTR
= 0x01010002,
326 MIN_SDK_VERSION_ATTR
= 0x0101020c,
327 MAX_SDK_VERSION_ATTR
= 0x01010271,
328 REQ_TOUCH_SCREEN_ATTR
= 0x01010227,
329 REQ_KEYBOARD_TYPE_ATTR
= 0x01010228,
330 REQ_HARD_KEYBOARD_ATTR
= 0x01010229,
331 REQ_NAVIGATION_ATTR
= 0x0101022a,
332 REQ_FIVE_WAY_NAV_ATTR
= 0x01010232,
333 TARGET_SDK_VERSION_ATTR
= 0x01010270,
334 TEST_ONLY_ATTR
= 0x01010272,
335 DENSITY_ATTR
= 0x0101026c,
336 GL_ES_VERSION_ATTR
= 0x01010281,
337 SMALL_SCREEN_ATTR
= 0x01010284,
338 NORMAL_SCREEN_ATTR
= 0x01010285,
339 LARGE_SCREEN_ATTR
= 0x01010286,
340 XLARGE_SCREEN_ATTR
= 0x010102bf,
341 REQUIRED_ATTR
= 0x0101028e,
344 const char *getComponentName(String8
&pkgName
, String8
&componentName
) {
345 ssize_t idx
= componentName
.find(".");
346 String8
retStr(pkgName
);
348 retStr
+= componentName
;
349 } else if (idx
< 0) {
351 retStr
+= componentName
;
353 return componentName
.string();
355 return retStr
.string();
359 * Handle the "dump" command, to extract select data from an archive.
361 int doDump(Bundle
* bundle
)
363 status_t result
= UNKNOWN_ERROR
;
366 if (bundle
->getFileSpecCount() < 1) {
367 fprintf(stderr
, "ERROR: no dump option specified\n");
371 if (bundle
->getFileSpecCount() < 2) {
372 fprintf(stderr
, "ERROR: no dump file specified\n");
376 const char* option
= bundle
->getFileSpecEntry(0);
377 const char* filename
= bundle
->getFileSpecEntry(1);
381 if (!assets
.addAssetPath(String8(filename
), &assetsCookie
)) {
382 fprintf(stderr
, "ERROR: dump failed because assets could not be loaded\n");
386 const ResTable
& res
= assets
.getResources(false);
388 fprintf(stderr
, "ERROR: dump failed because no resource table was found\n");
392 if (strcmp("resources", option
) == 0) {
393 res
.print(bundle
->getValues());
395 } else if (strcmp("xmltree", option
) == 0) {
396 if (bundle
->getFileSpecCount() < 3) {
397 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
401 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
402 const char* resname
= bundle
->getFileSpecEntry(i
);
404 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
406 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
410 if (tree
.setTo(asset
->getBuffer(true),
411 asset
->getLength()) != NO_ERROR
) {
412 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
416 printXMLBlock(&tree
);
422 } else if (strcmp("xmlstrings", option
) == 0) {
423 if (bundle
->getFileSpecCount() < 3) {
424 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
428 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
429 const char* resname
= bundle
->getFileSpecEntry(i
);
431 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
433 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
437 if (tree
.setTo(asset
->getBuffer(true),
438 asset
->getLength()) != NO_ERROR
) {
439 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
442 printStringPool(&tree
.getStrings());
449 asset
= assets
.openNonAsset("AndroidManifest.xml",
450 Asset::ACCESS_BUFFER
);
452 fprintf(stderr
, "ERROR: dump failed because no AndroidManifest.xml found\n");
456 if (tree
.setTo(asset
->getBuffer(true),
457 asset
->getLength()) != NO_ERROR
) {
458 fprintf(stderr
, "ERROR: AndroidManifest.xml is corrupt\n");
463 if (strcmp("permissions", option
) == 0) {
465 ResXMLTree::event_code_t code
;
467 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
468 if (code
== ResXMLTree::END_TAG
) {
472 if (code
!= ResXMLTree::START_TAG
) {
476 String8
tag(tree
.getElementName(&len
));
477 //printf("Depth %d tag %s\n", depth, tag.string());
479 if (tag
!= "manifest") {
480 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
483 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
484 printf("package: %s\n", pkg
.string());
485 } else if (depth
== 2 && tag
== "permission") {
487 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
489 fprintf(stderr
, "ERROR: %s\n", error
.string());
492 printf("permission: %s\n", name
.string());
493 } else if (depth
== 2 && tag
== "uses-permission") {
495 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
497 fprintf(stderr
, "ERROR: %s\n", error
.string());
500 printf("uses-permission: %s\n", name
.string());
503 } else if (strcmp("badging", option
) == 0) {
505 ResXMLTree::event_code_t code
;
508 bool withinActivity
= false;
509 bool isMainActivity
= false;
510 bool isLauncherActivity
= false;
511 bool isSearchable
= false;
512 bool withinApplication
= false;
513 bool withinReceiver
= false;
514 bool withinService
= false;
515 bool withinIntentFilter
= false;
516 bool hasMainActivity
= false;
517 bool hasOtherActivities
= false;
518 bool hasOtherReceivers
= false;
519 bool hasOtherServices
= false;
520 bool hasWallpaperService
= false;
521 bool hasImeService
= false;
522 bool hasWidgetReceivers
= false;
523 bool hasIntentFilter
= false;
524 bool actMainActivity
= false;
525 bool actWidgetReceivers
= false;
526 bool actImeService
= false;
527 bool actWallpaperService
= false;
529 // This next group of variables is used to implement a group of
530 // backward-compatibility heuristics necessitated by the addition of
531 // some new uses-feature constants in 2.1 and 2.2. In most cases, the
532 // heuristic is "if an app requests a permission but doesn't explicitly
533 // request the corresponding <uses-feature>, presume it's there anyway".
534 bool specCameraFeature
= false; // camera-related
535 bool specCameraAutofocusFeature
= false;
536 bool reqCameraAutofocusFeature
= false;
537 bool reqCameraFlashFeature
= false;
538 bool hasCameraPermission
= false;
539 bool specLocationFeature
= false; // location-related
540 bool specNetworkLocFeature
= false;
541 bool reqNetworkLocFeature
= false;
542 bool specGpsFeature
= false;
543 bool reqGpsFeature
= false;
544 bool hasMockLocPermission
= false;
545 bool hasCoarseLocPermission
= false;
546 bool hasGpsPermission
= false;
547 bool hasGeneralLocPermission
= false;
548 bool specBluetoothFeature
= false; // Bluetooth API-related
549 bool hasBluetoothPermission
= false;
550 bool specMicrophoneFeature
= false; // microphone-related
551 bool hasRecordAudioPermission
= false;
552 bool specWiFiFeature
= false;
553 bool hasWiFiPermission
= false;
554 bool specTelephonyFeature
= false; // telephony-related
555 bool reqTelephonySubFeature
= false;
556 bool hasTelephonyPermission
= false;
557 bool specTouchscreenFeature
= false; // touchscreen-related
558 bool specMultitouchFeature
= false;
559 bool reqDistinctMultitouchFeature
= false;
560 // 2.2 also added some other features that apps can request, but that
561 // have no corresponding permission, so we cannot implement any
562 // back-compatibility heuristic for them. The below are thus unnecessary
563 // (but are retained here for documentary purposes.)
564 //bool specCompassFeature = false;
565 //bool specAccelerometerFeature = false;
566 //bool specProximityFeature = false;
567 //bool specAmbientLightFeature = false;
568 //bool specLiveWallpaperFeature = false;
572 int normalScreen
= 1;
574 int xlargeScreen
= 1;
576 String8 activityName
;
577 String8 activityLabel
;
578 String8 activityIcon
;
579 String8 receiverName
;
581 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
582 if (code
== ResXMLTree::END_TAG
) {
585 withinApplication
= false;
586 } else if (depth
< 3) {
587 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
588 const char *aName
= getComponentName(pkg
, activityName
);
590 printf("launchable activity name='%s'", aName
);
592 printf("label='%s' icon='%s'\n",
593 activityLabel
.string(),
594 activityIcon
.string());
596 if (!hasIntentFilter
) {
597 hasOtherActivities
|= withinActivity
;
598 hasOtherReceivers
|= withinReceiver
;
599 hasOtherServices
|= withinService
;
601 withinActivity
= false;
602 withinService
= false;
603 withinReceiver
= false;
604 hasIntentFilter
= false;
605 isMainActivity
= isLauncherActivity
= false;
606 } else if (depth
< 4) {
607 if (withinIntentFilter
) {
608 if (withinActivity
) {
609 hasMainActivity
|= actMainActivity
;
610 hasOtherActivities
|= !actMainActivity
;
611 } else if (withinReceiver
) {
612 hasWidgetReceivers
|= actWidgetReceivers
;
613 hasOtherReceivers
|= !actWidgetReceivers
;
614 } else if (withinService
) {
615 hasImeService
|= actImeService
;
616 hasWallpaperService
|= actWallpaperService
;
617 hasOtherServices
|= (!actImeService
&& !actWallpaperService
);
620 withinIntentFilter
= false;
624 if (code
!= ResXMLTree::START_TAG
) {
628 String8
tag(tree
.getElementName(&len
));
629 //printf("Depth %d, %s\n", depth, tag.string());
631 if (tag
!= "manifest") {
632 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
635 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
636 printf("package: name='%s' ", pkg
.string());
637 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
639 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
642 if (versionCode
> 0) {
643 printf("versionCode='%d' ", versionCode
);
645 printf("versionCode='' ");
647 String8 versionName
= getResolvedAttribute(&res
, tree
, VERSION_NAME_ATTR
, &error
);
649 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
652 printf("versionName='%s'\n", versionName
.string());
653 } else if (depth
== 2) {
654 withinApplication
= false;
655 if (tag
== "application") {
656 withinApplication
= true;
657 String8 label
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
659 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
662 printf("application: label='%s' ", label
.string());
663 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
665 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
668 printf("icon='%s'\n", icon
.string());
669 int32_t testOnly
= getIntegerAttribute(tree
, TEST_ONLY_ATTR
, &error
, 0);
671 fprintf(stderr
, "ERROR getting 'android:testOnly' attribute: %s\n", error
.string());
675 printf("testOnly='%d'\n", testOnly
);
677 } else if (tag
== "uses-sdk") {
678 int32_t code
= getIntegerAttribute(tree
, MIN_SDK_VERSION_ATTR
, &error
);
681 String8 name
= getResolvedAttribute(&res
, tree
, MIN_SDK_VERSION_ATTR
, &error
);
683 fprintf(stderr
, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
687 if (name
== "Donut") targetSdk
= 4;
688 printf("sdkVersion:'%s'\n", name
.string());
689 } else if (code
!= -1) {
691 printf("sdkVersion:'%d'\n", code
);
693 code
= getIntegerAttribute(tree
, MAX_SDK_VERSION_ATTR
, NULL
, -1);
695 printf("maxSdkVersion:'%d'\n", code
);
697 code
= getIntegerAttribute(tree
, TARGET_SDK_VERSION_ATTR
, &error
);
700 String8 name
= getResolvedAttribute(&res
, tree
, TARGET_SDK_VERSION_ATTR
, &error
);
702 fprintf(stderr
, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
706 if (name
== "Donut" && targetSdk
< 4) targetSdk
= 4;
707 printf("targetSdkVersion:'%s'\n", name
.string());
708 } else if (code
!= -1) {
709 if (targetSdk
< code
) {
712 printf("targetSdkVersion:'%d'\n", code
);
714 } else if (tag
== "uses-configuration") {
715 int32_t reqTouchScreen
= getIntegerAttribute(tree
,
716 REQ_TOUCH_SCREEN_ATTR
, NULL
, 0);
717 int32_t reqKeyboardType
= getIntegerAttribute(tree
,
718 REQ_KEYBOARD_TYPE_ATTR
, NULL
, 0);
719 int32_t reqHardKeyboard
= getIntegerAttribute(tree
,
720 REQ_HARD_KEYBOARD_ATTR
, NULL
, 0);
721 int32_t reqNavigation
= getIntegerAttribute(tree
,
722 REQ_NAVIGATION_ATTR
, NULL
, 0);
723 int32_t reqFiveWayNav
= getIntegerAttribute(tree
,
724 REQ_FIVE_WAY_NAV_ATTR
, NULL
, 0);
725 printf("uses-configuration:");
726 if (reqTouchScreen
!= 0) {
727 printf(" reqTouchScreen='%d'", reqTouchScreen
);
729 if (reqKeyboardType
!= 0) {
730 printf(" reqKeyboardType='%d'", reqKeyboardType
);
732 if (reqHardKeyboard
!= 0) {
733 printf(" reqHardKeyboard='%d'", reqHardKeyboard
);
735 if (reqNavigation
!= 0) {
736 printf(" reqNavigation='%d'", reqNavigation
);
738 if (reqFiveWayNav
!= 0) {
739 printf(" reqFiveWayNav='%d'", reqFiveWayNav
);
742 } else if (tag
== "supports-density") {
743 int32_t dens
= getIntegerAttribute(tree
, DENSITY_ATTR
, &error
);
745 fprintf(stderr
, "ERROR getting 'android:density' attribute: %s\n",
749 printf("supports-density:'%d'\n", dens
);
750 } else if (tag
== "supports-screens") {
751 smallScreen
= getIntegerAttribute(tree
,
752 SMALL_SCREEN_ATTR
, NULL
, 1);
753 normalScreen
= getIntegerAttribute(tree
,
754 NORMAL_SCREEN_ATTR
, NULL
, 1);
755 largeScreen
= getIntegerAttribute(tree
,
756 LARGE_SCREEN_ATTR
, NULL
, 1);
757 xlargeScreen
= getIntegerAttribute(tree
,
758 XLARGE_SCREEN_ATTR
, NULL
, 1);
759 } else if (tag
== "uses-feature") {
760 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
762 if (name
!= "" && error
== "") {
763 int req
= getIntegerAttribute(tree
,
764 REQUIRED_ATTR
, NULL
, 1);
766 if (name
== "android.hardware.camera") {
767 specCameraFeature
= true;
768 } else if (name
== "android.hardware.camera.autofocus") {
769 // these have no corresponding permission to check for,
770 // but should imply the foundational camera permission
771 reqCameraAutofocusFeature
= reqCameraAutofocusFeature
|| req
;
772 specCameraAutofocusFeature
= true;
773 } else if (req
&& (name
== "android.hardware.camera.flash")) {
774 // these have no corresponding permission to check for,
775 // but should imply the foundational camera permission
776 reqCameraFlashFeature
= true;
777 } else if (name
== "android.hardware.location") {
778 specLocationFeature
= true;
779 } else if (name
== "android.hardware.location.network") {
780 specNetworkLocFeature
= true;
781 reqNetworkLocFeature
= reqNetworkLocFeature
|| req
;
782 } else if (name
== "android.hardware.location.gps") {
783 specGpsFeature
= true;
784 reqGpsFeature
= reqGpsFeature
|| req
;
785 } else if (name
== "android.hardware.bluetooth") {
786 specBluetoothFeature
= true;
787 } else if (name
== "android.hardware.touchscreen") {
788 specTouchscreenFeature
= true;
789 } else if (name
== "android.hardware.touchscreen.multitouch") {
790 specMultitouchFeature
= true;
791 } else if (name
== "android.hardware.touchscreen.multitouch.distinct") {
792 reqDistinctMultitouchFeature
= reqDistinctMultitouchFeature
|| req
;
793 } else if (name
== "android.hardware.microphone") {
794 specMicrophoneFeature
= true;
795 } else if (name
== "android.hardware.wifi") {
796 specWiFiFeature
= true;
797 } else if (name
== "android.hardware.telephony") {
798 specTelephonyFeature
= true;
799 } else if (req
&& (name
== "android.hardware.telephony.gsm" ||
800 name
== "android.hardware.telephony.cdma")) {
801 // these have no corresponding permission to check for,
802 // but should imply the foundational telephony permission
803 reqTelephonySubFeature
= true;
805 printf("uses-feature%s:'%s'\n",
806 req
? "" : "-not-required", name
.string());
808 int vers
= getIntegerAttribute(tree
,
809 GL_ES_VERSION_ATTR
, &error
);
811 printf("uses-gl-es:'0x%x'\n", vers
);
814 } else if (tag
== "uses-permission") {
815 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
816 if (name
!= "" && error
== "") {
817 if (name
== "android.permission.CAMERA") {
818 hasCameraPermission
= true;
819 } else if (name
== "android.permission.ACCESS_FINE_LOCATION") {
820 hasGpsPermission
= true;
821 } else if (name
== "android.permission.ACCESS_MOCK_LOCATION") {
822 hasMockLocPermission
= true;
823 } else if (name
== "android.permission.ACCESS_COARSE_LOCATION") {
824 hasCoarseLocPermission
= true;
825 } else if (name
== "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
826 name
== "android.permission.INSTALL_LOCATION_PROVIDER") {
827 hasGeneralLocPermission
= true;
828 } else if (name
== "android.permission.BLUETOOTH" ||
829 name
== "android.permission.BLUETOOTH_ADMIN") {
830 hasBluetoothPermission
= true;
831 } else if (name
== "android.permission.RECORD_AUDIO") {
832 hasRecordAudioPermission
= true;
833 } else if (name
== "android.permission.ACCESS_WIFI_STATE" ||
834 name
== "android.permission.CHANGE_WIFI_STATE" ||
835 name
== "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
836 hasWiFiPermission
= true;
837 } else if (name
== "android.permission.CALL_PHONE" ||
838 name
== "android.permission.CALL_PRIVILEGED" ||
839 name
== "android.permission.MODIFY_PHONE_STATE" ||
840 name
== "android.permission.PROCESS_OUTGOING_CALLS" ||
841 name
== "android.permission.READ_SMS" ||
842 name
== "android.permission.RECEIVE_SMS" ||
843 name
== "android.permission.RECEIVE_MMS" ||
844 name
== "android.permission.RECEIVE_WAP_PUSH" ||
845 name
== "android.permission.SEND_SMS" ||
846 name
== "android.permission.WRITE_APN_SETTINGS" ||
847 name
== "android.permission.WRITE_SMS") {
848 hasTelephonyPermission
= true;
850 printf("uses-permission:'%s'\n", name
.string());
852 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
856 } else if (tag
== "uses-package") {
857 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
858 if (name
!= "" && error
== "") {
859 printf("uses-package:'%s'\n", name
.string());
861 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
865 } else if (tag
== "original-package") {
866 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
867 if (name
!= "" && error
== "") {
868 printf("original-package:'%s'\n", name
.string());
870 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
874 } else if (tag
== "uses-gl-texture") {
875 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
876 if (name
!= "" && error
== "") {
877 printf("uses-gl-texture:'%s'\n", name
.string());
879 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
884 } else if (depth
== 3 && withinApplication
) {
885 withinActivity
= false;
886 withinReceiver
= false;
887 withinService
= false;
888 hasIntentFilter
= false;
889 if(tag
== "activity") {
890 withinActivity
= true;
891 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
893 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
897 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
899 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
903 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
905 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
908 } else if (tag
== "uses-library") {
909 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
911 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
914 int req
= getIntegerAttribute(tree
,
915 REQUIRED_ATTR
, NULL
, 1);
916 printf("uses-library%s:'%s'\n",
917 req
? "" : "-not-required", libraryName
.string());
918 } else if (tag
== "receiver") {
919 withinReceiver
= true;
920 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
923 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
926 } else if (tag
== "service") {
927 withinService
= true;
928 serviceName
= getAttribute(tree
, NAME_ATTR
, &error
);
931 fprintf(stderr
, "ERROR getting 'android:name' attribute for service: %s\n", error
.string());
935 } else if ((depth
== 4) && (tag
== "intent-filter")) {
936 hasIntentFilter
= true;
937 withinIntentFilter
= true;
938 actMainActivity
= actWidgetReceivers
= actImeService
= actWallpaperService
= false;
939 } else if ((depth
== 5) && withinIntentFilter
){
941 if (tag
== "action") {
942 action
= getAttribute(tree
, NAME_ATTR
, &error
);
944 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
947 if (withinActivity
) {
948 if (action
== "android.intent.action.MAIN") {
949 isMainActivity
= true;
950 actMainActivity
= true;
952 } else if (withinReceiver
) {
953 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
954 actWidgetReceivers
= true;
956 } else if (withinService
) {
957 if (action
== "android.view.InputMethod") {
958 actImeService
= true;
959 } else if (action
== "android.service.wallpaper.WallpaperService") {
960 actWallpaperService
= true;
963 if (action
== "android.intent.action.SEARCH") {
968 if (tag
== "category") {
969 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
971 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
974 if (withinActivity
) {
975 if (category
== "android.intent.category.LAUNCHER") {
976 isLauncherActivity
= true;
983 /* The following blocks handle printing "inferred" uses-features, based
984 * on whether related features or permissions are used by the app.
985 * Note that the various spec*Feature variables denote whether the
986 * relevant tag was *present* in the AndroidManfest, not that it was
987 * present and set to true.
989 // Camera-related back-compatibility logic
990 if (!specCameraFeature
) {
991 if (reqCameraFlashFeature
|| reqCameraAutofocusFeature
) {
992 // if app requested a sub-feature (autofocus or flash) and didn't
993 // request the base camera feature, we infer that it meant to
994 printf("uses-feature:'android.hardware.camera'\n");
995 } else if (hasCameraPermission
) {
996 // if app wants to use camera but didn't request the feature, we infer
997 // that it meant to, and further that it wants autofocus
998 // (which was the 1.0 - 1.5 behavior)
999 printf("uses-feature:'android.hardware.camera'\n");
1000 if (!specCameraAutofocusFeature
) {
1001 printf("uses-feature:'android.hardware.camera.autofocus'\n");
1006 // Location-related back-compatibility logic
1007 if (!specLocationFeature
&&
1008 (hasMockLocPermission
|| hasCoarseLocPermission
|| hasGpsPermission
||
1009 hasGeneralLocPermission
|| reqNetworkLocFeature
|| reqGpsFeature
)) {
1010 // if app either takes a location-related permission or requests one of the
1011 // sub-features, we infer that it also meant to request the base location feature
1012 printf("uses-feature:'android.hardware.location'\n");
1014 if (!specGpsFeature
&& hasGpsPermission
) {
1015 // if app takes GPS (FINE location) perm but does not request the GPS
1016 // feature, we infer that it meant to
1017 printf("uses-feature:'android.hardware.location.gps'\n");
1019 if (!specNetworkLocFeature
&& hasCoarseLocPermission
) {
1020 // if app takes Network location (COARSE location) perm but does not request the
1021 // network location feature, we infer that it meant to
1022 printf("uses-feature:'android.hardware.location.network'\n");
1025 // Bluetooth-related compatibility logic
1026 if (!specBluetoothFeature
&& hasBluetoothPermission
&& (targetSdk
> 4)) {
1027 // if app takes a Bluetooth permission but does not request the Bluetooth
1028 // feature, we infer that it meant to
1029 printf("uses-feature:'android.hardware.bluetooth'\n");
1032 // Microphone-related compatibility logic
1033 if (!specMicrophoneFeature
&& hasRecordAudioPermission
) {
1034 // if app takes the record-audio permission but does not request the microphone
1035 // feature, we infer that it meant to
1036 printf("uses-feature:'android.hardware.microphone'\n");
1039 // WiFi-related compatibility logic
1040 if (!specWiFiFeature
&& hasWiFiPermission
) {
1041 // if app takes one of the WiFi permissions but does not request the WiFi
1042 // feature, we infer that it meant to
1043 printf("uses-feature:'android.hardware.wifi'\n");
1046 // Telephony-related compatibility logic
1047 if (!specTelephonyFeature
&& (hasTelephonyPermission
|| reqTelephonySubFeature
)) {
1048 // if app takes one of the telephony permissions or requests a sub-feature but
1049 // does not request the base telephony feature, we infer that it meant to
1050 printf("uses-feature:'android.hardware.telephony'\n");
1053 // Touchscreen-related back-compatibility logic
1054 if (!specTouchscreenFeature
) { // not a typo!
1055 // all apps are presumed to require a touchscreen, unless they explicitly say
1056 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1057 // Note that specTouchscreenFeature is true if the tag is present, regardless
1058 // of whether its value is true or false, so this is safe
1059 printf("uses-feature:'android.hardware.touchscreen'\n");
1061 if (!specMultitouchFeature
&& reqDistinctMultitouchFeature
) {
1062 // if app takes one of the telephony permissions or requests a sub-feature but
1063 // does not request the base telephony feature, we infer that it meant to
1064 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1067 if (hasMainActivity
) {
1070 if (hasWidgetReceivers
) {
1071 printf("app-widget\n");
1073 if (hasImeService
) {
1076 if (hasWallpaperService
) {
1077 printf("wallpaper\n");
1079 if (hasOtherActivities
) {
1080 printf("other-activities\n");
1085 if (hasOtherReceivers
) {
1086 printf("other-receivers\n");
1088 if (hasOtherServices
) {
1089 printf("other-services\n");
1092 // Determine default values for any unspecified screen sizes,
1093 // based on the target SDK of the package. As of 4 (donut)
1094 // the screen size support was introduced, so all default to
1096 if (smallScreen
> 0) {
1097 smallScreen
= targetSdk
>= 4 ? -1 : 0;
1099 if (normalScreen
> 0) {
1102 if (largeScreen
> 0) {
1103 largeScreen
= targetSdk
>= 4 ? -1 : 0;
1105 if (xlargeScreen
> 0) {
1106 // Introduced in Honeycomb.
1107 xlargeScreen
= targetSdk
>= 10 ? -1 : 0;
1109 printf("supports-screens:");
1110 if (smallScreen
!= 0) printf(" 'small'");
1111 if (normalScreen
!= 0) printf(" 'normal'");
1112 if (largeScreen
!= 0) printf(" 'large'");
1113 if (xlargeScreen
!= 0) printf(" 'xlarge'");
1117 Vector
<String8
> locales
;
1118 res
.getLocales(&locales
);
1119 const size_t NL
= locales
.size();
1120 for (size_t i
=0; i
<NL
; i
++) {
1121 const char* localeStr
= locales
[i
].string();
1122 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
1123 localeStr
= "--_--";
1125 printf(" '%s'", localeStr
);
1129 Vector
<ResTable_config
> configs
;
1130 res
.getConfigurations(&configs
);
1131 SortedVector
<int> densities
;
1132 const size_t NC
= configs
.size();
1133 for (size_t i
=0; i
<NC
; i
++) {
1134 int dens
= configs
[i
].density
;
1135 if (dens
== 0) dens
= 160;
1136 densities
.add(dens
);
1139 printf("densities:");
1140 const size_t ND
= densities
.size();
1141 for (size_t i
=0; i
<ND
; i
++) {
1142 printf(" '%d'", densities
[i
]);
1146 AssetDir
* dir
= assets
.openNonAssetDir(assetsCookie
, "lib");
1148 if (dir
->getFileCount() > 0) {
1149 printf("native-code:");
1150 for (size_t i
=0; i
<dir
->getFileCount(); i
++) {
1151 printf(" '%s'", dir
->getFileName(i
).string());
1157 } else if (strcmp("configurations", option
) == 0) {
1158 Vector
<ResTable_config
> configs
;
1159 res
.getConfigurations(&configs
);
1160 const size_t N
= configs
.size();
1161 for (size_t i
=0; i
<N
; i
++) {
1162 printf("%s\n", configs
[i
].toString().string());
1165 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
1176 return (result
!= NO_ERROR
);
1181 * Handle the "add" command, which wants to add files to a new or
1182 * pre-existing archive.
1184 int doAdd(Bundle
* bundle
)
1186 ZipFile
* zip
= NULL
;
1187 status_t result
= UNKNOWN_ERROR
;
1188 const char* zipFileName
;
1190 if (bundle
->getUpdate()) {
1191 /* avoid confusion */
1192 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
1196 if (bundle
->getFileSpecCount() < 1) {
1197 fprintf(stderr
, "ERROR: must specify zip file name\n");
1200 zipFileName
= bundle
->getFileSpecEntry(0);
1202 if (bundle
->getFileSpecCount() < 2) {
1203 fprintf(stderr
, "NOTE: nothing to do\n");
1207 zip
= openReadWrite(zipFileName
, true);
1209 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
1213 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1214 const char* fileName
= bundle
->getFileSpecEntry(i
);
1216 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
1217 printf(" '%s'... (from gzip)\n", fileName
);
1218 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
1220 if (bundle
->getJunkPath()) {
1221 String8 storageName
= String8(fileName
).getPathLeaf();
1222 printf(" '%s' as '%s'...\n", fileName
, storageName
.string());
1223 result
= zip
->add(fileName
, storageName
.string(),
1224 bundle
->getCompressionMethod(), NULL
);
1226 printf(" '%s'...\n", fileName
);
1227 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
1230 if (result
!= NO_ERROR
) {
1231 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
1232 if (result
== NAME_NOT_FOUND
)
1233 fprintf(stderr
, ": file not found\n");
1234 else if (result
== ALREADY_EXISTS
)
1235 fprintf(stderr
, ": already exists in archive\n");
1237 fprintf(stderr
, "\n");
1246 return (result
!= NO_ERROR
);
1251 * Delete files from an existing archive.
1253 int doRemove(Bundle
* bundle
)
1255 ZipFile
* zip
= NULL
;
1256 status_t result
= UNKNOWN_ERROR
;
1257 const char* zipFileName
;
1259 if (bundle
->getFileSpecCount() < 1) {
1260 fprintf(stderr
, "ERROR: must specify zip file name\n");
1263 zipFileName
= bundle
->getFileSpecEntry(0);
1265 if (bundle
->getFileSpecCount() < 2) {
1266 fprintf(stderr
, "NOTE: nothing to do\n");
1270 zip
= openReadWrite(zipFileName
, false);
1272 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
1277 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1278 const char* fileName
= bundle
->getFileSpecEntry(i
);
1281 entry
= zip
->getEntryByName(fileName
);
1282 if (entry
== NULL
) {
1283 printf(" '%s' NOT FOUND\n", fileName
);
1287 result
= zip
->remove(entry
);
1289 if (result
!= NO_ERROR
) {
1290 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
1291 bundle
->getFileSpecEntry(i
), zipFileName
);
1296 /* update the archive */
1301 return (result
!= NO_ERROR
);
1306 * Package up an asset directory and associated application files.
1308 int doPackage(Bundle
* bundle
)
1310 const char* outputAPKFile
;
1313 sp
<AaptAssets
> assets
;
1316 // -c zz_ZZ means do pseudolocalization
1317 ResourceFilter filter
;
1318 err
= filter
.parse(bundle
->getConfigurations());
1319 if (err
!= NO_ERROR
) {
1322 if (filter
.containsPseudo()) {
1323 bundle
->setPseudolocalize(true);
1326 N
= bundle
->getFileSpecCount();
1327 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
1328 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
1329 fprintf(stderr
, "ERROR: no input files\n");
1333 outputAPKFile
= bundle
->getOutputAPKFile();
1335 // Make sure the filenames provided exist and are of the appropriate type.
1336 if (outputAPKFile
) {
1338 type
= getFileType(outputAPKFile
);
1339 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
1341 "ERROR: output file '%s' exists but is not regular file\n",
1348 assets
= new AaptAssets();
1349 err
= assets
->slurpFromArgs(bundle
);
1354 if (bundle
->getVerbose()) {
1358 // If they asked for any files that need to be compiled, do so.
1359 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
1360 err
= buildResources(bundle
, assets
);
1366 // At this point we've read everything and processed everything. From here
1367 // on out it's just writing output files.
1368 if (SourcePos::hasErrors()) {
1372 // Write out R.java constants
1373 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
1374 if (bundle
->getCustomPackage() == NULL
) {
1375 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
1377 const String8
customPkg(bundle
->getCustomPackage());
1378 err
= writeResourceSymbols(bundle
, assets
, customPkg
, true);
1384 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
1388 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
1394 // Write out the ProGuard file
1395 err
= writeProguardFile(bundle
, assets
);
1401 if (outputAPKFile
) {
1402 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
1403 if (err
!= NO_ERROR
) {
1404 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
1411 if (SourcePos::hasErrors()) {
1412 SourcePos::printErrors(stderr
);