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 baseSet
->add(overlaySet
->keyAt(overlayIndex
),
479 overlaySet
->valueAt(overlayIndex
));
482 // this overlay didn't have resources for this type
485 overlay
= overlay
->getOverlay();
490 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
,
491 const char* attr8
, const char* value
)
497 const String16
ns(ns8
);
498 const String16
attr(attr8
);
500 if (node
->getAttribute(ns
, attr
) != NULL
) {
501 fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s)\n",
502 String8(attr
).string(), String8(ns
).string());
506 node
->addAttribute(ns
, attr
, String16(value
));
509 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
511 root
= root
->searchElement(String16(), String16("manifest"));
513 fprintf(stderr
, "No <manifest> tag.\n");
514 return UNKNOWN_ERROR
;
517 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
518 bundle
->getVersionCode());
519 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
520 bundle
->getVersionName());
522 if (bundle
->getMinSdkVersion() != NULL
523 || bundle
->getTargetSdkVersion() != NULL
524 || bundle
->getMaxSdkVersion() != NULL
) {
525 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
527 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
528 root
->insertChildAt(vers
, 0);
531 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
532 bundle
->getMinSdkVersion());
533 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
534 bundle
->getTargetSdkVersion());
535 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
536 bundle
->getMaxSdkVersion());
542 #define ASSIGN_IT(n) \
544 ssize_t index = resources->indexOfKey(String8(#n)); \
546 n ## s = resources->valueAt(index); \
550 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
552 // First, look for a package file to parse. This is required to
553 // be able to generate the resource information.
554 sp
<AaptGroup
> androidManifestFile
=
555 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
556 if (androidManifestFile
== NULL
) {
557 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
558 return UNKNOWN_ERROR
;
561 status_t err
= parsePackage(assets
, androidManifestFile
);
562 if (err
!= NO_ERROR
) {
566 NOISY(printf("Creating resources for package %s\n",
567 assets
->getPackage().string()));
569 ResourceTable
table(bundle
, String16(assets
->getPackage()));
570 err
= table
.addIncludedResources(bundle
, assets
);
571 if (err
!= NO_ERROR
) {
575 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
577 // --------------------------------------------------------------
578 // First, gather all resource information.
579 // --------------------------------------------------------------
581 // resType -> leafName -> group
582 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
583 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
584 collect_files(assets
, resources
);
586 sp
<ResourceTypeSet
> drawables
;
587 sp
<ResourceTypeSet
> layouts
;
588 sp
<ResourceTypeSet
> anims
;
589 sp
<ResourceTypeSet
> xmls
;
590 sp
<ResourceTypeSet
> raws
;
591 sp
<ResourceTypeSet
> colors
;
592 sp
<ResourceTypeSet
> menus
;
602 assets
->setResources(resources
);
603 // now go through any resource overlays and collect their files
604 sp
<AaptAssets
> current
= assets
->getOverlay();
605 while(current
.get()) {
606 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
607 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
608 current
->setResources(resources
);
609 collect_files(current
, resources
);
610 current
= current
->getOverlay();
612 // apply the overlay files to the base set
613 if (!applyFileOverlay(assets
, drawables
, "drawable") ||
614 !applyFileOverlay(assets
, layouts
, "layout") ||
615 !applyFileOverlay(assets
, anims
, "anim") ||
616 !applyFileOverlay(assets
, xmls
, "xml") ||
617 !applyFileOverlay(assets
, raws
, "raw") ||
618 !applyFileOverlay(assets
, colors
, "color") ||
619 !applyFileOverlay(assets
, menus
, "menu")) {
620 return UNKNOWN_ERROR
;
623 bool hasErrors
= false;
625 if (drawables
!= NULL
) {
626 err
= preProcessImages(bundle
, assets
, drawables
);
627 if (err
== NO_ERROR
) {
628 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
629 if (err
!= NO_ERROR
) {
637 if (layouts
!= NULL
) {
638 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
639 if (err
!= NO_ERROR
) {
645 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
646 if (err
!= NO_ERROR
) {
652 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
653 if (err
!= NO_ERROR
) {
659 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
660 if (err
!= NO_ERROR
) {
667 while(current
.get()) {
668 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
669 current
->getResources();
671 ssize_t index
= resources
->indexOfKey(String8("values"));
673 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
675 while ((res
=it
.next()) == NO_ERROR
) {
676 sp
<AaptFile
> file
= it
.getFile();
677 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
678 (current
!=assets
), &table
);
679 if (res
!= NO_ERROR
) {
684 current
= current
->getOverlay();
687 if (colors
!= NULL
) {
688 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
689 if (err
!= NO_ERROR
) {
695 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
696 if (err
!= NO_ERROR
) {
701 // --------------------------------------------------------------------
702 // Assignment of resource IDs and initial generation of resource table.
703 // --------------------------------------------------------------------
705 if (table
.hasResources()) {
706 sp
<AaptFile
> resFile(getResourceFile(assets
));
707 if (resFile
== NULL
) {
708 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
709 return UNKNOWN_ERROR
;
712 err
= table
.assignResourceIds();
713 if (err
< NO_ERROR
) {
718 // --------------------------------------------------------------
719 // Finally, we can now we can compile XML files, which may reference
721 // --------------------------------------------------------------
723 if (layouts
!= NULL
) {
724 ResourceDirIterator
it(layouts
, String8("layout"));
725 while ((err
=it
.next()) == NO_ERROR
) {
726 String8 src
= it
.getFile()->getPrintableSource();
727 err
= compileXmlFile(assets
, it
.getFile(), &table
);
728 if (err
== NO_ERROR
) {
730 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
731 checkForIds(src
, block
);
737 if (err
< NO_ERROR
) {
744 ResourceDirIterator
it(anims
, String8("anim"));
745 while ((err
=it
.next()) == NO_ERROR
) {
746 err
= compileXmlFile(assets
, it
.getFile(), &table
);
747 if (err
!= NO_ERROR
) {
752 if (err
< NO_ERROR
) {
759 ResourceDirIterator
it(xmls
, String8("xml"));
760 while ((err
=it
.next()) == NO_ERROR
) {
761 err
= compileXmlFile(assets
, it
.getFile(), &table
);
762 if (err
!= NO_ERROR
) {
767 if (err
< NO_ERROR
) {
773 if (drawables
!= NULL
) {
774 err
= postProcessImages(assets
, &table
, drawables
);
775 if (err
!= NO_ERROR
) {
780 if (colors
!= NULL
) {
781 ResourceDirIterator
it(colors
, String8("color"));
782 while ((err
=it
.next()) == NO_ERROR
) {
783 err
= compileXmlFile(assets
, it
.getFile(), &table
);
784 if (err
!= NO_ERROR
) {
789 if (err
< NO_ERROR
) {
796 ResourceDirIterator
it(menus
, String8("menu"));
797 while ((err
=it
.next()) == NO_ERROR
) {
798 String8 src
= it
.getFile()->getPrintableSource();
799 err
= compileXmlFile(assets
, it
.getFile(), &table
);
800 if (err
!= NO_ERROR
) {
804 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
805 checkForIds(src
, block
);
808 if (err
< NO_ERROR
) {
814 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
815 String8
manifestPath(manifestFile
->getPrintableSource());
817 // Perform a basic validation of the manifest file. This time we
818 // parse it with the comments intact, so that we can use them to
819 // generate java docs... so we are not going to write this one
820 // back out to the final manifest data.
821 err
= compileXmlFile(assets
, manifestFile
, &table
,
822 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
823 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
824 if (err
< NO_ERROR
) {
828 block
.setTo(manifestFile
->getData(), manifestFile
->getSize(), true);
829 String16
manifest16("manifest");
830 String16
permission16("permission");
831 String16
permission_group16("permission-group");
832 String16
uses_permission16("uses-permission");
833 String16
instrumentation16("instrumentation");
834 String16
application16("application");
835 String16
provider16("provider");
836 String16
service16("service");
837 String16
receiver16("receiver");
838 String16
activity16("activity");
839 String16
action16("action");
840 String16
category16("category");
841 String16
data16("scheme");
842 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
843 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
844 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
845 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
846 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
847 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
848 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
849 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
850 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
851 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
852 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
853 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
854 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
855 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
856 ResXMLTree::event_code_t code
;
857 sp
<AaptSymbols
> permissionSymbols
;
858 sp
<AaptSymbols
> permissionGroupSymbols
;
859 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
860 && code
> ResXMLTree::BAD_DOCUMENT
) {
861 if (code
== ResXMLTree::START_TAG
) {
863 if (block
.getElementNamespace(&len
) != NULL
) {
866 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
867 if (validateAttr(manifestPath
, block
, NULL
, "package",
868 packageIdentChars
, true) != ATTR_OKAY
) {
871 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
872 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
873 const bool isGroup
= strcmp16(block
.getElementName(&len
),
874 permission_group16
.string()) == 0;
875 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
876 isGroup
? packageIdentCharsWithTheStupid
877 : packageIdentChars
, true) != ATTR_OKAY
) {
880 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
881 sp
<AaptSymbols
> syms
;
883 syms
= permissionSymbols
;
885 sp
<AaptSymbols
> symbols
=
886 assets
->getSymbolsFor(String8("Manifest"));
887 syms
= permissionSymbols
= symbols
->addNestedSymbol(
888 String8("permission"), srcPos
);
891 syms
= permissionGroupSymbols
;
893 sp
<AaptSymbols
> symbols
=
894 assets
->getSymbolsFor(String8("Manifest"));
895 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
896 String8("permission_group"), srcPos
);
900 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
901 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
903 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
904 manifestPath
.string(), block
.getLineNumber(),
905 String8(block
.getElementName(&len
)).string());
910 char* p
= idStr
.lockBuffer(idStr
.size());
911 char* e
= p
+ idStr
.size();
912 bool begins_with_digit
= true; // init to true so an empty string fails
915 if (*e
>= '0' && *e
<= '9') {
916 begins_with_digit
= true;
919 if ((*e
>= 'a' && *e
<= 'z') ||
920 (*e
>= 'A' && *e
<= 'Z') ||
922 begins_with_digit
= false;
925 if (isGroup
&& (*e
== '-')) {
927 begins_with_digit
= false;
933 idStr
.unlockBuffer();
934 // verify that we stopped because we hit a period or
935 // the beginning of the string, and that the
936 // identifier didn't begin with a digit.
937 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
939 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
940 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
943 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
944 const uint16_t* cmt
= block
.getComment(&len
);
945 if (cmt
!= NULL
&& *cmt
!= 0) {
946 //printf("Comment of %s: %s\n", String8(e).string(),
947 // String8(cmt).string());
948 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
950 //printf("No comment for %s\n", String8(e).string());
952 syms
->makeSymbolPublic(String8(e
), srcPos
);
953 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
954 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
955 packageIdentChars
, true) != ATTR_OKAY
) {
958 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
959 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
960 classIdentChars
, true) != ATTR_OKAY
) {
963 if (validateAttr(manifestPath
, block
,
964 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
965 packageIdentChars
, true) != ATTR_OKAY
) {
968 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
969 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
970 classIdentChars
, false) != ATTR_OKAY
) {
973 if (validateAttr(manifestPath
, block
,
974 RESOURCES_ANDROID_NAMESPACE
, "permission",
975 packageIdentChars
, false) != ATTR_OKAY
) {
978 if (validateAttr(manifestPath
, block
,
979 RESOURCES_ANDROID_NAMESPACE
, "process",
980 processIdentChars
, false) != ATTR_OKAY
) {
983 if (validateAttr(manifestPath
, block
,
984 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
985 processIdentChars
, false) != ATTR_OKAY
) {
988 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
989 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
990 classIdentChars
, true) != ATTR_OKAY
) {
993 if (validateAttr(manifestPath
, block
,
994 RESOURCES_ANDROID_NAMESPACE
, "authorities",
995 authoritiesIdentChars
, true) != ATTR_OKAY
) {
998 if (validateAttr(manifestPath
, block
,
999 RESOURCES_ANDROID_NAMESPACE
, "permission",
1000 packageIdentChars
, false) != ATTR_OKAY
) {
1003 if (validateAttr(manifestPath
, block
,
1004 RESOURCES_ANDROID_NAMESPACE
, "process",
1005 processIdentChars
, false) != ATTR_OKAY
) {
1008 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1009 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1010 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1011 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1012 classIdentChars
, true) != ATTR_OKAY
) {
1015 if (validateAttr(manifestPath
, block
,
1016 RESOURCES_ANDROID_NAMESPACE
, "permission",
1017 packageIdentChars
, false) != ATTR_OKAY
) {
1020 if (validateAttr(manifestPath
, block
,
1021 RESOURCES_ANDROID_NAMESPACE
, "process",
1022 processIdentChars
, false) != ATTR_OKAY
) {
1025 if (validateAttr(manifestPath
, block
,
1026 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1027 processIdentChars
, false) != ATTR_OKAY
) {
1030 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1031 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1032 if (validateAttr(manifestPath
, block
,
1033 RESOURCES_ANDROID_NAMESPACE
, "name",
1034 packageIdentChars
, true) != ATTR_OKAY
) {
1037 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1038 if (validateAttr(manifestPath
, block
,
1039 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1040 typeIdentChars
, true) != ATTR_OKAY
) {
1043 if (validateAttr(manifestPath
, block
,
1044 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1045 schemeIdentChars
, true) != ATTR_OKAY
) {
1052 if (table
.validateLocalizations()) {
1057 return UNKNOWN_ERROR
;
1060 // Generate final compiled manifest file.
1061 manifestFile
->clearData();
1062 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1063 if (manifestTree
== NULL
) {
1064 return UNKNOWN_ERROR
;
1066 err
= massageManifest(bundle
, manifestTree
);
1067 if (err
< NO_ERROR
) {
1070 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1071 if (err
< NO_ERROR
) {
1076 //printXMLBlock(&block);
1078 // --------------------------------------------------------------
1079 // Generate the final resource table.
1080 // Re-flatten because we may have added new resource IDs
1081 // --------------------------------------------------------------
1083 if (table
.hasResources()) {
1084 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1085 err
= table
.addSymbols(symbols
);
1086 if (err
< NO_ERROR
) {
1090 sp
<AaptFile
> resFile(getResourceFile(assets
));
1091 if (resFile
== NULL
) {
1092 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1093 return UNKNOWN_ERROR
;
1096 err
= table
.flatten(bundle
, resFile
);
1097 if (err
< NO_ERROR
) {
1101 if (bundle
->getPublicOutputFile()) {
1102 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1104 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1105 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1106 return UNKNOWN_ERROR
;
1108 if (bundle
->getVerbose()) {
1109 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1111 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1117 rt
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1118 printf("Generated resources:\n");
1122 // These resources are now considered to be a part of the included
1123 // resources, for others to reference.
1124 err
= assets
->addIncludedResources(resFile
);
1125 if (err
< NO_ERROR
) {
1126 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1133 static const char* getIndentSpace(int indent
)
1135 static const char whitespace
[] =
1138 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1141 static status_t
fixupSymbol(String16
* inoutSymbol
)
1143 inoutSymbol
->replaceAll('.', '_');
1144 inoutSymbol
->replaceAll(':', '_');
1148 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1149 const String8
& name
,
1150 String16
* outTypeComment
= NULL
)
1152 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1154 //printf("Got R symbols!\n");
1155 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1157 //printf("Got attrs symbols! comment %s=%s\n",
1158 // name.string(), String8(asym->getComment(name)).string());
1159 if (outTypeComment
!= NULL
) {
1160 *outTypeComment
= asym
->getTypeComment(name
);
1162 return asym
->getComment(name
);
1168 static status_t
writeLayoutClasses(
1169 FILE* fp
, const sp
<AaptAssets
>& assets
,
1170 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1172 const char* indentStr
= getIndentSpace(indent
);
1173 if (!includePrivate
) {
1174 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1176 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1179 String16
attr16("attr");
1180 String16
package16(assets
->getPackage());
1182 indentStr
= getIndentSpace(indent
);
1183 bool hasErrors
= false;
1186 size_t N
= symbols
->getNestedSymbols().size();
1187 for (i
=0; i
<N
; i
++) {
1188 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1189 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1190 String8
realClassName(nclassName16
);
1191 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1194 String8
nclassName(nclassName16
);
1196 SortedVector
<uint32_t> idents
;
1197 Vector
<uint32_t> origOrder
;
1198 Vector
<bool> publicFlags
;
1201 size_t NA
= nsymbols
->getSymbols().size();
1202 for (a
=0; a
<NA
; a
++) {
1203 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1204 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1206 bool isPublic
= true;
1208 String16
name16(sym
.name
);
1209 uint32_t typeSpecFlags
;
1210 code
= assets
->getIncludedResources().identifierForName(
1211 name16
.string(), name16
.size(),
1212 attr16
.string(), attr16
.size(),
1213 package16
.string(), package16
.size(), &typeSpecFlags
);
1215 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1216 nclassName
.string(), sym
.name
.string());
1219 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1222 origOrder
.add(code
);
1223 publicFlags
.add(isPublic
);
1228 String16 comment
= symbols
->getComment(realClassName
);
1229 fprintf(fp
, "%s/** ", indentStr
);
1230 if (comment
.size() > 0) {
1231 fprintf(fp
, "%s\n", String8(comment
).string());
1233 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1235 bool hasTable
= false;
1236 for (a
=0; a
<NA
; a
++) {
1237 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1242 "%s <p>Includes the following attributes:</p>\n"
1243 "%s <table border=\"2\" width=\"85%%\" align=\"center\" frame=\"hsides\" rules=\"all\" cellpadding=\"5\">\n"
1244 "%s <colgroup align=\"left\" />\n"
1245 "%s <colgroup align=\"left\" />\n"
1246 "%s <tr><th>Attribute<th>Summary</tr>\n",
1253 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1254 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1257 String8
name8(sym
.name
);
1258 String16
comment(sym
.comment
);
1259 if (comment
.size() <= 0) {
1260 comment
= getAttributeComment(assets
, name8
);
1262 if (comment
.size() > 0) {
1263 const char16_t* p
= comment
.string();
1264 while (*p
!= 0 && *p
!= '.') {
1266 while (*p
!= 0 && *p
!= '}') {
1276 comment
= String16(comment
.string(), p
-comment
.string());
1278 String16
name(name8
);
1280 fprintf(fp
, "%s <tr><th><code>{@link #%s_%s %s:%s}</code><td>%s</tr>\n",
1281 indentStr
, nclassName
.string(),
1282 String8(name
).string(),
1283 assets
->getPackage().string(),
1284 String8(name
).string(),
1285 String8(comment
).string());
1289 fprintf(fp
, "%s </table>\n", indentStr
);
1291 for (a
=0; a
<NA
; a
++) {
1292 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1294 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1295 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1298 String16
name(sym
.name
);
1300 fprintf(fp
, "%s @see #%s_%s\n",
1301 indentStr
, nclassName
.string(),
1302 String8(name
).string());
1305 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1308 "%spublic static final int[] %s = {\n"
1310 indentStr
, nclassName
.string(),
1311 getIndentSpace(indent
+1));
1313 for (a
=0; a
<NA
; a
++) {
1316 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1321 fprintf(fp
, "0x%08x", idents
[a
]);
1324 fprintf(fp
, "\n%s};\n", indentStr
);
1326 for (a
=0; a
<NA
; a
++) {
1327 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1329 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1330 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1333 String8
name8(sym
.name
);
1334 String16
comment(sym
.comment
);
1335 String16 typeComment
;
1336 if (comment
.size() <= 0) {
1337 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1339 getAttributeComment(assets
, name8
, &typeComment
);
1341 String16
name(name8
);
1342 if (fixupSymbol(&name
) != NO_ERROR
) {
1346 uint32_t typeSpecFlags
= 0;
1347 String16
name16(sym
.name
);
1348 assets
->getIncludedResources().identifierForName(
1349 name16
.string(), name16
.size(),
1350 attr16
.string(), attr16
.size(),
1351 package16
.string(), package16
.size(), &typeSpecFlags
);
1352 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1353 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1354 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1356 fprintf(fp
, "%s/**\n", indentStr
);
1357 if (comment
.size() > 0) {
1358 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1359 fprintf(fp
, "%s %s\n", indentStr
, String8(comment
).string());
1362 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1363 "%s attribute's value can be found in the {@link #%s} array.\n",
1365 pub
? assets
->getPackage().string()
1366 : assets
->getSymbolsPrivatePackage().string(),
1367 String8(name
).string(),
1368 indentStr
, nclassName
.string());
1370 if (typeComment
.size() > 0) {
1371 fprintf(fp
, "\n\n%s %s\n", indentStr
, String8(typeComment
).string());
1373 if (comment
.size() > 0) {
1376 "%s <p>This corresponds to the global attribute"
1377 "%s resource symbol {@link %s.R.attr#%s}.\n",
1378 indentStr
, indentStr
,
1379 assets
->getPackage().string(),
1380 String8(name
).string());
1383 "%s <p>This is a private symbol.\n", indentStr
);
1386 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1387 "android", String8(name
).string());
1388 fprintf(fp
, "%s*/\n", indentStr
);
1390 "%spublic static final int %s_%s = %d;\n",
1391 indentStr
, nclassName
.string(),
1392 String8(name
).string(), (int)pos
);
1398 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1399 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1402 static status_t
writeSymbolClass(
1403 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1404 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
)
1406 fprintf(fp
, "%spublic %sfinal class %s {\n",
1407 getIndentSpace(indent
),
1408 indent
!= 0 ? "static " : "", className
.string());
1412 status_t err
= NO_ERROR
;
1414 size_t N
= symbols
->getSymbols().size();
1415 for (i
=0; i
<N
; i
++) {
1416 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1417 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1420 if (!includePrivate
&& !sym
.isPublic
) {
1423 String16
name(sym
.name
);
1424 String8
realName(name
);
1425 if (fixupSymbol(&name
) != NO_ERROR
) {
1426 return UNKNOWN_ERROR
;
1428 String16
comment(sym
.comment
);
1429 bool haveComment
= false;
1430 if (comment
.size() > 0) {
1434 getIndentSpace(indent
), String8(comment
).string());
1435 } else if (sym
.isPublic
&& !includePrivate
) {
1436 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1437 assets
->getPackage().string(), className
.string(),
1438 String8(sym
.name
).string());
1440 String16
typeComment(sym
.typeComment
);
1441 if (typeComment
.size() > 0) {
1446 getIndentSpace(indent
), String8(typeComment
).string());
1450 getIndentSpace(indent
), String8(typeComment
).string());
1454 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1456 fprintf(fp
, "%spublic static final int %s=0x%08x;\n",
1457 getIndentSpace(indent
),
1458 String8(name
).string(), (int)sym
.int32Val
);
1461 for (i
=0; i
<N
; i
++) {
1462 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1463 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1466 if (!includePrivate
&& !sym
.isPublic
) {
1469 String16
name(sym
.name
);
1470 if (fixupSymbol(&name
) != NO_ERROR
) {
1471 return UNKNOWN_ERROR
;
1473 String16
comment(sym
.comment
);
1474 if (comment
.size() > 0) {
1478 getIndentSpace(indent
), String8(comment
).string(),
1479 getIndentSpace(indent
));
1480 } else if (sym
.isPublic
&& !includePrivate
) {
1481 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1482 assets
->getPackage().string(), className
.string(),
1483 String8(sym
.name
).string());
1485 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1486 getIndentSpace(indent
),
1487 String8(name
).string(), sym
.stringVal
.string());
1490 sp
<AaptSymbols
> styleableSymbols
;
1492 N
= symbols
->getNestedSymbols().size();
1493 for (i
=0; i
<N
; i
++) {
1494 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1495 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1496 if (nclassName
== "styleable") {
1497 styleableSymbols
= nsymbols
;
1499 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
);
1501 if (err
!= NO_ERROR
) {
1506 if (styleableSymbols
!= NULL
) {
1507 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1508 if (err
!= NO_ERROR
) {
1514 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1518 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1519 const String8
& package
, bool includePrivate
)
1521 if (!bundle
->getRClassDir()) {
1525 const size_t N
= assets
->getSymbols().size();
1526 for (size_t i
=0; i
<N
; i
++) {
1527 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1528 String8
className(assets
->getSymbols().keyAt(i
));
1529 String8
dest(bundle
->getRClassDir());
1530 if (bundle
->getMakePackageDirs()) {
1531 String8
pkg(package
);
1532 const char* last
= pkg
.string();
1533 const char* s
= last
-1;
1536 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1537 String8
part(last
, s
-last
);
1538 dest
.appendPath(part
);
1539 #ifdef HAVE_MS_C_RUNTIME
1540 _mkdir(dest
.string());
1542 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1548 dest
.appendPath(className
);
1549 dest
.append(".java");
1550 FILE* fp
= fopen(dest
.string(), "w+");
1552 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1553 dest
.string(), strerror(errno
));
1554 return UNKNOWN_ERROR
;
1556 if (bundle
->getVerbose()) {
1557 printf(" Writing symbols for class %s.\n", className
.string());
1561 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1563 " * This class was automatically generated by the\n"
1564 " * aapt tool from the resource data it found. It\n"
1565 " * should not be modified by hand.\n"
1568 "package %s;\n\n", package
.string());
1570 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0);
1571 if (err
!= NO_ERROR
) {