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
;
276 while ((res
=it
.next()) == NO_ERROR
) {
277 res
= preProcessImage(bundle
, assets
, it
.getFile(), NULL
);
278 if (res
!= NO_ERROR
) {
286 status_t
postProcessImages(const sp
<AaptAssets
>& assets
,
287 ResourceTable
* table
,
288 const sp
<ResourceTypeSet
>& set
)
290 ResourceDirIterator
it(set
, String8("drawable"));
292 while ((res
=it
.next()) == NO_ERROR
) {
293 res
= postProcessImage(assets
, table
, it
.getFile());
294 if (res
!= NO_ERROR
) {
299 return res
< NO_ERROR
? res
: (status_t
)NO_ERROR
;
302 static void collect_files(const sp
<AaptDir
>& dir
,
303 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
305 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& groups
= dir
->getFiles();
306 int N
= groups
.size();
307 for (int i
=0; i
<N
; i
++) {
308 String8 leafName
= groups
.keyAt(i
);
309 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
311 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
314 if (files
.size() == 0) {
318 String8 resType
= files
.valueAt(0)->getResourceType();
320 ssize_t index
= resources
->indexOfKey(resType
);
323 sp
<ResourceTypeSet
> set
= new ResourceTypeSet();
324 set
->add(leafName
, group
);
325 resources
->add(resType
, set
);
327 sp
<ResourceTypeSet
> set
= resources
->valueAt(index
);
328 index
= set
->indexOfKey(leafName
);
330 set
->add(leafName
, group
);
332 sp
<AaptGroup
> existingGroup
= set
->valueAt(index
);
333 int M
= files
.size();
334 for (int j
=0; j
<M
; j
++) {
335 existingGroup
->addFile(files
.valueAt(j
));
342 static void collect_files(const sp
<AaptAssets
>& ass
,
343 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
345 const Vector
<sp
<AaptDir
> >& dirs
= ass
->resDirs();
348 for (int i
=0; i
<N
; i
++) {
349 sp
<AaptDir
> d
= dirs
.itemAt(i
);
350 collect_files(d
, resources
);
352 // don't try to include the res dir
353 ass
->removeDir(d
->getLeaf());
360 ATTR_LEADING_SPACES
= -3,
361 ATTR_TRAILING_SPACES
= -4
363 static int validateAttr(const String8
& path
, const ResXMLParser
& parser
,
364 const char* ns
, const char* attr
, const char* validChars
, bool required
)
368 ssize_t index
= parser
.indexOfAttribute(ns
, attr
);
370 if (index
>= 0 && (str
=parser
.getAttributeStringValue(index
, &len
)) != NULL
) {
372 for (size_t i
=0; i
<len
; i
++) {
374 const char* p
= validChars
;
384 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n",
385 path
.string(), parser
.getLineNumber(),
386 String8(parser
.getElementName(&len
)).string(), attr
, (char)str
[i
]);
392 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not start with a space.\n",
393 path
.string(), parser
.getLineNumber(),
394 String8(parser
.getElementName(&len
)).string(), attr
);
395 return ATTR_LEADING_SPACES
;
397 if (str
[len
-1] == ' ') {
398 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not end with a space.\n",
399 path
.string(), parser
.getLineNumber(),
400 String8(parser
.getElementName(&len
)).string(), attr
);
401 return ATTR_TRAILING_SPACES
;
406 fprintf(stderr
, "%s:%d: Tag <%s> missing required attribute %s.\n",
407 path
.string(), parser
.getLineNumber(),
408 String8(parser
.getElementName(&len
)).string(), attr
);
409 return ATTR_NOT_FOUND
;
414 static void checkForIds(const String8
& path
, ResXMLParser
& parser
)
416 ResXMLTree::event_code_t code
;
417 while ((code
=parser
.next()) != ResXMLTree::END_DOCUMENT
418 && code
> ResXMLTree::BAD_DOCUMENT
) {
419 if (code
== ResXMLTree::START_TAG
) {
420 ssize_t index
= parser
.indexOfAttribute(NULL
, "id");
422 fprintf(stderr
, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n",
423 path
.string(), parser
.getLineNumber());
429 static bool applyFileOverlay(const sp
<AaptAssets
>& assets
,
430 const sp
<ResourceTypeSet
>& baseSet
,
433 // Replace any base level files in this category with any found from the overlay
434 // Also add any found only in the overlay.
435 sp
<AaptAssets
> overlay
= assets
->getOverlay();
436 String8
resTypeString(resType
);
438 // work through the linked list of overlays
439 while (overlay
.get()) {
440 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* overlayRes
= overlay
->getResources();
442 // get the overlay resources of the requested type
443 ssize_t index
= overlayRes
->indexOfKey(resTypeString
);
445 sp
<ResourceTypeSet
> overlaySet
= overlayRes
->valueAt(index
);
447 // for each of the resources, check for a match in the previously built
448 // non-overlay "baseset".
449 size_t overlayCount
= overlaySet
->size();
450 for (size_t overlayIndex
=0; overlayIndex
<overlayCount
; overlayIndex
++) {
451 size_t baseIndex
= baseSet
->indexOfKey(overlaySet
->keyAt(overlayIndex
));
452 if (baseIndex
< UNKNOWN_ERROR
) {
453 // look for same flavor. For a given file (strings.xml, for example)
454 // there may be a locale specific or other flavors - we want to match
456 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
457 sp
<AaptGroup
> baseGroup
= baseSet
->valueAt(baseIndex
);
459 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > baseFiles
=
460 baseGroup
->getFiles();
461 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
462 overlayGroup
->getFiles();
463 size_t overlayGroupSize
= overlayFiles
.size();
464 for (size_t overlayGroupIndex
= 0;
465 overlayGroupIndex
<overlayGroupSize
;
466 overlayGroupIndex
++) {
467 size_t baseFileIndex
=
468 baseFiles
.indexOfKey(overlayFiles
.keyAt(overlayGroupIndex
));
469 if(baseFileIndex
< UNKNOWN_ERROR
) {
470 baseGroup
->removeFile(baseFileIndex
);
472 // didn't find a match fall through and add it..
474 baseGroup
->addFile(overlayFiles
.valueAt(overlayGroupIndex
));
477 // this group doesn't exist (a file that's only in the overlay)
478 fprintf(stderr
, "aapt: error: "
479 "*** Resource file '%s' exists only in an overlay\n",
480 overlaySet
->keyAt(overlayIndex
).string());
484 // this overlay didn't have resources for this type
487 overlay
= overlay
->getOverlay();
492 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
,
493 const char* attr8
, const char* value
)
499 const String16
ns(ns8
);
500 const String16
attr(attr8
);
502 if (node
->getAttribute(ns
, attr
) != NULL
) {
503 fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s)\n",
504 String8(attr
).string(), String8(ns
).string());
508 node
->addAttribute(ns
, attr
, String16(value
));
511 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
513 root
= root
->searchElement(String16(), String16("manifest"));
515 fprintf(stderr
, "No <manifest> tag.\n");
516 return UNKNOWN_ERROR
;
519 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
520 bundle
->getVersionCode());
521 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
522 bundle
->getVersionName());
524 if (bundle
->getMinSdkVersion() != NULL
525 || bundle
->getTargetSdkVersion() != NULL
526 || bundle
->getMaxSdkVersion() != NULL
) {
527 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
529 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
530 root
->insertChildAt(vers
, 0);
533 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
534 bundle
->getMinSdkVersion());
535 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
536 bundle
->getTargetSdkVersion());
537 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
538 bundle
->getMaxSdkVersion());
544 #define ASSIGN_IT(n) \
546 ssize_t index = resources->indexOfKey(String8(#n)); \
548 n ## s = resources->valueAt(index); \
552 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
554 // First, look for a package file to parse. This is required to
555 // be able to generate the resource information.
556 sp
<AaptGroup
> androidManifestFile
=
557 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
558 if (androidManifestFile
== NULL
) {
559 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
560 return UNKNOWN_ERROR
;
563 status_t err
= parsePackage(assets
, androidManifestFile
);
564 if (err
!= NO_ERROR
) {
568 NOISY(printf("Creating resources for package %s\n",
569 assets
->getPackage().string()));
571 ResourceTable
table(bundle
, String16(assets
->getPackage()));
572 err
= table
.addIncludedResources(bundle
, assets
);
573 if (err
!= NO_ERROR
) {
577 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
579 // --------------------------------------------------------------
580 // First, gather all resource information.
581 // --------------------------------------------------------------
583 // resType -> leafName -> group
584 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
585 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
586 collect_files(assets
, resources
);
588 sp
<ResourceTypeSet
> drawables
;
589 sp
<ResourceTypeSet
> layouts
;
590 sp
<ResourceTypeSet
> anims
;
591 sp
<ResourceTypeSet
> xmls
;
592 sp
<ResourceTypeSet
> raws
;
593 sp
<ResourceTypeSet
> colors
;
594 sp
<ResourceTypeSet
> menus
;
604 assets
->setResources(resources
);
605 // now go through any resource overlays and collect their files
606 sp
<AaptAssets
> current
= assets
->getOverlay();
607 while(current
.get()) {
608 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
609 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
610 current
->setResources(resources
);
611 collect_files(current
, resources
);
612 current
= current
->getOverlay();
614 // apply the overlay files to the base set
615 if (!applyFileOverlay(assets
, drawables
, "drawable") ||
616 !applyFileOverlay(assets
, layouts
, "layout") ||
617 !applyFileOverlay(assets
, anims
, "anim") ||
618 !applyFileOverlay(assets
, xmls
, "xml") ||
619 !applyFileOverlay(assets
, raws
, "raw") ||
620 !applyFileOverlay(assets
, colors
, "color") ||
621 !applyFileOverlay(assets
, menus
, "menu")) {
622 return UNKNOWN_ERROR
;
625 bool hasErrors
= false;
627 if (drawables
!= NULL
) {
628 err
= preProcessImages(bundle
, assets
, drawables
);
629 if (err
== NO_ERROR
) {
630 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
631 if (err
!= NO_ERROR
) {
639 if (layouts
!= NULL
) {
640 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
641 if (err
!= NO_ERROR
) {
647 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
648 if (err
!= NO_ERROR
) {
654 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
655 if (err
!= NO_ERROR
) {
661 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
662 if (err
!= NO_ERROR
) {
669 while(current
.get()) {
670 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
671 current
->getResources();
673 ssize_t index
= resources
->indexOfKey(String8("values"));
675 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
677 while ((res
=it
.next()) == NO_ERROR
) {
678 sp
<AaptFile
> file
= it
.getFile();
679 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
680 (current
!=assets
), &table
);
681 if (res
!= NO_ERROR
) {
686 current
= current
->getOverlay();
689 if (colors
!= NULL
) {
690 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
691 if (err
!= NO_ERROR
) {
697 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
698 if (err
!= NO_ERROR
) {
703 // --------------------------------------------------------------------
704 // Assignment of resource IDs and initial generation of resource table.
705 // --------------------------------------------------------------------
707 if (table
.hasResources()) {
708 sp
<AaptFile
> resFile(getResourceFile(assets
));
709 if (resFile
== NULL
) {
710 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
711 return UNKNOWN_ERROR
;
714 err
= table
.assignResourceIds();
715 if (err
< NO_ERROR
) {
720 // --------------------------------------------------------------
721 // Finally, we can now we can compile XML files, which may reference
723 // --------------------------------------------------------------
725 if (layouts
!= NULL
) {
726 ResourceDirIterator
it(layouts
, String8("layout"));
727 while ((err
=it
.next()) == NO_ERROR
) {
728 String8 src
= it
.getFile()->getPrintableSource();
729 err
= compileXmlFile(assets
, it
.getFile(), &table
);
730 if (err
== NO_ERROR
) {
732 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
733 checkForIds(src
, block
);
739 if (err
< NO_ERROR
) {
746 ResourceDirIterator
it(anims
, String8("anim"));
747 while ((err
=it
.next()) == NO_ERROR
) {
748 err
= compileXmlFile(assets
, it
.getFile(), &table
);
749 if (err
!= NO_ERROR
) {
754 if (err
< NO_ERROR
) {
761 ResourceDirIterator
it(xmls
, String8("xml"));
762 while ((err
=it
.next()) == NO_ERROR
) {
763 err
= compileXmlFile(assets
, it
.getFile(), &table
);
764 if (err
!= NO_ERROR
) {
769 if (err
< NO_ERROR
) {
775 if (drawables
!= NULL
) {
776 err
= postProcessImages(assets
, &table
, drawables
);
777 if (err
!= NO_ERROR
) {
782 if (colors
!= NULL
) {
783 ResourceDirIterator
it(colors
, String8("color"));
784 while ((err
=it
.next()) == NO_ERROR
) {
785 err
= compileXmlFile(assets
, it
.getFile(), &table
);
786 if (err
!= NO_ERROR
) {
791 if (err
< NO_ERROR
) {
798 ResourceDirIterator
it(menus
, String8("menu"));
799 while ((err
=it
.next()) == NO_ERROR
) {
800 String8 src
= it
.getFile()->getPrintableSource();
801 err
= compileXmlFile(assets
, it
.getFile(), &table
);
802 if (err
!= NO_ERROR
) {
806 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
807 checkForIds(src
, block
);
810 if (err
< NO_ERROR
) {
816 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
817 String8
manifestPath(manifestFile
->getPrintableSource());
819 // Perform a basic validation of the manifest file. This time we
820 // parse it with the comments intact, so that we can use them to
821 // generate java docs... so we are not going to write this one
822 // back out to the final manifest data.
823 err
= compileXmlFile(assets
, manifestFile
, &table
,
824 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
825 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
826 if (err
< NO_ERROR
) {
830 block
.setTo(manifestFile
->getData(), manifestFile
->getSize(), true);
831 String16
manifest16("manifest");
832 String16
permission16("permission");
833 String16
permission_group16("permission-group");
834 String16
uses_permission16("uses-permission");
835 String16
instrumentation16("instrumentation");
836 String16
application16("application");
837 String16
provider16("provider");
838 String16
service16("service");
839 String16
receiver16("receiver");
840 String16
activity16("activity");
841 String16
action16("action");
842 String16
category16("category");
843 String16
data16("scheme");
844 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
845 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
846 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
847 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
848 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
849 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
850 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
851 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
852 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
853 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
854 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
855 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
856 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
857 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
858 ResXMLTree::event_code_t code
;
859 sp
<AaptSymbols
> permissionSymbols
;
860 sp
<AaptSymbols
> permissionGroupSymbols
;
861 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
862 && code
> ResXMLTree::BAD_DOCUMENT
) {
863 if (code
== ResXMLTree::START_TAG
) {
865 if (block
.getElementNamespace(&len
) != NULL
) {
868 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
869 if (validateAttr(manifestPath
, block
, NULL
, "package",
870 packageIdentChars
, true) != ATTR_OKAY
) {
873 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
874 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
875 const bool isGroup
= strcmp16(block
.getElementName(&len
),
876 permission_group16
.string()) == 0;
877 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
878 isGroup
? packageIdentCharsWithTheStupid
879 : packageIdentChars
, true) != ATTR_OKAY
) {
882 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
883 sp
<AaptSymbols
> syms
;
885 syms
= permissionSymbols
;
887 sp
<AaptSymbols
> symbols
=
888 assets
->getSymbolsFor(String8("Manifest"));
889 syms
= permissionSymbols
= symbols
->addNestedSymbol(
890 String8("permission"), srcPos
);
893 syms
= permissionGroupSymbols
;
895 sp
<AaptSymbols
> symbols
=
896 assets
->getSymbolsFor(String8("Manifest"));
897 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
898 String8("permission_group"), srcPos
);
902 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
903 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
905 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
906 manifestPath
.string(), block
.getLineNumber(),
907 String8(block
.getElementName(&len
)).string());
912 char* p
= idStr
.lockBuffer(idStr
.size());
913 char* e
= p
+ idStr
.size();
914 bool begins_with_digit
= true; // init to true so an empty string fails
917 if (*e
>= '0' && *e
<= '9') {
918 begins_with_digit
= true;
921 if ((*e
>= 'a' && *e
<= 'z') ||
922 (*e
>= 'A' && *e
<= 'Z') ||
924 begins_with_digit
= false;
927 if (isGroup
&& (*e
== '-')) {
929 begins_with_digit
= false;
935 idStr
.unlockBuffer();
936 // verify that we stopped because we hit a period or
937 // the beginning of the string, and that the
938 // identifier didn't begin with a digit.
939 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
941 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
942 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
945 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
946 const uint16_t* cmt
= block
.getComment(&len
);
947 if (cmt
!= NULL
&& *cmt
!= 0) {
948 //printf("Comment of %s: %s\n", String8(e).string(),
949 // String8(cmt).string());
950 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
952 //printf("No comment for %s\n", String8(e).string());
954 syms
->makeSymbolPublic(String8(e
), srcPos
);
955 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
956 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
957 packageIdentChars
, true) != ATTR_OKAY
) {
960 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
961 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
962 classIdentChars
, true) != ATTR_OKAY
) {
965 if (validateAttr(manifestPath
, block
,
966 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
967 packageIdentChars
, true) != ATTR_OKAY
) {
970 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
971 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
972 classIdentChars
, false) != ATTR_OKAY
) {
975 if (validateAttr(manifestPath
, block
,
976 RESOURCES_ANDROID_NAMESPACE
, "permission",
977 packageIdentChars
, false) != ATTR_OKAY
) {
980 if (validateAttr(manifestPath
, block
,
981 RESOURCES_ANDROID_NAMESPACE
, "process",
982 processIdentChars
, false) != ATTR_OKAY
) {
985 if (validateAttr(manifestPath
, block
,
986 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
987 processIdentChars
, false) != ATTR_OKAY
) {
990 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
991 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
992 classIdentChars
, true) != ATTR_OKAY
) {
995 if (validateAttr(manifestPath
, block
,
996 RESOURCES_ANDROID_NAMESPACE
, "authorities",
997 authoritiesIdentChars
, true) != ATTR_OKAY
) {
1000 if (validateAttr(manifestPath
, block
,
1001 RESOURCES_ANDROID_NAMESPACE
, "permission",
1002 packageIdentChars
, false) != ATTR_OKAY
) {
1005 if (validateAttr(manifestPath
, block
,
1006 RESOURCES_ANDROID_NAMESPACE
, "process",
1007 processIdentChars
, false) != ATTR_OKAY
) {
1010 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1011 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1012 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1013 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1014 classIdentChars
, true) != ATTR_OKAY
) {
1017 if (validateAttr(manifestPath
, block
,
1018 RESOURCES_ANDROID_NAMESPACE
, "permission",
1019 packageIdentChars
, false) != ATTR_OKAY
) {
1022 if (validateAttr(manifestPath
, block
,
1023 RESOURCES_ANDROID_NAMESPACE
, "process",
1024 processIdentChars
, false) != ATTR_OKAY
) {
1027 if (validateAttr(manifestPath
, block
,
1028 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1029 processIdentChars
, false) != ATTR_OKAY
) {
1032 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1033 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1034 if (validateAttr(manifestPath
, block
,
1035 RESOURCES_ANDROID_NAMESPACE
, "name",
1036 packageIdentChars
, true) != ATTR_OKAY
) {
1039 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1040 if (validateAttr(manifestPath
, block
,
1041 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1042 typeIdentChars
, true) != ATTR_OKAY
) {
1045 if (validateAttr(manifestPath
, block
,
1046 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1047 schemeIdentChars
, true) != ATTR_OKAY
) {
1054 if (table
.validateLocalizations()) {
1059 return UNKNOWN_ERROR
;
1062 // Generate final compiled manifest file.
1063 manifestFile
->clearData();
1064 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1065 if (manifestTree
== NULL
) {
1066 return UNKNOWN_ERROR
;
1068 err
= massageManifest(bundle
, manifestTree
);
1069 if (err
< NO_ERROR
) {
1072 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1073 if (err
< NO_ERROR
) {
1078 //printXMLBlock(&block);
1080 // --------------------------------------------------------------
1081 // Generate the final resource table.
1082 // Re-flatten because we may have added new resource IDs
1083 // --------------------------------------------------------------
1085 if (table
.hasResources()) {
1086 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1087 err
= table
.addSymbols(symbols
);
1088 if (err
< NO_ERROR
) {
1092 sp
<AaptFile
> resFile(getResourceFile(assets
));
1093 if (resFile
== NULL
) {
1094 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1095 return UNKNOWN_ERROR
;
1098 err
= table
.flatten(bundle
, resFile
);
1099 if (err
< NO_ERROR
) {
1103 if (bundle
->getPublicOutputFile()) {
1104 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1106 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1107 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1108 return UNKNOWN_ERROR
;
1110 if (bundle
->getVerbose()) {
1111 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1113 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1119 rt
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1120 printf("Generated resources:\n");
1124 // These resources are now considered to be a part of the included
1125 // resources, for others to reference.
1126 err
= assets
->addIncludedResources(resFile
);
1127 if (err
< NO_ERROR
) {
1128 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1135 static const char* getIndentSpace(int indent
)
1137 static const char whitespace
[] =
1140 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1143 static status_t
fixupSymbol(String16
* inoutSymbol
)
1145 inoutSymbol
->replaceAll('.', '_');
1146 inoutSymbol
->replaceAll(':', '_');
1150 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1151 const String8
& name
,
1152 String16
* outTypeComment
= NULL
)
1154 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1156 //printf("Got R symbols!\n");
1157 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1159 //printf("Got attrs symbols! comment %s=%s\n",
1160 // name.string(), String8(asym->getComment(name)).string());
1161 if (outTypeComment
!= NULL
) {
1162 *outTypeComment
= asym
->getTypeComment(name
);
1164 return asym
->getComment(name
);
1170 static status_t
writeLayoutClasses(
1171 FILE* fp
, const sp
<AaptAssets
>& assets
,
1172 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1174 const char* indentStr
= getIndentSpace(indent
);
1175 if (!includePrivate
) {
1176 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1178 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1181 String16
attr16("attr");
1182 String16
package16(assets
->getPackage());
1184 indentStr
= getIndentSpace(indent
);
1185 bool hasErrors
= false;
1188 size_t N
= symbols
->getNestedSymbols().size();
1189 for (i
=0; i
<N
; i
++) {
1190 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1191 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1192 String8
realClassName(nclassName16
);
1193 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1196 String8
nclassName(nclassName16
);
1198 SortedVector
<uint32_t> idents
;
1199 Vector
<uint32_t> origOrder
;
1200 Vector
<bool> publicFlags
;
1203 size_t NA
= nsymbols
->getSymbols().size();
1204 for (a
=0; a
<NA
; a
++) {
1205 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1206 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1208 bool isPublic
= true;
1210 String16
name16(sym
.name
);
1211 uint32_t typeSpecFlags
;
1212 code
= assets
->getIncludedResources().identifierForName(
1213 name16
.string(), name16
.size(),
1214 attr16
.string(), attr16
.size(),
1215 package16
.string(), package16
.size(), &typeSpecFlags
);
1217 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1218 nclassName
.string(), sym
.name
.string());
1221 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1224 origOrder
.add(code
);
1225 publicFlags
.add(isPublic
);
1230 String16 comment
= symbols
->getComment(realClassName
);
1231 fprintf(fp
, "%s/** ", indentStr
);
1232 if (comment
.size() > 0) {
1233 fprintf(fp
, "%s\n", String8(comment
).string());
1235 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1237 bool hasTable
= false;
1238 for (a
=0; a
<NA
; a
++) {
1239 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1244 "%s <p>Includes the following attributes:</p>\n"
1245 "%s <table border=\"2\" width=\"85%%\" align=\"center\" frame=\"hsides\" rules=\"all\" cellpadding=\"5\">\n"
1246 "%s <colgroup align=\"left\" />\n"
1247 "%s <colgroup align=\"left\" />\n"
1248 "%s <tr><th>Attribute<th>Summary</tr>\n",
1255 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1256 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1259 String8
name8(sym
.name
);
1260 String16
comment(sym
.comment
);
1261 if (comment
.size() <= 0) {
1262 comment
= getAttributeComment(assets
, name8
);
1264 if (comment
.size() > 0) {
1265 const char16_t* p
= comment
.string();
1266 while (*p
!= 0 && *p
!= '.') {
1268 while (*p
!= 0 && *p
!= '}') {
1278 comment
= String16(comment
.string(), p
-comment
.string());
1280 String16
name(name8
);
1282 fprintf(fp
, "%s <tr><th><code>{@link #%s_%s %s:%s}</code><td>%s</tr>\n",
1283 indentStr
, nclassName
.string(),
1284 String8(name
).string(),
1285 assets
->getPackage().string(),
1286 String8(name
).string(),
1287 String8(comment
).string());
1291 fprintf(fp
, "%s </table>\n", indentStr
);
1293 for (a
=0; a
<NA
; a
++) {
1294 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1296 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1297 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1300 String16
name(sym
.name
);
1302 fprintf(fp
, "%s @see #%s_%s\n",
1303 indentStr
, nclassName
.string(),
1304 String8(name
).string());
1307 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1310 "%spublic static final int[] %s = {\n"
1312 indentStr
, nclassName
.string(),
1313 getIndentSpace(indent
+1));
1315 for (a
=0; a
<NA
; a
++) {
1318 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1323 fprintf(fp
, "0x%08x", idents
[a
]);
1326 fprintf(fp
, "\n%s};\n", indentStr
);
1328 for (a
=0; a
<NA
; a
++) {
1329 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1331 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1332 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1335 String8
name8(sym
.name
);
1336 String16
comment(sym
.comment
);
1337 String16 typeComment
;
1338 if (comment
.size() <= 0) {
1339 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1341 getAttributeComment(assets
, name8
, &typeComment
);
1343 String16
name(name8
);
1344 if (fixupSymbol(&name
) != NO_ERROR
) {
1348 uint32_t typeSpecFlags
= 0;
1349 String16
name16(sym
.name
);
1350 assets
->getIncludedResources().identifierForName(
1351 name16
.string(), name16
.size(),
1352 attr16
.string(), attr16
.size(),
1353 package16
.string(), package16
.size(), &typeSpecFlags
);
1354 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1355 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1356 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1358 fprintf(fp
, "%s/**\n", indentStr
);
1359 if (comment
.size() > 0) {
1360 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1361 fprintf(fp
, "%s %s\n", indentStr
, String8(comment
).string());
1364 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1365 "%s attribute's value can be found in the {@link #%s} array.\n",
1367 pub
? assets
->getPackage().string()
1368 : assets
->getSymbolsPrivatePackage().string(),
1369 String8(name
).string(),
1370 indentStr
, nclassName
.string());
1372 if (typeComment
.size() > 0) {
1373 fprintf(fp
, "\n\n%s %s\n", indentStr
, String8(typeComment
).string());
1375 if (comment
.size() > 0) {
1378 "%s <p>This corresponds to the global attribute"
1379 "%s resource symbol {@link %s.R.attr#%s}.\n",
1380 indentStr
, indentStr
,
1381 assets
->getPackage().string(),
1382 String8(name
).string());
1385 "%s <p>This is a private symbol.\n", indentStr
);
1388 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1389 "android", String8(name
).string());
1390 fprintf(fp
, "%s*/\n", indentStr
);
1392 "%spublic static final int %s_%s = %d;\n",
1393 indentStr
, nclassName
.string(),
1394 String8(name
).string(), (int)pos
);
1400 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1401 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1404 static status_t
writeSymbolClass(
1405 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1406 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
)
1408 fprintf(fp
, "%spublic %sfinal class %s {\n",
1409 getIndentSpace(indent
),
1410 indent
!= 0 ? "static " : "", className
.string());
1414 status_t err
= NO_ERROR
;
1416 size_t N
= symbols
->getSymbols().size();
1417 for (i
=0; i
<N
; i
++) {
1418 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1419 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1422 if (!includePrivate
&& !sym
.isPublic
) {
1425 String16
name(sym
.name
);
1426 String8
realName(name
);
1427 if (fixupSymbol(&name
) != NO_ERROR
) {
1428 return UNKNOWN_ERROR
;
1430 String16
comment(sym
.comment
);
1431 bool haveComment
= false;
1432 if (comment
.size() > 0) {
1436 getIndentSpace(indent
), String8(comment
).string());
1437 } else if (sym
.isPublic
&& !includePrivate
) {
1438 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1439 assets
->getPackage().string(), className
.string(),
1440 String8(sym
.name
).string());
1442 String16
typeComment(sym
.typeComment
);
1443 if (typeComment
.size() > 0) {
1448 getIndentSpace(indent
), String8(typeComment
).string());
1452 getIndentSpace(indent
), String8(typeComment
).string());
1456 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1458 fprintf(fp
, "%spublic static final int %s=0x%08x;\n",
1459 getIndentSpace(indent
),
1460 String8(name
).string(), (int)sym
.int32Val
);
1463 for (i
=0; i
<N
; i
++) {
1464 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1465 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1468 if (!includePrivate
&& !sym
.isPublic
) {
1471 String16
name(sym
.name
);
1472 if (fixupSymbol(&name
) != NO_ERROR
) {
1473 return UNKNOWN_ERROR
;
1475 String16
comment(sym
.comment
);
1476 if (comment
.size() > 0) {
1480 getIndentSpace(indent
), String8(comment
).string(),
1481 getIndentSpace(indent
));
1482 } else if (sym
.isPublic
&& !includePrivate
) {
1483 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1484 assets
->getPackage().string(), className
.string(),
1485 String8(sym
.name
).string());
1487 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1488 getIndentSpace(indent
),
1489 String8(name
).string(), sym
.stringVal
.string());
1492 sp
<AaptSymbols
> styleableSymbols
;
1494 N
= symbols
->getNestedSymbols().size();
1495 for (i
=0; i
<N
; i
++) {
1496 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1497 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1498 if (nclassName
== "styleable") {
1499 styleableSymbols
= nsymbols
;
1501 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
);
1503 if (err
!= NO_ERROR
) {
1508 if (styleableSymbols
!= NULL
) {
1509 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1510 if (err
!= NO_ERROR
) {
1516 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1520 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1521 const String8
& package
, bool includePrivate
)
1523 if (!bundle
->getRClassDir()) {
1527 const size_t N
= assets
->getSymbols().size();
1528 for (size_t i
=0; i
<N
; i
++) {
1529 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1530 String8
className(assets
->getSymbols().keyAt(i
));
1531 String8
dest(bundle
->getRClassDir());
1532 if (bundle
->getMakePackageDirs()) {
1533 String8
pkg(package
);
1534 const char* last
= pkg
.string();
1535 const char* s
= last
-1;
1538 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1539 String8
part(last
, s
-last
);
1540 dest
.appendPath(part
);
1541 #ifdef HAVE_MS_C_RUNTIME
1542 _mkdir(dest
.string());
1544 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1550 dest
.appendPath(className
);
1551 dest
.append(".java");
1552 FILE* fp
= fopen(dest
.string(), "w+");
1554 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1555 dest
.string(), strerror(errno
));
1556 return UNKNOWN_ERROR
;
1558 if (bundle
->getVerbose()) {
1559 printf(" Writing symbols for class %s.\n", className
.string());
1563 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1565 " * This class was automatically generated by the\n"
1566 " * aapt tool from the resource data it found. It\n"
1567 " * should not be modified by hand.\n"
1570 "package %s;\n\n", package
.string());
1572 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0);
1573 if (err
!= NO_ERROR
) {