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 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%% %s %08lx %s\n",
163 (long) entry
->getUncompressedLen(),
164 compressionName(entry
->getCompressionMethod()),
165 (long) entry
->getCompressedLen(),
166 calcPercent(entry
->getUncompressedLen(),
167 entry
->getCompressedLen()),
170 entry
->getFileName());
172 printf("%s\n", entry
->getFileName());
175 totalUncLen
+= entry
->getUncompressedLen();
176 totalCompLen
+= entry
->getCompressedLen();
179 if (bundle
->getVerbose()) {
181 "-------- ------- --- -------\n");
182 printf("%8ld %7ld %2d%% %d files\n",
185 calcPercent(totalUncLen
, totalCompLen
),
186 zip
->getNumEntries());
189 if (bundle
->getAndroidList()) {
191 if (!assets
.addAssetPath(String8(zipFileName
), NULL
)) {
192 fprintf(stderr
, "ERROR: list -a failed because assets could not be loaded\n");
196 const ResTable
& res
= assets
.getResources(false);
198 printf("\nNo resource table found.\n");
200 #ifndef HAVE_ANDROID_OS
201 printf("\nResource table:\n");
206 Asset
* manifestAsset
= assets
.openNonAsset("AndroidManifest.xml",
207 Asset::ACCESS_BUFFER
);
208 if (manifestAsset
== NULL
) {
209 printf("\nNo AndroidManifest.xml found.\n");
211 printf("\nAndroid manifest:\n");
213 tree
.setTo(manifestAsset
->getBuffer(true),
214 manifestAsset
->getLength());
215 printXMLBlock(&tree
);
217 delete manifestAsset
;
227 static ssize_t
indexOfAttribute(const ResXMLTree
& tree
, uint32_t attrRes
)
229 size_t N
= tree
.getAttributeCount();
230 for (size_t i
=0; i
<N
; i
++) {
231 if (tree
.getAttributeNameResID(i
) == attrRes
) {
238 String8
getAttribute(const ResXMLTree
& tree
, const char* ns
,
239 const char* attr
, String8
* outError
)
241 ssize_t idx
= tree
.indexOfAttribute(ns
, attr
);
246 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
247 if (value
.dataType
!= Res_value::TYPE_STRING
) {
248 if (outError
!= NULL
) *outError
= "attribute is not a string value";
253 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
254 return str
? String8(str
, len
) : String8();
257 static String8
getAttribute(const ResXMLTree
& tree
, uint32_t attrRes
, String8
* outError
)
259 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
264 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
265 if (value
.dataType
!= Res_value::TYPE_STRING
) {
266 if (outError
!= NULL
) *outError
= "attribute is not a string value";
271 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
272 return str
? String8(str
, len
) : String8();
275 static int32_t getIntegerAttribute(const ResXMLTree
& tree
, uint32_t attrRes
,
276 String8
* outError
, int32_t defValue
= -1)
278 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
283 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
284 if (value
.dataType
< Res_value::TYPE_FIRST_INT
285 || value
.dataType
> Res_value::TYPE_LAST_INT
) {
286 if (outError
!= NULL
) *outError
= "attribute is not an integer value";
293 static String8
getResolvedAttribute(const ResTable
* resTable
, const ResXMLTree
& tree
,
294 uint32_t attrRes
, String8
* outError
)
296 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
301 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
302 if (value
.dataType
== Res_value::TYPE_STRING
) {
304 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
305 return str
? String8(str
, len
) : String8();
307 resTable
->resolveReference(&value
, 0);
308 if (value
.dataType
!= Res_value::TYPE_STRING
) {
309 if (outError
!= NULL
) *outError
= "attribute is not a string value";
314 const Res_value
* value2
= &value
;
315 const char16_t* str
= const_cast<ResTable
*>(resTable
)->valueToString(value2
, 0, NULL
, &len
);
316 return str
? String8(str
, len
) : String8();
319 // These are attribute resource constants for the platform, as found
322 NAME_ATTR
= 0x01010003,
323 VERSION_CODE_ATTR
= 0x0101021b,
324 VERSION_NAME_ATTR
= 0x0101021c,
325 LABEL_ATTR
= 0x01010001,
326 ICON_ATTR
= 0x01010002,
327 MIN_SDK_VERSION_ATTR
= 0x0101020c,
328 MAX_SDK_VERSION_ATTR
= 0x01010271,
329 REQ_TOUCH_SCREEN_ATTR
= 0x01010227,
330 REQ_KEYBOARD_TYPE_ATTR
= 0x01010228,
331 REQ_HARD_KEYBOARD_ATTR
= 0x01010229,
332 REQ_NAVIGATION_ATTR
= 0x0101022a,
333 REQ_FIVE_WAY_NAV_ATTR
= 0x01010232,
334 TARGET_SDK_VERSION_ATTR
= 0x01010270,
335 TEST_ONLY_ATTR
= 0x01010272,
336 DENSITY_ATTR
= 0x0101026c,
337 GL_ES_VERSION_ATTR
= 0x01010281,
338 SMALL_SCREEN_ATTR
= 0x01010284,
339 NORMAL_SCREEN_ATTR
= 0x01010285,
340 LARGE_SCREEN_ATTR
= 0x01010286,
341 XLARGE_SCREEN_ATTR
= 0x010102bf,
342 REQUIRED_ATTR
= 0x0101028e,
345 const char *getComponentName(String8
&pkgName
, String8
&componentName
) {
346 ssize_t idx
= componentName
.find(".");
347 String8
retStr(pkgName
);
349 retStr
+= componentName
;
350 } else if (idx
< 0) {
352 retStr
+= componentName
;
354 return componentName
.string();
356 return retStr
.string();
360 * Handle the "dump" command, to extract select data from an archive.
362 int doDump(Bundle
* bundle
)
364 status_t result
= UNKNOWN_ERROR
;
367 if (bundle
->getFileSpecCount() < 1) {
368 fprintf(stderr
, "ERROR: no dump option specified\n");
372 if (bundle
->getFileSpecCount() < 2) {
373 fprintf(stderr
, "ERROR: no dump file specified\n");
377 const char* option
= bundle
->getFileSpecEntry(0);
378 const char* filename
= bundle
->getFileSpecEntry(1);
382 if (!assets
.addAssetPath(String8(filename
), &assetsCookie
)) {
383 fprintf(stderr
, "ERROR: dump failed because assets could not be loaded\n");
387 const ResTable
& res
= assets
.getResources(false);
389 fprintf(stderr
, "ERROR: dump failed because no resource table was found\n");
393 if (strcmp("resources", option
) == 0) {
394 #ifndef HAVE_ANDROID_OS
395 res
.print(bundle
->getValues());
397 } else if (strcmp("xmltree", option
) == 0) {
398 if (bundle
->getFileSpecCount() < 3) {
399 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
403 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
404 const char* resname
= bundle
->getFileSpecEntry(i
);
406 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
408 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
412 if (tree
.setTo(asset
->getBuffer(true),
413 asset
->getLength()) != NO_ERROR
) {
414 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
418 printXMLBlock(&tree
);
424 } else if (strcmp("xmlstrings", option
) == 0) {
425 if (bundle
->getFileSpecCount() < 3) {
426 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
430 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
431 const char* resname
= bundle
->getFileSpecEntry(i
);
433 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
435 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
439 if (tree
.setTo(asset
->getBuffer(true),
440 asset
->getLength()) != NO_ERROR
) {
441 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
444 printStringPool(&tree
.getStrings());
451 asset
= assets
.openNonAsset("AndroidManifest.xml",
452 Asset::ACCESS_BUFFER
);
454 fprintf(stderr
, "ERROR: dump failed because no AndroidManifest.xml found\n");
458 if (tree
.setTo(asset
->getBuffer(true),
459 asset
->getLength()) != NO_ERROR
) {
460 fprintf(stderr
, "ERROR: AndroidManifest.xml is corrupt\n");
465 if (strcmp("permissions", option
) == 0) {
467 ResXMLTree::event_code_t code
;
469 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
470 if (code
== ResXMLTree::END_TAG
) {
474 if (code
!= ResXMLTree::START_TAG
) {
478 String8
tag(tree
.getElementName(&len
));
479 //printf("Depth %d tag %s\n", depth, tag.string());
481 if (tag
!= "manifest") {
482 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
485 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
486 printf("package: %s\n", pkg
.string());
487 } else if (depth
== 2 && tag
== "permission") {
489 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
491 fprintf(stderr
, "ERROR: %s\n", error
.string());
494 printf("permission: %s\n", name
.string());
495 } else if (depth
== 2 && tag
== "uses-permission") {
497 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
499 fprintf(stderr
, "ERROR: %s\n", error
.string());
502 printf("uses-permission: %s\n", name
.string());
505 } else if (strcmp("badging", option
) == 0) {
507 ResXMLTree::event_code_t code
;
510 bool withinActivity
= false;
511 bool isMainActivity
= false;
512 bool isLauncherActivity
= false;
513 bool isSearchable
= false;
514 bool withinApplication
= false;
515 bool withinReceiver
= false;
516 bool withinService
= false;
517 bool withinIntentFilter
= false;
518 bool hasMainActivity
= false;
519 bool hasOtherActivities
= false;
520 bool hasOtherReceivers
= false;
521 bool hasOtherServices
= false;
522 bool hasWallpaperService
= false;
523 bool hasImeService
= false;
524 bool hasWidgetReceivers
= false;
525 bool hasIntentFilter
= false;
526 bool actMainActivity
= false;
527 bool actWidgetReceivers
= false;
528 bool actImeService
= false;
529 bool actWallpaperService
= false;
531 // This next group of variables is used to implement a group of
532 // backward-compatibility heuristics necessitated by the addition of
533 // some new uses-feature constants in 2.1 and 2.2. In most cases, the
534 // heuristic is "if an app requests a permission but doesn't explicitly
535 // request the corresponding <uses-feature>, presume it's there anyway".
536 bool specCameraFeature
= false; // camera-related
537 bool specCameraAutofocusFeature
= false;
538 bool reqCameraAutofocusFeature
= false;
539 bool reqCameraFlashFeature
= false;
540 bool hasCameraPermission
= false;
541 bool specLocationFeature
= false; // location-related
542 bool specNetworkLocFeature
= false;
543 bool reqNetworkLocFeature
= false;
544 bool specGpsFeature
= false;
545 bool reqGpsFeature
= false;
546 bool hasMockLocPermission
= false;
547 bool hasCoarseLocPermission
= false;
548 bool hasGpsPermission
= false;
549 bool hasGeneralLocPermission
= false;
550 bool specBluetoothFeature
= false; // Bluetooth API-related
551 bool hasBluetoothPermission
= false;
552 bool specMicrophoneFeature
= false; // microphone-related
553 bool hasRecordAudioPermission
= false;
554 bool specWiFiFeature
= false;
555 bool hasWiFiPermission
= false;
556 bool specTelephonyFeature
= false; // telephony-related
557 bool reqTelephonySubFeature
= false;
558 bool hasTelephonyPermission
= false;
559 bool specTouchscreenFeature
= false; // touchscreen-related
560 bool specMultitouchFeature
= false;
561 bool reqDistinctMultitouchFeature
= false;
562 // 2.2 also added some other features that apps can request, but that
563 // have no corresponding permission, so we cannot implement any
564 // back-compatibility heuristic for them. The below are thus unnecessary
565 // (but are retained here for documentary purposes.)
566 //bool specCompassFeature = false;
567 //bool specAccelerometerFeature = false;
568 //bool specProximityFeature = false;
569 //bool specAmbientLightFeature = false;
570 //bool specLiveWallpaperFeature = false;
574 int normalScreen
= 1;
576 int xlargeScreen
= 1;
578 String8 activityName
;
579 String8 activityLabel
;
580 String8 activityIcon
;
581 String8 receiverName
;
583 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
584 if (code
== ResXMLTree::END_TAG
) {
587 withinApplication
= false;
588 } else if (depth
< 3) {
589 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
590 const char *aName
= getComponentName(pkg
, activityName
);
592 printf("launchable activity name='%s'", aName
);
594 printf("label='%s' icon='%s'\n",
595 activityLabel
.string(),
596 activityIcon
.string());
598 if (!hasIntentFilter
) {
599 hasOtherActivities
|= withinActivity
;
600 hasOtherReceivers
|= withinReceiver
;
601 hasOtherServices
|= withinService
;
603 withinActivity
= false;
604 withinService
= false;
605 withinReceiver
= false;
606 hasIntentFilter
= false;
607 isMainActivity
= isLauncherActivity
= false;
608 } else if (depth
< 4) {
609 if (withinIntentFilter
) {
610 if (withinActivity
) {
611 hasMainActivity
|= actMainActivity
;
612 hasOtherActivities
|= !actMainActivity
;
613 } else if (withinReceiver
) {
614 hasWidgetReceivers
|= actWidgetReceivers
;
615 hasOtherReceivers
|= !actWidgetReceivers
;
616 } else if (withinService
) {
617 hasImeService
|= actImeService
;
618 hasWallpaperService
|= actWallpaperService
;
619 hasOtherServices
|= (!actImeService
&& !actWallpaperService
);
622 withinIntentFilter
= false;
626 if (code
!= ResXMLTree::START_TAG
) {
630 String8
tag(tree
.getElementName(&len
));
631 //printf("Depth %d, %s\n", depth, tag.string());
633 if (tag
!= "manifest") {
634 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
637 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
638 printf("package: name='%s' ", pkg
.string());
639 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
641 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
644 if (versionCode
> 0) {
645 printf("versionCode='%d' ", versionCode
);
647 printf("versionCode='' ");
649 String8 versionName
= getResolvedAttribute(&res
, tree
, VERSION_NAME_ATTR
, &error
);
651 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
654 printf("versionName='%s'\n", versionName
.string());
655 } else if (depth
== 2) {
656 withinApplication
= false;
657 if (tag
== "application") {
658 withinApplication
= true;
659 String8 label
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
661 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
664 printf("application: label='%s' ", label
.string());
665 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
667 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
670 printf("icon='%s'\n", icon
.string());
671 int32_t testOnly
= getIntegerAttribute(tree
, TEST_ONLY_ATTR
, &error
, 0);
673 fprintf(stderr
, "ERROR getting 'android:testOnly' attribute: %s\n", error
.string());
677 printf("testOnly='%d'\n", testOnly
);
679 } else if (tag
== "uses-sdk") {
680 int32_t code
= getIntegerAttribute(tree
, MIN_SDK_VERSION_ATTR
, &error
);
683 String8 name
= getResolvedAttribute(&res
, tree
, MIN_SDK_VERSION_ATTR
, &error
);
685 fprintf(stderr
, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
689 if (name
== "Donut") targetSdk
= 4;
690 printf("sdkVersion:'%s'\n", name
.string());
691 } else if (code
!= -1) {
693 printf("sdkVersion:'%d'\n", code
);
695 code
= getIntegerAttribute(tree
, MAX_SDK_VERSION_ATTR
, NULL
, -1);
697 printf("maxSdkVersion:'%d'\n", code
);
699 code
= getIntegerAttribute(tree
, TARGET_SDK_VERSION_ATTR
, &error
);
702 String8 name
= getResolvedAttribute(&res
, tree
, TARGET_SDK_VERSION_ATTR
, &error
);
704 fprintf(stderr
, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
708 if (name
== "Donut" && targetSdk
< 4) targetSdk
= 4;
709 printf("targetSdkVersion:'%s'\n", name
.string());
710 } else if (code
!= -1) {
711 if (targetSdk
< code
) {
714 printf("targetSdkVersion:'%d'\n", code
);
716 } else if (tag
== "uses-configuration") {
717 int32_t reqTouchScreen
= getIntegerAttribute(tree
,
718 REQ_TOUCH_SCREEN_ATTR
, NULL
, 0);
719 int32_t reqKeyboardType
= getIntegerAttribute(tree
,
720 REQ_KEYBOARD_TYPE_ATTR
, NULL
, 0);
721 int32_t reqHardKeyboard
= getIntegerAttribute(tree
,
722 REQ_HARD_KEYBOARD_ATTR
, NULL
, 0);
723 int32_t reqNavigation
= getIntegerAttribute(tree
,
724 REQ_NAVIGATION_ATTR
, NULL
, 0);
725 int32_t reqFiveWayNav
= getIntegerAttribute(tree
,
726 REQ_FIVE_WAY_NAV_ATTR
, NULL
, 0);
727 printf("uses-configuration:");
728 if (reqTouchScreen
!= 0) {
729 printf(" reqTouchScreen='%d'", reqTouchScreen
);
731 if (reqKeyboardType
!= 0) {
732 printf(" reqKeyboardType='%d'", reqKeyboardType
);
734 if (reqHardKeyboard
!= 0) {
735 printf(" reqHardKeyboard='%d'", reqHardKeyboard
);
737 if (reqNavigation
!= 0) {
738 printf(" reqNavigation='%d'", reqNavigation
);
740 if (reqFiveWayNav
!= 0) {
741 printf(" reqFiveWayNav='%d'", reqFiveWayNav
);
744 } else if (tag
== "supports-density") {
745 int32_t dens
= getIntegerAttribute(tree
, DENSITY_ATTR
, &error
);
747 fprintf(stderr
, "ERROR getting 'android:density' attribute: %s\n",
751 printf("supports-density:'%d'\n", dens
);
752 } else if (tag
== "supports-screens") {
753 smallScreen
= getIntegerAttribute(tree
,
754 SMALL_SCREEN_ATTR
, NULL
, 1);
755 normalScreen
= getIntegerAttribute(tree
,
756 NORMAL_SCREEN_ATTR
, NULL
, 1);
757 largeScreen
= getIntegerAttribute(tree
,
758 LARGE_SCREEN_ATTR
, NULL
, 1);
759 xlargeScreen
= getIntegerAttribute(tree
,
760 XLARGE_SCREEN_ATTR
, NULL
, 1);
761 } else if (tag
== "uses-feature") {
762 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
764 if (name
!= "" && error
== "") {
765 int req
= getIntegerAttribute(tree
,
766 REQUIRED_ATTR
, NULL
, 1);
768 if (name
== "android.hardware.camera") {
769 specCameraFeature
= true;
770 } else if (name
== "android.hardware.camera.autofocus") {
771 // these have no corresponding permission to check for,
772 // but should imply the foundational camera permission
773 reqCameraAutofocusFeature
= reqCameraAutofocusFeature
|| req
;
774 specCameraAutofocusFeature
= true;
775 } else if (req
&& (name
== "android.hardware.camera.flash")) {
776 // these have no corresponding permission to check for,
777 // but should imply the foundational camera permission
778 reqCameraFlashFeature
= true;
779 } else if (name
== "android.hardware.location") {
780 specLocationFeature
= true;
781 } else if (name
== "android.hardware.location.network") {
782 specNetworkLocFeature
= true;
783 reqNetworkLocFeature
= reqNetworkLocFeature
|| req
;
784 } else if (name
== "android.hardware.location.gps") {
785 specGpsFeature
= true;
786 reqGpsFeature
= reqGpsFeature
|| req
;
787 } else if (name
== "android.hardware.bluetooth") {
788 specBluetoothFeature
= true;
789 } else if (name
== "android.hardware.touchscreen") {
790 specTouchscreenFeature
= true;
791 } else if (name
== "android.hardware.touchscreen.multitouch") {
792 specMultitouchFeature
= true;
793 } else if (name
== "android.hardware.touchscreen.multitouch.distinct") {
794 reqDistinctMultitouchFeature
= reqDistinctMultitouchFeature
|| req
;
795 } else if (name
== "android.hardware.microphone") {
796 specMicrophoneFeature
= true;
797 } else if (name
== "android.hardware.wifi") {
798 specWiFiFeature
= true;
799 } else if (name
== "android.hardware.telephony") {
800 specTelephonyFeature
= true;
801 } else if (req
&& (name
== "android.hardware.telephony.gsm" ||
802 name
== "android.hardware.telephony.cdma")) {
803 // these have no corresponding permission to check for,
804 // but should imply the foundational telephony permission
805 reqTelephonySubFeature
= true;
807 printf("uses-feature%s:'%s'\n",
808 req
? "" : "-not-required", name
.string());
810 int vers
= getIntegerAttribute(tree
,
811 GL_ES_VERSION_ATTR
, &error
);
813 printf("uses-gl-es:'0x%x'\n", vers
);
816 } else if (tag
== "uses-permission") {
817 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
818 if (name
!= "" && error
== "") {
819 if (name
== "android.permission.CAMERA") {
820 hasCameraPermission
= true;
821 } else if (name
== "android.permission.ACCESS_FINE_LOCATION") {
822 hasGpsPermission
= true;
823 } else if (name
== "android.permission.ACCESS_MOCK_LOCATION") {
824 hasMockLocPermission
= true;
825 } else if (name
== "android.permission.ACCESS_COARSE_LOCATION") {
826 hasCoarseLocPermission
= true;
827 } else if (name
== "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
828 name
== "android.permission.INSTALL_LOCATION_PROVIDER") {
829 hasGeneralLocPermission
= true;
830 } else if (name
== "android.permission.BLUETOOTH" ||
831 name
== "android.permission.BLUETOOTH_ADMIN") {
832 hasBluetoothPermission
= true;
833 } else if (name
== "android.permission.RECORD_AUDIO") {
834 hasRecordAudioPermission
= true;
835 } else if (name
== "android.permission.ACCESS_WIFI_STATE" ||
836 name
== "android.permission.CHANGE_WIFI_STATE" ||
837 name
== "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
838 hasWiFiPermission
= true;
839 } else if (name
== "android.permission.CALL_PHONE" ||
840 name
== "android.permission.CALL_PRIVILEGED" ||
841 name
== "android.permission.MODIFY_PHONE_STATE" ||
842 name
== "android.permission.PROCESS_OUTGOING_CALLS" ||
843 name
== "android.permission.READ_SMS" ||
844 name
== "android.permission.RECEIVE_SMS" ||
845 name
== "android.permission.RECEIVE_MMS" ||
846 name
== "android.permission.RECEIVE_WAP_PUSH" ||
847 name
== "android.permission.SEND_SMS" ||
848 name
== "android.permission.WRITE_APN_SETTINGS" ||
849 name
== "android.permission.WRITE_SMS") {
850 hasTelephonyPermission
= true;
852 printf("uses-permission:'%s'\n", name
.string());
854 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
858 } else if (tag
== "original-package") {
859 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
860 if (name
!= "" && error
== "") {
861 printf("original-package:'%s'\n", name
.string());
863 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
868 } else if (depth
== 3 && withinApplication
) {
869 withinActivity
= false;
870 withinReceiver
= false;
871 withinService
= false;
872 hasIntentFilter
= false;
873 if(tag
== "activity") {
874 withinActivity
= true;
875 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
877 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
881 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
883 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
887 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
889 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
892 } else if (tag
== "uses-library") {
893 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
895 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
898 int req
= getIntegerAttribute(tree
,
899 REQUIRED_ATTR
, NULL
, 1);
900 printf("uses-library%s:'%s'\n",
901 req
? "" : "-not-required", libraryName
.string());
902 } else if (tag
== "receiver") {
903 withinReceiver
= true;
904 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
907 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
910 } else if (tag
== "service") {
911 withinService
= true;
912 serviceName
= getAttribute(tree
, NAME_ATTR
, &error
);
915 fprintf(stderr
, "ERROR getting 'android:name' attribute for service: %s\n", error
.string());
919 } else if ((depth
== 4) && (tag
== "intent-filter")) {
920 hasIntentFilter
= true;
921 withinIntentFilter
= true;
922 actMainActivity
= actWidgetReceivers
= actImeService
= actWallpaperService
= false;
923 } else if ((depth
== 5) && withinIntentFilter
){
925 if (tag
== "action") {
926 action
= getAttribute(tree
, NAME_ATTR
, &error
);
928 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
931 if (withinActivity
) {
932 if (action
== "android.intent.action.MAIN") {
933 isMainActivity
= true;
934 actMainActivity
= true;
936 } else if (withinReceiver
) {
937 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
938 actWidgetReceivers
= true;
940 } else if (withinService
) {
941 if (action
== "android.view.InputMethod") {
942 actImeService
= true;
943 } else if (action
== "android.service.wallpaper.WallpaperService") {
944 actWallpaperService
= true;
947 if (action
== "android.intent.action.SEARCH") {
952 if (tag
== "category") {
953 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
955 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
958 if (withinActivity
) {
959 if (category
== "android.intent.category.LAUNCHER") {
960 isLauncherActivity
= true;
967 /* The following blocks handle printing "inferred" uses-features, based
968 * on whether related features or permissions are used by the app.
969 * Note that the various spec*Feature variables denote whether the
970 * relevant tag was *present* in the AndroidManfest, not that it was
971 * present and set to true.
973 // Camera-related back-compatibility logic
974 if (!specCameraFeature
) {
975 if (reqCameraFlashFeature
|| reqCameraAutofocusFeature
) {
976 // if app requested a sub-feature (autofocus or flash) and didn't
977 // request the base camera feature, we infer that it meant to
978 printf("uses-feature:'android.hardware.camera'\n");
979 } else if (hasCameraPermission
) {
980 // if app wants to use camera but didn't request the feature, we infer
981 // that it meant to, and further that it wants autofocus
982 // (which was the 1.0 - 1.5 behavior)
983 printf("uses-feature:'android.hardware.camera'\n");
984 if (!specCameraAutofocusFeature
) {
985 printf("uses-feature:'android.hardware.camera.autofocus'\n");
990 // Location-related back-compatibility logic
991 if (!specLocationFeature
&&
992 (hasMockLocPermission
|| hasCoarseLocPermission
|| hasGpsPermission
||
993 hasGeneralLocPermission
|| reqNetworkLocFeature
|| reqGpsFeature
)) {
994 // if app either takes a location-related permission or requests one of the
995 // sub-features, we infer that it also meant to request the base location feature
996 printf("uses-feature:'android.hardware.location'\n");
998 if (!specGpsFeature
&& hasGpsPermission
) {
999 // if app takes GPS (FINE location) perm but does not request the GPS
1000 // feature, we infer that it meant to
1001 printf("uses-feature:'android.hardware.location.gps'\n");
1003 if (!specNetworkLocFeature
&& hasCoarseLocPermission
) {
1004 // if app takes Network location (COARSE location) perm but does not request the
1005 // network location feature, we infer that it meant to
1006 printf("uses-feature:'android.hardware.location.network'\n");
1009 // Bluetooth-related compatibility logic
1010 if (!specBluetoothFeature
&& hasBluetoothPermission
&& (targetSdk
> 4)) {
1011 // if app takes a Bluetooth permission but does not request the Bluetooth
1012 // feature, we infer that it meant to
1013 printf("uses-feature:'android.hardware.bluetooth'\n");
1016 // Microphone-related compatibility logic
1017 if (!specMicrophoneFeature
&& hasRecordAudioPermission
) {
1018 // if app takes the record-audio permission but does not request the microphone
1019 // feature, we infer that it meant to
1020 printf("uses-feature:'android.hardware.microphone'\n");
1023 // WiFi-related compatibility logic
1024 if (!specWiFiFeature
&& hasWiFiPermission
) {
1025 // if app takes one of the WiFi permissions but does not request the WiFi
1026 // feature, we infer that it meant to
1027 printf("uses-feature:'android.hardware.wifi'\n");
1030 // Telephony-related compatibility logic
1031 if (!specTelephonyFeature
&& (hasTelephonyPermission
|| reqTelephonySubFeature
)) {
1032 // if app takes one of the telephony permissions or requests a sub-feature but
1033 // does not request the base telephony feature, we infer that it meant to
1034 printf("uses-feature:'android.hardware.telephony'\n");
1037 // Touchscreen-related back-compatibility logic
1038 if (!specTouchscreenFeature
) { // not a typo!
1039 // all apps are presumed to require a touchscreen, unless they explicitly say
1040 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1041 // Note that specTouchscreenFeature is true if the tag is present, regardless
1042 // of whether its value is true or false, so this is safe
1043 printf("uses-feature:'android.hardware.touchscreen'\n");
1045 if (!specMultitouchFeature
&& reqDistinctMultitouchFeature
) {
1046 // if app takes one of the telephony permissions or requests a sub-feature but
1047 // does not request the base telephony feature, we infer that it meant to
1048 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1051 if (hasMainActivity
) {
1054 if (hasWidgetReceivers
) {
1055 printf("app-widget\n");
1057 if (hasImeService
) {
1060 if (hasWallpaperService
) {
1061 printf("wallpaper\n");
1063 if (hasOtherActivities
) {
1064 printf("other-activities\n");
1069 if (hasOtherReceivers
) {
1070 printf("other-receivers\n");
1072 if (hasOtherServices
) {
1073 printf("other-services\n");
1076 // Determine default values for any unspecified screen sizes,
1077 // based on the target SDK of the package. As of 4 (donut)
1078 // the screen size support was introduced, so all default to
1080 if (smallScreen
> 0) {
1081 smallScreen
= targetSdk
>= 4 ? -1 : 0;
1083 if (normalScreen
> 0) {
1086 if (largeScreen
> 0) {
1087 largeScreen
= targetSdk
>= 4 ? -1 : 0;
1089 if (xlargeScreen
> 0) {
1090 // Introduced in Honeycomb.
1091 xlargeScreen
= targetSdk
>= 10 ? -1 : 0;
1093 printf("supports-screens:");
1094 if (smallScreen
!= 0) printf(" 'small'");
1095 if (normalScreen
!= 0) printf(" 'normal'");
1096 if (largeScreen
!= 0) printf(" 'large'");
1097 if (xlargeScreen
!= 0) printf(" 'xlarge'");
1101 Vector
<String8
> locales
;
1102 res
.getLocales(&locales
);
1103 const size_t NL
= locales
.size();
1104 for (size_t i
=0; i
<NL
; i
++) {
1105 const char* localeStr
= locales
[i
].string();
1106 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
1107 localeStr
= "--_--";
1109 printf(" '%s'", localeStr
);
1113 Vector
<ResTable_config
> configs
;
1114 res
.getConfigurations(&configs
);
1115 SortedVector
<int> densities
;
1116 const size_t NC
= configs
.size();
1117 for (size_t i
=0; i
<NC
; i
++) {
1118 int dens
= configs
[i
].density
;
1119 if (dens
== 0) dens
= 160;
1120 densities
.add(dens
);
1123 printf("densities:");
1124 const size_t ND
= densities
.size();
1125 for (size_t i
=0; i
<ND
; i
++) {
1126 printf(" '%d'", densities
[i
]);
1130 AssetDir
* dir
= assets
.openNonAssetDir(assetsCookie
, "lib");
1132 if (dir
->getFileCount() > 0) {
1133 printf("native-code:");
1134 for (size_t i
=0; i
<dir
->getFileCount(); i
++) {
1135 printf(" '%s'", dir
->getFileName(i
).string());
1141 } else if (strcmp("configurations", option
) == 0) {
1142 Vector
<ResTable_config
> configs
;
1143 res
.getConfigurations(&configs
);
1144 const size_t N
= configs
.size();
1145 for (size_t i
=0; i
<N
; i
++) {
1146 printf("%s\n", configs
[i
].toString().string());
1149 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
1160 return (result
!= NO_ERROR
);
1165 * Handle the "add" command, which wants to add files to a new or
1166 * pre-existing archive.
1168 int doAdd(Bundle
* bundle
)
1170 ZipFile
* zip
= NULL
;
1171 status_t result
= UNKNOWN_ERROR
;
1172 const char* zipFileName
;
1174 if (bundle
->getUpdate()) {
1175 /* avoid confusion */
1176 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
1180 if (bundle
->getFileSpecCount() < 1) {
1181 fprintf(stderr
, "ERROR: must specify zip file name\n");
1184 zipFileName
= bundle
->getFileSpecEntry(0);
1186 if (bundle
->getFileSpecCount() < 2) {
1187 fprintf(stderr
, "NOTE: nothing to do\n");
1191 zip
= openReadWrite(zipFileName
, true);
1193 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
1197 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1198 const char* fileName
= bundle
->getFileSpecEntry(i
);
1200 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
1201 printf(" '%s'... (from gzip)\n", fileName
);
1202 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
1204 if (bundle
->getJunkPath()) {
1205 String8 storageName
= String8(fileName
).getPathLeaf();
1206 printf(" '%s' as '%s'...\n", fileName
, storageName
.string());
1207 result
= zip
->add(fileName
, storageName
.string(),
1208 bundle
->getCompressionMethod(), NULL
);
1210 printf(" '%s'...\n", fileName
);
1211 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
1214 if (result
!= NO_ERROR
) {
1215 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
1216 if (result
== NAME_NOT_FOUND
)
1217 fprintf(stderr
, ": file not found\n");
1218 else if (result
== ALREADY_EXISTS
)
1219 fprintf(stderr
, ": already exists in archive\n");
1221 fprintf(stderr
, "\n");
1230 return (result
!= NO_ERROR
);
1235 * Delete files from an existing archive.
1237 int doRemove(Bundle
* bundle
)
1239 ZipFile
* zip
= NULL
;
1240 status_t result
= UNKNOWN_ERROR
;
1241 const char* zipFileName
;
1243 if (bundle
->getFileSpecCount() < 1) {
1244 fprintf(stderr
, "ERROR: must specify zip file name\n");
1247 zipFileName
= bundle
->getFileSpecEntry(0);
1249 if (bundle
->getFileSpecCount() < 2) {
1250 fprintf(stderr
, "NOTE: nothing to do\n");
1254 zip
= openReadWrite(zipFileName
, false);
1256 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
1261 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1262 const char* fileName
= bundle
->getFileSpecEntry(i
);
1265 entry
= zip
->getEntryByName(fileName
);
1266 if (entry
== NULL
) {
1267 printf(" '%s' NOT FOUND\n", fileName
);
1271 result
= zip
->remove(entry
);
1273 if (result
!= NO_ERROR
) {
1274 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
1275 bundle
->getFileSpecEntry(i
), zipFileName
);
1280 /* update the archive */
1285 return (result
!= NO_ERROR
);
1290 * Package up an asset directory and associated application files.
1292 int doPackage(Bundle
* bundle
)
1294 const char* outputAPKFile
;
1297 sp
<AaptAssets
> assets
;
1300 // -c zz_ZZ means do pseudolocalization
1301 ResourceFilter filter
;
1302 err
= filter
.parse(bundle
->getConfigurations());
1303 if (err
!= NO_ERROR
) {
1306 if (filter
.containsPseudo()) {
1307 bundle
->setPseudolocalize(true);
1310 N
= bundle
->getFileSpecCount();
1311 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
1312 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
1313 fprintf(stderr
, "ERROR: no input files\n");
1317 outputAPKFile
= bundle
->getOutputAPKFile();
1319 // Make sure the filenames provided exist and are of the appropriate type.
1320 if (outputAPKFile
) {
1322 type
= getFileType(outputAPKFile
);
1323 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
1325 "ERROR: output file '%s' exists but is not regular file\n",
1332 assets
= new AaptAssets();
1333 err
= assets
->slurpFromArgs(bundle
);
1338 if (bundle
->getVerbose()) {
1342 // If they asked for any files that need to be compiled, do so.
1343 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
1344 err
= buildResources(bundle
, assets
);
1350 // At this point we've read everything and processed everything. From here
1351 // on out it's just writing output files.
1352 if (SourcePos::hasErrors()) {
1356 // Write out R.java constants
1357 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
1358 if (bundle
->getCustomPackage() == NULL
) {
1359 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
1361 const String8
customPkg(bundle
->getCustomPackage());
1362 err
= writeResourceSymbols(bundle
, assets
, customPkg
, true);
1368 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
1372 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
1378 // Write out the ProGuard file
1379 err
= writeProguardFile(bundle
, assets
);
1385 if (outputAPKFile
) {
1386 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
1387 if (err
!= NO_ERROR
) {
1388 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
1395 if (SourcePos::hasErrors()) {
1396 SourcePos::printErrors(stderr
);