2 // Copyright 2006 The Android Open Source Project
4 // Android Asset Packaging Tool main entry point.
8 #include "ResourceTable.h"
11 #include <utils/Log.h>
12 #include <utils/threads.h>
13 #include <utils/List.h>
14 #include <utils/Errors.h>
19 using namespace android
;
22 * Show version info. All the cool kids do it.
24 int doVersion(Bundle
* bundle
)
26 if (bundle
->getFileSpecCount() != 0)
27 printf("(ignoring extra arguments)\n");
28 printf("Android Asset Packaging Tool, v0.2\n");
35 * Open the file read only. The call fails if the file doesn't exist.
37 * Returns NULL on failure.
39 ZipFile
* openReadOnly(const char* fileName
)
45 result
= zip
->open(fileName
, ZipFile::kOpenReadOnly
);
46 if (result
!= NO_ERROR
) {
47 if (result
== NAME_NOT_FOUND
)
48 fprintf(stderr
, "ERROR: '%s' not found\n", fileName
);
49 else if (result
== PERMISSION_DENIED
)
50 fprintf(stderr
, "ERROR: '%s' access denied\n", fileName
);
52 fprintf(stderr
, "ERROR: failed opening '%s' as Zip file\n",
62 * Open the file read-write. The file will be created if it doesn't
63 * already exist and "okayToCreate" is set.
65 * Returns NULL on failure.
67 ZipFile
* openReadWrite(const char* fileName
, bool okayToCreate
)
73 flags
= ZipFile::kOpenReadWrite
;
75 flags
|= ZipFile::kOpenCreate
;
78 result
= zip
->open(fileName
, flags
);
79 if (result
!= NO_ERROR
) {
91 * Return a short string describing the compression method.
93 const char* compressionName(int method
)
95 if (method
== ZipEntry::kCompressStored
)
97 else if (method
== ZipEntry::kCompressDeflated
)
104 * Return the percent reduction in size (0% == no compression).
106 int calcPercent(long uncompressedLen
, long compressedLen
)
108 if (!uncompressedLen
)
111 return (int) (100.0 - (compressedLen
* 100.0) / uncompressedLen
+ 0.5);
115 * Handle the "list" command, which can be a simple file dump or
118 * The verbose listing closely matches the output of the Info-ZIP "unzip"
121 int doList(Bundle
* bundle
)
125 const ZipEntry
* entry
;
126 long totalUncLen
, totalCompLen
;
127 const char* zipFileName
;
129 if (bundle
->getFileSpecCount() != 1) {
130 fprintf(stderr
, "ERROR: specify zip file name (only)\n");
133 zipFileName
= bundle
->getFileSpecEntry(0);
135 zip
= openReadOnly(zipFileName
);
141 if (bundle
->getVerbose()) {
142 printf("Archive: %s\n", zipFileName
);
144 " Length Method Size Ratio Offset Date Time CRC-32 Name\n");
146 "-------- ------ ------- ----- ------- ---- ---- ------ ----\n");
149 totalUncLen
= totalCompLen
= 0;
151 count
= zip
->getNumEntries();
152 for (i
= 0; i
< count
; i
++) {
153 entry
= zip
->getEntryByIndex(i
);
154 if (bundle
->getVerbose()) {
158 when
= entry
->getModWhen();
159 strftime(dateBuf
, sizeof(dateBuf
), "%m-%d-%y %H:%M",
162 printf("%8ld %-7.7s %7ld %3d%% %8zd %s %08lx %s\n",
163 (long) entry
->getUncompressedLen(),
164 compressionName(entry
->getCompressionMethod()),
165 (long) entry
->getCompressedLen(),
166 calcPercent(entry
->getUncompressedLen(),
167 entry
->getCompressedLen()),
168 (size_t) entry
->getLFHOffset(),
171 entry
->getFileName());
173 printf("%s\n", entry
->getFileName());
176 totalUncLen
+= entry
->getUncompressedLen();
177 totalCompLen
+= entry
->getCompressedLen();
180 if (bundle
->getVerbose()) {
182 "-------- ------- --- -------\n");
183 printf("%8ld %7ld %2d%% %d files\n",
186 calcPercent(totalUncLen
, totalCompLen
),
187 zip
->getNumEntries());
190 if (bundle
->getAndroidList()) {
192 if (!assets
.addAssetPath(String8(zipFileName
), NULL
)) {
193 fprintf(stderr
, "ERROR: list -a failed because assets could not be loaded\n");
197 const ResTable
& res
= assets
.getResources(false);
199 printf("\nNo resource table found.\n");
201 #ifndef HAVE_ANDROID_OS
202 printf("\nResource table:\n");
207 Asset
* manifestAsset
= assets
.openNonAsset("AndroidManifest.xml",
208 Asset::ACCESS_BUFFER
);
209 if (manifestAsset
== NULL
) {
210 printf("\nNo AndroidManifest.xml found.\n");
212 printf("\nAndroid manifest:\n");
214 tree
.setTo(manifestAsset
->getBuffer(true),
215 manifestAsset
->getLength());
216 printXMLBlock(&tree
);
218 delete manifestAsset
;
228 static ssize_t
indexOfAttribute(const ResXMLTree
& tree
, uint32_t attrRes
)
230 size_t N
= tree
.getAttributeCount();
231 for (size_t i
=0; i
<N
; i
++) {
232 if (tree
.getAttributeNameResID(i
) == attrRes
) {
239 String8
getAttribute(const ResXMLTree
& tree
, const char* ns
,
240 const char* attr
, String8
* outError
)
242 ssize_t idx
= tree
.indexOfAttribute(ns
, attr
);
247 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
248 if (value
.dataType
!= Res_value::TYPE_STRING
) {
249 if (outError
!= NULL
) *outError
= "attribute is not a string value";
254 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
255 return str
? String8(str
, len
) : String8();
258 static String8
getAttribute(const ResXMLTree
& tree
, uint32_t attrRes
, String8
* outError
)
260 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
265 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
266 if (value
.dataType
!= Res_value::TYPE_STRING
) {
267 if (outError
!= NULL
) *outError
= "attribute is not a string value";
272 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
273 return str
? String8(str
, len
) : String8();
276 static int32_t getIntegerAttribute(const ResXMLTree
& tree
, uint32_t attrRes
,
277 String8
* outError
, int32_t defValue
= -1)
279 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
284 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
285 if (value
.dataType
< Res_value::TYPE_FIRST_INT
286 || value
.dataType
> Res_value::TYPE_LAST_INT
) {
287 if (outError
!= NULL
) *outError
= "attribute is not an integer value";
294 static String8
getResolvedAttribute(const ResTable
* resTable
, const ResXMLTree
& tree
,
295 uint32_t attrRes
, String8
* outError
)
297 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
302 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
303 if (value
.dataType
== Res_value::TYPE_STRING
) {
305 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
306 return str
? String8(str
, len
) : String8();
308 resTable
->resolveReference(&value
, 0);
309 if (value
.dataType
!= Res_value::TYPE_STRING
) {
310 if (outError
!= NULL
) *outError
= "attribute is not a string value";
315 const Res_value
* value2
= &value
;
316 const char16_t* str
= const_cast<ResTable
*>(resTable
)->valueToString(value2
, 0, NULL
, &len
);
317 return str
? String8(str
, len
) : String8();
320 // These are attribute resource constants for the platform, as found
323 NAME_ATTR
= 0x01010003,
324 VERSION_CODE_ATTR
= 0x0101021b,
325 VERSION_NAME_ATTR
= 0x0101021c,
326 LABEL_ATTR
= 0x01010001,
327 ICON_ATTR
= 0x01010002,
328 MIN_SDK_VERSION_ATTR
= 0x0101020c,
329 MAX_SDK_VERSION_ATTR
= 0x01010271,
330 REQ_TOUCH_SCREEN_ATTR
= 0x01010227,
331 REQ_KEYBOARD_TYPE_ATTR
= 0x01010228,
332 REQ_HARD_KEYBOARD_ATTR
= 0x01010229,
333 REQ_NAVIGATION_ATTR
= 0x0101022a,
334 REQ_FIVE_WAY_NAV_ATTR
= 0x01010232,
335 TARGET_SDK_VERSION_ATTR
= 0x01010270,
336 TEST_ONLY_ATTR
= 0x01010272,
337 DENSITY_ATTR
= 0x0101026c,
338 GL_ES_VERSION_ATTR
= 0x01010281,
339 SMALL_SCREEN_ATTR
= 0x01010284,
340 NORMAL_SCREEN_ATTR
= 0x01010285,
341 LARGE_SCREEN_ATTR
= 0x01010286,
342 XLARGE_SCREEN_ATTR
= 0x010102bf,
343 REQUIRED_ATTR
= 0x0101028e,
346 const char *getComponentName(String8
&pkgName
, String8
&componentName
) {
347 ssize_t idx
= componentName
.find(".");
348 String8
retStr(pkgName
);
350 retStr
+= componentName
;
351 } else if (idx
< 0) {
353 retStr
+= componentName
;
355 return componentName
.string();
357 return retStr
.string();
361 * Handle the "dump" command, to extract select data from an archive.
363 int doDump(Bundle
* bundle
)
365 status_t result
= UNKNOWN_ERROR
;
368 if (bundle
->getFileSpecCount() < 1) {
369 fprintf(stderr
, "ERROR: no dump option specified\n");
373 if (bundle
->getFileSpecCount() < 2) {
374 fprintf(stderr
, "ERROR: no dump file specified\n");
378 const char* option
= bundle
->getFileSpecEntry(0);
379 const char* filename
= bundle
->getFileSpecEntry(1);
383 if (!assets
.addAssetPath(String8(filename
), &assetsCookie
)) {
384 fprintf(stderr
, "ERROR: dump failed because assets could not be loaded\n");
388 const ResTable
& res
= assets
.getResources(false);
390 fprintf(stderr
, "ERROR: dump failed because no resource table was found\n");
394 if (strcmp("resources", option
) == 0) {
395 #ifndef HAVE_ANDROID_OS
396 res
.print(bundle
->getValues());
398 } else if (strcmp("xmltree", option
) == 0) {
399 if (bundle
->getFileSpecCount() < 3) {
400 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
404 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
405 const char* resname
= bundle
->getFileSpecEntry(i
);
407 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
409 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
413 if (tree
.setTo(asset
->getBuffer(true),
414 asset
->getLength()) != NO_ERROR
) {
415 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
419 printXMLBlock(&tree
);
425 } else if (strcmp("xmlstrings", option
) == 0) {
426 if (bundle
->getFileSpecCount() < 3) {
427 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
431 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
432 const char* resname
= bundle
->getFileSpecEntry(i
);
434 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
436 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
440 if (tree
.setTo(asset
->getBuffer(true),
441 asset
->getLength()) != NO_ERROR
) {
442 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
445 printStringPool(&tree
.getStrings());
452 asset
= assets
.openNonAsset("AndroidManifest.xml",
453 Asset::ACCESS_BUFFER
);
455 fprintf(stderr
, "ERROR: dump failed because no AndroidManifest.xml found\n");
459 if (tree
.setTo(asset
->getBuffer(true),
460 asset
->getLength()) != NO_ERROR
) {
461 fprintf(stderr
, "ERROR: AndroidManifest.xml is corrupt\n");
466 if (strcmp("permissions", option
) == 0) {
468 ResXMLTree::event_code_t code
;
470 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
471 if (code
== ResXMLTree::END_TAG
) {
475 if (code
!= ResXMLTree::START_TAG
) {
479 String8
tag(tree
.getElementName(&len
));
480 //printf("Depth %d tag %s\n", depth, tag.string());
482 if (tag
!= "manifest") {
483 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
486 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
487 printf("package: %s\n", pkg
.string());
488 } else if (depth
== 2 && tag
== "permission") {
490 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
492 fprintf(stderr
, "ERROR: %s\n", error
.string());
495 printf("permission: %s\n", name
.string());
496 } else if (depth
== 2 && tag
== "uses-permission") {
498 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
500 fprintf(stderr
, "ERROR: %s\n", error
.string());
503 printf("uses-permission: %s\n", name
.string());
506 } else if (strcmp("badging", option
) == 0) {
508 ResXMLTree::event_code_t code
;
511 bool withinActivity
= false;
512 bool isMainActivity
= false;
513 bool isLauncherActivity
= false;
514 bool isSearchable
= false;
515 bool withinApplication
= false;
516 bool withinReceiver
= false;
517 bool withinService
= false;
518 bool withinIntentFilter
= false;
519 bool hasMainActivity
= false;
520 bool hasOtherActivities
= false;
521 bool hasOtherReceivers
= false;
522 bool hasOtherServices
= false;
523 bool hasWallpaperService
= false;
524 bool hasImeService
= false;
525 bool hasWidgetReceivers
= false;
526 bool hasIntentFilter
= false;
527 bool actMainActivity
= false;
528 bool actWidgetReceivers
= false;
529 bool actImeService
= false;
530 bool actWallpaperService
= false;
532 // This next group of variables is used to implement a group of
533 // backward-compatibility heuristics necessitated by the addition of
534 // some new uses-feature constants in 2.1 and 2.2. In most cases, the
535 // heuristic is "if an app requests a permission but doesn't explicitly
536 // request the corresponding <uses-feature>, presume it's there anyway".
537 bool specCameraFeature
= false; // camera-related
538 bool specCameraAutofocusFeature
= false;
539 bool reqCameraAutofocusFeature
= false;
540 bool reqCameraFlashFeature
= false;
541 bool hasCameraPermission
= false;
542 bool specLocationFeature
= false; // location-related
543 bool specNetworkLocFeature
= false;
544 bool reqNetworkLocFeature
= false;
545 bool specGpsFeature
= false;
546 bool reqGpsFeature
= false;
547 bool hasMockLocPermission
= false;
548 bool hasCoarseLocPermission
= false;
549 bool hasGpsPermission
= false;
550 bool hasGeneralLocPermission
= false;
551 bool specBluetoothFeature
= false; // Bluetooth API-related
552 bool hasBluetoothPermission
= false;
553 bool specMicrophoneFeature
= false; // microphone-related
554 bool hasRecordAudioPermission
= false;
555 bool specWiFiFeature
= false;
556 bool hasWiFiPermission
= false;
557 bool specTelephonyFeature
= false; // telephony-related
558 bool reqTelephonySubFeature
= false;
559 bool hasTelephonyPermission
= false;
560 bool specTouchscreenFeature
= false; // touchscreen-related
561 bool specMultitouchFeature
= false;
562 bool reqDistinctMultitouchFeature
= false;
563 // 2.2 also added some other features that apps can request, but that
564 // have no corresponding permission, so we cannot implement any
565 // back-compatibility heuristic for them. The below are thus unnecessary
566 // (but are retained here for documentary purposes.)
567 //bool specCompassFeature = false;
568 //bool specAccelerometerFeature = false;
569 //bool specProximityFeature = false;
570 //bool specAmbientLightFeature = false;
571 //bool specLiveWallpaperFeature = false;
575 int normalScreen
= 1;
577 int xlargeScreen
= 1;
579 String8 activityName
;
580 String8 activityLabel
;
581 String8 activityIcon
;
582 String8 receiverName
;
584 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
585 if (code
== ResXMLTree::END_TAG
) {
588 withinApplication
= false;
589 } else if (depth
< 3) {
590 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
591 const char *aName
= getComponentName(pkg
, activityName
);
593 printf("launchable activity name='%s'", aName
);
595 printf("label='%s' icon='%s'\n",
596 activityLabel
.string(),
597 activityIcon
.string());
599 if (!hasIntentFilter
) {
600 hasOtherActivities
|= withinActivity
;
601 hasOtherReceivers
|= withinReceiver
;
602 hasOtherServices
|= withinService
;
604 withinActivity
= false;
605 withinService
= false;
606 withinReceiver
= false;
607 hasIntentFilter
= false;
608 isMainActivity
= isLauncherActivity
= false;
609 } else if (depth
< 4) {
610 if (withinIntentFilter
) {
611 if (withinActivity
) {
612 hasMainActivity
|= actMainActivity
;
613 hasOtherActivities
|= !actMainActivity
;
614 } else if (withinReceiver
) {
615 hasWidgetReceivers
|= actWidgetReceivers
;
616 hasOtherReceivers
|= !actWidgetReceivers
;
617 } else if (withinService
) {
618 hasImeService
|= actImeService
;
619 hasWallpaperService
|= actWallpaperService
;
620 hasOtherServices
|= (!actImeService
&& !actWallpaperService
);
623 withinIntentFilter
= false;
627 if (code
!= ResXMLTree::START_TAG
) {
631 String8
tag(tree
.getElementName(&len
));
632 //printf("Depth %d, %s\n", depth, tag.string());
634 if (tag
!= "manifest") {
635 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
638 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
639 printf("package: name='%s' ", pkg
.string());
640 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
642 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
645 if (versionCode
> 0) {
646 printf("versionCode='%d' ", versionCode
);
648 printf("versionCode='' ");
650 String8 versionName
= getResolvedAttribute(&res
, tree
, VERSION_NAME_ATTR
, &error
);
652 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
655 printf("versionName='%s'\n", versionName
.string());
656 } else if (depth
== 2) {
657 withinApplication
= false;
658 if (tag
== "application") {
659 withinApplication
= true;
660 String8 label
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
662 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
665 printf("application: label='%s' ", label
.string());
666 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
668 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
671 printf("icon='%s'\n", icon
.string());
672 int32_t testOnly
= getIntegerAttribute(tree
, TEST_ONLY_ATTR
, &error
, 0);
674 fprintf(stderr
, "ERROR getting 'android:testOnly' attribute: %s\n", error
.string());
678 printf("testOnly='%d'\n", testOnly
);
680 } else if (tag
== "uses-sdk") {
681 int32_t code
= getIntegerAttribute(tree
, MIN_SDK_VERSION_ATTR
, &error
);
684 String8 name
= getResolvedAttribute(&res
, tree
, MIN_SDK_VERSION_ATTR
, &error
);
686 fprintf(stderr
, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
690 if (name
== "Donut") targetSdk
= 4;
691 printf("sdkVersion:'%s'\n", name
.string());
692 } else if (code
!= -1) {
694 printf("sdkVersion:'%d'\n", code
);
696 code
= getIntegerAttribute(tree
, MAX_SDK_VERSION_ATTR
, NULL
, -1);
698 printf("maxSdkVersion:'%d'\n", code
);
700 code
= getIntegerAttribute(tree
, TARGET_SDK_VERSION_ATTR
, &error
);
703 String8 name
= getResolvedAttribute(&res
, tree
, TARGET_SDK_VERSION_ATTR
, &error
);
705 fprintf(stderr
, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
709 if (name
== "Donut" && targetSdk
< 4) targetSdk
= 4;
710 printf("targetSdkVersion:'%s'\n", name
.string());
711 } else if (code
!= -1) {
712 if (targetSdk
< code
) {
715 printf("targetSdkVersion:'%d'\n", code
);
717 } else if (tag
== "uses-configuration") {
718 int32_t reqTouchScreen
= getIntegerAttribute(tree
,
719 REQ_TOUCH_SCREEN_ATTR
, NULL
, 0);
720 int32_t reqKeyboardType
= getIntegerAttribute(tree
,
721 REQ_KEYBOARD_TYPE_ATTR
, NULL
, 0);
722 int32_t reqHardKeyboard
= getIntegerAttribute(tree
,
723 REQ_HARD_KEYBOARD_ATTR
, NULL
, 0);
724 int32_t reqNavigation
= getIntegerAttribute(tree
,
725 REQ_NAVIGATION_ATTR
, NULL
, 0);
726 int32_t reqFiveWayNav
= getIntegerAttribute(tree
,
727 REQ_FIVE_WAY_NAV_ATTR
, NULL
, 0);
728 printf("uses-configuration:");
729 if (reqTouchScreen
!= 0) {
730 printf(" reqTouchScreen='%d'", reqTouchScreen
);
732 if (reqKeyboardType
!= 0) {
733 printf(" reqKeyboardType='%d'", reqKeyboardType
);
735 if (reqHardKeyboard
!= 0) {
736 printf(" reqHardKeyboard='%d'", reqHardKeyboard
);
738 if (reqNavigation
!= 0) {
739 printf(" reqNavigation='%d'", reqNavigation
);
741 if (reqFiveWayNav
!= 0) {
742 printf(" reqFiveWayNav='%d'", reqFiveWayNav
);
745 } else if (tag
== "supports-density") {
746 int32_t dens
= getIntegerAttribute(tree
, DENSITY_ATTR
, &error
);
748 fprintf(stderr
, "ERROR getting 'android:density' attribute: %s\n",
752 printf("supports-density:'%d'\n", dens
);
753 } else if (tag
== "supports-screens") {
754 smallScreen
= getIntegerAttribute(tree
,
755 SMALL_SCREEN_ATTR
, NULL
, 1);
756 normalScreen
= getIntegerAttribute(tree
,
757 NORMAL_SCREEN_ATTR
, NULL
, 1);
758 largeScreen
= getIntegerAttribute(tree
,
759 LARGE_SCREEN_ATTR
, NULL
, 1);
760 xlargeScreen
= getIntegerAttribute(tree
,
761 XLARGE_SCREEN_ATTR
, NULL
, 1);
762 } else if (tag
== "uses-feature") {
763 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
765 if (name
!= "" && error
== "") {
766 int req
= getIntegerAttribute(tree
,
767 REQUIRED_ATTR
, NULL
, 1);
769 if (name
== "android.hardware.camera") {
770 specCameraFeature
= true;
771 } else if (name
== "android.hardware.camera.autofocus") {
772 // these have no corresponding permission to check for,
773 // but should imply the foundational camera permission
774 reqCameraAutofocusFeature
= reqCameraAutofocusFeature
|| req
;
775 specCameraAutofocusFeature
= true;
776 } else if (req
&& (name
== "android.hardware.camera.flash")) {
777 // these have no corresponding permission to check for,
778 // but should imply the foundational camera permission
779 reqCameraFlashFeature
= true;
780 } else if (name
== "android.hardware.location") {
781 specLocationFeature
= true;
782 } else if (name
== "android.hardware.location.network") {
783 specNetworkLocFeature
= true;
784 reqNetworkLocFeature
= reqNetworkLocFeature
|| req
;
785 } else if (name
== "android.hardware.location.gps") {
786 specGpsFeature
= true;
787 reqGpsFeature
= reqGpsFeature
|| req
;
788 } else if (name
== "android.hardware.bluetooth") {
789 specBluetoothFeature
= true;
790 } else if (name
== "android.hardware.touchscreen") {
791 specTouchscreenFeature
= true;
792 } else if (name
== "android.hardware.touchscreen.multitouch") {
793 specMultitouchFeature
= true;
794 } else if (name
== "android.hardware.touchscreen.multitouch.distinct") {
795 reqDistinctMultitouchFeature
= reqDistinctMultitouchFeature
|| req
;
796 } else if (name
== "android.hardware.microphone") {
797 specMicrophoneFeature
= true;
798 } else if (name
== "android.hardware.wifi") {
799 specWiFiFeature
= true;
800 } else if (name
== "android.hardware.telephony") {
801 specTelephonyFeature
= true;
802 } else if (req
&& (name
== "android.hardware.telephony.gsm" ||
803 name
== "android.hardware.telephony.cdma")) {
804 // these have no corresponding permission to check for,
805 // but should imply the foundational telephony permission
806 reqTelephonySubFeature
= true;
808 printf("uses-feature%s:'%s'\n",
809 req
? "" : "-not-required", name
.string());
811 int vers
= getIntegerAttribute(tree
,
812 GL_ES_VERSION_ATTR
, &error
);
814 printf("uses-gl-es:'0x%x'\n", vers
);
817 } else if (tag
== "uses-permission") {
818 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
819 if (name
!= "" && error
== "") {
820 if (name
== "android.permission.CAMERA") {
821 hasCameraPermission
= true;
822 } else if (name
== "android.permission.ACCESS_FINE_LOCATION") {
823 hasGpsPermission
= true;
824 } else if (name
== "android.permission.ACCESS_MOCK_LOCATION") {
825 hasMockLocPermission
= true;
826 } else if (name
== "android.permission.ACCESS_COARSE_LOCATION") {
827 hasCoarseLocPermission
= true;
828 } else if (name
== "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
829 name
== "android.permission.INSTALL_LOCATION_PROVIDER") {
830 hasGeneralLocPermission
= true;
831 } else if (name
== "android.permission.BLUETOOTH" ||
832 name
== "android.permission.BLUETOOTH_ADMIN") {
833 hasBluetoothPermission
= true;
834 } else if (name
== "android.permission.RECORD_AUDIO") {
835 hasRecordAudioPermission
= true;
836 } else if (name
== "android.permission.ACCESS_WIFI_STATE" ||
837 name
== "android.permission.CHANGE_WIFI_STATE" ||
838 name
== "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
839 hasWiFiPermission
= true;
840 } else if (name
== "android.permission.CALL_PHONE" ||
841 name
== "android.permission.CALL_PRIVILEGED" ||
842 name
== "android.permission.MODIFY_PHONE_STATE" ||
843 name
== "android.permission.PROCESS_OUTGOING_CALLS" ||
844 name
== "android.permission.READ_SMS" ||
845 name
== "android.permission.RECEIVE_SMS" ||
846 name
== "android.permission.RECEIVE_MMS" ||
847 name
== "android.permission.RECEIVE_WAP_PUSH" ||
848 name
== "android.permission.SEND_SMS" ||
849 name
== "android.permission.WRITE_APN_SETTINGS" ||
850 name
== "android.permission.WRITE_SMS") {
851 hasTelephonyPermission
= true;
853 printf("uses-permission:'%s'\n", name
.string());
855 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
859 } else if (tag
== "uses-package") {
860 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
861 if (name
!= "" && error
== "") {
862 printf("uses-package:'%s'\n", name
.string());
864 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
868 } else if (tag
== "original-package") {
869 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
870 if (name
!= "" && error
== "") {
871 printf("original-package:'%s'\n", name
.string());
873 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
877 } else if (tag
== "uses-gl-texture") {
878 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
879 if (name
!= "" && error
== "") {
880 printf("uses-gl-texture:'%s'\n", name
.string());
882 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
887 } else if (depth
== 3 && withinApplication
) {
888 withinActivity
= false;
889 withinReceiver
= false;
890 withinService
= false;
891 hasIntentFilter
= false;
892 if(tag
== "activity") {
893 withinActivity
= true;
894 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
896 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
900 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
902 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
906 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
908 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
911 } else if (tag
== "uses-library") {
912 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
914 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
917 int req
= getIntegerAttribute(tree
,
918 REQUIRED_ATTR
, NULL
, 1);
919 printf("uses-library%s:'%s'\n",
920 req
? "" : "-not-required", libraryName
.string());
921 } else if (tag
== "receiver") {
922 withinReceiver
= true;
923 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
926 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
929 } else if (tag
== "service") {
930 withinService
= true;
931 serviceName
= getAttribute(tree
, NAME_ATTR
, &error
);
934 fprintf(stderr
, "ERROR getting 'android:name' attribute for service: %s\n", error
.string());
938 } else if ((depth
== 4) && (tag
== "intent-filter")) {
939 hasIntentFilter
= true;
940 withinIntentFilter
= true;
941 actMainActivity
= actWidgetReceivers
= actImeService
= actWallpaperService
= false;
942 } else if ((depth
== 5) && withinIntentFilter
){
944 if (tag
== "action") {
945 action
= getAttribute(tree
, NAME_ATTR
, &error
);
947 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
950 if (withinActivity
) {
951 if (action
== "android.intent.action.MAIN") {
952 isMainActivity
= true;
953 actMainActivity
= true;
955 } else if (withinReceiver
) {
956 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
957 actWidgetReceivers
= true;
959 } else if (withinService
) {
960 if (action
== "android.view.InputMethod") {
961 actImeService
= true;
962 } else if (action
== "android.service.wallpaper.WallpaperService") {
963 actWallpaperService
= true;
966 if (action
== "android.intent.action.SEARCH") {
971 if (tag
== "category") {
972 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
974 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
977 if (withinActivity
) {
978 if (category
== "android.intent.category.LAUNCHER") {
979 isLauncherActivity
= true;
986 /* The following blocks handle printing "inferred" uses-features, based
987 * on whether related features or permissions are used by the app.
988 * Note that the various spec*Feature variables denote whether the
989 * relevant tag was *present* in the AndroidManfest, not that it was
990 * present and set to true.
992 // Camera-related back-compatibility logic
993 if (!specCameraFeature
) {
994 if (reqCameraFlashFeature
|| reqCameraAutofocusFeature
) {
995 // if app requested a sub-feature (autofocus or flash) and didn't
996 // request the base camera feature, we infer that it meant to
997 printf("uses-feature:'android.hardware.camera'\n");
998 } else if (hasCameraPermission
) {
999 // if app wants to use camera but didn't request the feature, we infer
1000 // that it meant to, and further that it wants autofocus
1001 // (which was the 1.0 - 1.5 behavior)
1002 printf("uses-feature:'android.hardware.camera'\n");
1003 if (!specCameraAutofocusFeature
) {
1004 printf("uses-feature:'android.hardware.camera.autofocus'\n");
1009 // Location-related back-compatibility logic
1010 if (!specLocationFeature
&&
1011 (hasMockLocPermission
|| hasCoarseLocPermission
|| hasGpsPermission
||
1012 hasGeneralLocPermission
|| reqNetworkLocFeature
|| reqGpsFeature
)) {
1013 // if app either takes a location-related permission or requests one of the
1014 // sub-features, we infer that it also meant to request the base location feature
1015 printf("uses-feature:'android.hardware.location'\n");
1017 if (!specGpsFeature
&& hasGpsPermission
) {
1018 // if app takes GPS (FINE location) perm but does not request the GPS
1019 // feature, we infer that it meant to
1020 printf("uses-feature:'android.hardware.location.gps'\n");
1022 if (!specNetworkLocFeature
&& hasCoarseLocPermission
) {
1023 // if app takes Network location (COARSE location) perm but does not request the
1024 // network location feature, we infer that it meant to
1025 printf("uses-feature:'android.hardware.location.network'\n");
1028 // Bluetooth-related compatibility logic
1029 if (!specBluetoothFeature
&& hasBluetoothPermission
&& (targetSdk
> 4)) {
1030 // if app takes a Bluetooth permission but does not request the Bluetooth
1031 // feature, we infer that it meant to
1032 printf("uses-feature:'android.hardware.bluetooth'\n");
1035 // Microphone-related compatibility logic
1036 if (!specMicrophoneFeature
&& hasRecordAudioPermission
) {
1037 // if app takes the record-audio permission but does not request the microphone
1038 // feature, we infer that it meant to
1039 printf("uses-feature:'android.hardware.microphone'\n");
1042 // WiFi-related compatibility logic
1043 if (!specWiFiFeature
&& hasWiFiPermission
) {
1044 // if app takes one of the WiFi permissions but does not request the WiFi
1045 // feature, we infer that it meant to
1046 printf("uses-feature:'android.hardware.wifi'\n");
1049 // Telephony-related compatibility logic
1050 if (!specTelephonyFeature
&& (hasTelephonyPermission
|| reqTelephonySubFeature
)) {
1051 // if app takes one of the telephony permissions or requests a sub-feature but
1052 // does not request the base telephony feature, we infer that it meant to
1053 printf("uses-feature:'android.hardware.telephony'\n");
1056 // Touchscreen-related back-compatibility logic
1057 if (!specTouchscreenFeature
) { // not a typo!
1058 // all apps are presumed to require a touchscreen, unless they explicitly say
1059 // <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>
1060 // Note that specTouchscreenFeature is true if the tag is present, regardless
1061 // of whether its value is true or false, so this is safe
1062 printf("uses-feature:'android.hardware.touchscreen'\n");
1064 if (!specMultitouchFeature
&& reqDistinctMultitouchFeature
) {
1065 // if app takes one of the telephony permissions or requests a sub-feature but
1066 // does not request the base telephony feature, we infer that it meant to
1067 printf("uses-feature:'android.hardware.touchscreen.multitouch'\n");
1070 if (hasMainActivity
) {
1073 if (hasWidgetReceivers
) {
1074 printf("app-widget\n");
1076 if (hasImeService
) {
1079 if (hasWallpaperService
) {
1080 printf("wallpaper\n");
1082 if (hasOtherActivities
) {
1083 printf("other-activities\n");
1088 if (hasOtherReceivers
) {
1089 printf("other-receivers\n");
1091 if (hasOtherServices
) {
1092 printf("other-services\n");
1095 // Determine default values for any unspecified screen sizes,
1096 // based on the target SDK of the package. As of 4 (donut)
1097 // the screen size support was introduced, so all default to
1099 if (smallScreen
> 0) {
1100 smallScreen
= targetSdk
>= 4 ? -1 : 0;
1102 if (normalScreen
> 0) {
1105 if (largeScreen
> 0) {
1106 largeScreen
= targetSdk
>= 4 ? -1 : 0;
1108 if (xlargeScreen
> 0) {
1109 // Introduced in Honeycomb.
1110 xlargeScreen
= targetSdk
>= 10 ? -1 : 0;
1112 printf("supports-screens:");
1113 if (smallScreen
!= 0) printf(" 'small'");
1114 if (normalScreen
!= 0) printf(" 'normal'");
1115 if (largeScreen
!= 0) printf(" 'large'");
1116 if (xlargeScreen
!= 0) printf(" 'xlarge'");
1120 Vector
<String8
> locales
;
1121 res
.getLocales(&locales
);
1122 const size_t NL
= locales
.size();
1123 for (size_t i
=0; i
<NL
; i
++) {
1124 const char* localeStr
= locales
[i
].string();
1125 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
1126 localeStr
= "--_--";
1128 printf(" '%s'", localeStr
);
1132 Vector
<ResTable_config
> configs
;
1133 res
.getConfigurations(&configs
);
1134 SortedVector
<int> densities
;
1135 const size_t NC
= configs
.size();
1136 for (size_t i
=0; i
<NC
; i
++) {
1137 int dens
= configs
[i
].density
;
1138 if (dens
== 0) dens
= 160;
1139 densities
.add(dens
);
1142 printf("densities:");
1143 const size_t ND
= densities
.size();
1144 for (size_t i
=0; i
<ND
; i
++) {
1145 printf(" '%d'", densities
[i
]);
1149 AssetDir
* dir
= assets
.openNonAssetDir(assetsCookie
, "lib");
1151 if (dir
->getFileCount() > 0) {
1152 printf("native-code:");
1153 for (size_t i
=0; i
<dir
->getFileCount(); i
++) {
1154 printf(" '%s'", dir
->getFileName(i
).string());
1160 } else if (strcmp("configurations", option
) == 0) {
1161 Vector
<ResTable_config
> configs
;
1162 res
.getConfigurations(&configs
);
1163 const size_t N
= configs
.size();
1164 for (size_t i
=0; i
<N
; i
++) {
1165 printf("%s\n", configs
[i
].toString().string());
1168 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
1179 return (result
!= NO_ERROR
);
1184 * Handle the "add" command, which wants to add files to a new or
1185 * pre-existing archive.
1187 int doAdd(Bundle
* bundle
)
1189 ZipFile
* zip
= NULL
;
1190 status_t result
= UNKNOWN_ERROR
;
1191 const char* zipFileName
;
1193 if (bundle
->getUpdate()) {
1194 /* avoid confusion */
1195 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
1199 if (bundle
->getFileSpecCount() < 1) {
1200 fprintf(stderr
, "ERROR: must specify zip file name\n");
1203 zipFileName
= bundle
->getFileSpecEntry(0);
1205 if (bundle
->getFileSpecCount() < 2) {
1206 fprintf(stderr
, "NOTE: nothing to do\n");
1210 zip
= openReadWrite(zipFileName
, true);
1212 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
1216 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1217 const char* fileName
= bundle
->getFileSpecEntry(i
);
1219 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
1220 printf(" '%s'... (from gzip)\n", fileName
);
1221 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
1223 if (bundle
->getJunkPath()) {
1224 String8 storageName
= String8(fileName
).getPathLeaf();
1225 printf(" '%s' as '%s'...\n", fileName
, storageName
.string());
1226 result
= zip
->add(fileName
, storageName
.string(),
1227 bundle
->getCompressionMethod(), NULL
);
1229 printf(" '%s'...\n", fileName
);
1230 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
1233 if (result
!= NO_ERROR
) {
1234 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
1235 if (result
== NAME_NOT_FOUND
)
1236 fprintf(stderr
, ": file not found\n");
1237 else if (result
== ALREADY_EXISTS
)
1238 fprintf(stderr
, ": already exists in archive\n");
1240 fprintf(stderr
, "\n");
1249 return (result
!= NO_ERROR
);
1254 * Delete files from an existing archive.
1256 int doRemove(Bundle
* bundle
)
1258 ZipFile
* zip
= NULL
;
1259 status_t result
= UNKNOWN_ERROR
;
1260 const char* zipFileName
;
1262 if (bundle
->getFileSpecCount() < 1) {
1263 fprintf(stderr
, "ERROR: must specify zip file name\n");
1266 zipFileName
= bundle
->getFileSpecEntry(0);
1268 if (bundle
->getFileSpecCount() < 2) {
1269 fprintf(stderr
, "NOTE: nothing to do\n");
1273 zip
= openReadWrite(zipFileName
, false);
1275 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
1280 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1281 const char* fileName
= bundle
->getFileSpecEntry(i
);
1284 entry
= zip
->getEntryByName(fileName
);
1285 if (entry
== NULL
) {
1286 printf(" '%s' NOT FOUND\n", fileName
);
1290 result
= zip
->remove(entry
);
1292 if (result
!= NO_ERROR
) {
1293 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
1294 bundle
->getFileSpecEntry(i
), zipFileName
);
1299 /* update the archive */
1304 return (result
!= NO_ERROR
);
1309 * Package up an asset directory and associated application files.
1311 int doPackage(Bundle
* bundle
)
1313 const char* outputAPKFile
;
1316 sp
<AaptAssets
> assets
;
1319 // -c zz_ZZ means do pseudolocalization
1320 ResourceFilter filter
;
1321 err
= filter
.parse(bundle
->getConfigurations());
1322 if (err
!= NO_ERROR
) {
1325 if (filter
.containsPseudo()) {
1326 bundle
->setPseudolocalize(true);
1329 N
= bundle
->getFileSpecCount();
1330 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
1331 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
1332 fprintf(stderr
, "ERROR: no input files\n");
1336 outputAPKFile
= bundle
->getOutputAPKFile();
1338 // Make sure the filenames provided exist and are of the appropriate type.
1339 if (outputAPKFile
) {
1341 type
= getFileType(outputAPKFile
);
1342 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
1344 "ERROR: output file '%s' exists but is not regular file\n",
1351 assets
= new AaptAssets();
1352 err
= assets
->slurpFromArgs(bundle
);
1357 if (bundle
->getVerbose()) {
1361 // If they asked for any files that need to be compiled, do so.
1362 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
1363 err
= buildResources(bundle
, assets
);
1369 // At this point we've read everything and processed everything. From here
1370 // on out it's just writing output files.
1371 if (SourcePos::hasErrors()) {
1375 // Write out R.java constants
1376 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
1377 if (bundle
->getCustomPackage() == NULL
) {
1378 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
1380 const String8
customPkg(bundle
->getCustomPackage());
1381 err
= writeResourceSymbols(bundle
, assets
, customPkg
, true);
1387 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
1391 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
1397 // Write out the ProGuard file
1398 err
= writeProguardFile(bundle
, assets
);
1404 if (outputAPKFile
) {
1405 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
1406 if (err
!= NO_ERROR
) {
1407 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
1414 if (SourcePos::hasErrors()) {
1415 SourcePos::printErrors(stderr
);