]>
git.saurik.com Git - android/aapt.git/blob - Command.cpp
76a5acdb4c6f0aadbdb5271c1f3a80fb61268e9a
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 withinApplication
= false;
507 bool withinReceiver
= false;
510 int normalScreen
= 1;
513 String8 activityName
;
514 String8 activityLabel
;
515 String8 activityIcon
;
516 String8 receiverName
;
517 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
518 if (code
== ResXMLTree::END_TAG
) {
522 if (code
!= ResXMLTree::START_TAG
) {
526 String8
tag(tree
.getElementName(&len
));
527 //printf("Depth %d tag %s\n", depth, tag.string());
529 if (tag
!= "manifest") {
530 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
533 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
534 printf("package: name='%s' ", pkg
.string());
535 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
537 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
540 if (versionCode
> 0) {
541 printf("versionCode='%d' ", versionCode
);
543 printf("versionCode='' ");
545 String8 versionName
= getAttribute(tree
, VERSION_NAME_ATTR
, &error
);
547 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
550 printf("versionName='%s'\n", versionName
.string());
551 } else if (depth
== 2) {
552 withinApplication
= false;
553 if (tag
== "application") {
554 withinApplication
= true;
555 String8 label
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
557 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
560 printf("application: label='%s' ", label
.string());
561 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
563 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
566 printf("icon='%s'\n", icon
.string());
567 int32_t testOnly
= getIntegerAttribute(tree
, TEST_ONLY_ATTR
, &error
, 0);
569 fprintf(stderr
, "ERROR getting 'android:testOnly' attribute: %s\n", error
.string());
573 printf("testOnly='%d'\n", testOnly
);
575 } else if (tag
== "uses-sdk") {
576 int32_t code
= getIntegerAttribute(tree
, MIN_SDK_VERSION_ATTR
, &error
);
579 String8 name
= getResolvedAttribute(&res
, tree
, MIN_SDK_VERSION_ATTR
, &error
);
581 fprintf(stderr
, "ERROR getting 'android:minSdkVersion' attribute: %s\n",
585 if (name
== "Donut") targetSdk
= 4;
586 printf("sdkVersion:'%s'\n", name
.string());
587 } else if (code
!= -1) {
589 printf("sdkVersion:'%d'\n", code
);
591 code
= getIntegerAttribute(tree
, MAX_SDK_VERSION_ATTR
, NULL
, -1);
593 printf("maxSdkVersion:'%d'\n", code
);
595 code
= getIntegerAttribute(tree
, TARGET_SDK_VERSION_ATTR
, &error
);
598 String8 name
= getResolvedAttribute(&res
, tree
, TARGET_SDK_VERSION_ATTR
, &error
);
600 fprintf(stderr
, "ERROR getting 'android:targetSdkVersion' attribute: %s\n",
604 if (name
== "Donut" && targetSdk
< 4) targetSdk
= 4;
605 printf("targetSdkVersion:'%s'\n", name
.string());
606 } else if (code
!= -1) {
607 if (targetSdk
< code
) {
610 printf("targetSdkVersion:'%d'\n", code
);
612 } else if (tag
== "uses-configuration") {
613 int32_t reqTouchScreen
= getIntegerAttribute(tree
,
614 REQ_TOUCH_SCREEN_ATTR
, NULL
, 0);
615 int32_t reqKeyboardType
= getIntegerAttribute(tree
,
616 REQ_KEYBOARD_TYPE_ATTR
, NULL
, 0);
617 int32_t reqHardKeyboard
= getIntegerAttribute(tree
,
618 REQ_HARD_KEYBOARD_ATTR
, NULL
, 0);
619 int32_t reqNavigation
= getIntegerAttribute(tree
,
620 REQ_NAVIGATION_ATTR
, NULL
, 0);
621 int32_t reqFiveWayNav
= getIntegerAttribute(tree
,
622 REQ_FIVE_WAY_NAV_ATTR
, NULL
, 0);
623 printf("uses-configuation:");
624 if (reqTouchScreen
!= 0) {
625 printf(" reqTouchScreen='%d'", reqTouchScreen
);
627 if (reqKeyboardType
!= 0) {
628 printf(" reqKeyboardType='%d'", reqKeyboardType
);
630 if (reqHardKeyboard
!= 0) {
631 printf(" reqHardKeyboard='%d'", reqHardKeyboard
);
633 if (reqNavigation
!= 0) {
634 printf(" reqNavigation='%d'", reqNavigation
);
636 if (reqFiveWayNav
!= 0) {
637 printf(" reqFiveWayNav='%d'", reqFiveWayNav
);
640 } else if (tag
== "supports-density") {
641 int32_t dens
= getIntegerAttribute(tree
, DENSITY_ATTR
, &error
);
643 fprintf(stderr
, "ERROR getting 'android:density' attribute: %s\n",
647 printf("supports-density:'%d'\n", dens
);
648 } else if (tag
== "supports-screens") {
649 smallScreen
= getIntegerAttribute(tree
,
650 SMALL_SCREEN_ATTR
, NULL
, 1);
651 normalScreen
= getIntegerAttribute(tree
,
652 NORMAL_SCREEN_ATTR
, NULL
, 1);
653 largeScreen
= getIntegerAttribute(tree
,
654 LARGE_SCREEN_ATTR
, NULL
, 1);
656 } else if (depth
== 3 && withinApplication
) {
657 withinActivity
= false;
658 withinReceiver
= false;
659 if(tag
== "activity") {
660 withinActivity
= true;
661 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
663 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
667 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
669 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
673 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
675 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
678 } else if (tag
== "uses-library") {
679 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
681 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
684 printf("uses-library:'%s'\n", libraryName
.string());
685 } else if (tag
== "receiver") {
686 withinReceiver
= true;
687 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
690 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
694 } else if (depth
== 5) {
695 if (withinActivity
) {
696 if (tag
== "action") {
697 //printf("LOG: action tag\n");
698 String8 action
= getAttribute(tree
, NAME_ATTR
, &error
);
700 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
703 if (action
== "android.intent.action.MAIN") {
704 isMainActivity
= true;
705 //printf("LOG: isMainActivity==true\n");
707 } else if (tag
== "category") {
708 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
710 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
713 if (category
== "android.intent.category.LAUNCHER") {
714 isLauncherActivity
= true;
715 //printf("LOG: isLauncherActivity==true\n");
718 } else if (withinReceiver
) {
719 if (tag
== "action") {
720 String8 action
= getAttribute(tree
, NAME_ATTR
, &error
);
722 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
725 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
726 const char *rName
= getComponentName(pkg
, receiverName
);
728 printf("gadget-receiver:'%s/%s'\n", pkg
.string(), rName
);
736 withinApplication
= false;
739 //if (withinActivity) printf("LOG: withinActivity==false\n");
740 withinActivity
= false;
741 withinReceiver
= false;
745 //if (isMainActivity) printf("LOG: isMainActivity==false\n");
746 //if (isLauncherActivity) printf("LOG: isLauncherActivity==false\n");
747 isMainActivity
= false;
748 isLauncherActivity
= false;
751 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
752 printf("launchable activity:");
753 const char *aName
= getComponentName(pkg
, activityName
);
755 printf(" name='%s'", aName
);
757 printf("label='%s' icon='%s'\n",
758 activityLabel
.string(),
759 activityIcon
.string());
763 // Determine default values for any unspecified screen sizes,
764 // based on the target SDK of the package. As of 4 (donut)
765 // the screen size support was introduced, so all default to
767 if (smallScreen
> 0) {
768 smallScreen
= targetSdk
>= 4 ? -1 : 0;
770 if (normalScreen
> 0) {
773 if (largeScreen
> 0) {
774 largeScreen
= targetSdk
>= 4 ? -1 : 0;
776 printf("supports-screens:");
777 if (smallScreen
!= 0) printf(" 'small'");
778 if (normalScreen
!= 0) printf(" 'normal'");
779 if (largeScreen
!= 0) printf(" 'large'");
783 Vector
<String8
> locales
;
784 res
.getLocales(&locales
);
785 const size_t NL
= locales
.size();
786 for (size_t i
=0; i
<NL
; i
++) {
787 const char* localeStr
= locales
[i
].string();
788 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
791 printf(" '%s'", localeStr
);
795 Vector
<ResTable_config
> configs
;
796 res
.getConfigurations(&configs
);
797 SortedVector
<int> densities
;
798 const size_t NC
= configs
.size();
799 for (size_t i
=0; i
<NC
; i
++) {
800 int dens
= configs
[i
].density
;
801 if (dens
== 0) dens
= 160;
805 printf("densities:");
806 const size_t ND
= densities
.size();
807 for (size_t i
=0; i
<ND
; i
++) {
808 printf(" '%d'", densities
[i
]);
812 AssetDir
* dir
= assets
.openNonAssetDir(assetsCookie
, "lib");
814 if (dir
->getFileCount() > 0) {
815 printf("native-code:");
816 for (size_t i
=0; i
<dir
->getFileCount(); i
++) {
817 printf(" '%s'", dir
->getFileName(i
).string());
823 } else if (strcmp("configurations", option
) == 0) {
824 Vector
<ResTable_config
> configs
;
825 res
.getConfigurations(&configs
);
826 const size_t N
= configs
.size();
827 for (size_t i
=0; i
<N
; i
++) {
828 printf("%s\n", configs
[i
].toString().string());
831 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
842 return (result
!= NO_ERROR
);
847 * Handle the "add" command, which wants to add files to a new or
848 * pre-existing archive.
850 int doAdd(Bundle
* bundle
)
853 status_t result
= UNKNOWN_ERROR
;
854 const char* zipFileName
;
856 if (bundle
->getUpdate()) {
857 /* avoid confusion */
858 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
862 if (bundle
->getFileSpecCount() < 1) {
863 fprintf(stderr
, "ERROR: must specify zip file name\n");
866 zipFileName
= bundle
->getFileSpecEntry(0);
868 if (bundle
->getFileSpecCount() < 2) {
869 fprintf(stderr
, "NOTE: nothing to do\n");
873 zip
= openReadWrite(zipFileName
, true);
875 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
879 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
880 const char* fileName
= bundle
->getFileSpecEntry(i
);
882 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
883 printf(" '%s'... (from gzip)\n", fileName
);
884 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
886 printf(" '%s'...\n", fileName
);
887 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
889 if (result
!= NO_ERROR
) {
890 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
891 if (result
== NAME_NOT_FOUND
)
892 fprintf(stderr
, ": file not found\n");
893 else if (result
== ALREADY_EXISTS
)
894 fprintf(stderr
, ": already exists in archive\n");
896 fprintf(stderr
, "\n");
905 return (result
!= NO_ERROR
);
910 * Delete files from an existing archive.
912 int doRemove(Bundle
* bundle
)
915 status_t result
= UNKNOWN_ERROR
;
916 const char* zipFileName
;
918 if (bundle
->getFileSpecCount() < 1) {
919 fprintf(stderr
, "ERROR: must specify zip file name\n");
922 zipFileName
= bundle
->getFileSpecEntry(0);
924 if (bundle
->getFileSpecCount() < 2) {
925 fprintf(stderr
, "NOTE: nothing to do\n");
929 zip
= openReadWrite(zipFileName
, false);
931 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
936 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
937 const char* fileName
= bundle
->getFileSpecEntry(i
);
940 entry
= zip
->getEntryByName(fileName
);
942 printf(" '%s' NOT FOUND\n", fileName
);
946 result
= zip
->remove(entry
);
948 if (result
!= NO_ERROR
) {
949 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
950 bundle
->getFileSpecEntry(i
), zipFileName
);
955 /* update the archive */
960 return (result
!= NO_ERROR
);
965 * Package up an asset directory and associated application files.
967 int doPackage(Bundle
* bundle
)
969 const char* outputAPKFile
;
972 sp
<AaptAssets
> assets
;
975 // -c zz_ZZ means do pseudolocalization
976 ResourceFilter filter
;
977 err
= filter
.parse(bundle
->getConfigurations());
978 if (err
!= NO_ERROR
) {
981 if (filter
.containsPseudo()) {
982 bundle
->setPseudolocalize(true);
985 N
= bundle
->getFileSpecCount();
986 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
987 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
988 fprintf(stderr
, "ERROR: no input files\n");
992 outputAPKFile
= bundle
->getOutputAPKFile();
994 // Make sure the filenames provided exist and are of the appropriate type.
997 type
= getFileType(outputAPKFile
);
998 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
1000 "ERROR: output file '%s' exists but is not regular file\n",
1007 assets
= new AaptAssets();
1008 err
= assets
->slurpFromArgs(bundle
);
1013 if (bundle
->getVerbose()) {
1017 // If they asked for any files that need to be compiled, do so.
1018 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
1019 err
= buildResources(bundle
, assets
);
1025 // At this point we've read everything and processed everything. From here
1026 // on out it's just writing output files.
1027 if (SourcePos::hasErrors()) {
1031 // Write out R.java constants
1032 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
1033 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
1038 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
1042 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
1049 if (outputAPKFile
) {
1050 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
1051 if (err
!= NO_ERROR
) {
1052 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
1059 if (SourcePos::hasErrors()) {
1060 SourcePos::printErrors(stderr
);