]>
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"
12 #include <utils/ZipFile.h>
17 using namespace android
;
20 * Show version info. All the cool kids do it.
22 int doVersion(Bundle
* bundle
)
24 if (bundle
->getFileSpecCount() != 0)
25 printf("(ignoring extra arguments)\n");
26 printf("Android Asset Packaging Tool, v0.2\n");
33 * Open the file read only. The call fails if the file doesn't exist.
35 * Returns NULL on failure.
37 ZipFile
* openReadOnly(const char* fileName
)
43 result
= zip
->open(fileName
, ZipFile::kOpenReadOnly
);
44 if (result
!= NO_ERROR
) {
45 if (result
== NAME_NOT_FOUND
)
46 fprintf(stderr
, "ERROR: '%s' not found\n", fileName
);
47 else if (result
== PERMISSION_DENIED
)
48 fprintf(stderr
, "ERROR: '%s' access denied\n", fileName
);
50 fprintf(stderr
, "ERROR: failed opening '%s' as Zip file\n",
60 * Open the file read-write. The file will be created if it doesn't
61 * already exist and "okayToCreate" is set.
63 * Returns NULL on failure.
65 ZipFile
* openReadWrite(const char* fileName
, bool okayToCreate
)
71 flags
= ZipFile::kOpenReadWrite
;
73 flags
|= ZipFile::kOpenCreate
;
76 result
= zip
->open(fileName
, flags
);
77 if (result
!= NO_ERROR
) {
89 * Return a short string describing the compression method.
91 const char* compressionName(int method
)
93 if (method
== ZipEntry::kCompressStored
)
95 else if (method
== ZipEntry::kCompressDeflated
)
102 * Return the percent reduction in size (0% == no compression).
104 int calcPercent(long uncompressedLen
, long compressedLen
)
106 if (!uncompressedLen
)
109 return (int) (100.0 - (compressedLen
* 100.0) / uncompressedLen
+ 0.5);
113 * Handle the "list" command, which can be a simple file dump or
116 * The verbose listing closely matches the output of the Info-ZIP "unzip"
119 int doList(Bundle
* bundle
)
123 const ZipEntry
* entry
;
124 long totalUncLen
, totalCompLen
;
125 const char* zipFileName
;
127 if (bundle
->getFileSpecCount() != 1) {
128 fprintf(stderr
, "ERROR: specify zip file name (only)\n");
131 zipFileName
= bundle
->getFileSpecEntry(0);
133 zip
= openReadOnly(zipFileName
);
139 if (bundle
->getVerbose()) {
140 printf("Archive: %s\n", zipFileName
);
142 " Length Method Size Ratio Date Time CRC-32 Name\n");
144 "-------- ------ ------- ----- ---- ---- ------ ----\n");
147 totalUncLen
= totalCompLen
= 0;
149 count
= zip
->getNumEntries();
150 for (i
= 0; i
< count
; i
++) {
151 entry
= zip
->getEntryByIndex(i
);
152 if (bundle
->getVerbose()) {
156 when
= entry
->getModWhen();
157 strftime(dateBuf
, sizeof(dateBuf
), "%m-%d-%y %H:%M",
160 printf("%8ld %-7.7s %7ld %3d%% %s %08lx %s\n",
161 (long) entry
->getUncompressedLen(),
162 compressionName(entry
->getCompressionMethod()),
163 (long) entry
->getCompressedLen(),
164 calcPercent(entry
->getUncompressedLen(),
165 entry
->getCompressedLen()),
168 entry
->getFileName());
170 printf("%s\n", entry
->getFileName());
173 totalUncLen
+= entry
->getUncompressedLen();
174 totalCompLen
+= entry
->getCompressedLen();
177 if (bundle
->getVerbose()) {
179 "-------- ------- --- -------\n");
180 printf("%8ld %7ld %2d%% %d files\n",
183 calcPercent(totalUncLen
, totalCompLen
),
184 zip
->getNumEntries());
187 if (bundle
->getAndroidList()) {
189 if (!assets
.addAssetPath(String8(zipFileName
), NULL
)) {
190 fprintf(stderr
, "ERROR: list -a failed because assets could not be loaded\n");
194 const ResTable
& res
= assets
.getResources(false);
196 printf("\nNo resource table found.\n");
198 printf("\nResource table:\n");
202 Asset
* manifestAsset
= assets
.openNonAsset("AndroidManifest.xml",
203 Asset::ACCESS_BUFFER
);
204 if (manifestAsset
== NULL
) {
205 printf("\nNo AndroidManifest.xml found.\n");
207 printf("\nAndroid manifest:\n");
209 tree
.setTo(manifestAsset
->getBuffer(true),
210 manifestAsset
->getLength());
211 printXMLBlock(&tree
);
213 delete manifestAsset
;
223 static ssize_t
indexOfAttribute(const ResXMLTree
& tree
, uint32_t attrRes
)
225 size_t N
= tree
.getAttributeCount();
226 for (size_t i
=0; i
<N
; i
++) {
227 if (tree
.getAttributeNameResID(i
) == attrRes
) {
234 static String8
getAttribute(const ResXMLTree
& tree
, const char* ns
,
235 const char* attr
, String8
* outError
)
237 ssize_t idx
= tree
.indexOfAttribute(ns
, attr
);
242 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
243 if (value
.dataType
!= Res_value::TYPE_STRING
) {
244 if (outError
!= NULL
) *outError
= "attribute is not a string value";
249 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
250 return str
? String8(str
, len
) : String8();
253 static String8
getAttribute(const ResXMLTree
& tree
, uint32_t attrRes
, String8
* outError
)
255 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
260 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
261 if (value
.dataType
!= Res_value::TYPE_STRING
) {
262 if (outError
!= NULL
) *outError
= "attribute is not a string value";
267 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
268 return str
? String8(str
, len
) : String8();
271 static int32_t getIntegerAttribute(const ResXMLTree
& tree
, uint32_t attrRes
, String8
* outError
)
273 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
278 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
279 if (value
.dataType
!= Res_value::TYPE_INT_DEC
) {
280 if (outError
!= NULL
) *outError
= "attribute is not an integer value";
287 static String8
getResolvedAttribute(const ResTable
* resTable
, const ResXMLTree
& tree
,
288 uint32_t attrRes
, String8
* outError
)
290 ssize_t idx
= indexOfAttribute(tree
, attrRes
);
295 if (tree
.getAttributeValue(idx
, &value
) != NO_ERROR
) {
296 if (value
.dataType
== Res_value::TYPE_STRING
) {
298 const uint16_t* str
= tree
.getAttributeStringValue(idx
, &len
);
299 return str
? String8(str
, len
) : String8();
301 resTable
->resolveReference(&value
, 0);
302 if (value
.dataType
!= Res_value::TYPE_STRING
) {
303 if (outError
!= NULL
) *outError
= "attribute is not a string value";
308 const Res_value
* value2
= &value
;
309 const char16_t* str
= const_cast<ResTable
*>(resTable
)->valueToString(value2
, 0, NULL
, &len
);
310 return str
? String8(str
, len
) : String8();
313 // These are attribute resource constants for the platform, as found
316 NAME_ATTR
= 0x01010003,
317 VERSION_CODE_ATTR
= 0x0101021b,
318 VERSION_NAME_ATTR
= 0x0101021c,
319 LABEL_ATTR
= 0x01010001,
320 ICON_ATTR
= 0x01010002,
321 MIN_SDK_VERSION_ATTR
= 0x0101020c
324 const char *getComponentName(String8
&pkgName
, String8
&componentName
) {
325 ssize_t idx
= componentName
.find(".");
326 String8
retStr(pkgName
);
328 retStr
+= componentName
;
329 } else if (idx
< 0) {
331 retStr
+= componentName
;
333 return componentName
.string();
335 return retStr
.string();
339 * Handle the "dump" command, to extract select data from an archive.
341 int doDump(Bundle
* bundle
)
343 status_t result
= UNKNOWN_ERROR
;
346 if (bundle
->getFileSpecCount() < 1) {
347 fprintf(stderr
, "ERROR: no dump option specified\n");
351 if (bundle
->getFileSpecCount() < 2) {
352 fprintf(stderr
, "ERROR: no dump file specified\n");
356 const char* option
= bundle
->getFileSpecEntry(0);
357 const char* filename
= bundle
->getFileSpecEntry(1);
360 if (!assets
.addAssetPath(String8(filename
), NULL
)) {
361 fprintf(stderr
, "ERROR: dump failed because assets could not be loaded\n");
365 const ResTable
& res
= assets
.getResources(false);
367 fprintf(stderr
, "ERROR: dump failed because no resource table was found\n");
371 if (strcmp("resources", option
) == 0) {
374 } else if (strcmp("xmltree", option
) == 0) {
375 if (bundle
->getFileSpecCount() < 3) {
376 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
380 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
381 const char* resname
= bundle
->getFileSpecEntry(i
);
383 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
385 fprintf(stderr
, "ERROR: dump failed because resource %p found\n", resname
);
389 if (tree
.setTo(asset
->getBuffer(true),
390 asset
->getLength()) != NO_ERROR
) {
391 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
395 printXMLBlock(&tree
);
400 } else if (strcmp("xmlstrings", option
) == 0) {
401 if (bundle
->getFileSpecCount() < 3) {
402 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
406 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
407 const char* resname
= bundle
->getFileSpecEntry(i
);
409 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
411 fprintf(stderr
, "ERROR: dump failed because resource %p found\n", resname
);
415 if (tree
.setTo(asset
->getBuffer(true),
416 asset
->getLength()) != NO_ERROR
) {
417 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
420 printStringPool(&tree
.getStrings());
427 asset
= assets
.openNonAsset("AndroidManifest.xml",
428 Asset::ACCESS_BUFFER
);
430 fprintf(stderr
, "ERROR: dump failed because no AndroidManifest.xml found\n");
434 if (tree
.setTo(asset
->getBuffer(true),
435 asset
->getLength()) != NO_ERROR
) {
436 fprintf(stderr
, "ERROR: AndroidManifest.xml is corrupt\n");
441 if (strcmp("permissions", option
) == 0) {
443 ResXMLTree::event_code_t code
;
445 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
446 if (code
== ResXMLTree::END_TAG
) {
450 if (code
!= ResXMLTree::START_TAG
) {
454 String8
tag(tree
.getElementName(&len
));
455 //printf("Depth %d tag %s\n", depth, tag.string());
457 if (tag
!= "manifest") {
458 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
461 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
462 printf("package: %s\n", pkg
.string());
463 } else if (depth
== 2 && tag
== "permission") {
465 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
467 fprintf(stderr
, "ERROR: %s\n", error
.string());
470 printf("permission: %s\n", name
.string());
471 } else if (depth
== 2 && tag
== "uses-permission") {
473 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
475 fprintf(stderr
, "ERROR: %s\n", error
.string());
478 printf("uses-permission: %s\n", name
.string());
481 } else if (strcmp("badging", option
) == 0) {
483 ResXMLTree::event_code_t code
;
486 bool withinActivity
= false;
487 bool isMainActivity
= false;
488 bool isLauncherActivity
= false;
489 bool withinApplication
= false;
490 bool withinReceiver
= false;
492 String8 activityName
;
493 String8 activityLabel
;
494 String8 activityIcon
;
495 String8 receiverName
;
496 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
497 if (code
== ResXMLTree::END_TAG
) {
501 if (code
!= ResXMLTree::START_TAG
) {
505 String8
tag(tree
.getElementName(&len
));
506 //printf("Depth %d tag %s\n", depth, tag.string());
508 if (tag
!= "manifest") {
509 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
512 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
513 printf("package: name='%s' ", pkg
.string());
514 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
516 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
519 if (versionCode
> 0) {
520 printf("versionCode='%d' ", versionCode
);
522 printf("versionCode='' ");
524 String8 versionName
= getAttribute(tree
, VERSION_NAME_ATTR
, &error
);
526 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
529 printf("versionName='%s'\n", versionName
.string());
530 } else if (depth
== 2) {
531 withinApplication
= false;
532 if (tag
== "application") {
533 withinApplication
= true;
534 String8 label
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
536 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
539 printf("application: label='%s' ", label
.string());
540 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
542 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
545 printf("icon='%s'\n", icon
.string());
546 } else if (tag
== "uses-sdk") {
547 int32_t sdkVersion
= getIntegerAttribute(tree
, MIN_SDK_VERSION_ATTR
, &error
);
549 fprintf(stderr
, "ERROR getting 'android:minSdkVersion' attribute: %s\n", error
.string());
552 if (sdkVersion
!= -1) {
553 printf("sdkVersion:'%d'\n", sdkVersion
);
556 } else if (depth
== 3 && withinApplication
) {
557 withinActivity
= false;
558 withinReceiver
= false;
559 if(tag
== "activity") {
560 withinActivity
= true;
561 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
563 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
567 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
569 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
573 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
575 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
578 } else if (tag
== "uses-library") {
579 String8 libraryName
= getAttribute(tree
, NAME_ATTR
, &error
);
581 fprintf(stderr
, "ERROR getting 'android:name' attribute for uses-library: %s\n", error
.string());
584 printf("uses-library:'%s'\n", libraryName
.string());
585 } else if (tag
== "receiver") {
586 withinReceiver
= true;
587 receiverName
= getAttribute(tree
, NAME_ATTR
, &error
);
590 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
594 } else if (depth
== 5) {
595 if (withinActivity
) {
596 if (tag
== "action") {
597 //printf("LOG: action tag\n");
598 String8 action
= getAttribute(tree
, NAME_ATTR
, &error
);
600 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
603 if (action
== "android.intent.action.MAIN") {
604 isMainActivity
= true;
605 //printf("LOG: isMainActivity==true\n");
607 } else if (tag
== "category") {
608 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
610 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
613 if (category
== "android.intent.category.LAUNCHER") {
614 isLauncherActivity
= true;
615 //printf("LOG: isLauncherActivity==true\n");
618 } else if (withinReceiver
) {
619 if (tag
== "action") {
620 String8 action
= getAttribute(tree
, NAME_ATTR
, &error
);
622 fprintf(stderr
, "ERROR getting 'android:name' attribute for receiver: %s\n", error
.string());
625 if (action
== "android.appwidget.action.APPWIDGET_UPDATE") {
626 const char *rName
= getComponentName(pkg
, receiverName
);
628 printf("gadget-receiver:'%s/%s'\n", pkg
.string(), rName
);
636 withinApplication
= false;
639 //if (withinActivity) printf("LOG: withinActivity==false\n");
640 withinActivity
= false;
641 withinReceiver
= false;
645 //if (isMainActivity) printf("LOG: isMainActivity==false\n");
646 //if (isLauncherActivity) printf("LOG: isLauncherActivity==false\n");
647 isMainActivity
= false;
648 isLauncherActivity
= false;
651 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
652 printf("launchable activity:");
653 const char *aName
= getComponentName(pkg
, activityName
);
655 printf(" name='%s'", aName
);
657 printf("label='%s' icon='%s'\n",
658 activityLabel
.string(),
659 activityIcon
.string());
663 Vector
<String8
> locales
;
664 res
.getLocales(&locales
);
665 const size_t N
= locales
.size();
666 for (size_t i
=0; i
<N
; i
++) {
667 const char* localeStr
= locales
[i
].string();
668 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
671 printf(" '%s'", localeStr
);
674 } else if (strcmp("configurations", option
) == 0) {
675 Vector
<ResTable_config
> configs
;
676 res
.getConfigurations(&configs
);
677 const size_t N
= configs
.size();
678 for (size_t i
=0; i
<N
; i
++) {
679 printf("%s\n", configs
[i
].toString().string());
682 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
693 return (result
!= NO_ERROR
);
698 * Handle the "add" command, which wants to add files to a new or
699 * pre-existing archive.
701 int doAdd(Bundle
* bundle
)
704 status_t result
= UNKNOWN_ERROR
;
705 const char* zipFileName
;
707 if (bundle
->getUpdate()) {
708 /* avoid confusion */
709 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
713 if (bundle
->getFileSpecCount() < 1) {
714 fprintf(stderr
, "ERROR: must specify zip file name\n");
717 zipFileName
= bundle
->getFileSpecEntry(0);
719 if (bundle
->getFileSpecCount() < 2) {
720 fprintf(stderr
, "NOTE: nothing to do\n");
724 zip
= openReadWrite(zipFileName
, true);
726 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
730 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
731 const char* fileName
= bundle
->getFileSpecEntry(i
);
733 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
734 printf(" '%s'... (from gzip)\n", fileName
);
735 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
737 printf(" '%s'...\n", fileName
);
738 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
740 if (result
!= NO_ERROR
) {
741 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
742 if (result
== NAME_NOT_FOUND
)
743 fprintf(stderr
, ": file not found\n");
744 else if (result
== ALREADY_EXISTS
)
745 fprintf(stderr
, ": already exists in archive\n");
747 fprintf(stderr
, "\n");
756 return (result
!= NO_ERROR
);
761 * Delete files from an existing archive.
763 int doRemove(Bundle
* bundle
)
766 status_t result
= UNKNOWN_ERROR
;
767 const char* zipFileName
;
769 if (bundle
->getFileSpecCount() < 1) {
770 fprintf(stderr
, "ERROR: must specify zip file name\n");
773 zipFileName
= bundle
->getFileSpecEntry(0);
775 if (bundle
->getFileSpecCount() < 2) {
776 fprintf(stderr
, "NOTE: nothing to do\n");
780 zip
= openReadWrite(zipFileName
, false);
782 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
787 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
788 const char* fileName
= bundle
->getFileSpecEntry(i
);
791 entry
= zip
->getEntryByName(fileName
);
793 printf(" '%s' NOT FOUND\n", fileName
);
797 result
= zip
->remove(entry
);
799 if (result
!= NO_ERROR
) {
800 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
801 bundle
->getFileSpecEntry(i
), zipFileName
);
806 /* update the archive */
811 return (result
!= NO_ERROR
);
816 * Package up an asset directory and associated application files.
818 int doPackage(Bundle
* bundle
)
820 const char* outputAPKFile
;
823 sp
<AaptAssets
> assets
;
826 // -c zz_ZZ means do pseudolocalization
827 ResourceFilter filter
;
828 err
= filter
.parse(bundle
->getConfigurations());
829 if (err
!= NO_ERROR
) {
832 if (filter
.containsPseudo()) {
833 bundle
->setPseudolocalize(true);
836 N
= bundle
->getFileSpecCount();
837 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
838 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
839 fprintf(stderr
, "ERROR: no input files\n");
843 outputAPKFile
= bundle
->getOutputAPKFile();
845 // Make sure the filenames provided exist and are of the appropriate type.
848 type
= getFileType(outputAPKFile
);
849 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
851 "ERROR: output file '%s' exists but is not regular file\n",
858 assets
= new AaptAssets();
859 err
= assets
->slurpFromArgs(bundle
);
864 if (bundle
->getVerbose()) {
868 // If they asked for any files that need to be compiled, do so.
869 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
870 err
= buildResources(bundle
, assets
);
876 // At this point we've read everything and processed everything. From here
877 // on out it's just writing output files.
878 if (SourcePos::hasErrors()) {
882 // Write out R.java constants
883 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
884 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
889 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
893 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
901 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
902 if (err
!= NO_ERROR
) {
903 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
910 if (SourcePos::hasErrors()) {
911 SourcePos::printErrors(stderr
);