2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
7 #include "AaptAssets.h"
8 #include "StringPool.h"
10 #include "ResourceTable.h"
15 // ==========================================================================
16 // ==========================================================================
17 // ==========================================================================
29 status_t
parsePackage(const sp
<AaptGroup
>& grp
);
32 // ==========================================================================
33 // ==========================================================================
34 // ==========================================================================
36 static String8
parseResourceName(const String8
& leaf
)
38 const char* firstDot
= strchr(leaf
.string(), '.');
39 const char* str
= leaf
.string();
42 return String8(str
, firstDot
-str
);
48 class ResourceTypeSet
: public RefBase
,
49 public KeyedVector
<String8
,sp
<AaptGroup
> >
55 ResourceTypeSet::ResourceTypeSet()
57 KeyedVector
<String8
,sp
<AaptGroup
> >()
61 class ResourceDirIterator
64 ResourceDirIterator(const sp
<ResourceTypeSet
>& set
, const String8
& resType
)
65 : mResType(resType
), mSet(set
), mSetPos(0), mGroupPos(0)
69 inline const sp
<AaptGroup
>& getGroup() const { return mGroup
; }
70 inline const sp
<AaptFile
>& getFile() const { return mFile
; }
72 inline const String8
& getBaseName() const { return mBaseName
; }
73 inline const String8
& getLeafName() const { return mLeafName
; }
74 inline String8
getPath() const { return mPath
; }
75 inline const ResTable_config
& getParams() const { return mParams
; }
87 // Try to get next file in this current group.
88 if (mGroup
!= NULL
&& mGroupPos
< mGroup
->getFiles().size()) {
90 file
= group
->getFiles().valueAt(mGroupPos
++);
92 // Try to get the next group/file in this directory
93 } else if (mSetPos
< mSet
->size()) {
94 mGroup
= group
= mSet
->valueAt(mSetPos
++);
95 if (group
->getFiles().size() < 1) {
98 file
= group
->getFiles().valueAt(0);
108 String8
leaf(group
->getLeaf());
109 mLeafName
= String8(leaf
);
110 mParams
= file
->getGroupEntry().toParams();
111 NOISY(printf("Dir %s: mcc=%d mnc=%d lang=%c%c cnt=%c%c orient=%d density=%d touch=%d key=%d inp=%d nav=%d\n",
112 group
->getPath().string(), mParams
.mcc
, mParams
.mnc
,
113 mParams
.language
[0] ? mParams
.language
[0] : '-',
114 mParams
.language
[1] ? mParams
.language
[1] : '-',
115 mParams
.country
[0] ? mParams
.country
[0] : '-',
116 mParams
.country
[1] ? mParams
.country
[1] : '-',
118 mParams
.density
, mParams
.touchscreen
, mParams
.keyboard
,
119 mParams
.inputFlags
, mParams
.navigation
));
121 mPath
.appendPath(file
->getGroupEntry().toDirName(mResType
));
122 mPath
.appendPath(leaf
);
123 mBaseName
= parseResourceName(leaf
);
124 if (mBaseName
== "") {
125 fprintf(stderr
, "Error: malformed resource filename %s\n",
126 file
->getPrintableSource().string());
127 return UNKNOWN_ERROR
;
130 NOISY(printf("file name=%s\n", mBaseName
.string()));
139 const sp
<ResourceTypeSet
> mSet
;
142 sp
<AaptGroup
> mGroup
;
149 ResTable_config mParams
;
152 // ==========================================================================
153 // ==========================================================================
154 // ==========================================================================
156 bool isValidResourceType(const String8
& type
)
158 return type
== "anim" || type
== "drawable" || type
== "layout"
159 || type
== "values" || type
== "xml" || type
== "raw"
160 || type
== "color" || type
== "menu";
163 static sp
<AaptFile
> getResourceFile(const sp
<AaptAssets
>& assets
, bool makeIfNecessary
=true)
165 sp
<AaptGroup
> group
= assets
->getFiles().valueFor(String8("resources.arsc"));
168 file
= group
->getFiles().valueFor(AaptGroupEntry());
174 if (!makeIfNecessary
) {
177 return assets
->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(),
181 static status_t
parsePackage(const sp
<AaptAssets
>& assets
, const sp
<AaptGroup
>& grp
)
183 if (grp
->getFiles().size() != 1) {
184 fprintf(stderr
, "WARNING: Multiple AndroidManifest.xml files found, using %s\n",
185 grp
->getFiles().valueAt(0)->getPrintableSource().string());
188 sp
<AaptFile
> file
= grp
->getFiles().valueAt(0);
191 status_t err
= parseXMLResource(file
, &block
);
192 if (err
!= NO_ERROR
) {
195 //printXMLBlock(&block);
197 ResXMLTree::event_code_t code
;
198 while ((code
=block
.next()) != ResXMLTree::START_TAG
199 && code
!= ResXMLTree::END_DOCUMENT
200 && code
!= ResXMLTree::BAD_DOCUMENT
) {
204 if (code
!= ResXMLTree::START_TAG
) {
205 fprintf(stderr
, "%s:%d: No start tag found\n",
206 file
->getPrintableSource().string(), block
.getLineNumber());
207 return UNKNOWN_ERROR
;
209 if (strcmp16(block
.getElementName(&len
), String16("manifest").string()) != 0) {
210 fprintf(stderr
, "%s:%d: Invalid start tag %s, expected <manifest>\n",
211 file
->getPrintableSource().string(), block
.getLineNumber(),
212 String8(block
.getElementName(&len
)).string());
213 return UNKNOWN_ERROR
;
216 ssize_t nameIndex
= block
.indexOfAttribute(NULL
, "package");
218 fprintf(stderr
, "%s:%d: <manifest> does not have package attribute.\n",
219 file
->getPrintableSource().string(), block
.getLineNumber());
220 return UNKNOWN_ERROR
;
223 assets
->setPackage(String8(block
.getAttributeStringValue(nameIndex
, &len
)));
228 // ==========================================================================
229 // ==========================================================================
230 // ==========================================================================
232 static status_t
makeFileResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
233 ResourceTable
* table
,
234 const sp
<ResourceTypeSet
>& set
,
237 String8
type8(resType
);
238 String16
type16(resType
);
240 bool hasErrors
= false;
242 ResourceDirIterator
it(set
, String8(resType
));
244 while ((res
=it
.next()) == NO_ERROR
) {
245 if (bundle
->getVerbose()) {
246 printf(" (new resource id %s from %s)\n",
247 it
.getBaseName().string(), it
.getFile()->getPrintableSource().string());
249 String16
baseName(it
.getBaseName());
250 const char16_t* str
= baseName
.string();
251 const char16_t* const end
= str
+ baseName
.size();
253 if (!((*str
>= 'a' && *str
<= 'z')
254 || (*str
>= '0' && *str
<= '9')
255 || *str
== '_' || *str
== '.')) {
256 fprintf(stderr
, "%s: Invalid file name: must contain only [a-z0-9_.]\n",
257 it
.getPath().string());
262 String8 resPath
= it
.getPath();
263 resPath
.convertToResPath();
264 table
->addEntry(SourcePos(it
.getPath(), 0), String16(assets
->getPackage()),
270 assets
->addResource(it
.getLeafName(), resPath
, it
.getFile(), type8
);
273 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
276 static status_t
preProcessImages(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
277 const sp
<ResourceTypeSet
>& set
)
279 ResourceDirIterator
it(set
, String8("drawable"));
280 Vector
<sp
<AaptFile
> > newNameFiles
;
281 Vector
<String8
> newNamePaths
;
283 while ((res
=it
.next()) == NO_ERROR
) {
284 res
= preProcessImage(bundle
, assets
, it
.getFile(), NULL
);
285 if (res
!= NO_ERROR
) {
293 status_t
postProcessImages(const sp
<AaptAssets
>& assets
,
294 ResourceTable
* table
,
295 const sp
<ResourceTypeSet
>& set
)
297 ResourceDirIterator
it(set
, String8("drawable"));
299 while ((res
=it
.next()) == NO_ERROR
) {
300 res
= postProcessImage(assets
, table
, it
.getFile());
301 if (res
!= NO_ERROR
) {
306 return res
< NO_ERROR
? res
: (status_t
)NO_ERROR
;
309 static void collect_files(const sp
<AaptDir
>& dir
,
310 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
312 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& groups
= dir
->getFiles();
313 int N
= groups
.size();
314 for (int i
=0; i
<N
; i
++) {
315 String8 leafName
= groups
.keyAt(i
);
316 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
318 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
321 if (files
.size() == 0) {
325 String8 resType
= files
.valueAt(0)->getResourceType();
327 ssize_t index
= resources
->indexOfKey(resType
);
330 sp
<ResourceTypeSet
> set
= new ResourceTypeSet();
331 set
->add(leafName
, group
);
332 resources
->add(resType
, set
);
334 sp
<ResourceTypeSet
> set
= resources
->valueAt(index
);
335 index
= set
->indexOfKey(leafName
);
337 set
->add(leafName
, group
);
339 sp
<AaptGroup
> existingGroup
= set
->valueAt(index
);
340 int M
= files
.size();
341 for (int j
=0; j
<M
; j
++) {
342 existingGroup
->addFile(files
.valueAt(j
));
349 static void collect_files(const sp
<AaptAssets
>& ass
,
350 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
352 const Vector
<sp
<AaptDir
> >& dirs
= ass
->resDirs();
355 for (int i
=0; i
<N
; i
++) {
356 sp
<AaptDir
> d
= dirs
.itemAt(i
);
357 collect_files(d
, resources
);
359 // don't try to include the res dir
360 ass
->removeDir(d
->getLeaf());
367 ATTR_LEADING_SPACES
= -3,
368 ATTR_TRAILING_SPACES
= -4
370 static int validateAttr(const String8
& path
, const ResXMLParser
& parser
,
371 const char* ns
, const char* attr
, const char* validChars
, bool required
)
375 ssize_t index
= parser
.indexOfAttribute(ns
, attr
);
377 if (index
>= 0 && (str
=parser
.getAttributeStringValue(index
, &len
)) != NULL
) {
379 for (size_t i
=0; i
<len
; i
++) {
381 const char* p
= validChars
;
391 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n",
392 path
.string(), parser
.getLineNumber(),
393 String8(parser
.getElementName(&len
)).string(), attr
, (char)str
[i
]);
399 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not start with a space.\n",
400 path
.string(), parser
.getLineNumber(),
401 String8(parser
.getElementName(&len
)).string(), attr
);
402 return ATTR_LEADING_SPACES
;
404 if (str
[len
-1] == ' ') {
405 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not end with a space.\n",
406 path
.string(), parser
.getLineNumber(),
407 String8(parser
.getElementName(&len
)).string(), attr
);
408 return ATTR_TRAILING_SPACES
;
413 fprintf(stderr
, "%s:%d: Tag <%s> missing required attribute %s.\n",
414 path
.string(), parser
.getLineNumber(),
415 String8(parser
.getElementName(&len
)).string(), attr
);
416 return ATTR_NOT_FOUND
;
421 static void checkForIds(const String8
& path
, ResXMLParser
& parser
)
423 ResXMLTree::event_code_t code
;
424 while ((code
=parser
.next()) != ResXMLTree::END_DOCUMENT
425 && code
> ResXMLTree::BAD_DOCUMENT
) {
426 if (code
== ResXMLTree::START_TAG
) {
427 ssize_t index
= parser
.indexOfAttribute(NULL
, "id");
429 fprintf(stderr
, "%s:%d: WARNING: found plain 'id' attribute; did you mean the new 'android:id' name?\n",
430 path
.string(), parser
.getLineNumber());
436 #define ASSIGN_IT(n) \
438 ssize_t index = resources.indexOfKey(String8(#n)); \
440 n ## s = resources.valueAt(index); \
444 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
446 // First, look for a package file to parse. This is required to
447 // be able to generate the resource information.
448 sp
<AaptGroup
> androidManifestFile
=
449 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
450 if (androidManifestFile
== NULL
) {
451 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
452 return UNKNOWN_ERROR
;
455 status_t err
= parsePackage(assets
, androidManifestFile
);
456 if (err
!= NO_ERROR
) {
460 NOISY(printf("Creating resources for package %s\n",
461 assets
->getPackage().string()));
463 ResourceTable
table(bundle
, String16(assets
->getPackage()));
464 err
= table
.addIncludedResources(bundle
, assets
);
465 if (err
!= NO_ERROR
) {
469 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
471 sp
<AaptDir
> res
= assets
->getDirs().valueFor(String8("res"));
473 // --------------------------------------------------------------
474 // First, gather all resource information.
475 // --------------------------------------------------------------
477 // resType -> leafName -> group
478 KeyedVector
<String8
, sp
<ResourceTypeSet
> > resources
;
479 collect_files(assets
, &resources
);
481 sp
<ResourceTypeSet
> drawables
;
482 sp
<ResourceTypeSet
> valuess
;
483 sp
<ResourceTypeSet
> layouts
;
484 sp
<ResourceTypeSet
> anims
;
485 sp
<ResourceTypeSet
> xmls
;
486 sp
<ResourceTypeSet
> raws
;
487 sp
<ResourceTypeSet
> colors
;
488 sp
<ResourceTypeSet
> menus
;
499 bool hasErrors
= false;
501 if (drawables
!= NULL
) {
502 err
= preProcessImages(bundle
, assets
, drawables
);
503 if (err
== NO_ERROR
) {
504 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
505 if (err
!= NO_ERROR
) {
513 if (layouts
!= NULL
) {
514 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
515 if (err
!= NO_ERROR
) {
521 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
522 if (err
!= NO_ERROR
) {
528 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
529 if (err
!= NO_ERROR
) {
535 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
536 if (err
!= NO_ERROR
) {
541 if (valuess
!= NULL
) {
542 ResourceDirIterator
it(valuess
, String8("values"));
544 while ((res
=it
.next()) == NO_ERROR
) {
545 sp
<AaptFile
> file
= it
.getFile();
547 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(), &table
);
548 if (res
!= NO_ERROR
) {
554 if (colors
!= NULL
) {
555 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
556 if (err
!= NO_ERROR
) {
562 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
563 if (err
!= NO_ERROR
) {
568 // --------------------------------------------------------------------
569 // Assignment of resource IDs and initial generation of resource table.
570 // --------------------------------------------------------------------
572 if (table
.hasResources()) {
573 sp
<AaptFile
> resFile(getResourceFile(assets
));
574 if (resFile
== NULL
) {
575 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
576 return UNKNOWN_ERROR
;
579 err
= table
.assignResourceIds();
580 if (err
< NO_ERROR
) {
585 // --------------------------------------------------------------
586 // Finally, we can now we can compile XML files, which may reference
588 // --------------------------------------------------------------
590 if (layouts
!= NULL
) {
591 ResourceDirIterator
it(layouts
, String8("layout"));
592 while ((err
=it
.next()) == NO_ERROR
) {
593 String8 src
= it
.getFile()->getPrintableSource();
594 err
= compileXmlFile(assets
, it
.getFile(), &table
);
595 if (err
== NO_ERROR
) {
597 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
598 checkForIds(src
, block
);
604 if (err
< NO_ERROR
) {
611 ResourceDirIterator
it(anims
, String8("anim"));
612 while ((err
=it
.next()) == NO_ERROR
) {
613 err
= compileXmlFile(assets
, it
.getFile(), &table
);
614 if (err
!= NO_ERROR
) {
619 if (err
< NO_ERROR
) {
626 ResourceDirIterator
it(xmls
, String8("xml"));
627 while ((err
=it
.next()) == NO_ERROR
) {
628 err
= compileXmlFile(assets
, it
.getFile(), &table
);
629 if (err
!= NO_ERROR
) {
634 if (err
< NO_ERROR
) {
640 if (drawables
!= NULL
) {
641 err
= postProcessImages(assets
, &table
, drawables
);
642 if (err
!= NO_ERROR
) {
647 if (colors
!= NULL
) {
648 ResourceDirIterator
it(colors
, String8("color"));
649 while ((err
=it
.next()) == NO_ERROR
) {
650 err
= compileXmlFile(assets
, it
.getFile(), &table
);
651 if (err
!= NO_ERROR
) {
656 if (err
< NO_ERROR
) {
663 ResourceDirIterator
it(menus
, String8("menu"));
664 while ((err
=it
.next()) == NO_ERROR
) {
665 String8 src
= it
.getFile()->getPrintableSource();
666 err
= compileXmlFile(assets
, it
.getFile(), &table
);
667 if (err
!= NO_ERROR
) {
671 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
672 checkForIds(src
, block
);
675 if (err
< NO_ERROR
) {
681 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
682 String8
manifestPath(manifestFile
->getPrintableSource());
684 // Perform a basic validation of the manifest file. This time we
685 // parse it with the comments intact, so that we can use them to
686 // generate java docs... so we are not going to write this one
687 // back out to the final manifest data.
688 err
= compileXmlFile(assets
, manifestFile
, &table
,
689 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
690 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
691 if (err
< NO_ERROR
) {
695 block
.setTo(manifestFile
->getData(), manifestFile
->getSize(), true);
696 String16
manifest16("manifest");
697 String16
permission16("permission");
698 String16
permission_group16("permission-group");
699 String16
uses_permission16("uses-permission");
700 String16
instrumentation16("instrumentation");
701 String16
application16("application");
702 String16
provider16("provider");
703 String16
service16("service");
704 String16
receiver16("receiver");
705 String16
activity16("activity");
706 String16
action16("action");
707 String16
category16("category");
708 String16
data16("scheme");
709 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
710 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
711 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
712 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
713 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
714 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
715 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
716 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
717 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
718 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
719 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
720 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
721 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
722 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
723 ResXMLTree::event_code_t code
;
724 sp
<AaptSymbols
> permissionSymbols
;
725 sp
<AaptSymbols
> permissionGroupSymbols
;
726 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
727 && code
> ResXMLTree::BAD_DOCUMENT
) {
728 if (code
== ResXMLTree::START_TAG
) {
730 if (block
.getElementNamespace(&len
) != NULL
) {
733 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
734 if (validateAttr(manifestPath
, block
, NULL
, "package",
735 packageIdentChars
, true) != ATTR_OKAY
) {
738 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
739 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
740 const bool isGroup
= strcmp16(block
.getElementName(&len
),
741 permission_group16
.string()) == 0;
742 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
743 isGroup
? packageIdentCharsWithTheStupid
744 : packageIdentChars
, true) != ATTR_OKAY
) {
747 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
748 sp
<AaptSymbols
> syms
;
750 syms
= permissionSymbols
;
752 sp
<AaptSymbols
> symbols
=
753 assets
->getSymbolsFor(String8("Manifest"));
754 syms
= permissionSymbols
= symbols
->addNestedSymbol(
755 String8("permission"), srcPos
);
758 syms
= permissionGroupSymbols
;
760 sp
<AaptSymbols
> symbols
=
761 assets
->getSymbolsFor(String8("Manifest"));
762 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
763 String8("permission_group"), srcPos
);
767 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
768 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
770 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
771 manifestPath
.string(), block
.getLineNumber(),
772 String8(block
.getElementName(&len
)).string());
777 char* p
= idStr
.lockBuffer(idStr
.size());
778 char* e
= p
+ idStr
.size();
779 bool begins_with_digit
= true; // init to true so an empty string fails
782 if (*e
>= '0' && *e
<= '9') {
783 begins_with_digit
= true;
786 if ((*e
>= 'a' && *e
<= 'z') ||
787 (*e
>= 'A' && *e
<= 'Z') ||
789 begins_with_digit
= false;
792 if (isGroup
&& (*e
== '-')) {
794 begins_with_digit
= false;
800 idStr
.unlockBuffer();
801 // verify that we stopped because we hit a period or
802 // the beginning of the string, and that the
803 // identifier didn't begin with a digit.
804 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
806 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
807 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
810 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
811 const uint16_t* cmt
= block
.getComment(&len
);
812 if (cmt
!= NULL
&& *cmt
!= 0) {
813 //printf("Comment of %s: %s\n", String8(e).string(),
814 // String8(cmt).string());
815 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
817 //printf("No comment for %s\n", String8(e).string());
819 syms
->makeSymbolPublic(String8(e
), srcPos
);
820 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
821 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
822 packageIdentChars
, true) != ATTR_OKAY
) {
825 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
826 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
827 classIdentChars
, true) != ATTR_OKAY
) {
830 if (validateAttr(manifestPath
, block
,
831 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
832 packageIdentChars
, true) != ATTR_OKAY
) {
835 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
836 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
837 classIdentChars
, false) != ATTR_OKAY
) {
840 if (validateAttr(manifestPath
, block
,
841 RESOURCES_ANDROID_NAMESPACE
, "permission",
842 packageIdentChars
, false) != ATTR_OKAY
) {
845 if (validateAttr(manifestPath
, block
,
846 RESOURCES_ANDROID_NAMESPACE
, "process",
847 processIdentChars
, false) != ATTR_OKAY
) {
850 if (validateAttr(manifestPath
, block
,
851 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
852 processIdentChars
, false) != ATTR_OKAY
) {
855 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
856 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
857 classIdentChars
, true) != ATTR_OKAY
) {
860 if (validateAttr(manifestPath
, block
,
861 RESOURCES_ANDROID_NAMESPACE
, "authorities",
862 authoritiesIdentChars
, true) != ATTR_OKAY
) {
865 if (validateAttr(manifestPath
, block
,
866 RESOURCES_ANDROID_NAMESPACE
, "permission",
867 packageIdentChars
, false) != ATTR_OKAY
) {
870 if (validateAttr(manifestPath
, block
,
871 RESOURCES_ANDROID_NAMESPACE
, "process",
872 processIdentChars
, false) != ATTR_OKAY
) {
875 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
876 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
877 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
878 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
879 classIdentChars
, true) != ATTR_OKAY
) {
882 if (validateAttr(manifestPath
, block
,
883 RESOURCES_ANDROID_NAMESPACE
, "permission",
884 packageIdentChars
, false) != ATTR_OKAY
) {
887 if (validateAttr(manifestPath
, block
,
888 RESOURCES_ANDROID_NAMESPACE
, "process",
889 processIdentChars
, false) != ATTR_OKAY
) {
892 if (validateAttr(manifestPath
, block
,
893 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
894 processIdentChars
, false) != ATTR_OKAY
) {
897 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
898 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
899 if (validateAttr(manifestPath
, block
,
900 RESOURCES_ANDROID_NAMESPACE
, "name",
901 packageIdentChars
, true) != ATTR_OKAY
) {
904 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
905 if (validateAttr(manifestPath
, block
,
906 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
907 typeIdentChars
, true) != ATTR_OKAY
) {
910 if (validateAttr(manifestPath
, block
,
911 RESOURCES_ANDROID_NAMESPACE
, "scheme",
912 schemeIdentChars
, true) != ATTR_OKAY
) {
920 return UNKNOWN_ERROR
;
923 // Generate final compiled manifest file.
924 manifestFile
->clearData();
925 err
= compileXmlFile(assets
, manifestFile
, &table
);
926 if (err
< NO_ERROR
) {
931 //printXMLBlock(&block);
933 // --------------------------------------------------------------
934 // Generate the final resource table.
935 // Re-flatten because we may have added new resource IDs
936 // --------------------------------------------------------------
938 if (table
.hasResources()) {
939 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
940 err
= table
.addSymbols(symbols
);
941 if (err
< NO_ERROR
) {
945 sp
<AaptFile
> resFile(getResourceFile(assets
));
946 if (resFile
== NULL
) {
947 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
948 return UNKNOWN_ERROR
;
951 err
= table
.flatten(bundle
, resFile
);
952 if (err
< NO_ERROR
) {
956 if (bundle
->getPublicOutputFile()) {
957 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
959 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
960 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
961 return UNKNOWN_ERROR
;
963 if (bundle
->getVerbose()) {
964 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
966 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
971 rt
.add(resFile
->getData(), resFile
->getSize(), NULL
);
972 printf("Generated resources:\n");
976 // These resources are now considered to be a part of the included
977 // resources, for others to reference.
978 err
= assets
->addIncludedResources(resFile
);
979 if (err
< NO_ERROR
) {
980 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
988 static const char* getIndentSpace(int indent
)
990 static const char whitespace
[] =
993 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
996 static status_t
fixupSymbol(String16
* inoutSymbol
)
998 inoutSymbol
->replaceAll('.', '_');
999 inoutSymbol
->replaceAll(':', '_');
1003 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1004 const String8
& name
,
1005 String16
* outTypeComment
= NULL
)
1007 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1009 //printf("Got R symbols!\n");
1010 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1012 //printf("Got attrs symbols! comment %s=%s\n",
1013 // name.string(), String8(asym->getComment(name)).string());
1014 if (outTypeComment
!= NULL
) {
1015 *outTypeComment
= asym
->getTypeComment(name
);
1017 return asym
->getComment(name
);
1023 static status_t
writeLayoutClasses(
1024 FILE* fp
, const sp
<AaptAssets
>& assets
,
1025 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1027 const char* indentStr
= getIndentSpace(indent
);
1028 if (!includePrivate
) {
1029 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1031 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1034 String16
attr16("attr");
1035 String16
package16(assets
->getPackage());
1037 indentStr
= getIndentSpace(indent
);
1038 bool hasErrors
= false;
1041 size_t N
= symbols
->getNestedSymbols().size();
1042 for (i
=0; i
<N
; i
++) {
1043 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1044 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1045 String8
realClassName(nclassName16
);
1046 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1049 String8
nclassName(nclassName16
);
1051 SortedVector
<uint32_t> idents
;
1052 Vector
<uint32_t> origOrder
;
1053 Vector
<bool> publicFlags
;
1056 size_t NA
= nsymbols
->getSymbols().size();
1057 for (a
=0; a
<NA
; a
++) {
1058 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1059 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1061 bool isPublic
= true;
1063 String16
name16(sym
.name
);
1064 uint32_t typeSpecFlags
;
1065 code
= assets
->getIncludedResources().identifierForName(
1066 name16
.string(), name16
.size(),
1067 attr16
.string(), attr16
.size(),
1068 package16
.string(), package16
.size(), &typeSpecFlags
);
1070 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1071 nclassName
.string(), sym
.name
.string());
1074 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1077 origOrder
.add(code
);
1078 publicFlags
.add(isPublic
);
1083 String16 comment
= symbols
->getComment(realClassName
);
1084 fprintf(fp
, "%s/** ", indentStr
);
1085 if (comment
.size() > 0) {
1086 fprintf(fp
, "%s\n", String8(comment
).string());
1088 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1090 bool hasTable
= false;
1091 for (a
=0; a
<NA
; a
++) {
1092 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1097 "%s <p>Includes the following attributes:</p>\n"
1098 "%s <table border=\"2\" width=\"85%%\" align=\"center\" frame=\"hsides\" rules=\"all\" cellpadding=\"5\">\n"
1099 "%s <colgroup align=\"left\" />\n"
1100 "%s <colgroup align=\"left\" />\n"
1101 "%s <tr><th>Attribute<th>Summary</tr>\n",
1108 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1109 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1112 String8
name8(sym
.name
);
1113 String16
comment(sym
.comment
);
1114 if (comment
.size() <= 0) {
1115 comment
= getAttributeComment(assets
, name8
);
1117 if (comment
.size() > 0) {
1118 const char16_t* p
= comment
.string();
1119 while (*p
!= 0 && *p
!= '.') {
1121 while (*p
!= 0 && *p
!= '}') {
1131 comment
= String16(comment
.string(), p
-comment
.string());
1133 String16
name(name8
);
1135 fprintf(fp
, "%s <tr><th><code>{@link #%s_%s %s:%s}</code><td>%s</tr>\n",
1136 indentStr
, nclassName
.string(),
1137 String8(name
).string(),
1138 assets
->getPackage().string(),
1139 String8(name
).string(),
1140 String8(comment
).string());
1144 fprintf(fp
, "%s </table>\n", indentStr
);
1146 for (a
=0; a
<NA
; a
++) {
1147 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1149 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1150 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1153 String16
name(sym
.name
);
1155 fprintf(fp
, "%s @see #%s_%s\n",
1156 indentStr
, nclassName
.string(),
1157 String8(name
).string());
1160 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1163 "%spublic static final int[] %s = {\n"
1165 indentStr
, nclassName
.string(),
1166 getIndentSpace(indent
+1));
1168 for (a
=0; a
<NA
; a
++) {
1171 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1176 fprintf(fp
, "0x%08x", idents
[a
]);
1179 fprintf(fp
, "\n%s};\n", indentStr
);
1181 for (a
=0; a
<NA
; a
++) {
1182 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1184 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1185 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1188 String8
name8(sym
.name
);
1189 String16
comment(sym
.comment
);
1190 String16 typeComment
;
1191 if (comment
.size() <= 0) {
1192 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1194 getAttributeComment(assets
, name8
, &typeComment
);
1196 String16
name(name8
);
1197 if (fixupSymbol(&name
) != NO_ERROR
) {
1201 uint32_t typeSpecFlags
= 0;
1202 String16
name16(sym
.name
);
1203 assets
->getIncludedResources().identifierForName(
1204 name16
.string(), name16
.size(),
1205 attr16
.string(), attr16
.size(),
1206 package16
.string(), package16
.size(), &typeSpecFlags
);
1207 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1208 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1209 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1211 fprintf(fp
, "%s/**\n", indentStr
);
1212 if (comment
.size() > 0) {
1213 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1214 fprintf(fp
, "%s %s\n", indentStr
, String8(comment
).string());
1217 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1218 "%s attribute's value can be found in the {@link #%s} array.\n",
1220 pub
? assets
->getPackage().string()
1221 : assets
->getSymbolsPrivatePackage().string(),
1222 String8(name
).string(),
1223 indentStr
, nclassName
.string());
1225 if (typeComment
.size() > 0) {
1226 fprintf(fp
, "\n\n%s %s\n", indentStr
, String8(typeComment
).string());
1228 if (comment
.size() > 0) {
1231 "%s <p>This corresponds to the global attribute"
1232 "%s resource symbol {@link %s.R.attr#%s}.\n",
1233 indentStr
, indentStr
,
1234 assets
->getPackage().string(),
1235 String8(name
).string());
1238 "%s <p>This is a private symbol.\n", indentStr
);
1241 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1242 "android", String8(name
).string());
1243 fprintf(fp
, "%s*/\n", indentStr
);
1245 "%spublic static final int %s_%s = %d;\n",
1246 indentStr
, nclassName
.string(),
1247 String8(name
).string(), (int)pos
);
1253 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1254 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1257 static status_t
writeSymbolClass(
1258 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1259 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
)
1261 fprintf(fp
, "%spublic %sfinal class %s {\n",
1262 getIndentSpace(indent
),
1263 indent
!= 0 ? "static " : "", className
.string());
1267 status_t err
= NO_ERROR
;
1269 size_t N
= symbols
->getSymbols().size();
1270 for (i
=0; i
<N
; i
++) {
1271 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1272 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1275 if (!includePrivate
&& !sym
.isPublic
) {
1278 String16
name(sym
.name
);
1279 String8
realName(name
);
1280 if (fixupSymbol(&name
) != NO_ERROR
) {
1281 return UNKNOWN_ERROR
;
1283 String16
comment(sym
.comment
);
1284 bool haveComment
= false;
1285 if (comment
.size() > 0) {
1289 getIndentSpace(indent
), String8(comment
).string());
1290 } else if (sym
.isPublic
&& !includePrivate
) {
1291 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1292 assets
->getPackage().string(), className
.string(),
1293 String8(sym
.name
).string());
1295 String16
typeComment(sym
.typeComment
);
1296 if (typeComment
.size() > 0) {
1301 getIndentSpace(indent
), String8(typeComment
).string());
1305 getIndentSpace(indent
), String8(typeComment
).string());
1309 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1311 fprintf(fp
, "%spublic static final int %s=0x%08x;\n",
1312 getIndentSpace(indent
),
1313 String8(name
).string(), (int)sym
.int32Val
);
1316 for (i
=0; i
<N
; i
++) {
1317 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1318 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1321 if (!includePrivate
&& !sym
.isPublic
) {
1324 String16
name(sym
.name
);
1325 if (fixupSymbol(&name
) != NO_ERROR
) {
1326 return UNKNOWN_ERROR
;
1328 String16
comment(sym
.comment
);
1329 if (comment
.size() > 0) {
1333 getIndentSpace(indent
), String8(comment
).string(),
1334 getIndentSpace(indent
));
1335 } else if (sym
.isPublic
&& !includePrivate
) {
1336 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1337 assets
->getPackage().string(), className
.string(),
1338 String8(sym
.name
).string());
1340 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1341 getIndentSpace(indent
),
1342 String8(name
).string(), sym
.stringVal
.string());
1345 sp
<AaptSymbols
> styleableSymbols
;
1347 N
= symbols
->getNestedSymbols().size();
1348 for (i
=0; i
<N
; i
++) {
1349 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1350 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1351 if (nclassName
== "styleable") {
1352 styleableSymbols
= nsymbols
;
1354 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
);
1356 if (err
!= NO_ERROR
) {
1361 if (styleableSymbols
!= NULL
) {
1362 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1363 if (err
!= NO_ERROR
) {
1369 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1373 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1374 const String8
& package
, bool includePrivate
)
1376 if (!bundle
->getRClassDir()) {
1380 const size_t N
= assets
->getSymbols().size();
1381 for (size_t i
=0; i
<N
; i
++) {
1382 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1383 String8
className(assets
->getSymbols().keyAt(i
));
1384 String8
dest(bundle
->getRClassDir());
1385 if (bundle
->getMakePackageDirs()) {
1386 String8
pkg(package
);
1387 const char* last
= pkg
.string();
1388 const char* s
= last
-1;
1391 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1392 String8
part(last
, s
-last
);
1393 dest
.appendPath(part
);
1394 #ifdef HAVE_MS_C_RUNTIME
1395 _mkdir(dest
.string());
1397 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1403 dest
.appendPath(className
);
1404 dest
.append(".java");
1405 FILE* fp
= fopen(dest
.string(), "w+");
1407 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1408 dest
.string(), strerror(errno
));
1409 return UNKNOWN_ERROR
;
1411 if (bundle
->getVerbose()) {
1412 printf(" Writing symbols for class %s.\n", className
.string());
1416 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1418 " * This class was automatically generated by the\n"
1419 " * aapt tool from the resource data it found. It\n"
1420 " * should not be modified by hand.\n"
1423 "package %s;\n\n", package
.string());
1425 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0);
1426 if (err
!= NO_ERROR
) {