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
));
475 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
478 // this group doesn't exist (a file that's only in the overlay)
479 baseSet
->add(overlaySet
->keyAt(overlayIndex
),
480 overlaySet
->valueAt(overlayIndex
));
481 // make sure all flavors are defined in the resources.
482 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
483 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
484 overlayGroup
->getFiles();
485 size_t overlayGroupSize
= overlayFiles
.size();
486 for (size_t overlayGroupIndex
= 0;
487 overlayGroupIndex
<overlayGroupSize
;
488 overlayGroupIndex
++) {
489 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
493 // this overlay didn't have resources for this type
496 overlay
= overlay
->getOverlay();
501 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
,
502 const char* attr8
, const char* value
)
508 const String16
ns(ns8
);
509 const String16
attr(attr8
);
511 if (node
->getAttribute(ns
, attr
) != NULL
) {
512 fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s)\n",
513 String8(attr
).string(), String8(ns
).string());
517 node
->addAttribute(ns
, attr
, String16(value
));
520 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
522 root
= root
->searchElement(String16(), String16("manifest"));
524 fprintf(stderr
, "No <manifest> tag.\n");
525 return UNKNOWN_ERROR
;
528 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
529 bundle
->getVersionCode());
530 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
531 bundle
->getVersionName());
533 if (bundle
->getMinSdkVersion() != NULL
534 || bundle
->getTargetSdkVersion() != NULL
535 || bundle
->getMaxSdkVersion() != NULL
) {
536 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
538 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
539 root
->insertChildAt(vers
, 0);
542 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
543 bundle
->getMinSdkVersion());
544 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
545 bundle
->getTargetSdkVersion());
546 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
547 bundle
->getMaxSdkVersion());
553 #define ASSIGN_IT(n) \
555 ssize_t index = resources->indexOfKey(String8(#n)); \
557 n ## s = resources->valueAt(index); \
561 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
563 // First, look for a package file to parse. This is required to
564 // be able to generate the resource information.
565 sp
<AaptGroup
> androidManifestFile
=
566 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
567 if (androidManifestFile
== NULL
) {
568 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
569 return UNKNOWN_ERROR
;
572 status_t err
= parsePackage(assets
, androidManifestFile
);
573 if (err
!= NO_ERROR
) {
577 NOISY(printf("Creating resources for package %s\n",
578 assets
->getPackage().string()));
580 ResourceTable
table(bundle
, String16(assets
->getPackage()));
581 err
= table
.addIncludedResources(bundle
, assets
);
582 if (err
!= NO_ERROR
) {
586 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
588 // --------------------------------------------------------------
589 // First, gather all resource information.
590 // --------------------------------------------------------------
592 // resType -> leafName -> group
593 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
594 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
595 collect_files(assets
, resources
);
597 sp
<ResourceTypeSet
> drawables
;
598 sp
<ResourceTypeSet
> layouts
;
599 sp
<ResourceTypeSet
> anims
;
600 sp
<ResourceTypeSet
> xmls
;
601 sp
<ResourceTypeSet
> raws
;
602 sp
<ResourceTypeSet
> colors
;
603 sp
<ResourceTypeSet
> menus
;
613 assets
->setResources(resources
);
614 // now go through any resource overlays and collect their files
615 sp
<AaptAssets
> current
= assets
->getOverlay();
616 while(current
.get()) {
617 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
618 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
619 current
->setResources(resources
);
620 collect_files(current
, resources
);
621 current
= current
->getOverlay();
623 // apply the overlay files to the base set
624 if (!applyFileOverlay(assets
, drawables
, "drawable") ||
625 !applyFileOverlay(assets
, layouts
, "layout") ||
626 !applyFileOverlay(assets
, anims
, "anim") ||
627 !applyFileOverlay(assets
, xmls
, "xml") ||
628 !applyFileOverlay(assets
, raws
, "raw") ||
629 !applyFileOverlay(assets
, colors
, "color") ||
630 !applyFileOverlay(assets
, menus
, "menu")) {
631 return UNKNOWN_ERROR
;
634 bool hasErrors
= false;
636 if (drawables
!= NULL
) {
637 err
= preProcessImages(bundle
, assets
, drawables
);
638 if (err
== NO_ERROR
) {
639 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
640 if (err
!= NO_ERROR
) {
648 if (layouts
!= NULL
) {
649 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
650 if (err
!= NO_ERROR
) {
656 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
657 if (err
!= NO_ERROR
) {
663 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
664 if (err
!= NO_ERROR
) {
670 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
671 if (err
!= NO_ERROR
) {
678 while(current
.get()) {
679 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
680 current
->getResources();
682 ssize_t index
= resources
->indexOfKey(String8("values"));
684 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
686 while ((res
=it
.next()) == NO_ERROR
) {
687 sp
<AaptFile
> file
= it
.getFile();
688 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
689 (current
!=assets
), &table
);
690 if (res
!= NO_ERROR
) {
695 current
= current
->getOverlay();
698 if (colors
!= NULL
) {
699 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
700 if (err
!= NO_ERROR
) {
706 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
707 if (err
!= NO_ERROR
) {
712 // --------------------------------------------------------------------
713 // Assignment of resource IDs and initial generation of resource table.
714 // --------------------------------------------------------------------
716 if (table
.hasResources()) {
717 sp
<AaptFile
> resFile(getResourceFile(assets
));
718 if (resFile
== NULL
) {
719 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
720 return UNKNOWN_ERROR
;
723 err
= table
.assignResourceIds();
724 if (err
< NO_ERROR
) {
729 // --------------------------------------------------------------
730 // Finally, we can now we can compile XML files, which may reference
732 // --------------------------------------------------------------
734 if (layouts
!= NULL
) {
735 ResourceDirIterator
it(layouts
, String8("layout"));
736 while ((err
=it
.next()) == NO_ERROR
) {
737 String8 src
= it
.getFile()->getPrintableSource();
738 err
= compileXmlFile(assets
, it
.getFile(), &table
);
739 if (err
== NO_ERROR
) {
741 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
742 checkForIds(src
, block
);
748 if (err
< NO_ERROR
) {
755 ResourceDirIterator
it(anims
, String8("anim"));
756 while ((err
=it
.next()) == NO_ERROR
) {
757 err
= compileXmlFile(assets
, it
.getFile(), &table
);
758 if (err
!= NO_ERROR
) {
763 if (err
< NO_ERROR
) {
770 ResourceDirIterator
it(xmls
, String8("xml"));
771 while ((err
=it
.next()) == NO_ERROR
) {
772 err
= compileXmlFile(assets
, it
.getFile(), &table
);
773 if (err
!= NO_ERROR
) {
778 if (err
< NO_ERROR
) {
784 if (drawables
!= NULL
) {
785 err
= postProcessImages(assets
, &table
, drawables
);
786 if (err
!= NO_ERROR
) {
791 if (colors
!= NULL
) {
792 ResourceDirIterator
it(colors
, String8("color"));
793 while ((err
=it
.next()) == NO_ERROR
) {
794 err
= compileXmlFile(assets
, it
.getFile(), &table
);
795 if (err
!= NO_ERROR
) {
800 if (err
< NO_ERROR
) {
807 ResourceDirIterator
it(menus
, String8("menu"));
808 while ((err
=it
.next()) == NO_ERROR
) {
809 String8 src
= it
.getFile()->getPrintableSource();
810 err
= compileXmlFile(assets
, it
.getFile(), &table
);
811 if (err
!= NO_ERROR
) {
815 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
816 checkForIds(src
, block
);
819 if (err
< NO_ERROR
) {
825 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
826 String8
manifestPath(manifestFile
->getPrintableSource());
828 // Perform a basic validation of the manifest file. This time we
829 // parse it with the comments intact, so that we can use them to
830 // generate java docs... so we are not going to write this one
831 // back out to the final manifest data.
832 err
= compileXmlFile(assets
, manifestFile
, &table
,
833 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
834 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
835 if (err
< NO_ERROR
) {
839 block
.setTo(manifestFile
->getData(), manifestFile
->getSize(), true);
840 String16
manifest16("manifest");
841 String16
permission16("permission");
842 String16
permission_group16("permission-group");
843 String16
uses_permission16("uses-permission");
844 String16
instrumentation16("instrumentation");
845 String16
application16("application");
846 String16
provider16("provider");
847 String16
service16("service");
848 String16
receiver16("receiver");
849 String16
activity16("activity");
850 String16
action16("action");
851 String16
category16("category");
852 String16
data16("scheme");
853 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
854 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
855 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
856 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
857 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
858 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
859 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
860 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
861 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
862 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
863 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
864 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
865 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
866 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
867 ResXMLTree::event_code_t code
;
868 sp
<AaptSymbols
> permissionSymbols
;
869 sp
<AaptSymbols
> permissionGroupSymbols
;
870 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
871 && code
> ResXMLTree::BAD_DOCUMENT
) {
872 if (code
== ResXMLTree::START_TAG
) {
874 if (block
.getElementNamespace(&len
) != NULL
) {
877 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
878 if (validateAttr(manifestPath
, block
, NULL
, "package",
879 packageIdentChars
, true) != ATTR_OKAY
) {
882 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
883 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
884 const bool isGroup
= strcmp16(block
.getElementName(&len
),
885 permission_group16
.string()) == 0;
886 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
887 isGroup
? packageIdentCharsWithTheStupid
888 : packageIdentChars
, true) != ATTR_OKAY
) {
891 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
892 sp
<AaptSymbols
> syms
;
894 syms
= permissionSymbols
;
896 sp
<AaptSymbols
> symbols
=
897 assets
->getSymbolsFor(String8("Manifest"));
898 syms
= permissionSymbols
= symbols
->addNestedSymbol(
899 String8("permission"), srcPos
);
902 syms
= permissionGroupSymbols
;
904 sp
<AaptSymbols
> symbols
=
905 assets
->getSymbolsFor(String8("Manifest"));
906 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
907 String8("permission_group"), srcPos
);
911 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
912 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
914 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
915 manifestPath
.string(), block
.getLineNumber(),
916 String8(block
.getElementName(&len
)).string());
921 char* p
= idStr
.lockBuffer(idStr
.size());
922 char* e
= p
+ idStr
.size();
923 bool begins_with_digit
= true; // init to true so an empty string fails
926 if (*e
>= '0' && *e
<= '9') {
927 begins_with_digit
= true;
930 if ((*e
>= 'a' && *e
<= 'z') ||
931 (*e
>= 'A' && *e
<= 'Z') ||
933 begins_with_digit
= false;
936 if (isGroup
&& (*e
== '-')) {
938 begins_with_digit
= false;
944 idStr
.unlockBuffer();
945 // verify that we stopped because we hit a period or
946 // the beginning of the string, and that the
947 // identifier didn't begin with a digit.
948 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
950 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
951 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
954 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
955 const uint16_t* cmt
= block
.getComment(&len
);
956 if (cmt
!= NULL
&& *cmt
!= 0) {
957 //printf("Comment of %s: %s\n", String8(e).string(),
958 // String8(cmt).string());
959 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
961 //printf("No comment for %s\n", String8(e).string());
963 syms
->makeSymbolPublic(String8(e
), srcPos
);
964 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
965 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
966 packageIdentChars
, true) != ATTR_OKAY
) {
969 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
970 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
971 classIdentChars
, true) != ATTR_OKAY
) {
974 if (validateAttr(manifestPath
, block
,
975 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
976 packageIdentChars
, true) != ATTR_OKAY
) {
979 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
980 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
981 classIdentChars
, false) != ATTR_OKAY
) {
984 if (validateAttr(manifestPath
, block
,
985 RESOURCES_ANDROID_NAMESPACE
, "permission",
986 packageIdentChars
, false) != ATTR_OKAY
) {
989 if (validateAttr(manifestPath
, block
,
990 RESOURCES_ANDROID_NAMESPACE
, "process",
991 processIdentChars
, false) != ATTR_OKAY
) {
994 if (validateAttr(manifestPath
, block
,
995 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
996 processIdentChars
, false) != ATTR_OKAY
) {
999 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
1000 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1001 classIdentChars
, true) != ATTR_OKAY
) {
1004 if (validateAttr(manifestPath
, block
,
1005 RESOURCES_ANDROID_NAMESPACE
, "authorities",
1006 authoritiesIdentChars
, true) != ATTR_OKAY
) {
1009 if (validateAttr(manifestPath
, block
,
1010 RESOURCES_ANDROID_NAMESPACE
, "permission",
1011 packageIdentChars
, false) != ATTR_OKAY
) {
1014 if (validateAttr(manifestPath
, block
,
1015 RESOURCES_ANDROID_NAMESPACE
, "process",
1016 processIdentChars
, false) != ATTR_OKAY
) {
1019 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1020 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1021 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1022 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1023 classIdentChars
, true) != ATTR_OKAY
) {
1026 if (validateAttr(manifestPath
, block
,
1027 RESOURCES_ANDROID_NAMESPACE
, "permission",
1028 packageIdentChars
, false) != ATTR_OKAY
) {
1031 if (validateAttr(manifestPath
, block
,
1032 RESOURCES_ANDROID_NAMESPACE
, "process",
1033 processIdentChars
, false) != ATTR_OKAY
) {
1036 if (validateAttr(manifestPath
, block
,
1037 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1038 processIdentChars
, false) != ATTR_OKAY
) {
1041 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1042 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1043 if (validateAttr(manifestPath
, block
,
1044 RESOURCES_ANDROID_NAMESPACE
, "name",
1045 packageIdentChars
, true) != ATTR_OKAY
) {
1048 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1049 if (validateAttr(manifestPath
, block
,
1050 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1051 typeIdentChars
, true) != ATTR_OKAY
) {
1054 if (validateAttr(manifestPath
, block
,
1055 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1056 schemeIdentChars
, true) != ATTR_OKAY
) {
1063 if (table
.validateLocalizations()) {
1068 return UNKNOWN_ERROR
;
1071 // Generate final compiled manifest file.
1072 manifestFile
->clearData();
1073 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1074 if (manifestTree
== NULL
) {
1075 return UNKNOWN_ERROR
;
1077 err
= massageManifest(bundle
, manifestTree
);
1078 if (err
< NO_ERROR
) {
1081 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1082 if (err
< NO_ERROR
) {
1087 //printXMLBlock(&block);
1089 // --------------------------------------------------------------
1090 // Generate the final resource table.
1091 // Re-flatten because we may have added new resource IDs
1092 // --------------------------------------------------------------
1094 if (table
.hasResources()) {
1095 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1096 err
= table
.addSymbols(symbols
);
1097 if (err
< NO_ERROR
) {
1101 sp
<AaptFile
> resFile(getResourceFile(assets
));
1102 if (resFile
== NULL
) {
1103 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1104 return UNKNOWN_ERROR
;
1107 err
= table
.flatten(bundle
, resFile
);
1108 if (err
< NO_ERROR
) {
1112 if (bundle
->getPublicOutputFile()) {
1113 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1115 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1116 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1117 return UNKNOWN_ERROR
;
1119 if (bundle
->getVerbose()) {
1120 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1122 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1128 rt
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1129 printf("Generated resources:\n");
1133 // These resources are now considered to be a part of the included
1134 // resources, for others to reference.
1135 err
= assets
->addIncludedResources(resFile
);
1136 if (err
< NO_ERROR
) {
1137 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1144 static const char* getIndentSpace(int indent
)
1146 static const char whitespace
[] =
1149 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1152 static status_t
fixupSymbol(String16
* inoutSymbol
)
1154 inoutSymbol
->replaceAll('.', '_');
1155 inoutSymbol
->replaceAll(':', '_');
1159 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1160 const String8
& name
,
1161 String16
* outTypeComment
= NULL
)
1163 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1165 //printf("Got R symbols!\n");
1166 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1168 //printf("Got attrs symbols! comment %s=%s\n",
1169 // name.string(), String8(asym->getComment(name)).string());
1170 if (outTypeComment
!= NULL
) {
1171 *outTypeComment
= asym
->getTypeComment(name
);
1173 return asym
->getComment(name
);
1179 static status_t
writeLayoutClasses(
1180 FILE* fp
, const sp
<AaptAssets
>& assets
,
1181 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1183 const char* indentStr
= getIndentSpace(indent
);
1184 if (!includePrivate
) {
1185 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1187 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1190 String16
attr16("attr");
1191 String16
package16(assets
->getPackage());
1193 indentStr
= getIndentSpace(indent
);
1194 bool hasErrors
= false;
1197 size_t N
= symbols
->getNestedSymbols().size();
1198 for (i
=0; i
<N
; i
++) {
1199 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1200 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1201 String8
realClassName(nclassName16
);
1202 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1205 String8
nclassName(nclassName16
);
1207 SortedVector
<uint32_t> idents
;
1208 Vector
<uint32_t> origOrder
;
1209 Vector
<bool> publicFlags
;
1212 size_t NA
= nsymbols
->getSymbols().size();
1213 for (a
=0; a
<NA
; a
++) {
1214 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1215 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1217 bool isPublic
= true;
1219 String16
name16(sym
.name
);
1220 uint32_t typeSpecFlags
;
1221 code
= assets
->getIncludedResources().identifierForName(
1222 name16
.string(), name16
.size(),
1223 attr16
.string(), attr16
.size(),
1224 package16
.string(), package16
.size(), &typeSpecFlags
);
1226 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1227 nclassName
.string(), sym
.name
.string());
1230 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1233 origOrder
.add(code
);
1234 publicFlags
.add(isPublic
);
1239 String16 comment
= symbols
->getComment(realClassName
);
1240 fprintf(fp
, "%s/** ", indentStr
);
1241 if (comment
.size() > 0) {
1242 fprintf(fp
, "%s\n", String8(comment
).string());
1244 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1246 bool hasTable
= false;
1247 for (a
=0; a
<NA
; a
++) {
1248 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1253 "%s <p>Includes the following attributes:</p>\n"
1254 "%s <table border=\"2\" width=\"85%%\" align=\"center\" frame=\"hsides\" rules=\"all\" cellpadding=\"5\">\n"
1255 "%s <colgroup align=\"left\" />\n"
1256 "%s <colgroup align=\"left\" />\n"
1257 "%s <tr><th>Attribute<th>Summary</tr>\n",
1264 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1265 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1268 String8
name8(sym
.name
);
1269 String16
comment(sym
.comment
);
1270 if (comment
.size() <= 0) {
1271 comment
= getAttributeComment(assets
, name8
);
1273 if (comment
.size() > 0) {
1274 const char16_t* p
= comment
.string();
1275 while (*p
!= 0 && *p
!= '.') {
1277 while (*p
!= 0 && *p
!= '}') {
1287 comment
= String16(comment
.string(), p
-comment
.string());
1289 String16
name(name8
);
1291 fprintf(fp
, "%s <tr><th><code>{@link #%s_%s %s:%s}</code><td>%s</tr>\n",
1292 indentStr
, nclassName
.string(),
1293 String8(name
).string(),
1294 assets
->getPackage().string(),
1295 String8(name
).string(),
1296 String8(comment
).string());
1300 fprintf(fp
, "%s </table>\n", indentStr
);
1302 for (a
=0; a
<NA
; a
++) {
1303 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1305 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1306 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1309 String16
name(sym
.name
);
1311 fprintf(fp
, "%s @see #%s_%s\n",
1312 indentStr
, nclassName
.string(),
1313 String8(name
).string());
1316 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1319 "%spublic static final int[] %s = {\n"
1321 indentStr
, nclassName
.string(),
1322 getIndentSpace(indent
+1));
1324 for (a
=0; a
<NA
; a
++) {
1327 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1332 fprintf(fp
, "0x%08x", idents
[a
]);
1335 fprintf(fp
, "\n%s};\n", indentStr
);
1337 for (a
=0; a
<NA
; a
++) {
1338 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1340 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1341 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1344 String8
name8(sym
.name
);
1345 String16
comment(sym
.comment
);
1346 String16 typeComment
;
1347 if (comment
.size() <= 0) {
1348 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1350 getAttributeComment(assets
, name8
, &typeComment
);
1352 String16
name(name8
);
1353 if (fixupSymbol(&name
) != NO_ERROR
) {
1357 uint32_t typeSpecFlags
= 0;
1358 String16
name16(sym
.name
);
1359 assets
->getIncludedResources().identifierForName(
1360 name16
.string(), name16
.size(),
1361 attr16
.string(), attr16
.size(),
1362 package16
.string(), package16
.size(), &typeSpecFlags
);
1363 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1364 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1365 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1367 fprintf(fp
, "%s/**\n", indentStr
);
1368 if (comment
.size() > 0) {
1369 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1370 fprintf(fp
, "%s %s\n", indentStr
, String8(comment
).string());
1373 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1374 "%s attribute's value can be found in the {@link #%s} array.\n",
1376 pub
? assets
->getPackage().string()
1377 : assets
->getSymbolsPrivatePackage().string(),
1378 String8(name
).string(),
1379 indentStr
, nclassName
.string());
1381 if (typeComment
.size() > 0) {
1382 fprintf(fp
, "\n\n%s %s\n", indentStr
, String8(typeComment
).string());
1384 if (comment
.size() > 0) {
1387 "%s <p>This corresponds to the global attribute"
1388 "%s resource symbol {@link %s.R.attr#%s}.\n",
1389 indentStr
, indentStr
,
1390 assets
->getPackage().string(),
1391 String8(name
).string());
1394 "%s <p>This is a private symbol.\n", indentStr
);
1397 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1398 "android", String8(name
).string());
1399 fprintf(fp
, "%s*/\n", indentStr
);
1401 "%spublic static final int %s_%s = %d;\n",
1402 indentStr
, nclassName
.string(),
1403 String8(name
).string(), (int)pos
);
1409 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1410 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1413 static status_t
writeSymbolClass(
1414 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1415 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
)
1417 fprintf(fp
, "%spublic %sfinal class %s {\n",
1418 getIndentSpace(indent
),
1419 indent
!= 0 ? "static " : "", className
.string());
1423 status_t err
= NO_ERROR
;
1425 size_t N
= symbols
->getSymbols().size();
1426 for (i
=0; i
<N
; i
++) {
1427 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1428 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1431 if (!includePrivate
&& !sym
.isPublic
) {
1434 String16
name(sym
.name
);
1435 String8
realName(name
);
1436 if (fixupSymbol(&name
) != NO_ERROR
) {
1437 return UNKNOWN_ERROR
;
1439 String16
comment(sym
.comment
);
1440 bool haveComment
= false;
1441 if (comment
.size() > 0) {
1445 getIndentSpace(indent
), String8(comment
).string());
1446 } else if (sym
.isPublic
&& !includePrivate
) {
1447 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1448 assets
->getPackage().string(), className
.string(),
1449 String8(sym
.name
).string());
1451 String16
typeComment(sym
.typeComment
);
1452 if (typeComment
.size() > 0) {
1457 getIndentSpace(indent
), String8(typeComment
).string());
1461 getIndentSpace(indent
), String8(typeComment
).string());
1465 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1467 fprintf(fp
, "%spublic static final int %s=0x%08x;\n",
1468 getIndentSpace(indent
),
1469 String8(name
).string(), (int)sym
.int32Val
);
1472 for (i
=0; i
<N
; i
++) {
1473 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1474 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1477 if (!includePrivate
&& !sym
.isPublic
) {
1480 String16
name(sym
.name
);
1481 if (fixupSymbol(&name
) != NO_ERROR
) {
1482 return UNKNOWN_ERROR
;
1484 String16
comment(sym
.comment
);
1485 if (comment
.size() > 0) {
1489 getIndentSpace(indent
), String8(comment
).string(),
1490 getIndentSpace(indent
));
1491 } else if (sym
.isPublic
&& !includePrivate
) {
1492 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1493 assets
->getPackage().string(), className
.string(),
1494 String8(sym
.name
).string());
1496 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1497 getIndentSpace(indent
),
1498 String8(name
).string(), sym
.stringVal
.string());
1501 sp
<AaptSymbols
> styleableSymbols
;
1503 N
= symbols
->getNestedSymbols().size();
1504 for (i
=0; i
<N
; i
++) {
1505 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1506 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1507 if (nclassName
== "styleable") {
1508 styleableSymbols
= nsymbols
;
1510 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
);
1512 if (err
!= NO_ERROR
) {
1517 if (styleableSymbols
!= NULL
) {
1518 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1519 if (err
!= NO_ERROR
) {
1525 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1529 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1530 const String8
& package
, bool includePrivate
)
1532 if (!bundle
->getRClassDir()) {
1536 const size_t N
= assets
->getSymbols().size();
1537 for (size_t i
=0; i
<N
; i
++) {
1538 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1539 String8
className(assets
->getSymbols().keyAt(i
));
1540 String8
dest(bundle
->getRClassDir());
1541 if (bundle
->getMakePackageDirs()) {
1542 String8
pkg(package
);
1543 const char* last
= pkg
.string();
1544 const char* s
= last
-1;
1547 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1548 String8
part(last
, s
-last
);
1549 dest
.appendPath(part
);
1550 #ifdef HAVE_MS_C_RUNTIME
1551 _mkdir(dest
.string());
1553 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1559 dest
.appendPath(className
);
1560 dest
.append(".java");
1561 FILE* fp
= fopen(dest
.string(), "w+");
1563 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1564 dest
.string(), strerror(errno
));
1565 return UNKNOWN_ERROR
;
1567 if (bundle
->getVerbose()) {
1568 printf(" Writing symbols for class %s.\n", className
.string());
1572 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1574 " * This class was automatically generated by the\n"
1575 " * aapt tool from the resource data it found. It\n"
1576 " * should not be modified by hand.\n"
1579 "package %s;\n\n", package
.string());
1581 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0);
1582 if (err
!= NO_ERROR
) {