]>
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 static 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 SMALL_SCREEN_ATTR
= 0x01010284,
336 NORMAL_SCREEN_ATTR
= 0x01010285,
337 LARGE_SCREEN_ATTR
= 0x01010286,
340 const char *getComponentName(String8
&pkgName
, String8
&componentName
) {
341 ssize_t idx
= componentName
.find(".");
342 String8
retStr(pkgName
);
344 retStr
+= componentName
;
345 } else if (idx
< 0) {
347 retStr
+= componentName
;
349 return componentName
.string();
351 return retStr
.string();
355 * Handle the "dump" command, to extract select data from an archive.
357 int doDump(Bundle
* bundle
)
359 status_t result
= UNKNOWN_ERROR
;
362 if (bundle
->getFileSpecCount() < 1) {
363 fprintf(stderr
, "ERROR: no dump option specified\n");
367 if (bundle
->getFileSpecCount() < 2) {
368 fprintf(stderr
, "ERROR: no dump file specified\n");
372 const char* option
= bundle
->getFileSpecEntry(0);
373 const char* filename
= bundle
->getFileSpecEntry(1);
377 if (!assets
.addAssetPath(String8(filename
), &assetsCookie
)) {
378 fprintf(stderr
, "ERROR: dump failed because assets could not be loaded\n");
382 const ResTable
& res
= assets
.getResources(false);
384 fprintf(stderr
, "ERROR: dump failed because no resource table was found\n");
388 if (strcmp("resources", option
) == 0) {
389 res
.print(bundle
->getValues());
391 } else if (strcmp("xmltree", option
) == 0) {
392 if (bundle
->getFileSpecCount() < 3) {
393 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
397 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
398 const char* resname
= bundle
->getFileSpecEntry(i
);
400 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
402 fprintf(stderr
, "ERROR: dump failed because resource %p found\n", resname
);
406 if (tree
.setTo(asset
->getBuffer(true),
407 asset
->getLength()) != NO_ERROR
) {
408 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
412 printXMLBlock(&tree
);
417 } else if (strcmp("xmlstrings", option
) == 0) {
418 if (bundle
->getFileSpecCount() < 3) {
419 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
423 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
424 const char* resname
= bundle
->getFileSpecEntry(i
);
426 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
428 fprintf(stderr
, "ERROR: dump failed because resource %p found\n", resname
);
432 if (tree
.setTo(asset
->getBuffer(true),
433 asset
->getLength()) != NO_ERROR
) {
434 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
437 printStringPool(&tree
.getStrings());
444 asset
= assets
.openNonAsset("AndroidManifest.xml",
445 Asset::ACCESS_BUFFER
);
447 fprintf(stderr
, "ERROR: dump failed because no AndroidManifest.xml found\n");
451 if (tree
.setTo(asset
->getBuffer(true),
452 asset
->getLength()) != NO_ERROR
) {
453 fprintf(stderr
, "ERROR: AndroidManifest.xml is corrupt\n");
458 if (strcmp("permissions", option
) == 0) {
460 ResXMLTree::event_code_t code
;
462 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
463 if (code
== ResXMLTree::END_TAG
) {
467 if (code
!= ResXMLTree::START_TAG
) {
471 String8
tag(tree
.getElementName(&len
));
472 //printf("Depth %d tag %s\n", depth, tag.string());
474 if (tag
!= "manifest") {
475 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
478 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
479 printf("package: %s\n", pkg
.string());
480 } else if (depth
== 2 && tag
== "permission") {
482 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
484 fprintf(stderr
, "ERROR: %s\n", error
.string());
487 printf("permission: %s\n", name
.string());
488 } else if (depth
== 2 && tag
== "uses-permission") {
490 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
492 fprintf(stderr
, "ERROR: %s\n", error
.string());
495 printf("uses-permission: %s\n", name
.string());
498 } else if (strcmp("badging", option
) == 0) {
500 ResXMLTree::event_code_t code
;
503 bool withinActivity
= false;
504 bool isMainActivity
= false;
505 bool isLauncherActivity
= false;
506 bool isSearchable
= false;
507 bool withinApplication
= false;
508 bool withinReceiver
= false;
509 bool withinService
= false;
510 bool withinIntentFilter
= false;
511 bool hasMainActivity
= false;
512 bool hasOtherActivities
= false;
513 bool hasOtherReceivers
= false;
514 bool hasOtherServices
= false;
515 bool hasWallpaperService
= false;
516 bool hasImeService
= false;
517 bool hasWidgetReceivers
= false;
518 bool hasIntentFilter
= false;
519 bool actMainActivity
= false;
520 bool actWidgetReceivers
= false;
521 bool actImeService
= false;
522 bool actWallpaperService
= false;
525 int normalScreen
= 1;
528 String8 activityName
;
529 String8 activityLabel
;
530 String8 activityIcon
;
531 String8 receiverName
;
533 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
534 if (code
== ResXMLTree::END_TAG
) {
537 withinApplication
= false;
538 } else if (depth
< 3) {
539 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
540 const char *aName
= getComponentName(pkg
, activityName
);
542 printf("launchable activity name='%s'", aName
);
544 printf("label='%s' icon='%s'\n",
545 activityLabel
.string(),
546 activityIcon
.string());
548 if (!hasIntentFilter
) {
549 hasOtherActivities
|= withinActivity
;
550 hasOtherReceivers
|= withinReceiver
;
551 hasOtherServices
|= withinService
;
553 withinActivity
= false;
554 withinService
= false;
555 withinReceiver
= false;
556 hasIntentFilter
= false;
557 isMainActivity
= isLauncherActivity
= false;
558 } else if (depth
< 4) {
559 if (withinIntentFilter
) {
560 if (withinActivity
) {
561 hasMainActivity
|= actMainActivity
;
562 hasOtherActivities
|= !actMainActivity
;
563 } else if (withinReceiver
) {
564 hasWidgetReceivers
|= actWidgetReceivers
;
565 hasOtherReceivers
|= !actWidgetReceivers
;
566 } else if (withinService
) {
567 hasImeService
|= actImeService
;
568 hasWallpaperService
|= actWallpaperService
;
569 hasOtherServices
|= (!actImeService
&& !actWallpaperService
);
572 withinIntentFilter
= false;
576 if (code
!= ResXMLTree::START_TAG
) {
580 String8
tag(tree
.getElementName(&len
));
581 //printf("Depth %d, %s\n", depth, tag.string());
583 if (tag
!= "manifest") {
584 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
587 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
588 printf("package: name='%s' ", pkg
.string());
589 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
591 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
594 if (versionCode
> 0) {
595 printf("versionCode='%d' ", versionCode
);
597 printf("versionCode='' ");
599 String8 versionName
= getAttribute(tree
, VERSION_NAME_ATTR
, &error
);
601 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
604 printf("versionName='%s'\n", versionName
.string());
605 } else if (depth
== 2) {
606 withinApplication
= false;
607 if (tag
== "application") {
608 withinApplication
= true;
609 String8 label
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
611 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
614 printf("application: label='%s' ", label
.string());
615 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
617 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
620 printf("icon='%s'\n", icon
.string());
621 int32_t testOnly
= getIntegerAttribute(tree
, TEST_ONLY_ATTR
, &error
, 0);
623 fprintf(stderr
, "ERROR getting 'android:testOnly' attribute: %s\n", error
.string());
627 printf("testOnly='%d'\n", testOnly
);
629 } else if (tag
== "uses-sdk") {
630 int32_t code
= getIntegerAttribute(tree
, MIN_SDK_VERSION_ATTR
, &error
);
633 String8 name
= getResolvedAttribute(&res
, tree
, MIN_SDK_VERSION_ATTR
, &error
);
635 fprintf(stderr
, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
639 if (name
== "Donut") targetSdk
= 4;
640 printf("sdkVersion:'%s'\n", name
.string());
641 } else if (code
!= -1) {
643 printf("sdkVersion:'%d'\n", code
);
645 code
= getIntegerAttribute(tree
, MAX_SDK_VERSION_ATTR
, NULL
, -1);
647 printf("maxSdkVersion:'%d'\n", code
);
649 code
= getIntegerAttribute(tree
, TARGET_SDK_VERSION_ATTR
, &error
);
652 String8 name
= getResolvedAttribute(&res
, tree
, TARGET_SDK_VERSION_ATTR
, &error
);
654 fprintf(stderr
, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
658 if (name
== "Donut" && targetSdk
< 4) targetSdk
= 4;
659 printf("targetSdkVersion:'%s'\n", name
.string());
660 } else if (code
!= -1) {
661 if (targetSdk
< code
) {
664 printf("targetSdkVersion:'%d'\n", code
);
666 } else if (tag
== "uses-configuration") {
667 int32_t reqTouchScreen
= getIntegerAttribute(tree
,
668 REQ_TOUCH_SCREEN_ATTR
, NULL
, 0);
669 int32_t reqKeyboardType
= getIntegerAttribute(tree
,
670 REQ_KEYBOARD_TYPE_ATTR
, NULL
, 0);
671 int32_t reqHardKeyboard
= getIntegerAttribute(tree
,
672 REQ_HARD_KEYBOARD_ATTR
, NULL
, 0);
673 int32_t reqNavigation
= getIntegerAttribute(tree
,
674 REQ_NAVIGATION_ATTR
, NULL
, 0);
675 int32_t reqFiveWayNav
= getIntegerAttribute(tree
,
676 REQ_FIVE_WAY_NAV_ATTR
, NULL
, 0);
677 printf("uses-configuation:");
678 if (reqTouchScreen
!= 0) {
679 printf(" reqTouchScreen='%d'", reqTouchScreen
);
681 if (reqKeyboardType
!= 0) {
682 printf(" reqKeyboardType='%d'", reqKeyboardType
);
684 if (reqHardKeyboard
!= 0) {
685 printf(" reqHardKeyboard='%d'", reqHardKeyboard
);
687 if (reqNavigation
!= 0) {
688 printf(" reqNavigation='%d'", reqNavigation
);
690 if (reqFiveWayNav
!= 0) {
691 printf(" reqFiveWayNav='%d'", reqFiveWayNav
);
694 } else if (tag
== "supports-density") {
695 int32_t dens
= getIntegerAttribute(tree
, DENSITY_ATTR
, &error
);
697 fprintf(stderr
, "ERROR getting 'android:density' attribute: %s\n",
701 printf("supports-density:'%d'\n", dens
);
702 } else if (tag
== "supports-screens") {
703 smallScreen
= getIntegerAttribute(tree
,
704 SMALL_SCREEN_ATTR
, NULL
, 1);
705 normalScreen
= getIntegerAttribute(tree
,
706 NORMAL_SCREEN_ATTR
, NULL
, 1);
707 largeScreen
= getIntegerAttribute(tree
,
708 LARGE_SCREEN_ATTR
, NULL
, 1);
710 } else if (depth
== 3 && withinApplication
) {
711 withinActivity
= false;
712 withinReceiver
= false;
713 withinService
= false;
714 hasIntentFilter
= false;
715 if(tag
== "activity") {
716 withinActivity
= true;
717 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
719 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
723 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
725 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
729 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
731 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
734 } else if (tag
== "uses-library") {
735 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
737 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
740 printf("uses-library:'%s'\n", libraryName
.string());
741 } else if (tag
== "receiver") {
742 withinReceiver
= true;
743 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
746 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
749 } else if (tag
== "service") {
750 withinService
= true;
751 serviceName
= getAttribute(tree
, NAME_ATTR
, &error
);
754 fprintf(stderr
, "ERROR getting 'android:name' attribute for service: %s\n", error
.string());
758 } else if ((depth
== 4) && (tag
== "intent-filter")) {
759 hasIntentFilter
= true;
760 withinIntentFilter
= true;
761 actMainActivity
= actWidgetReceivers
= actImeService
= actWallpaperService
= false;
762 } else if ((depth
== 5) && withinIntentFilter
){
764 if (tag
== "action") {
765 action
= getAttribute(tree
, NAME_ATTR
, &error
);
767 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
770 if (withinActivity
) {
771 if (action
== "android.intent.action.MAIN") {
772 isMainActivity
= true;
773 actMainActivity
= true;
775 } else if (withinReceiver
) {
776 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
777 actWidgetReceivers
= true;
779 } else if (withinService
) {
780 if (action
== "android.view.InputMethod") {
781 actImeService
= true;
782 } else if (action
== "android.service.wallpaper.WallpaperService") {
783 actWallpaperService
= true;
786 if (action
== "android.intent.action.SEARCH") {
791 if (tag
== "category") {
792 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
794 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
797 if (withinActivity
) {
798 if (category
== "android.intent.category.LAUNCHER") {
799 isLauncherActivity
= true;
806 if (hasMainActivity
) {
809 if (hasWidgetReceivers
) {
810 printf("app-widget\n");
815 if (hasWallpaperService
) {
816 printf("wallpaper\n");
818 if (hasOtherActivities
) {
819 printf("other-activities\n");
824 if (hasOtherReceivers
) {
825 printf("other-receivers\n");
827 if (hasOtherServices
) {
828 printf("other-services\n");
831 // Determine default values for any unspecified screen sizes,
832 // based on the target SDK of the package. As of 4 (donut)
833 // the screen size support was introduced, so all default to
835 if (smallScreen
> 0) {
836 smallScreen
= targetSdk
>= 4 ? -1 : 0;
838 if (normalScreen
> 0) {
841 if (largeScreen
> 0) {
842 largeScreen
= targetSdk
>= 4 ? -1 : 0;
844 printf("supports-screens:");
845 if (smallScreen
!= 0) printf(" 'small'");
846 if (normalScreen
!= 0) printf(" 'normal'");
847 if (largeScreen
!= 0) printf(" 'large'");
851 Vector
<String8
> locales
;
852 res
.getLocales(&locales
);
853 const size_t NL
= locales
.size();
854 for (size_t i
=0; i
<NL
; i
++) {
855 const char* localeStr
= locales
[i
].string();
856 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
859 printf(" '%s'", localeStr
);
863 Vector
<ResTable_config
> configs
;
864 res
.getConfigurations(&configs
);
865 SortedVector
<int> densities
;
866 const size_t NC
= configs
.size();
867 for (size_t i
=0; i
<NC
; i
++) {
868 int dens
= configs
[i
].density
;
869 if (dens
== 0) dens
= 160;
873 printf("densities:");
874 const size_t ND
= densities
.size();
875 for (size_t i
=0; i
<ND
; i
++) {
876 printf(" '%d'", densities
[i
]);
880 AssetDir
* dir
= assets
.openNonAssetDir(assetsCookie
, "lib");
882 if (dir
->getFileCount() > 0) {
883 printf("native-code:");
884 for (size_t i
=0; i
<dir
->getFileCount(); i
++) {
885 printf(" '%s'", dir
->getFileName(i
).string());
891 } else if (strcmp("configurations", option
) == 0) {
892 Vector
<ResTable_config
> configs
;
893 res
.getConfigurations(&configs
);
894 const size_t N
= configs
.size();
895 for (size_t i
=0; i
<N
; i
++) {
896 printf("%s\n", configs
[i
].toString().string());
899 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
910 return (result
!= NO_ERROR
);
915 * Handle the "add" command, which wants to add files to a new or
916 * pre-existing archive.
918 int doAdd(Bundle
* bundle
)
921 status_t result
= UNKNOWN_ERROR
;
922 const char* zipFileName
;
924 if (bundle
->getUpdate()) {
925 /* avoid confusion */
926 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
930 if (bundle
->getFileSpecCount() < 1) {
931 fprintf(stderr
, "ERROR: must specify zip file name\n");
934 zipFileName
= bundle
->getFileSpecEntry(0);
936 if (bundle
->getFileSpecCount() < 2) {
937 fprintf(stderr
, "NOTE: nothing to do\n");
941 zip
= openReadWrite(zipFileName
, true);
943 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
947 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
948 const char* fileName
= bundle
->getFileSpecEntry(i
);
950 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
951 printf(" '%s'... (from gzip)\n", fileName
);
952 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
954 printf(" '%s'...\n", fileName
);
955 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
957 if (result
!= NO_ERROR
) {
958 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
959 if (result
== NAME_NOT_FOUND
)
960 fprintf(stderr
, ": file not found\n");
961 else if (result
== ALREADY_EXISTS
)
962 fprintf(stderr
, ": already exists in archive\n");
964 fprintf(stderr
, "\n");
973 return (result
!= NO_ERROR
);
978 * Delete files from an existing archive.
980 int doRemove(Bundle
* bundle
)
983 status_t result
= UNKNOWN_ERROR
;
984 const char* zipFileName
;
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
, false);
999 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
1004 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
1005 const char* fileName
= bundle
->getFileSpecEntry(i
);
1008 entry
= zip
->getEntryByName(fileName
);
1009 if (entry
== NULL
) {
1010 printf(" '%s' NOT FOUND\n", fileName
);
1014 result
= zip
->remove(entry
);
1016 if (result
!= NO_ERROR
) {
1017 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
1018 bundle
->getFileSpecEntry(i
), zipFileName
);
1023 /* update the archive */
1028 return (result
!= NO_ERROR
);
1033 * Package up an asset directory and associated application files.
1035 int doPackage(Bundle
* bundle
)
1037 const char* outputAPKFile
;
1040 sp
<AaptAssets
> assets
;
1043 // -c zz_ZZ means do pseudolocalization
1044 ResourceFilter filter
;
1045 err
= filter
.parse(bundle
->getConfigurations());
1046 if (err
!= NO_ERROR
) {
1049 if (filter
.containsPseudo()) {
1050 bundle
->setPseudolocalize(true);
1053 N
= bundle
->getFileSpecCount();
1054 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
1055 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
1056 fprintf(stderr
, "ERROR: no input files\n");
1060 outputAPKFile
= bundle
->getOutputAPKFile();
1062 // Make sure the filenames provided exist and are of the appropriate type.
1063 if (outputAPKFile
) {
1065 type
= getFileType(outputAPKFile
);
1066 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
1068 "ERROR: output file '%s' exists but is not regular file\n",
1075 assets
= new AaptAssets();
1076 err
= assets
->slurpFromArgs(bundle
);
1081 if (bundle
->getVerbose()) {
1085 // If they asked for any files that need to be compiled, do so.
1086 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
1087 err
= buildResources(bundle
, assets
);
1093 // At this point we've read everything and processed everything. From here
1094 // on out it's just writing output files.
1095 if (SourcePos::hasErrors()) {
1099 // Write out R.java constants
1100 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
1101 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
1106 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
1110 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
1117 if (outputAPKFile
) {
1118 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
1119 if (err
!= NO_ERROR
) {
1120 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
1127 if (SourcePos::hasErrors()) {
1128 SourcePos::printErrors(stderr
);