]>
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,
324 * Handle the "dump" command, to extract select data from an archive.
326 int doDump(Bundle
* bundle
)
328 status_t result
= UNKNOWN_ERROR
;
331 if (bundle
->getFileSpecCount() < 1) {
332 fprintf(stderr
, "ERROR: no dump option specified\n");
336 if (bundle
->getFileSpecCount() < 2) {
337 fprintf(stderr
, "ERROR: no dump file specified\n");
341 const char* option
= bundle
->getFileSpecEntry(0);
342 const char* filename
= bundle
->getFileSpecEntry(1);
345 if (!assets
.addAssetPath(String8(filename
), NULL
)) {
346 fprintf(stderr
, "ERROR: dump failed because assets could not be loaded\n");
350 const ResTable
& res
= assets
.getResources(false);
352 fprintf(stderr
, "ERROR: dump failed because no resource table was found\n");
356 if (strcmp("resources", option
) == 0) {
359 } else if (strcmp("xmltree", option
) == 0) {
360 if (bundle
->getFileSpecCount() < 3) {
361 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
365 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
366 const char* resname
= bundle
->getFileSpecEntry(i
);
368 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
370 fprintf(stderr
, "ERROR: dump failed because resource %p found\n", resname
);
374 if (tree
.setTo(asset
->getBuffer(true),
375 asset
->getLength()) != NO_ERROR
) {
376 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
380 printXMLBlock(&tree
);
385 } else if (strcmp("xmlstrings", option
) == 0) {
386 if (bundle
->getFileSpecCount() < 3) {
387 fprintf(stderr
, "ERROR: no dump xmltree resource file specified\n");
391 for (int i
=2; i
<bundle
->getFileSpecCount(); i
++) {
392 const char* resname
= bundle
->getFileSpecEntry(i
);
394 asset
= assets
.openNonAsset(resname
, Asset::ACCESS_BUFFER
);
396 fprintf(stderr
, "ERROR: dump failed because resource %p found\n", resname
);
400 if (tree
.setTo(asset
->getBuffer(true),
401 asset
->getLength()) != NO_ERROR
) {
402 fprintf(stderr
, "ERROR: Resource %s is corrupt\n", resname
);
405 printStringPool(&tree
.getStrings());
412 asset
= assets
.openNonAsset("AndroidManifest.xml",
413 Asset::ACCESS_BUFFER
);
415 fprintf(stderr
, "ERROR: dump failed because no AndroidManifest.xml found\n");
419 if (tree
.setTo(asset
->getBuffer(true),
420 asset
->getLength()) != NO_ERROR
) {
421 fprintf(stderr
, "ERROR: AndroidManifest.xml is corrupt\n");
426 if (strcmp("permissions", option
) == 0) {
428 ResXMLTree::event_code_t code
;
430 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
431 if (code
== ResXMLTree::END_TAG
) {
435 if (code
!= ResXMLTree::START_TAG
) {
439 String8
tag(tree
.getElementName(&len
));
440 //printf("Depth %d tag %s\n", depth, tag.string());
442 if (tag
!= "manifest") {
443 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
446 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
447 printf("package: %s\n", pkg
.string());
448 } else if (depth
== 2 && tag
== "permission") {
450 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
452 fprintf(stderr
, "ERROR: %s\n", error
.string());
455 printf("permission: %s\n", name
.string());
456 } else if (depth
== 2 && tag
== "uses-permission") {
458 String8 name
= getAttribute(tree
, NAME_ATTR
, &error
);
460 fprintf(stderr
, "ERROR: %s\n", error
.string());
463 printf("uses-permission: %s\n", name
.string());
466 } else if (strcmp("badging", option
) == 0) {
468 ResXMLTree::event_code_t code
;
471 bool withinActivity
= false;
472 bool isMainActivity
= false;
473 bool isLauncherActivity
= false;
474 String8 activityName
;
475 String8 activityLabel
;
476 String8 activityIcon
;
477 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
478 if (code
== ResXMLTree::END_TAG
) {
482 if (code
!= ResXMLTree::START_TAG
) {
486 String8
tag(tree
.getElementName(&len
));
487 //printf("Depth %d tag %s\n", depth, tag.string());
489 if (tag
!= "manifest") {
490 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
493 String8 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
494 printf("package: name='%s' ", pkg
.string());
495 int32_t versionCode
= getIntegerAttribute(tree
, VERSION_CODE_ATTR
, &error
);
497 fprintf(stderr
, "ERROR getting 'android:versionCode' attribute: %s\n", error
.string());
500 if (versionCode
> 0) {
501 printf("versionCode='%d' ", versionCode
);
503 printf("versionCode='' ");
505 String8 versionName
= getAttribute(tree
, VERSION_NAME_ATTR
, &error
);
507 fprintf(stderr
, "ERROR getting 'android:versionName' attribute: %s\n", error
.string());
510 printf("versionName='%s'\n", versionName
.string());
511 } else if (depth
== 2 && tag
== "application") {
512 String8 label
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
514 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
517 printf("application: label='%s' ", label
.string());
519 String8 icon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
521 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
524 printf("icon='%s'\n", icon
.string());
525 } else if (depth
== 3 && tag
== "activity") {
526 withinActivity
= true;
527 //printf("LOG: withinActivity==true\n");
529 activityName
= getAttribute(tree
, NAME_ATTR
, &error
);
531 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
535 activityLabel
= getResolvedAttribute(&res
, tree
, LABEL_ATTR
, &error
);
537 fprintf(stderr
, "ERROR getting 'android:label' attribute: %s\n", error
.string());
541 activityIcon
= getResolvedAttribute(&res
, tree
, ICON_ATTR
, &error
);
543 fprintf(stderr
, "ERROR getting 'android:icon' attribute: %s\n", error
.string());
546 } else if (depth
== 5 && withinActivity
) {
547 if (tag
== "action") {
548 //printf("LOG: action tag\n");
549 String8 action
= getAttribute(tree
, NAME_ATTR
, &error
);
551 fprintf(stderr
, "ERROR getting 'android:name' attribute: %s\n", error
.string());
554 if (action
== "android.intent.action.MAIN") {
555 isMainActivity
= true;
556 //printf("LOG: isMainActivity==true\n");
558 } else if (tag
== "category") {
559 String8 category
= getAttribute(tree
, NAME_ATTR
, &error
);
561 fprintf(stderr
, "ERROR getting 'name' attribute: %s\n", error
.string());
564 if (category
== "android.intent.category.LAUNCHER") {
565 isLauncherActivity
= true;
566 //printf("LOG: isLauncherActivity==true\n");
572 //if (withinActivity) printf("LOG: withinActivity==false\n");
573 withinActivity
= false;
577 //if (isMainActivity) printf("LOG: isMainActivity==false\n");
578 //if (isLauncherActivity) printf("LOG: isLauncherActivity==false\n");
579 isMainActivity
= false;
580 isLauncherActivity
= false;
583 if (withinActivity
&& isMainActivity
&& isLauncherActivity
) {
584 printf("launchable activity: name='%s' label='%s' icon='%s'\n",
585 activityName
.string(), activityLabel
.string(),
586 activityIcon
.string());
590 Vector
<String8
> locales
;
591 res
.getLocales(&locales
);
592 const size_t N
= locales
.size();
593 for (size_t i
=0; i
<N
; i
++) {
594 const char* localeStr
= locales
[i
].string();
595 if (localeStr
== NULL
|| strlen(localeStr
) == 0) {
598 printf(" '%s'", localeStr
);
601 } else if (strcmp("configurations", option
) == 0) {
602 Vector
<ResTable_config
> configs
;
603 res
.getConfigurations(&configs
);
604 const size_t N
= configs
.size();
605 for (size_t i
=0; i
<N
; i
++) {
606 printf("%s\n", configs
[i
].toString().string());
609 fprintf(stderr
, "ERROR: unknown dump option '%s'\n", option
);
620 return (result
!= NO_ERROR
);
625 * Handle the "add" command, which wants to add files to a new or
626 * pre-existing archive.
628 int doAdd(Bundle
* bundle
)
631 status_t result
= UNKNOWN_ERROR
;
632 const char* zipFileName
;
634 if (bundle
->getUpdate()) {
635 /* avoid confusion */
636 fprintf(stderr
, "ERROR: can't use '-u' with add\n");
640 if (bundle
->getFileSpecCount() < 1) {
641 fprintf(stderr
, "ERROR: must specify zip file name\n");
644 zipFileName
= bundle
->getFileSpecEntry(0);
646 if (bundle
->getFileSpecCount() < 2) {
647 fprintf(stderr
, "NOTE: nothing to do\n");
651 zip
= openReadWrite(zipFileName
, true);
653 fprintf(stderr
, "ERROR: failed opening/creating '%s' as Zip file\n", zipFileName
);
657 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
658 const char* fileName
= bundle
->getFileSpecEntry(i
);
660 if (strcasecmp(String8(fileName
).getPathExtension().string(), ".gz") == 0) {
661 printf(" '%s'... (from gzip)\n", fileName
);
662 result
= zip
->addGzip(fileName
, String8(fileName
).getBasePath().string(), NULL
);
664 printf(" '%s'...\n", fileName
);
665 result
= zip
->add(fileName
, bundle
->getCompressionMethod(), NULL
);
667 if (result
!= NO_ERROR
) {
668 fprintf(stderr
, "Unable to add '%s' to '%s'", bundle
->getFileSpecEntry(i
), zipFileName
);
669 if (result
== NAME_NOT_FOUND
)
670 fprintf(stderr
, ": file not found\n");
671 else if (result
== ALREADY_EXISTS
)
672 fprintf(stderr
, ": already exists in archive\n");
674 fprintf(stderr
, "\n");
683 return (result
!= NO_ERROR
);
688 * Delete files from an existing archive.
690 int doRemove(Bundle
* bundle
)
693 status_t result
= UNKNOWN_ERROR
;
694 const char* zipFileName
;
696 if (bundle
->getFileSpecCount() < 1) {
697 fprintf(stderr
, "ERROR: must specify zip file name\n");
700 zipFileName
= bundle
->getFileSpecEntry(0);
702 if (bundle
->getFileSpecCount() < 2) {
703 fprintf(stderr
, "NOTE: nothing to do\n");
707 zip
= openReadWrite(zipFileName
, false);
709 fprintf(stderr
, "ERROR: failed opening Zip archive '%s'\n",
714 for (int i
= 1; i
< bundle
->getFileSpecCount(); i
++) {
715 const char* fileName
= bundle
->getFileSpecEntry(i
);
718 entry
= zip
->getEntryByName(fileName
);
720 printf(" '%s' NOT FOUND\n", fileName
);
724 result
= zip
->remove(entry
);
726 if (result
!= NO_ERROR
) {
727 fprintf(stderr
, "Unable to delete '%s' from '%s'\n",
728 bundle
->getFileSpecEntry(i
), zipFileName
);
733 /* update the archive */
738 return (result
!= NO_ERROR
);
743 * Package up an asset directory and associated application files.
745 int doPackage(Bundle
* bundle
)
747 const char* outputAPKFile
;
750 sp
<AaptAssets
> assets
;
753 // -c zz_ZZ means do pseudolocalization
754 ResourceFilter filter
;
755 err
= filter
.parse(bundle
->getConfigurations());
756 if (err
!= NO_ERROR
) {
759 if (filter
.containsPseudo()) {
760 bundle
->setPseudolocalize(true);
763 N
= bundle
->getFileSpecCount();
764 if (N
< 1 && bundle
->getResourceSourceDirs().size() == 0 && bundle
->getJarFiles().size() == 0
765 && bundle
->getAndroidManifestFile() == NULL
&& bundle
->getAssetSourceDir() == NULL
) {
766 fprintf(stderr
, "ERROR: no input files\n");
770 outputAPKFile
= bundle
->getOutputAPKFile();
772 // Make sure the filenames provided exist and are of the appropriate type.
775 type
= getFileType(outputAPKFile
);
776 if (type
!= kFileTypeNonexistent
&& type
!= kFileTypeRegular
) {
778 "ERROR: output file '%s' exists but is not regular file\n",
785 assets
= new AaptAssets();
786 err
= assets
->slurpFromArgs(bundle
);
791 if (bundle
->getVerbose()) {
795 // If they asked for any files that need to be compiled, do so.
796 if (bundle
->getResourceSourceDirs().size() || bundle
->getAndroidManifestFile()) {
797 err
= buildResources(bundle
, assets
);
803 // At this point we've read everything and processed everything. From here
804 // on out it's just writing output files.
805 if (SourcePos::hasErrors()) {
809 // Write out R.java constants
810 if (assets
->getPackage() == assets
->getSymbolsPrivatePackage()) {
811 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), true);
816 err
= writeResourceSymbols(bundle
, assets
, assets
->getPackage(), false);
820 err
= writeResourceSymbols(bundle
, assets
, assets
->getSymbolsPrivatePackage(), true);
828 err
= writeAPK(bundle
, assets
, String8(outputAPKFile
));
829 if (err
!= NO_ERROR
) {
830 fprintf(stderr
, "ERROR: packaging of '%s' failed\n", outputAPKFile
);
837 if (SourcePos::hasErrors()) {
838 SourcePos::printErrors(stderr
);