]>
git.saurik.com Git - android/aapt.git/blob - Command.cpp
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 printf("\nResource table:\n");
204 Asset
* manifestAsset
= assets
.openNonAsset("AndroidManifest.xml",
205 Asset::ACCESS_BUFFER
);
206 if (manifestAsset
== NULL
) {
207 printf("\nNo AndroidManifest.xml found.\n");
209 printf("\nAndroid manifest:\n");
211 tree
.setTo(manifestAsset
->getBuffer(true),
212 manifestAsset
->getLength());
213 printXMLBlock(&tree
);
215 delete manifestAsset
;
225 static ssize_t
indexOfAttribute(const ResXMLTree
& tree
, uint32_t attrRes
)
227 size_t N
= tree
.getAttributeCount();
228 for (size_t i
=0; i
<N
; i
++) {
229 if (tree
.getAttributeNameResID(i
) == attrRes
) {
236 String8
getAttribute(const ResXMLTree
& tree
, const char* ns
,
237 const char* attr
, String8
* outError
)
239 ssize_t idx
= tree
.indexOfAttribute(ns
, attr
);
244 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
245 if (value
.dataType
!= Res_value::TYPE_STRING
) {
246 if (outError
!= NULL
) *outError
= "attribute is not a string value";
251 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
252 return str
? String8(str
, len
) : String8();
255 static String8
getAttribute(const ResXMLTree
& tree
, uint32_t attrRes
, String8
* outError
)
257 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
262 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
263 if (value
.dataType
!= Res_value::TYPE_STRING
) {
264 if (outError
!= NULL
) *outError
= "attribute is not a string value";
269 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
270 return str
? String8(str
, len
) : String8();
273 static int32_t getIntegerAttribute(const ResXMLTree
& tree
, uint32_t attrRes
,
274 String8
* outError
, int32_t defValue
= -1)
276 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
281 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
282 if (value
.dataType
< Res_value::TYPE_FIRST_INT
283 || value
.dataType
> Res_value::TYPE_LAST_INT
) {
284 if (outError
!= NULL
) *outError
= "attribute is not an integer value";
291 static String8
getResolvedAttribute(const ResTable
* resTable
, const ResXMLTree
& tree
,
292 uint32_t attrRes
, String8
* outError
)
294 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
299 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
300 if (value
.dataType
== Res_value::TYPE_STRING
) {
302 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
303 return str
? String8(str
, len
) : String8();
305 resTable
->resolveReference(&value
, 0);
306 if (value
.dataType
!= Res_value::TYPE_STRING
) {
307 if (outError
!= NULL
) *outError
= "attribute is not a string value";
312 const Res_value
* value2
= &value
;
313 const char16_t* str
= const_cast<ResTable
*>(resTable
)->valueToString(value2
, 0, NULL
, &len
);
314 return str
? String8(str
, len
) : String8();
317 // These are attribute resource constants for the platform, as found
320 NAME_ATTR
= 0x01010003,
321 VERSION_CODE_ATTR
= 0x0101021b,
322 VERSION_NAME_ATTR
= 0x0101021c,
323 LABEL_ATTR
= 0x01010001,
324 ICON_ATTR
= 0x01010002,
325 MIN_SDK_VERSION_ATTR
= 0x0101020c,
326 MAX_SDK_VERSION_ATTR
= 0x01010271,
327 REQ_TOUCH_SCREEN_ATTR
= 0x01010227,
328 REQ_KEYBOARD_TYPE_ATTR
= 0x01010228,
329 REQ_HARD_KEYBOARD_ATTR
= 0x01010229,
330 REQ_NAVIGATION_ATTR
= 0x0101022a,
331 REQ_FIVE_WAY_NAV_ATTR
= 0x01010232,
332 TARGET_SDK_VERSION_ATTR
= 0x01010270,
333 TEST_ONLY_ATTR
= 0x01010272,
334 DENSITY_ATTR
= 0x0101026c,
335 GL_ES_VERSION_ATTR
= 0x01010281,
336 SMALL_SCREEN_ATTR
= 0x01010284,
337 NORMAL_SCREEN_ATTR
= 0x01010285,
338 LARGE_SCREEN_ATTR
= 0x01010286,
339 REQUIRED_ATTR
= 0x0101028e,
342 const char *getComponentName(String8
&pkgName
, String8
&componentName
) {
343 ssize_t idx
= componentName
.find(".");
344 String8
retStr(pkgName
);
346 retStr
+= componentName
;
347 } else if (idx
< 0) {
349 retStr
+= componentName
;
351 return componentName
.string();
353 return retStr
.string();
357 * Handle the "dump" command, to extract select data from an archive.
359 int doDump(Bundle
* bundle
)
361 status_t result
= UNKNOWN_ERROR
;
364 if (bundle
->getFileSpecCount() < 1) {
365 fprintf(stderr
, "ERROR: no dump option specified\n");
369 if (bundle
->getFileSpecCount() < 2) {
370 fprintf(stderr
, "ERROR: no dump file specified\n");
374 const char* option
= bundle
->getFileSpecEntry(0);
375 const char* filename
= bundle
->getFileSpecEntry(1);
379 if (!assets
.addAssetPath(String8(filename
), &assetsCookie
)) {
380 fprintf(stderr
, "ERROR: dump failed because assets could not be loaded\n");
384 const ResTable
& res
= assets
.getResources(false);
386 fprintf(stderr
, "ERROR: dump failed because no resource table was found\n");
390 if (strcmp("resources", option
) == 0) {
391 res
.print(bundle
->getValues());
393 } else if (strcmp("xmltree", option
) == 0) {
394 if (bundle
->getFileSpecCount() < 3) {
395 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
399 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
400 const char* resname
= bundle
->getFileSpecEntry(i
);
402 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
404 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
408 if (tree
.setTo(asset
->getBuffer(true),
409 asset
->getLength()) != NO_ERROR
) {
410 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
414 printXMLBlock(&tree
);
420 } else if (strcmp("xmlstrings", option
) == 0) {
421 if (bundle
->getFileSpecCount() < 3) {
422 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
426 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
427 const char* resname
= bundle
->getFileSpecEntry(i
);
429 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
431 fprintf(stderr
, "ERROR: dump failed because resource %s found\n", resname
);
435 if (tree
.setTo(asset
->getBuffer(true),
436 asset
->getLength()) != NO_ERROR
) {
437 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
440 printStringPool(&tree
.getStrings());
447 asset
= assets
.openNonAsset("AndroidManifest.xml",
448 Asset::ACCESS_BUFFER
);
450 fprintf(stderr
, "ERROR: dump failed because no AndroidManifest.xml found\n");
454 if (tree
.setTo(asset
->getBuffer(true),
455 asset
->getLength()) != NO_ERROR
) {
456 fprintf(stderr
, "ERROR: AndroidManifest.xml is corrupt\n");
461 if (strcmp("permissions", option
) == 0) {
463 ResXMLTree::event_code_t code
;
465 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
466 if (code
== ResXMLTree::END_TAG
) {
470 if (code
!= ResXMLTree::START_TAG
) {
474 String8
tag(tree
.getElementName(&len
));
475 //printf("Depth %d tag %s\n", depth, tag.string());
477 if (tag
!= "manifest") {
478 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
481 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
482 printf("package: %s\n", pkg
.string());
483 } else if (depth
== 2 && tag
== "permission") {
485 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
487 fprintf(stderr
, "ERROR: %s\n", error
.string());
490 printf("permission: %s\n", name
.string());
491 } else if (depth
== 2 && tag
== "uses-permission") {
493 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
495 fprintf(stderr
, "ERROR: %s\n", error
.string());
498 printf("uses-permission: %s\n", name
.string());
501 } else if (strcmp("badging", option
) == 0) {
503 ResXMLTree::event_code_t code
;
506 bool withinActivity
= false;
507 bool isMainActivity
= false;
508 bool isLauncherActivity
= false;
509 bool isSearchable
= false;
510 bool withinApplication
= false;
511 bool withinReceiver
= false;
512 bool withinService
= false;
513 bool withinIntentFilter
= false;
514 bool hasMainActivity
= false;
515 bool hasOtherActivities
= false;
516 bool hasOtherReceivers
= false;
517 bool hasOtherServices
= false;
518 bool hasWallpaperService
= false;
519 bool hasImeService
= false;
520 bool hasWidgetReceivers
= false;
521 bool hasIntentFilter
= false;
522 bool actMainActivity
= false;
523 bool actWidgetReceivers
= false;
524 bool actImeService
= false;
525 bool actWallpaperService
= false;
526 bool specCameraFeature
= false;
527 bool hasCameraPermission
= false;
530 int normalScreen
= 1;
533 String8 activityName
;
534 String8 activityLabel
;
535 String8 activityIcon
;
536 String8 receiverName
;
538 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
539 if (code
== ResXMLTree::END_TAG
) {
542 withinApplication
= false;
543 } else if (depth
< 3) {
544 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
545 const char *aName
= getComponentName(pkg
, activityName
);
547 printf("launchable activity name='%s'", aName
);
549 printf("label='%s' icon='%s'\n",
550 activityLabel
.string(),
551 activityIcon
.string());
553 if (!hasIntentFilter
) {
554 hasOtherActivities
|= withinActivity
;
555 hasOtherReceivers
|= withinReceiver
;
556 hasOtherServices
|= withinService
;
558 withinActivity
= false;
559 withinService
= false;
560 withinReceiver
= false;
561 hasIntentFilter
= false;
562 isMainActivity
= isLauncherActivity
= false;
563 } else if (depth
< 4) {
564 if (withinIntentFilter
) {
565 if (withinActivity
) {
566 hasMainActivity
|= actMainActivity
;
567 hasOtherActivities
|= !actMainActivity
;
568 } else if (withinReceiver
) {
569 hasWidgetReceivers
|= actWidgetReceivers
;
570 hasOtherReceivers
|= !actWidgetReceivers
;
571 } else if (withinService
) {
572 hasImeService
|= actImeService
;
573 hasWallpaperService
|= actWallpaperService
;
574 hasOtherServices
|= (!actImeService
&& !actWallpaperService
);
577 withinIntentFilter
= false;
581 if (code
!= ResXMLTree::START_TAG
) {
585 String8
tag(tree
.getElementName(&len
));
586 //printf("Depth %d, %s\n", depth, tag.string());
588 if (tag
!= "manifest") {
589 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
592 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
593 printf("package: name='%s' ", pkg
.string());
594 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
596 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
599 if (versionCode
> 0) {
600 printf("versionCode='%d' ", versionCode
);
602 printf("versionCode='' ");
604 String8 versionName
= getAttribute(tree
, VERSION_NAME_ATTR
, &error
);
606 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
609 printf("versionName='%s'\n", versionName
.string());
610 } else if (depth
== 2) {
611 withinApplication
= false;
612 if (tag
== "application") {
613 withinApplication
= true;
614 String8 label
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
616 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
619 printf("application: label='%s' ", label
.string());
620 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
622 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
625 printf("icon='%s'\n", icon
.string());
626 int32_t testOnly
= getIntegerAttribute(tree
, TEST_ONLY_ATTR
, &error
, 0);
628 fprintf(stderr
, "ERROR getting 'android:testOnly' attribute: %s\n", error
.string());
632 printf("testOnly='%d'\n", testOnly
);
634 } else if (tag
== "uses-sdk") {
635 int32_t code
= getIntegerAttribute(tree
, MIN_SDK_VERSION_ATTR
, &error
);
638 String8 name
= getResolvedAttribute(&res
, tree
, MIN_SDK_VERSION_ATTR
, &error
);
640 fprintf(stderr
, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
644 if (name
== "Donut") targetSdk
= 4;
645 printf("sdkVersion:'%s'\n", name
.string());
646 } else if (code
!= -1) {
648 printf("sdkVersion:'%d'\n", code
);
650 code
= getIntegerAttribute(tree
, MAX_SDK_VERSION_ATTR
, NULL
, -1);
652 printf("maxSdkVersion:'%d'\n", code
);
654 code
= getIntegerAttribute(tree
, TARGET_SDK_VERSION_ATTR
, &error
);
657 String8 name
= getResolvedAttribute(&res
, tree
, TARGET_SDK_VERSION_ATTR
, &error
);
659 fprintf(stderr
, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
663 if (name
== "Donut" && targetSdk
< 4) targetSdk
= 4;
664 printf("targetSdkVersion:'%s'\n", name
.string());
665 } else if (code
!= -1) {
666 if (targetSdk
< code
) {
669 printf("targetSdkVersion:'%d'\n", code
);
671 } else if (tag
== "uses-configuration") {
672 int32_t reqTouchScreen
= getIntegerAttribute(tree
,
673 REQ_TOUCH_SCREEN_ATTR
, NULL
, 0);
674 int32_t reqKeyboardType
= getIntegerAttribute(tree
,
675 REQ_KEYBOARD_TYPE_ATTR
, NULL
, 0);
676 int32_t reqHardKeyboard
= getIntegerAttribute(tree
,
677 REQ_HARD_KEYBOARD_ATTR
, NULL
, 0);
678 int32_t reqNavigation
= getIntegerAttribute(tree
,
679 REQ_NAVIGATION_ATTR
, NULL
, 0);
680 int32_t reqFiveWayNav
= getIntegerAttribute(tree
,
681 REQ_FIVE_WAY_NAV_ATTR
, NULL
, 0);
682 printf("uses-configuration:");
683 if (reqTouchScreen
!= 0) {
684 printf(" reqTouchScreen='%d'", reqTouchScreen
);
686 if (reqKeyboardType
!= 0) {
687 printf(" reqKeyboardType='%d'", reqKeyboardType
);
689 if (reqHardKeyboard
!= 0) {
690 printf(" reqHardKeyboard='%d'", reqHardKeyboard
);
692 if (reqNavigation
!= 0) {
693 printf(" reqNavigation='%d'", reqNavigation
);
695 if (reqFiveWayNav
!= 0) {
696 printf(" reqFiveWayNav='%d'", reqFiveWayNav
);
699 } else if (tag
== "supports-density") {
700 int32_t dens
= getIntegerAttribute(tree
, DENSITY_ATTR
, &error
);
702 fprintf(stderr
, "ERROR getting 'android:density' attribute: %s\n",
706 printf("supports-density:'%d'\n", dens
);
707 } else if (tag
== "supports-screens") {
708 smallScreen
= getIntegerAttribute(tree
,
709 SMALL_SCREEN_ATTR
, NULL
, 1);
710 normalScreen
= getIntegerAttribute(tree
,
711 NORMAL_SCREEN_ATTR
, NULL
, 1);
712 largeScreen
= getIntegerAttribute(tree
,
713 LARGE_SCREEN_ATTR
, NULL
, 1);
714 } else if (tag
== "uses-feature") {
715 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
717 if (name
!= "" && error
== "") {
718 int req
= getIntegerAttribute(tree
,
719 REQUIRED_ATTR
, NULL
, 1);
720 if (name
== "android.hardware.camera") {
721 specCameraFeature
= true;
723 printf("uses-feature%s:'%s'\n",
724 req
? "" : "-not-required", name
.string());
726 int vers
= getIntegerAttribute(tree
,
727 GL_ES_VERSION_ATTR
, &error
);
729 printf("uses-gl-es:'0x%x'\n", vers
);
732 } else if (tag
== "uses-permission") {
733 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
734 if (name
!= "" && error
== "") {
735 if (name
== "android.permission.CAMERA") {
736 hasCameraPermission
= true;
738 printf("uses-permission:'%s'\n", name
.string());
740 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
744 } else if (tag
== "original-package") {
745 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
746 if (name
!= "" && error
== "") {
747 printf("original-package:'%s'\n", name
.string());
749 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n",
754 } else if (depth
== 3 && withinApplication
) {
755 withinActivity
= false;
756 withinReceiver
= false;
757 withinService
= false;
758 hasIntentFilter
= false;
759 if(tag
== "activity") {
760 withinActivity
= true;
761 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
763 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
767 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
769 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
773 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
775 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
778 } else if (tag
== "uses-library") {
779 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
781 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
784 int req
= getIntegerAttribute(tree
,
785 REQUIRED_ATTR
, NULL
, 1);
786 printf("uses-library%s:'%s'\n",
787 req
? "" : "-not-required", libraryName
.string());
788 } else if (tag
== "receiver") {
789 withinReceiver
= true;
790 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
793 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
796 } else if (tag
== "service") {
797 withinService
= true;
798 serviceName
= getAttribute(tree
, NAME_ATTR
, &error
);
801 fprintf(stderr
, "ERROR getting 'android:name' attribute for service: %s\n", error
.string());
805 } else if ((depth
== 4) && (tag
== "intent-filter")) {
806 hasIntentFilter
= true;
807 withinIntentFilter
= true;
808 actMainActivity
= actWidgetReceivers
= actImeService
= actWallpaperService
= false;
809 } else if ((depth
== 5) && withinIntentFilter
){
811 if (tag
== "action") {
812 action
= getAttribute(tree
, NAME_ATTR
, &error
);
814 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
817 if (withinActivity
) {
818 if (action
== "android.intent.action.MAIN") {
819 isMainActivity
= true;
820 actMainActivity
= true;
822 } else if (withinReceiver
) {
823 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
824 actWidgetReceivers
= true;
826 } else if (withinService
) {
827 if (action
== "android.view.InputMethod") {
828 actImeService
= true;
829 } else if (action
== "android.service.wallpaper.WallpaperService") {
830 actWallpaperService
= true;
833 if (action
== "android.intent.action.SEARCH") {
838 if (tag
== "category") {
839 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
841 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
844 if (withinActivity
) {
845 if (category
== "android.intent.category.LAUNCHER") {
846 isLauncherActivity
= true;
853 if (!specCameraFeature
&& hasCameraPermission
) {
854 // For applications that have not explicitly stated their
855 // camera feature requirements, but have requested the camera
856 // permission, we are going to give them compatibility treatment
857 // of requiring the equivalent to original android devices.
858 printf("uses-feature:'android.hardware.camera'\n");
859 printf("uses-feature:'android.hardware.camera.autofocus'\n");
862 if (hasMainActivity
) {
865 if (hasWidgetReceivers
) {
866 printf("app-widget\n");
871 if (hasWallpaperService
) {
872 printf("wallpaper\n");
874 if (hasOtherActivities
) {
875 printf("other-activities\n");
880 if (hasOtherReceivers
) {
881 printf("other-receivers\n");
883 if (hasOtherServices
) {
884 printf("other-services\n");
887 // Determine default values for any unspecified screen sizes,
888 // based on the target SDK of the package. As of 4 (donut)
889 // the screen size support was introduced, so all default to
891 if (smallScreen
> 0) {
892 smallScreen
= targetSdk
>= 4 ? -1 : 0;
894 if (normalScreen
> 0) {
897 if (largeScreen
> 0) {
898 largeScreen
= targetSdk
>= 4 ? -1 : 0;
900 printf("supports-screens:");
901 if (smallScreen
!= 0) printf(" 'small'");
902 if (normalScreen
!= 0) printf(" 'normal'");
903 if (largeScreen
!= 0) printf(" 'large'");
907 Vector
<String8
> locales
;
908 res
.getLocales(&locales
);
909 const size_t NL
= locales
.size();
910 for (size_t i
=0; i
<NL
; i
++) {
911 const char* localeStr
= locales
[i
].string();
912 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
915 printf(" '%s'", localeStr
);
919 Vector
<ResTable_config
> configs
;
920 res
.getConfigurations(&configs
);
921 SortedVector
<int> densities
;
922 const size_t NC
= configs
.size();
923 for (size_t i
=0; i
<NC
; i
++) {
924 int dens
= configs
[i
].density
;
925 if (dens
== 0) dens
= 160;
929 printf("densities:");
930 const size_t ND
= densities
.size();
931 for (size_t i
=0; i
<ND
; i
++) {
932 printf(" '%d'", densities
[i
]);
936 AssetDir
* dir
= assets
.openNonAssetDir(assetsCookie
, "lib");
938 if (dir
->getFileCount() > 0) {
939 printf("native-code:");
940 for (size_t i
=0; i
<dir
->getFileCount(); i
++) {
941 printf(" '%s'", dir
->getFileName(i
).string());
947 } else if (strcmp("configurations", option
) == 0) {
948 Vector
<ResTable_config
> configs
;
949 res
.getConfigurations(&configs
);
950 const size_t N
= configs
.size();
951 for (size_t i
=0; i
<N
; i
++) {
952 printf("%s\n", configs
[i
].toString().string());
955 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
966 return (result
!= NO_ERROR
);
971 * Handle the "add" command, which wants to add files to a new or
972 * pre-existing archive.
974 int doAdd(Bundle
* bundle
)
977 status_t result
= UNKNOWN_ERROR
;
978 const char* zipFileName
;
980 if (bundle
->getUpdate()) {
981 /* avoid confusion */
982 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
986 if (bundle
->getFileSpecCount() < 1) {
987 fprintf(stderr
, "ERROR: must specify zip file name\n");
990 zipFileName
= bundle
->getFileSpecEntry(0);
992 if (bundle
->getFileSpecCount() < 2) {
993 fprintf(stderr
, "NOTE: nothing to do\n");
997 zip
= openReadWrite(zipFileName
, true);
999 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
1003 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1004 const char* fileName
= bundle
->getFileSpecEntry(i
);
1006 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
1007 printf(" '%s'... (from gzip)\n", fileName
);
1008 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
1010 if (bundle
->getJunkPath()) {
1011 String8 storageName
= String8(fileName
).getPathLeaf();
1012 printf(" '%s' as '%s'...\n", fileName
, storageName
.string());
1013 result
= zip
->add(fileName
, storageName
.string(),
1014 bundle
->getCompressionMethod(), NULL
);
1016 printf(" '%s'...\n", fileName
);
1017 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
1020 if (result
!= NO_ERROR
) {
1021 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
1022 if (result
== NAME_NOT_FOUND
)
1023 fprintf(stderr
, ": file not found\n");
1024 else if (result
== ALREADY_EXISTS
)
1025 fprintf(stderr
, ": already exists in archive\n");
1027 fprintf(stderr
, "\n");
1036 return (result
!= NO_ERROR
);
1041 * Delete files from an existing archive.
1043 int doRemove(Bundle
* bundle
)
1045 ZipFile
* zip
= NULL
;
1046 status_t result
= UNKNOWN_ERROR
;
1047 const char* zipFileName
;
1049 if (bundle
->getFileSpecCount() < 1) {
1050 fprintf(stderr
, "ERROR: must specify zip file name\n");
1053 zipFileName
= bundle
->getFileSpecEntry(0);
1055 if (bundle
->getFileSpecCount() < 2) {
1056 fprintf(stderr
, "NOTE: nothing to do\n");
1060 zip
= openReadWrite(zipFileName
, false);
1062 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
1067 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1068 const char* fileName
= bundle
->getFileSpecEntry(i
);
1071 entry
= zip
->getEntryByName(fileName
);
1072 if (entry
== NULL
) {
1073 printf(" '%s' NOT FOUND\n", fileName
);
1077 result
= zip
->remove(entry
);
1079 if (result
!= NO_ERROR
) {
1080 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
1081 bundle
->getFileSpecEntry(i
), zipFileName
);
1086 /* update the archive */
1091 return (result
!= NO_ERROR
);
1096 * Package up an asset directory and associated application files.
1098 int doPackage(Bundle
* bundle
)
1100 const char* outputAPKFile
;
1103 sp
<AaptAssets
> assets
;
1106 // -c zz_ZZ means do pseudolocalization
1107 ResourceFilter filter
;
1108 err
= filter
.parse(bundle
->getConfigurations());
1109 if (err
!= NO_ERROR
) {
1112 if (filter
.containsPseudo()) {
1113 bundle
->setPseudolocalize(true);
1116 N
= bundle
->getFileSpecCount();
1117 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
1118 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
1119 fprintf(stderr
, "ERROR: no input files\n");
1123 outputAPKFile
= bundle
->getOutputAPKFile();
1125 // Make sure the filenames provided exist and are of the appropriate type.
1126 if (outputAPKFile
) {
1128 type
= getFileType(outputAPKFile
);
1129 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
1131 "ERROR: output file '%s' exists but is not regular file\n",
1138 assets
= new AaptAssets();
1139 err
= assets
->slurpFromArgs(bundle
);
1144 if (bundle
->getVerbose()) {
1148 // If they asked for any files that need to be compiled, do so.
1149 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
1150 err
= buildResources(bundle
, assets
);
1156 // At this point we've read everything and processed everything. From here
1157 // on out it's just writing output files.
1158 if (SourcePos::hasErrors()) {
1162 // Write out R.java constants
1163 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
1164 if (bundle
->getCustomPackage() == NULL
) {
1165 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
1167 const String8
customPkg(bundle
->getCustomPackage());
1168 err
= writeResourceSymbols(bundle
, assets
, customPkg
, true);
1174 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
1178 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
1184 // Write out the ProGuard file
1185 err
= writeProguardFile(bundle
, assets
);
1191 if (outputAPKFile
) {
1192 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
1193 if (err
!= NO_ERROR
) {
1194 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
1201 if (SourcePos::hasErrors()) {
1202 SourcePos::printErrors(stderr
);