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 ResourceTypeSet::ResourceTypeSet()
50 KeyedVector
<String8
,sp
<AaptGroup
> >()
54 class ResourceDirIterator
57 ResourceDirIterator(const sp
<ResourceTypeSet
>& set
, const String8
& resType
)
58 : mResType(resType
), mSet(set
), mSetPos(0), mGroupPos(0)
62 inline const sp
<AaptGroup
>& getGroup() const { return mGroup
; }
63 inline const sp
<AaptFile
>& getFile() const { return mFile
; }
65 inline const String8
& getBaseName() const { return mBaseName
; }
66 inline const String8
& getLeafName() const { return mLeafName
; }
67 inline String8
getPath() const { return mPath
; }
68 inline const ResTable_config
& getParams() const { return mParams
; }
80 // Try to get next file in this current group.
81 if (mGroup
!= NULL
&& mGroupPos
< mGroup
->getFiles().size()) {
83 file
= group
->getFiles().valueAt(mGroupPos
++);
85 // Try to get the next group/file in this directory
86 } else if (mSetPos
< mSet
->size()) {
87 mGroup
= group
= mSet
->valueAt(mSetPos
++);
88 if (group
->getFiles().size() < 1) {
91 file
= group
->getFiles().valueAt(0);
101 String8
leaf(group
->getLeaf());
102 mLeafName
= String8(leaf
);
103 mParams
= file
->getGroupEntry().toParams();
104 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",
105 group
->getPath().string(), mParams
.mcc
, mParams
.mnc
,
106 mParams
.language
[0] ? mParams
.language
[0] : '-',
107 mParams
.language
[1] ? mParams
.language
[1] : '-',
108 mParams
.country
[0] ? mParams
.country
[0] : '-',
109 mParams
.country
[1] ? mParams
.country
[1] : '-',
111 mParams
.density
, mParams
.touchscreen
, mParams
.keyboard
,
112 mParams
.inputFlags
, mParams
.navigation
));
114 mPath
.appendPath(file
->getGroupEntry().toDirName(mResType
));
115 mPath
.appendPath(leaf
);
116 mBaseName
= parseResourceName(leaf
);
117 if (mBaseName
== "") {
118 fprintf(stderr
, "Error: malformed resource filename %s\n",
119 file
->getPrintableSource().string());
120 return UNKNOWN_ERROR
;
123 NOISY(printf("file name=%s\n", mBaseName
.string()));
132 const sp
<ResourceTypeSet
> mSet
;
135 sp
<AaptGroup
> mGroup
;
142 ResTable_config mParams
;
145 // ==========================================================================
146 // ==========================================================================
147 // ==========================================================================
149 bool isValidResourceType(const String8
& type
)
151 return type
== "anim" || type
== "drawable" || type
== "layout"
152 || type
== "values" || type
== "xml" || type
== "raw"
153 || type
== "color" || type
== "menu";
156 static sp
<AaptFile
> getResourceFile(const sp
<AaptAssets
>& assets
, bool makeIfNecessary
=true)
158 sp
<AaptGroup
> group
= assets
->getFiles().valueFor(String8("resources.arsc"));
161 file
= group
->getFiles().valueFor(AaptGroupEntry());
167 if (!makeIfNecessary
) {
170 return assets
->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(),
174 static status_t
parsePackage(const sp
<AaptAssets
>& assets
, const sp
<AaptGroup
>& grp
)
176 if (grp
->getFiles().size() != 1) {
177 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
178 grp
->getFiles().valueAt(0)->getPrintableSource().string());
181 sp
<AaptFile
> file
= grp
->getFiles().valueAt(0);
184 status_t err
= parseXMLResource(file
, &block
);
185 if (err
!= NO_ERROR
) {
188 //printXMLBlock(&block);
190 ResXMLTree::event_code_t code
;
191 while ((code
=block
.next()) != ResXMLTree::START_TAG
192 && code
!= ResXMLTree::END_DOCUMENT
193 && code
!= ResXMLTree::BAD_DOCUMENT
) {
197 if (code
!= ResXMLTree::START_TAG
) {
198 fprintf(stderr
, "%s:%d: No start tag found\n",
199 file
->getPrintableSource().string(), block
.getLineNumber());
200 return UNKNOWN_ERROR
;
202 if (strcmp16(block
.getElementName(&len
), String16("manifest").string()) != 0) {
203 fprintf(stderr
, "%s:%d: Invalid start tag %s, expected <manifest>\n",
204 file
->getPrintableSource().string(), block
.getLineNumber(),
205 String8(block
.getElementName(&len
)).string());
206 return UNKNOWN_ERROR
;
209 ssize_t nameIndex
= block
.indexOfAttribute(NULL
, "package");
211 fprintf(stderr
, "%s:%d: <manifest> does not have package attribute.\n",
212 file
->getPrintableSource().string(), block
.getLineNumber());
213 return UNKNOWN_ERROR
;
216 assets
->setPackage(String8(block
.getAttributeStringValue(nameIndex
, &len
)));
221 // ==========================================================================
222 // ==========================================================================
223 // ==========================================================================
225 static status_t
makeFileResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
226 ResourceTable
* table
,
227 const sp
<ResourceTypeSet
>& set
,
230 String8
type8(resType
);
231 String16
type16(resType
);
233 bool hasErrors
= false;
235 ResourceDirIterator
it(set
, String8(resType
));
237 while ((res
=it
.next()) == NO_ERROR
) {
238 if (bundle
->getVerbose()) {
239 printf(" (new resource id %s from %s)\n",
240 it
.getBaseName().string(), it
.getFile()->getPrintableSource().string());
242 String16
baseName(it
.getBaseName());
243 const char16_t* str
= baseName
.string();
244 const char16_t* const end
= str
+ baseName
.size();
246 if (!((*str
>= 'a' && *str
<= 'z')
247 || (*str
>= '0' && *str
<= '9')
248 || *str
== '_' || *str
== '.')) {
249 fprintf(stderr
, "%s: Invalid file name: must contain only [a-z0-9_.]\n",
250 it
.getPath().string());
255 String8 resPath
= it
.getPath();
256 resPath
.convertToResPath();
257 table
->addEntry(SourcePos(it
.getPath(), 0), String16(assets
->getPackage()),
263 assets
->addResource(it
.getLeafName(), resPath
, it
.getFile(), type8
);
266 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
269 static status_t
preProcessImages(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
270 const sp
<ResourceTypeSet
>& set
)
272 ResourceDirIterator
it(set
, String8("drawable"));
273 Vector
<sp
<AaptFile
> > newNameFiles
;
274 Vector
<String8
> newNamePaths
;
275 bool hasErrors
= false;
277 while ((res
=it
.next()) == NO_ERROR
) {
278 res
= preProcessImage(bundle
, assets
, it
.getFile(), NULL
);
279 if (res
< NO_ERROR
) {
284 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
287 status_t
postProcessImages(const sp
<AaptAssets
>& assets
,
288 ResourceTable
* table
,
289 const sp
<ResourceTypeSet
>& set
)
291 ResourceDirIterator
it(set
, String8("drawable"));
292 bool hasErrors
= false;
294 while ((res
=it
.next()) == NO_ERROR
) {
295 res
= postProcessImage(assets
, table
, it
.getFile());
296 if (res
< NO_ERROR
) {
301 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
304 static void collect_files(const sp
<AaptDir
>& dir
,
305 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
307 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& groups
= dir
->getFiles();
308 int N
= groups
.size();
309 for (int i
=0; i
<N
; i
++) {
310 String8 leafName
= groups
.keyAt(i
);
311 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
313 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
316 if (files
.size() == 0) {
320 String8 resType
= files
.valueAt(0)->getResourceType();
322 ssize_t index
= resources
->indexOfKey(resType
);
325 sp
<ResourceTypeSet
> set
= new ResourceTypeSet();
326 set
->add(leafName
, group
);
327 resources
->add(resType
, set
);
329 sp
<ResourceTypeSet
> set
= resources
->valueAt(index
);
330 index
= set
->indexOfKey(leafName
);
332 set
->add(leafName
, group
);
334 sp
<AaptGroup
> existingGroup
= set
->valueAt(index
);
335 int M
= files
.size();
336 for (int j
=0; j
<M
; j
++) {
337 existingGroup
->addFile(files
.valueAt(j
));
344 static void collect_files(const sp
<AaptAssets
>& ass
,
345 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
347 const Vector
<sp
<AaptDir
> >& dirs
= ass
->resDirs();
350 for (int i
=0; i
<N
; i
++) {
351 sp
<AaptDir
> d
= dirs
.itemAt(i
);
352 collect_files(d
, resources
);
354 // don't try to include the res dir
355 ass
->removeDir(d
->getLeaf());
362 ATTR_LEADING_SPACES
= -3,
363 ATTR_TRAILING_SPACES
= -4
365 static int validateAttr(const String8
& path
, const ResXMLParser
& parser
,
366 const char* ns
, const char* attr
, const char* validChars
, bool required
)
370 ssize_t index
= parser
.indexOfAttribute(ns
, attr
);
372 if (index
>= 0 && (str
=parser
.getAttributeStringValue(index
, &len
)) != NULL
) {
374 for (size_t i
=0; i
<len
; i
++) {
376 const char* p
= validChars
;
386 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n",
387 path
.string(), parser
.getLineNumber(),
388 String8(parser
.getElementName(&len
)).string(), attr
, (char)str
[i
]);
394 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not start with a space.\n",
395 path
.string(), parser
.getLineNumber(),
396 String8(parser
.getElementName(&len
)).string(), attr
);
397 return ATTR_LEADING_SPACES
;
399 if (str
[len
-1] == ' ') {
400 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not end with a space.\n",
401 path
.string(), parser
.getLineNumber(),
402 String8(parser
.getElementName(&len
)).string(), attr
);
403 return ATTR_TRAILING_SPACES
;
408 fprintf(stderr
, "%s:%d: Tag <%s> missing required attribute %s.\n",
409 path
.string(), parser
.getLineNumber(),
410 String8(parser
.getElementName(&len
)).string(), attr
);
411 return ATTR_NOT_FOUND
;
416 static void checkForIds(const String8
& path
, ResXMLParser
& parser
)
418 ResXMLTree::event_code_t code
;
419 while ((code
=parser
.next()) != ResXMLTree::END_DOCUMENT
420 && code
> ResXMLTree::BAD_DOCUMENT
) {
421 if (code
== ResXMLTree::START_TAG
) {
422 ssize_t index
= parser
.indexOfAttribute(NULL
, "id");
424 fprintf(stderr
, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n",
425 path
.string(), parser
.getLineNumber());
431 static bool applyFileOverlay(const sp
<AaptAssets
>& assets
,
432 const sp
<ResourceTypeSet
>& baseSet
,
435 // Replace any base level files in this category with any found from the overlay
436 // Also add any found only in the overlay.
437 sp
<AaptAssets
> overlay
= assets
->getOverlay();
438 String8
resTypeString(resType
);
440 // work through the linked list of overlays
441 while (overlay
.get()) {
442 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* overlayRes
= overlay
->getResources();
444 // get the overlay resources of the requested type
445 ssize_t index
= overlayRes
->indexOfKey(resTypeString
);
447 sp
<ResourceTypeSet
> overlaySet
= overlayRes
->valueAt(index
);
449 // for each of the resources, check for a match in the previously built
450 // non-overlay "baseset".
451 size_t overlayCount
= overlaySet
->size();
452 for (size_t overlayIndex
=0; overlayIndex
<overlayCount
; overlayIndex
++) {
453 size_t baseIndex
= baseSet
->indexOfKey(overlaySet
->keyAt(overlayIndex
));
454 if (baseIndex
< UNKNOWN_ERROR
) {
455 // look for same flavor. For a given file (strings.xml, for example)
456 // there may be a locale specific or other flavors - we want to match
458 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
459 sp
<AaptGroup
> baseGroup
= baseSet
->valueAt(baseIndex
);
461 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > baseFiles
=
462 baseGroup
->getFiles();
463 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
464 overlayGroup
->getFiles();
465 size_t overlayGroupSize
= overlayFiles
.size();
466 for (size_t overlayGroupIndex
= 0;
467 overlayGroupIndex
<overlayGroupSize
;
468 overlayGroupIndex
++) {
469 size_t baseFileIndex
=
470 baseFiles
.indexOfKey(overlayFiles
.keyAt(overlayGroupIndex
));
471 if(baseFileIndex
< UNKNOWN_ERROR
) {
472 baseGroup
->removeFile(baseFileIndex
);
474 // didn't find a match fall through and add it..
476 baseGroup
->addFile(overlayFiles
.valueAt(overlayGroupIndex
));
477 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
480 // this group doesn't exist (a file that's only in the overlay)
481 baseSet
->add(overlaySet
->keyAt(overlayIndex
),
482 overlaySet
->valueAt(overlayIndex
));
483 // make sure all flavors are defined in the resources.
484 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
485 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
486 overlayGroup
->getFiles();
487 size_t overlayGroupSize
= overlayFiles
.size();
488 for (size_t overlayGroupIndex
= 0;
489 overlayGroupIndex
<overlayGroupSize
;
490 overlayGroupIndex
++) {
491 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
495 // this overlay didn't have resources for this type
498 overlay
= overlay
->getOverlay();
503 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
,
504 const char* attr8
, const char* value
)
510 const String16
ns(ns8
);
511 const String16
attr(attr8
);
513 if (node
->getAttribute(ns
, attr
) != NULL
) {
514 fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s)\n",
515 String8(attr
).string(), String8(ns
).string());
519 node
->addAttribute(ns
, attr
, String16(value
));
522 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
524 root
= root
->searchElement(String16(), String16("manifest"));
526 fprintf(stderr
, "No <manifest> tag.\n");
527 return UNKNOWN_ERROR
;
530 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
531 bundle
->getVersionCode());
532 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
533 bundle
->getVersionName());
535 if (bundle
->getMinSdkVersion() != NULL
536 || bundle
->getTargetSdkVersion() != NULL
537 || bundle
->getMaxSdkVersion() != NULL
) {
538 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
540 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
541 root
->insertChildAt(vers
, 0);
544 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
545 bundle
->getMinSdkVersion());
546 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
547 bundle
->getTargetSdkVersion());
548 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
549 bundle
->getMaxSdkVersion());
555 #define ASSIGN_IT(n) \
557 ssize_t index = resources->indexOfKey(String8(#n)); \
559 n ## s = resources->valueAt(index); \
563 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
565 // First, look for a package file to parse. This is required to
566 // be able to generate the resource information.
567 sp
<AaptGroup
> androidManifestFile
=
568 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
569 if (androidManifestFile
== NULL
) {
570 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
571 return UNKNOWN_ERROR
;
574 status_t err
= parsePackage(assets
, androidManifestFile
);
575 if (err
!= NO_ERROR
) {
579 NOISY(printf("Creating resources for package %s\n",
580 assets
->getPackage().string()));
582 ResourceTable
table(bundle
, String16(assets
->getPackage()));
583 err
= table
.addIncludedResources(bundle
, assets
);
584 if (err
!= NO_ERROR
) {
588 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
590 // --------------------------------------------------------------
591 // First, gather all resource information.
592 // --------------------------------------------------------------
594 // resType -> leafName -> group
595 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
596 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
597 collect_files(assets
, resources
);
599 sp
<ResourceTypeSet
> drawables
;
600 sp
<ResourceTypeSet
> layouts
;
601 sp
<ResourceTypeSet
> anims
;
602 sp
<ResourceTypeSet
> xmls
;
603 sp
<ResourceTypeSet
> raws
;
604 sp
<ResourceTypeSet
> colors
;
605 sp
<ResourceTypeSet
> menus
;
615 assets
->setResources(resources
);
616 // now go through any resource overlays and collect their files
617 sp
<AaptAssets
> current
= assets
->getOverlay();
618 while(current
.get()) {
619 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
620 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
621 current
->setResources(resources
);
622 collect_files(current
, resources
);
623 current
= current
->getOverlay();
625 // apply the overlay files to the base set
626 if (!applyFileOverlay(assets
, drawables
, "drawable") ||
627 !applyFileOverlay(assets
, layouts
, "layout") ||
628 !applyFileOverlay(assets
, anims
, "anim") ||
629 !applyFileOverlay(assets
, xmls
, "xml") ||
630 !applyFileOverlay(assets
, raws
, "raw") ||
631 !applyFileOverlay(assets
, colors
, "color") ||
632 !applyFileOverlay(assets
, menus
, "menu")) {
633 return UNKNOWN_ERROR
;
636 bool hasErrors
= false;
638 if (drawables
!= NULL
) {
639 err
= preProcessImages(bundle
, assets
, drawables
);
640 if (err
== NO_ERROR
) {
641 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
642 if (err
!= NO_ERROR
) {
650 if (layouts
!= NULL
) {
651 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
652 if (err
!= NO_ERROR
) {
658 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
659 if (err
!= NO_ERROR
) {
665 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
666 if (err
!= NO_ERROR
) {
672 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
673 if (err
!= NO_ERROR
) {
680 while(current
.get()) {
681 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
682 current
->getResources();
684 ssize_t index
= resources
->indexOfKey(String8("values"));
686 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
688 while ((res
=it
.next()) == NO_ERROR
) {
689 sp
<AaptFile
> file
= it
.getFile();
690 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
691 (current
!=assets
), &table
);
692 if (res
!= NO_ERROR
) {
697 current
= current
->getOverlay();
700 if (colors
!= NULL
) {
701 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
702 if (err
!= NO_ERROR
) {
708 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
709 if (err
!= NO_ERROR
) {
714 // --------------------------------------------------------------------
715 // Assignment of resource IDs and initial generation of resource table.
716 // --------------------------------------------------------------------
718 if (table
.hasResources()) {
719 sp
<AaptFile
> resFile(getResourceFile(assets
));
720 if (resFile
== NULL
) {
721 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
722 return UNKNOWN_ERROR
;
725 err
= table
.assignResourceIds();
726 if (err
< NO_ERROR
) {
731 // --------------------------------------------------------------
732 // Finally, we can now we can compile XML files, which may reference
734 // --------------------------------------------------------------
736 if (layouts
!= NULL
) {
737 ResourceDirIterator
it(layouts
, String8("layout"));
738 while ((err
=it
.next()) == NO_ERROR
) {
739 String8 src
= it
.getFile()->getPrintableSource();
740 err
= compileXmlFile(assets
, it
.getFile(), &table
);
741 if (err
== NO_ERROR
) {
743 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
744 checkForIds(src
, block
);
750 if (err
< NO_ERROR
) {
757 ResourceDirIterator
it(anims
, String8("anim"));
758 while ((err
=it
.next()) == NO_ERROR
) {
759 err
= compileXmlFile(assets
, it
.getFile(), &table
);
760 if (err
!= NO_ERROR
) {
765 if (err
< NO_ERROR
) {
772 ResourceDirIterator
it(xmls
, String8("xml"));
773 while ((err
=it
.next()) == NO_ERROR
) {
774 err
= compileXmlFile(assets
, it
.getFile(), &table
);
775 if (err
!= NO_ERROR
) {
780 if (err
< NO_ERROR
) {
786 if (drawables
!= NULL
) {
787 err
= postProcessImages(assets
, &table
, drawables
);
788 if (err
!= NO_ERROR
) {
793 if (colors
!= NULL
) {
794 ResourceDirIterator
it(colors
, String8("color"));
795 while ((err
=it
.next()) == NO_ERROR
) {
796 err
= compileXmlFile(assets
, it
.getFile(), &table
);
797 if (err
!= NO_ERROR
) {
802 if (err
< NO_ERROR
) {
809 ResourceDirIterator
it(menus
, String8("menu"));
810 while ((err
=it
.next()) == NO_ERROR
) {
811 String8 src
= it
.getFile()->getPrintableSource();
812 err
= compileXmlFile(assets
, it
.getFile(), &table
);
813 if (err
!= NO_ERROR
) {
817 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
818 checkForIds(src
, block
);
821 if (err
< NO_ERROR
) {
827 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
828 String8
manifestPath(manifestFile
->getPrintableSource());
830 // Perform a basic validation of the manifest file. This time we
831 // parse it with the comments intact, so that we can use them to
832 // generate java docs... so we are not going to write this one
833 // back out to the final manifest data.
834 err
= compileXmlFile(assets
, manifestFile
, &table
,
835 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
836 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
837 if (err
< NO_ERROR
) {
841 block
.setTo(manifestFile
->getData(), manifestFile
->getSize(), true);
842 String16
manifest16("manifest");
843 String16
permission16("permission");
844 String16
permission_group16("permission-group");
845 String16
uses_permission16("uses-permission");
846 String16
instrumentation16("instrumentation");
847 String16
application16("application");
848 String16
provider16("provider");
849 String16
service16("service");
850 String16
receiver16("receiver");
851 String16
activity16("activity");
852 String16
action16("action");
853 String16
category16("category");
854 String16
data16("scheme");
855 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
856 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
857 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
858 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
859 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
860 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
861 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
862 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
863 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
864 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
865 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
866 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
867 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
868 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
869 ResXMLTree::event_code_t code
;
870 sp
<AaptSymbols
> permissionSymbols
;
871 sp
<AaptSymbols
> permissionGroupSymbols
;
872 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
873 && code
> ResXMLTree::BAD_DOCUMENT
) {
874 if (code
== ResXMLTree::START_TAG
) {
876 if (block
.getElementNamespace(&len
) != NULL
) {
879 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
880 if (validateAttr(manifestPath
, block
, NULL
, "package",
881 packageIdentChars
, true) != ATTR_OKAY
) {
884 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
885 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
886 const bool isGroup
= strcmp16(block
.getElementName(&len
),
887 permission_group16
.string()) == 0;
888 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
889 isGroup
? packageIdentCharsWithTheStupid
890 : packageIdentChars
, true) != ATTR_OKAY
) {
893 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
894 sp
<AaptSymbols
> syms
;
896 syms
= permissionSymbols
;
898 sp
<AaptSymbols
> symbols
=
899 assets
->getSymbolsFor(String8("Manifest"));
900 syms
= permissionSymbols
= symbols
->addNestedSymbol(
901 String8("permission"), srcPos
);
904 syms
= permissionGroupSymbols
;
906 sp
<AaptSymbols
> symbols
=
907 assets
->getSymbolsFor(String8("Manifest"));
908 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
909 String8("permission_group"), srcPos
);
913 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
914 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
916 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
917 manifestPath
.string(), block
.getLineNumber(),
918 String8(block
.getElementName(&len
)).string());
923 char* p
= idStr
.lockBuffer(idStr
.size());
924 char* e
= p
+ idStr
.size();
925 bool begins_with_digit
= true; // init to true so an empty string fails
928 if (*e
>= '0' && *e
<= '9') {
929 begins_with_digit
= true;
932 if ((*e
>= 'a' && *e
<= 'z') ||
933 (*e
>= 'A' && *e
<= 'Z') ||
935 begins_with_digit
= false;
938 if (isGroup
&& (*e
== '-')) {
940 begins_with_digit
= false;
946 idStr
.unlockBuffer();
947 // verify that we stopped because we hit a period or
948 // the beginning of the string, and that the
949 // identifier didn't begin with a digit.
950 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
952 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
953 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
956 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
957 const uint16_t* cmt
= block
.getComment(&len
);
958 if (cmt
!= NULL
&& *cmt
!= 0) {
959 //printf("Comment of %s: %s\n", String8(e).string(),
960 // String8(cmt).string());
961 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
963 //printf("No comment for %s\n", String8(e).string());
965 syms
->makeSymbolPublic(String8(e
), srcPos
);
966 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
967 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
968 packageIdentChars
, true) != ATTR_OKAY
) {
971 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
972 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
973 classIdentChars
, true) != ATTR_OKAY
) {
976 if (validateAttr(manifestPath
, block
,
977 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
978 packageIdentChars
, true) != ATTR_OKAY
) {
981 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
982 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
983 classIdentChars
, false) != ATTR_OKAY
) {
986 if (validateAttr(manifestPath
, block
,
987 RESOURCES_ANDROID_NAMESPACE
, "permission",
988 packageIdentChars
, false) != ATTR_OKAY
) {
991 if (validateAttr(manifestPath
, block
,
992 RESOURCES_ANDROID_NAMESPACE
, "process",
993 processIdentChars
, false) != ATTR_OKAY
) {
996 if (validateAttr(manifestPath
, block
,
997 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
998 processIdentChars
, false) != ATTR_OKAY
) {
1001 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
1002 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1003 classIdentChars
, true) != ATTR_OKAY
) {
1006 if (validateAttr(manifestPath
, block
,
1007 RESOURCES_ANDROID_NAMESPACE
, "authorities",
1008 authoritiesIdentChars
, true) != ATTR_OKAY
) {
1011 if (validateAttr(manifestPath
, block
,
1012 RESOURCES_ANDROID_NAMESPACE
, "permission",
1013 packageIdentChars
, false) != ATTR_OKAY
) {
1016 if (validateAttr(manifestPath
, block
,
1017 RESOURCES_ANDROID_NAMESPACE
, "process",
1018 processIdentChars
, false) != ATTR_OKAY
) {
1021 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1022 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1023 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1024 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1025 classIdentChars
, true) != ATTR_OKAY
) {
1028 if (validateAttr(manifestPath
, block
,
1029 RESOURCES_ANDROID_NAMESPACE
, "permission",
1030 packageIdentChars
, false) != ATTR_OKAY
) {
1033 if (validateAttr(manifestPath
, block
,
1034 RESOURCES_ANDROID_NAMESPACE
, "process",
1035 processIdentChars
, false) != ATTR_OKAY
) {
1038 if (validateAttr(manifestPath
, block
,
1039 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1040 processIdentChars
, false) != ATTR_OKAY
) {
1043 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1044 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1045 if (validateAttr(manifestPath
, block
,
1046 RESOURCES_ANDROID_NAMESPACE
, "name",
1047 packageIdentChars
, true) != ATTR_OKAY
) {
1050 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1051 if (validateAttr(manifestPath
, block
,
1052 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1053 typeIdentChars
, true) != ATTR_OKAY
) {
1056 if (validateAttr(manifestPath
, block
,
1057 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1058 schemeIdentChars
, true) != ATTR_OKAY
) {
1065 if (table
.validateLocalizations()) {
1070 return UNKNOWN_ERROR
;
1073 // Generate final compiled manifest file.
1074 manifestFile
->clearData();
1075 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1076 if (manifestTree
== NULL
) {
1077 return UNKNOWN_ERROR
;
1079 err
= massageManifest(bundle
, manifestTree
);
1080 if (err
< NO_ERROR
) {
1083 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1084 if (err
< NO_ERROR
) {
1089 //printXMLBlock(&block);
1091 // --------------------------------------------------------------
1092 // Generate the final resource table.
1093 // Re-flatten because we may have added new resource IDs
1094 // --------------------------------------------------------------
1096 if (table
.hasResources()) {
1097 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1098 err
= table
.addSymbols(symbols
);
1099 if (err
< NO_ERROR
) {
1103 sp
<AaptFile
> resFile(getResourceFile(assets
));
1104 if (resFile
== NULL
) {
1105 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1106 return UNKNOWN_ERROR
;
1109 err
= table
.flatten(bundle
, resFile
);
1110 if (err
< NO_ERROR
) {
1114 if (bundle
->getPublicOutputFile()) {
1115 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1117 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1118 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1119 return UNKNOWN_ERROR
;
1121 if (bundle
->getVerbose()) {
1122 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1124 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1130 rt
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1131 printf("Generated resources:\n");
1135 // These resources are now considered to be a part of the included
1136 // resources, for others to reference.
1137 err
= assets
->addIncludedResources(resFile
);
1138 if (err
< NO_ERROR
) {
1139 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1146 static const char* getIndentSpace(int indent
)
1148 static const char whitespace
[] =
1151 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1154 static status_t
fixupSymbol(String16
* inoutSymbol
)
1156 inoutSymbol
->replaceAll('.', '_');
1157 inoutSymbol
->replaceAll(':', '_');
1161 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1162 const String8
& name
,
1163 String16
* outTypeComment
= NULL
)
1165 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1167 //printf("Got R symbols!\n");
1168 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1170 //printf("Got attrs symbols! comment %s=%s\n",
1171 // name.string(), String8(asym->getComment(name)).string());
1172 if (outTypeComment
!= NULL
) {
1173 *outTypeComment
= asym
->getTypeComment(name
);
1175 return asym
->getComment(name
);
1181 static status_t
writeLayoutClasses(
1182 FILE* fp
, const sp
<AaptAssets
>& assets
,
1183 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1185 const char* indentStr
= getIndentSpace(indent
);
1186 if (!includePrivate
) {
1187 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1189 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1192 String16
attr16("attr");
1193 String16
package16(assets
->getPackage());
1195 indentStr
= getIndentSpace(indent
);
1196 bool hasErrors
= false;
1199 size_t N
= symbols
->getNestedSymbols().size();
1200 for (i
=0; i
<N
; i
++) {
1201 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1202 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1203 String8
realClassName(nclassName16
);
1204 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1207 String8
nclassName(nclassName16
);
1209 SortedVector
<uint32_t> idents
;
1210 Vector
<uint32_t> origOrder
;
1211 Vector
<bool> publicFlags
;
1214 size_t NA
= nsymbols
->getSymbols().size();
1215 for (a
=0; a
<NA
; a
++) {
1216 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1217 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1219 bool isPublic
= true;
1221 String16
name16(sym
.name
);
1222 uint32_t typeSpecFlags
;
1223 code
= assets
->getIncludedResources().identifierForName(
1224 name16
.string(), name16
.size(),
1225 attr16
.string(), attr16
.size(),
1226 package16
.string(), package16
.size(), &typeSpecFlags
);
1228 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1229 nclassName
.string(), sym
.name
.string());
1232 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1235 origOrder
.add(code
);
1236 publicFlags
.add(isPublic
);
1241 String16 comment
= symbols
->getComment(realClassName
);
1242 fprintf(fp
, "%s/** ", indentStr
);
1243 if (comment
.size() > 0) {
1244 fprintf(fp
, "%s\n", String8(comment
).string());
1246 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1248 bool hasTable
= false;
1249 for (a
=0; a
<NA
; a
++) {
1250 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1255 "%s <p>Includes the following attributes:</p>\n"
1256 "%s <table border=\"2\" width=\"85%%\" align=\"center\" frame=\"hsides\" rules=\"all\" cellpadding=\"5\">\n"
1257 "%s <colgroup align=\"left\" />\n"
1258 "%s <colgroup align=\"left\" />\n"
1259 "%s <tr><th>Attribute<th>Summary</tr>\n",
1266 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1267 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1270 String8
name8(sym
.name
);
1271 String16
comment(sym
.comment
);
1272 if (comment
.size() <= 0) {
1273 comment
= getAttributeComment(assets
, name8
);
1275 if (comment
.size() > 0) {
1276 const char16_t* p
= comment
.string();
1277 while (*p
!= 0 && *p
!= '.') {
1279 while (*p
!= 0 && *p
!= '}') {
1289 comment
= String16(comment
.string(), p
-comment
.string());
1291 String16
name(name8
);
1293 fprintf(fp
, "%s <tr><th><code>{@link #%s_%s %s:%s}</code><td>%s</tr>\n",
1294 indentStr
, nclassName
.string(),
1295 String8(name
).string(),
1296 assets
->getPackage().string(),
1297 String8(name
).string(),
1298 String8(comment
).string());
1302 fprintf(fp
, "%s </table>\n", indentStr
);
1304 for (a
=0; a
<NA
; a
++) {
1305 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1307 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1308 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1311 String16
name(sym
.name
);
1313 fprintf(fp
, "%s @see #%s_%s\n",
1314 indentStr
, nclassName
.string(),
1315 String8(name
).string());
1318 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1321 "%spublic static final int[] %s = {\n"
1323 indentStr
, nclassName
.string(),
1324 getIndentSpace(indent
+1));
1326 for (a
=0; a
<NA
; a
++) {
1329 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1334 fprintf(fp
, "0x%08x", idents
[a
]);
1337 fprintf(fp
, "\n%s};\n", indentStr
);
1339 for (a
=0; a
<NA
; a
++) {
1340 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1342 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1343 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1346 String8
name8(sym
.name
);
1347 String16
comment(sym
.comment
);
1348 String16 typeComment
;
1349 if (comment
.size() <= 0) {
1350 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1352 getAttributeComment(assets
, name8
, &typeComment
);
1354 String16
name(name8
);
1355 if (fixupSymbol(&name
) != NO_ERROR
) {
1359 uint32_t typeSpecFlags
= 0;
1360 String16
name16(sym
.name
);
1361 assets
->getIncludedResources().identifierForName(
1362 name16
.string(), name16
.size(),
1363 attr16
.string(), attr16
.size(),
1364 package16
.string(), package16
.size(), &typeSpecFlags
);
1365 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1366 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1367 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1369 fprintf(fp
, "%s/**\n", indentStr
);
1370 if (comment
.size() > 0) {
1371 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1372 fprintf(fp
, "%s %s\n", indentStr
, String8(comment
).string());
1375 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1376 "%s attribute's value can be found in the {@link #%s} array.\n",
1378 pub
? assets
->getPackage().string()
1379 : assets
->getSymbolsPrivatePackage().string(),
1380 String8(name
).string(),
1381 indentStr
, nclassName
.string());
1383 if (typeComment
.size() > 0) {
1384 fprintf(fp
, "\n\n%s %s\n", indentStr
, String8(typeComment
).string());
1386 if (comment
.size() > 0) {
1389 "%s <p>This corresponds to the global attribute"
1390 "%s resource symbol {@link %s.R.attr#%s}.\n",
1391 indentStr
, indentStr
,
1392 assets
->getPackage().string(),
1393 String8(name
).string());
1396 "%s <p>This is a private symbol.\n", indentStr
);
1399 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1400 "android", String8(name
).string());
1401 fprintf(fp
, "%s*/\n", indentStr
);
1403 "%spublic static final int %s_%s = %d;\n",
1404 indentStr
, nclassName
.string(),
1405 String8(name
).string(), (int)pos
);
1411 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1412 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1415 static status_t
writeSymbolClass(
1416 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1417 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
)
1419 fprintf(fp
, "%spublic %sfinal class %s {\n",
1420 getIndentSpace(indent
),
1421 indent
!= 0 ? "static " : "", className
.string());
1425 status_t err
= NO_ERROR
;
1427 size_t N
= symbols
->getSymbols().size();
1428 for (i
=0; i
<N
; i
++) {
1429 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1430 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1433 if (!includePrivate
&& !sym
.isPublic
) {
1436 String16
name(sym
.name
);
1437 String8
realName(name
);
1438 if (fixupSymbol(&name
) != NO_ERROR
) {
1439 return UNKNOWN_ERROR
;
1441 String16
comment(sym
.comment
);
1442 bool haveComment
= false;
1443 if (comment
.size() > 0) {
1447 getIndentSpace(indent
), String8(comment
).string());
1448 } else if (sym
.isPublic
&& !includePrivate
) {
1449 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1450 assets
->getPackage().string(), className
.string(),
1451 String8(sym
.name
).string());
1453 String16
typeComment(sym
.typeComment
);
1454 if (typeComment
.size() > 0) {
1459 getIndentSpace(indent
), String8(typeComment
).string());
1463 getIndentSpace(indent
), String8(typeComment
).string());
1467 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1469 fprintf(fp
, "%spublic static final int %s=0x%08x;\n",
1470 getIndentSpace(indent
),
1471 String8(name
).string(), (int)sym
.int32Val
);
1474 for (i
=0; i
<N
; i
++) {
1475 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1476 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1479 if (!includePrivate
&& !sym
.isPublic
) {
1482 String16
name(sym
.name
);
1483 if (fixupSymbol(&name
) != NO_ERROR
) {
1484 return UNKNOWN_ERROR
;
1486 String16
comment(sym
.comment
);
1487 if (comment
.size() > 0) {
1491 getIndentSpace(indent
), String8(comment
).string(),
1492 getIndentSpace(indent
));
1493 } else if (sym
.isPublic
&& !includePrivate
) {
1494 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1495 assets
->getPackage().string(), className
.string(),
1496 String8(sym
.name
).string());
1498 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1499 getIndentSpace(indent
),
1500 String8(name
).string(), sym
.stringVal
.string());
1503 sp
<AaptSymbols
> styleableSymbols
;
1505 N
= symbols
->getNestedSymbols().size();
1506 for (i
=0; i
<N
; i
++) {
1507 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1508 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1509 if (nclassName
== "styleable") {
1510 styleableSymbols
= nsymbols
;
1512 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
);
1514 if (err
!= NO_ERROR
) {
1519 if (styleableSymbols
!= NULL
) {
1520 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1521 if (err
!= NO_ERROR
) {
1527 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1531 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1532 const String8
& package
, bool includePrivate
)
1534 if (!bundle
->getRClassDir()) {
1538 const size_t N
= assets
->getSymbols().size();
1539 for (size_t i
=0; i
<N
; i
++) {
1540 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1541 String8
className(assets
->getSymbols().keyAt(i
));
1542 String8
dest(bundle
->getRClassDir());
1543 if (bundle
->getMakePackageDirs()) {
1544 String8
pkg(package
);
1545 const char* last
= pkg
.string();
1546 const char* s
= last
-1;
1549 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1550 String8
part(last
, s
-last
);
1551 dest
.appendPath(part
);
1552 #ifdef HAVE_MS_C_RUNTIME
1553 _mkdir(dest
.string());
1555 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1561 dest
.appendPath(className
);
1562 dest
.append(".java");
1563 FILE* fp
= fopen(dest
.string(), "w+");
1565 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1566 dest
.string(), strerror(errno
));
1567 return UNKNOWN_ERROR
;
1569 if (bundle
->getVerbose()) {
1570 printf(" Writing symbols for class %s.\n", className
.string());
1574 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1576 " * This class was automatically generated by the\n"
1577 " * aapt tool from the resource data it found. It\n"
1578 " * should not be modified by hand.\n"
1581 "package %s;\n\n", package
.string());
1583 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0);
1584 if (err
!= NO_ERROR
) {