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
== "original-package") {
857 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
858 if (name
!= "" && error
== "") {
859 printf("original-package:'%s'\n", name
.string());
861 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
866 } else if (depth
== 3 && withinApplication
) {
867 withinActivity
= false;
868 withinReceiver
= false;
869 withinService
= false;
870 hasIntentFilter
= false;
871 if(tag
== "activity") {
872 withinActivity
= true;
873 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
875 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
879 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
881 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
885 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
887 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
890 } else if (tag
== "uses-library") {
891 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
893 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
896 int req
= getIntegerAttribute(tree
,
897 REQUIRED_ATTR
, NULL
, 1);
898 printf("uses-library%s:'%s'\n",
899 req
? "" : "-not-required", libraryName
.string());
900 } else if (tag
== "receiver") {
901 withinReceiver
= true;
902 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
905 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
908 } else if (tag
== "service") {
909 withinService
= true;
910 serviceName
= getAttribute(tree
, NAME_ATTR
, &error
);
913 fprintf(stderr
, "ERROR getting 'android:name' attribute for service: %s\n", error
.string());
917 } else if ((depth
== 4) && (tag
== "intent-filter")) {
918 hasIntentFilter
= true;
919 withinIntentFilter
= true;
920 actMainActivity
= actWidgetReceivers
= actImeService
= actWallpaperService
= false;
921 } else if ((depth
== 5) && withinIntentFilter
){
923 if (tag
== "action") {
924 action
= getAttribute(tree
, NAME_ATTR
, &error
);
926 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
929 if (withinActivity
) {
930 if (action
== "android.intent.action.MAIN") {
931 isMainActivity
= true;
932 actMainActivity
= true;
934 } else if (withinReceiver
) {
935 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
936 actWidgetReceivers
= true;
938 } else if (withinService
) {
939 if (action
== "android.view.InputMethod") {
940 actImeService
= true;
941 } else if (action
== "android.service.wallpaper.WallpaperService") {
942 actWallpaperService
= true;
945 if (action
== "android.intent.action.SEARCH") {
950 if (tag
== "category") {
951 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
953 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
956 if (withinActivity
) {
957 if (category
== "android.intent.category.LAUNCHER") {
958 isLauncherActivity
= true;
965 /* The following blocks handle printing "inferred" uses-features, based
966 * on whether related features or permissions are used by the app.
967 * Note that the various spec*Feature variables denote whether the
968 * relevant tag was *present* in the AndroidManfest, not that it was
969 * present and set to true.
971 // Camera-related back-compatibility logic
972 if (!specCameraFeature
) {
973 if (reqCameraFlashFeature
|| reqCameraAutofocusFeature
) {
974 // if app requested a sub-feature (autofocus or flash) and didn't
975 // request the base camera feature, we infer that it meant to
976 printf("uses-feature:'android.hardware.camera'\n");
977 } else if (hasCameraPermission
) {
978 // if app wants to use camera but didn't request the feature, we infer
979 // that it meant to, and further that it wants autofocus
980 // (which was the 1.0 - 1.5 behavior)
981 printf("uses-feature:'android.hardware.camera'\n");
982 if (!specCameraAutofocusFeature
) {
983 printf("uses-feature:'android.hardware.camera.autofocus'\n");
988 // Location-related back-compatibility logic
989 if (!specLocationFeature
&&
990 (hasMockLocPermission
|| hasCoarseLocPermission
|| hasGpsPermission
||
991 hasGeneralLocPermission
|| reqNetworkLocFeature
|| reqGpsFeature
)) {
992 // if app either takes a location-related permission or requests one of the
993 // sub-features, we infer that it also meant to request the base location feature
994 printf("uses-feature:'android.hardware.location'\n");
996 if (!specGpsFeature
&& hasGpsPermission
) {
997 // if app takes GPS (FINE location) perm but does not request the GPS
998 // feature, we infer that it meant to
999 printf("uses-feature:'android.hardware.location.gps'\n");
1001 if (!specNetworkLocFeature
&& hasCoarseLocPermission
) {
1002 // if app takes Network location (COARSE location) perm but does not request the
1003 // network location feature, we infer that it meant to
1004 printf("uses-feature:'android.hardware.location.network'\n");
1007 // Bluetooth-related compatibility logic
1008 if (!specBluetoothFeature
&& hasBluetoothPermission
&& (targetSdk
> 4)) {
1009 // if app takes a Bluetooth permission but does not request the Bluetooth
1010 // feature, we infer that it meant to
1011 printf("uses-feature:'android.hardware.bluetooth'\n");
1014 // Microphone-related compatibility logic
1015 if (!specMicrophoneFeature
&& hasRecordAudioPermission
) {
1016 // if app takes the record-audio permission but does not request the microphone
1017 // feature, we infer that it meant to
1018 printf("uses-feature:'android.hardware.microphone'\n");
1021 // WiFi-related compatibility logic
1022 if (!specWiFiFeature
&& hasWiFiPermission
) {
1023 // if app takes one of the WiFi permissions but does not request the WiFi
1024 // feature, we infer that it meant to
1025 printf("uses-feature:'android.hardware.wifi'\n");
1028 // Telephony-related compatibility logic
1029 if (!specTelephonyFeature
&& (hasTelephonyPermission
|| reqTelephonySubFeature
)) {
1030 // if app takes one of the telephony permissions or requests a sub-feature but
1031 // does not request the base telephony feature, we infer that it meant to
1032 printf("uses-feature:'android.hardware.telephony'\n");
1035 // Touchscreen-related back-compatibility logic
1036 if (!specTouchscreenFeature
) { // not a typo!
1037 // all apps are presumed to require a touchscreen, unless they explicitly say
1038 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1039 // Note that specTouchscreenFeature is true if the tag is present, regardless
1040 // of whether its value is true or false, so this is safe
1041 printf("uses-feature:'android.hardware.touchscreen'\n");
1043 if (!specMultitouchFeature
&& reqDistinctMultitouchFeature
) {
1044 // if app takes one of the telephony permissions or requests a sub-feature but
1045 // does not request the base telephony feature, we infer that it meant to
1046 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1049 if (hasMainActivity
) {
1052 if (hasWidgetReceivers
) {
1053 printf("app-widget\n");
1055 if (hasImeService
) {
1058 if (hasWallpaperService
) {
1059 printf("wallpaper\n");
1061 if (hasOtherActivities
) {
1062 printf("other-activities\n");
1067 if (hasOtherReceivers
) {
1068 printf("other-receivers\n");
1070 if (hasOtherServices
) {
1071 printf("other-services\n");
1074 // Determine default values for any unspecified screen sizes,
1075 // based on the target SDK of the package. As of 4 (donut)
1076 // the screen size support was introduced, so all default to
1078 if (smallScreen
> 0) {
1079 smallScreen
= targetSdk
>= 4 ? -1 : 0;
1081 if (normalScreen
> 0) {
1084 if (largeScreen
> 0) {
1085 largeScreen
= targetSdk
>= 4 ? -1 : 0;
1087 if (xlargeScreen
> 0) {
1088 // Introduced in Honeycomb.
1089 xlargeScreen
= targetSdk
>= 10 ? -1 : 0;
1091 printf("supports-screens:");
1092 if (smallScreen
!= 0) printf(" 'small'");
1093 if (normalScreen
!= 0) printf(" 'normal'");
1094 if (largeScreen
!= 0) printf(" 'large'");
1095 if (xlargeScreen
!= 0) printf(" 'xlarge'");
1099 Vector
<String8
> locales
;
1100 res
.getLocales(&locales
);
1101 const size_t NL
= locales
.size();
1102 for (size_t i
=0; i
<NL
; i
++) {
1103 const char* localeStr
= locales
[i
].string();
1104 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
1105 localeStr
= "--_--";
1107 printf(" '%s'", localeStr
);
1111 Vector
<ResTable_config
> configs
;
1112 res
.getConfigurations(&configs
);
1113 SortedVector
<int> densities
;
1114 const size_t NC
= configs
.size();
1115 for (size_t i
=0; i
<NC
; i
++) {
1116 int dens
= configs
[i
].density
;
1117 if (dens
== 0) dens
= 160;
1118 densities
.add(dens
);
1121 printf("densities:");
1122 const size_t ND
= densities
.size();
1123 for (size_t i
=0; i
<ND
; i
++) {
1124 printf(" '%d'", densities
[i
]);
1128 AssetDir
* dir
= assets
.openNonAssetDir(assetsCookie
, "lib");
1130 if (dir
->getFileCount() > 0) {
1131 printf("native-code:");
1132 for (size_t i
=0; i
<dir
->getFileCount(); i
++) {
1133 printf(" '%s'", dir
->getFileName(i
).string());
1139 } else if (strcmp("configurations", option
) == 0) {
1140 Vector
<ResTable_config
> configs
;
1141 res
.getConfigurations(&configs
);
1142 const size_t N
= configs
.size();
1143 for (size_t i
=0; i
<N
; i
++) {
1144 printf("%s\n", configs
[i
].toString().string());
1147 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
1158 return (result
!= NO_ERROR
);
1163 * Handle the "add" command, which wants to add files to a new or
1164 * pre-existing archive.
1166 int doAdd(Bundle
* bundle
)
1168 ZipFile
* zip
= NULL
;
1169 status_t result
= UNKNOWN_ERROR
;
1170 const char* zipFileName
;
1172 if (bundle
->getUpdate()) {
1173 /* avoid confusion */
1174 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
1178 if (bundle
->getFileSpecCount() < 1) {
1179 fprintf(stderr
, "ERROR: must specify zip file name\n");
1182 zipFileName
= bundle
->getFileSpecEntry(0);
1184 if (bundle
->getFileSpecCount() < 2) {
1185 fprintf(stderr
, "NOTE: nothing to do\n");
1189 zip
= openReadWrite(zipFileName
, true);
1191 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
1195 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1196 const char* fileName
= bundle
->getFileSpecEntry(i
);
1198 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
1199 printf(" '%s'... (from gzip)\n", fileName
);
1200 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
1202 if (bundle
->getJunkPath()) {
1203 String8 storageName
= String8(fileName
).getPathLeaf();
1204 printf(" '%s' as '%s'...\n", fileName
, storageName
.string());
1205 result
= zip
->add(fileName
, storageName
.string(),
1206 bundle
->getCompressionMethod(), NULL
);
1208 printf(" '%s'...\n", fileName
);
1209 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
1212 if (result
!= NO_ERROR
) {
1213 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
1214 if (result
== NAME_NOT_FOUND
)
1215 fprintf(stderr
, ": file not found\n");
1216 else if (result
== ALREADY_EXISTS
)
1217 fprintf(stderr
, ": already exists in archive\n");
1219 fprintf(stderr
, "\n");
1228 return (result
!= NO_ERROR
);
1233 * Delete files from an existing archive.
1235 int doRemove(Bundle
* bundle
)
1237 ZipFile
* zip
= NULL
;
1238 status_t result
= UNKNOWN_ERROR
;
1239 const char* zipFileName
;
1241 if (bundle
->getFileSpecCount() < 1) {
1242 fprintf(stderr
, "ERROR: must specify zip file name\n");
1245 zipFileName
= bundle
->getFileSpecEntry(0);
1247 if (bundle
->getFileSpecCount() < 2) {
1248 fprintf(stderr
, "NOTE: nothing to do\n");
1252 zip
= openReadWrite(zipFileName
, false);
1254 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
1259 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1260 const char* fileName
= bundle
->getFileSpecEntry(i
);
1263 entry
= zip
->getEntryByName(fileName
);
1264 if (entry
== NULL
) {
1265 printf(" '%s' NOT FOUND\n", fileName
);
1269 result
= zip
->remove(entry
);
1271 if (result
!= NO_ERROR
) {
1272 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
1273 bundle
->getFileSpecEntry(i
), zipFileName
);
1278 /* update the archive */
1283 return (result
!= NO_ERROR
);
1288 * Package up an asset directory and associated application files.
1290 int doPackage(Bundle
* bundle
)
1292 const char* outputAPKFile
;
1295 sp
<AaptAssets
> assets
;
1298 // -c zz_ZZ means do pseudolocalization
1299 ResourceFilter filter
;
1300 err
= filter
.parse(bundle
->getConfigurations());
1301 if (err
!= NO_ERROR
) {
1304 if (filter
.containsPseudo()) {
1305 bundle
->setPseudolocalize(true);
1308 N
= bundle
->getFileSpecCount();
1309 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
1310 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
1311 fprintf(stderr
, "ERROR: no input files\n");
1315 outputAPKFile
= bundle
->getOutputAPKFile();
1317 // Make sure the filenames provided exist and are of the appropriate type.
1318 if (outputAPKFile
) {
1320 type
= getFileType(outputAPKFile
);
1321 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
1323 "ERROR: output file '%s' exists but is not regular file\n",
1330 assets
= new AaptAssets();
1331 err
= assets
->slurpFromArgs(bundle
);
1336 if (bundle
->getVerbose()) {
1340 // If they asked for any files that need to be compiled, do so.
1341 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
1342 err
= buildResources(bundle
, assets
);
1348 // At this point we've read everything and processed everything. From here
1349 // on out it's just writing output files.
1350 if (SourcePos::hasErrors()) {
1354 // Write out R.java constants
1355 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
1356 if (bundle
->getCustomPackage() == NULL
) {
1357 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
1359 const String8
customPkg(bundle
->getCustomPackage());
1360 err
= writeResourceSymbols(bundle
, assets
, customPkg
, true);
1366 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
1370 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
1376 // Write out the ProGuard file
1377 err
= writeProguardFile(bundle
, assets
);
1383 if (outputAPKFile
) {
1384 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
1385 if (err
!= NO_ERROR
) {
1386 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
1393 if (SourcePos::hasErrors()) {
1394 SourcePos::printErrors(stderr
);