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(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
175 const sp
<AaptGroup
>& grp
)
177 if (grp
->getFiles().size() != 1) {
178 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
179 grp
->getFiles().valueAt(0)->getPrintableSource().string());
182 sp
<AaptFile
> file
= grp
->getFiles().valueAt(0);
185 status_t err
= parseXMLResource(file
, &block
);
186 if (err
!= NO_ERROR
) {
189 //printXMLBlock(&block);
191 ResXMLTree::event_code_t code
;
192 while ((code
=block
.next()) != ResXMLTree::START_TAG
193 && code
!= ResXMLTree::END_DOCUMENT
194 && code
!= ResXMLTree::BAD_DOCUMENT
) {
198 if (code
!= ResXMLTree::START_TAG
) {
199 fprintf(stderr
, "%s:%d: No start tag found\n",
200 file
->getPrintableSource().string(), block
.getLineNumber());
201 return UNKNOWN_ERROR
;
203 if (strcmp16(block
.getElementName(&len
), String16("manifest").string()) != 0) {
204 fprintf(stderr
, "%s:%d: Invalid start tag %s, expected <manifest>\n",
205 file
->getPrintableSource().string(), block
.getLineNumber(),
206 String8(block
.getElementName(&len
)).string());
207 return UNKNOWN_ERROR
;
210 ssize_t nameIndex
= block
.indexOfAttribute(NULL
, "package");
212 fprintf(stderr
, "%s:%d: <manifest> does not have package attribute.\n",
213 file
->getPrintableSource().string(), block
.getLineNumber());
214 return UNKNOWN_ERROR
;
217 assets
->setPackage(String8(block
.getAttributeStringValue(nameIndex
, &len
)));
219 String16
uses_sdk16("uses-sdk");
220 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
221 && code
!= ResXMLTree::BAD_DOCUMENT
) {
222 if (code
== ResXMLTree::START_TAG
) {
223 if (strcmp16(block
.getElementName(&len
), uses_sdk16
.string()) == 0) {
224 ssize_t minSdkIndex
= block
.indexOfAttribute("android",
226 if (minSdkIndex
>= 0) {
227 String8 minSdkString
= String8(
228 block
.getAttributeStringValue(minSdkIndex
, &len
));
229 bundle
->setMinSdkVersion(minSdkString
.string());
238 // ==========================================================================
239 // ==========================================================================
240 // ==========================================================================
242 static status_t
makeFileResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
243 ResourceTable
* table
,
244 const sp
<ResourceTypeSet
>& set
,
247 String8
type8(resType
);
248 String16
type16(resType
);
250 bool hasErrors
= false;
252 ResourceDirIterator
it(set
, String8(resType
));
254 while ((res
=it
.next()) == NO_ERROR
) {
255 if (bundle
->getVerbose()) {
256 printf(" (new resource id %s from %s)\n",
257 it
.getBaseName().string(), it
.getFile()->getPrintableSource().string());
259 String16
baseName(it
.getBaseName());
260 const char16_t* str
= baseName
.string();
261 const char16_t* const end
= str
+ baseName
.size();
263 if (!((*str
>= 'a' && *str
<= 'z')
264 || (*str
>= '0' && *str
<= '9')
265 || *str
== '_' || *str
== '.')) {
266 fprintf(stderr
, "%s: Invalid file name: must contain only [a-z0-9_.]\n",
267 it
.getPath().string());
272 String8 resPath
= it
.getPath();
273 resPath
.convertToResPath();
274 table
->addEntry(SourcePos(it
.getPath(), 0), String16(assets
->getPackage()),
280 assets
->addResource(it
.getLeafName(), resPath
, it
.getFile(), type8
);
283 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
286 static status_t
preProcessImages(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
287 const sp
<ResourceTypeSet
>& set
)
289 ResourceDirIterator
it(set
, String8("drawable"));
290 Vector
<sp
<AaptFile
> > newNameFiles
;
291 Vector
<String8
> newNamePaths
;
292 bool hasErrors
= false;
294 while ((res
=it
.next()) == NO_ERROR
) {
295 res
= preProcessImage(bundle
, assets
, it
.getFile(), NULL
);
296 if (res
< NO_ERROR
) {
301 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
304 status_t
postProcessImages(const sp
<AaptAssets
>& assets
,
305 ResourceTable
* table
,
306 const sp
<ResourceTypeSet
>& set
)
308 ResourceDirIterator
it(set
, String8("drawable"));
309 bool hasErrors
= false;
311 while ((res
=it
.next()) == NO_ERROR
) {
312 res
= postProcessImage(assets
, table
, it
.getFile());
313 if (res
< NO_ERROR
) {
318 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
321 static void collect_files(const sp
<AaptDir
>& dir
,
322 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
324 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& groups
= dir
->getFiles();
325 int N
= groups
.size();
326 for (int i
=0; i
<N
; i
++) {
327 String8 leafName
= groups
.keyAt(i
);
328 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
330 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
333 if (files
.size() == 0) {
337 String8 resType
= files
.valueAt(0)->getResourceType();
339 ssize_t index
= resources
->indexOfKey(resType
);
342 sp
<ResourceTypeSet
> set
= new ResourceTypeSet();
343 set
->add(leafName
, group
);
344 resources
->add(resType
, set
);
346 sp
<ResourceTypeSet
> set
= resources
->valueAt(index
);
347 index
= set
->indexOfKey(leafName
);
349 set
->add(leafName
, group
);
351 sp
<AaptGroup
> existingGroup
= set
->valueAt(index
);
352 int M
= files
.size();
353 for (int j
=0; j
<M
; j
++) {
354 existingGroup
->addFile(files
.valueAt(j
));
361 static void collect_files(const sp
<AaptAssets
>& ass
,
362 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
364 const Vector
<sp
<AaptDir
> >& dirs
= ass
->resDirs();
367 for (int i
=0; i
<N
; i
++) {
368 sp
<AaptDir
> d
= dirs
.itemAt(i
);
369 collect_files(d
, resources
);
371 // don't try to include the res dir
372 ass
->removeDir(d
->getLeaf());
379 ATTR_LEADING_SPACES
= -3,
380 ATTR_TRAILING_SPACES
= -4
382 static int validateAttr(const String8
& path
, const ResXMLParser
& parser
,
383 const char* ns
, const char* attr
, const char* validChars
, bool required
)
387 ssize_t index
= parser
.indexOfAttribute(ns
, attr
);
389 if (index
>= 0 && (str
=parser
.getAttributeStringValue(index
, &len
)) != NULL
) {
391 for (size_t i
=0; i
<len
; i
++) {
393 const char* p
= validChars
;
403 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n",
404 path
.string(), parser
.getLineNumber(),
405 String8(parser
.getElementName(&len
)).string(), attr
, (char)str
[i
]);
411 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not start with a space.\n",
412 path
.string(), parser
.getLineNumber(),
413 String8(parser
.getElementName(&len
)).string(), attr
);
414 return ATTR_LEADING_SPACES
;
416 if (str
[len
-1] == ' ') {
417 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not end with a space.\n",
418 path
.string(), parser
.getLineNumber(),
419 String8(parser
.getElementName(&len
)).string(), attr
);
420 return ATTR_TRAILING_SPACES
;
425 fprintf(stderr
, "%s:%d: Tag <%s> missing required attribute %s.\n",
426 path
.string(), parser
.getLineNumber(),
427 String8(parser
.getElementName(&len
)).string(), attr
);
428 return ATTR_NOT_FOUND
;
433 static void checkForIds(const String8
& path
, ResXMLParser
& parser
)
435 ResXMLTree::event_code_t code
;
436 while ((code
=parser
.next()) != ResXMLTree::END_DOCUMENT
437 && code
> ResXMLTree::BAD_DOCUMENT
) {
438 if (code
== ResXMLTree::START_TAG
) {
439 ssize_t index
= parser
.indexOfAttribute(NULL
, "id");
441 fprintf(stderr
, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n",
442 path
.string(), parser
.getLineNumber());
448 static bool applyFileOverlay(Bundle
*bundle
,
449 const sp
<AaptAssets
>& assets
,
450 const sp
<ResourceTypeSet
>& baseSet
,
453 if (bundle
->getVerbose()) {
454 printf("applyFileOverlay for %s\n", resType
);
457 // Replace any base level files in this category with any found from the overlay
458 // Also add any found only in the overlay.
459 sp
<AaptAssets
> overlay
= assets
->getOverlay();
460 String8
resTypeString(resType
);
462 // work through the linked list of overlays
463 while (overlay
.get()) {
464 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* overlayRes
= overlay
->getResources();
466 // get the overlay resources of the requested type
467 ssize_t index
= overlayRes
->indexOfKey(resTypeString
);
469 sp
<ResourceTypeSet
> overlaySet
= overlayRes
->valueAt(index
);
471 // for each of the resources, check for a match in the previously built
472 // non-overlay "baseset".
473 size_t overlayCount
= overlaySet
->size();
474 for (size_t overlayIndex
=0; overlayIndex
<overlayCount
; overlayIndex
++) {
475 if (bundle
->getVerbose()) {
476 printf("trying overlaySet Key=%s\n",overlaySet
->keyAt(overlayIndex
).string());
478 size_t baseIndex
= baseSet
->indexOfKey(overlaySet
->keyAt(overlayIndex
));
479 if (baseIndex
< UNKNOWN_ERROR
) {
480 // look for same flavor. For a given file (strings.xml, for example)
481 // there may be a locale specific or other flavors - we want to match
483 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
484 sp
<AaptGroup
> baseGroup
= baseSet
->valueAt(baseIndex
);
486 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
487 overlayGroup
->getFiles();
488 if (bundle
->getVerbose()) {
489 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > baseFiles
=
490 baseGroup
->getFiles();
491 for (size_t i
=0; i
< baseFiles
.size(); i
++) {
492 printf("baseFile %d has flavor %s\n", i
,
493 baseFiles
.keyAt(i
).toString().string());
495 for (size_t i
=0; i
< overlayFiles
.size(); i
++) {
496 printf("overlayFile %d has flavor %s\n", i
,
497 overlayFiles
.keyAt(i
).toString().string());
501 size_t overlayGroupSize
= overlayFiles
.size();
502 for (size_t overlayGroupIndex
= 0;
503 overlayGroupIndex
<overlayGroupSize
;
504 overlayGroupIndex
++) {
505 size_t baseFileIndex
=
506 baseGroup
->getFiles().indexOfKey(overlayFiles
.
507 keyAt(overlayGroupIndex
));
508 if(baseFileIndex
< UNKNOWN_ERROR
) {
509 if (bundle
->getVerbose()) {
510 printf("found a match (%d) for overlay file %s, for flavor %s\n",
512 overlayGroup
->getLeaf().string(),
513 overlayFiles
.keyAt(overlayGroupIndex
).toString().string());
515 baseGroup
->removeFile(baseFileIndex
);
517 // didn't find a match fall through and add it..
519 baseGroup
->addFile(overlayFiles
.valueAt(overlayGroupIndex
));
520 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
523 // this group doesn't exist (a file that's only in the overlay)
524 baseSet
->add(overlaySet
->keyAt(overlayIndex
),
525 overlaySet
->valueAt(overlayIndex
));
526 // make sure all flavors are defined in the resources.
527 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
528 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
529 overlayGroup
->getFiles();
530 size_t overlayGroupSize
= overlayFiles
.size();
531 for (size_t overlayGroupIndex
= 0;
532 overlayGroupIndex
<overlayGroupSize
;
533 overlayGroupIndex
++) {
534 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
538 // this overlay didn't have resources for this type
541 overlay
= overlay
->getOverlay();
546 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
,
547 const char* attr8
, const char* value
)
553 const String16
ns(ns8
);
554 const String16
attr(attr8
);
556 if (node
->getAttribute(ns
, attr
) != NULL
) {
557 fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s)\n",
558 String8(attr
).string(), String8(ns
).string());
562 node
->addAttribute(ns
, attr
, String16(value
));
565 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
567 root
= root
->searchElement(String16(), String16("manifest"));
569 fprintf(stderr
, "No <manifest> tag.\n");
570 return UNKNOWN_ERROR
;
573 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
574 bundle
->getVersionCode());
575 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
576 bundle
->getVersionName());
578 if (bundle
->getMinSdkVersion() != NULL
579 || bundle
->getTargetSdkVersion() != NULL
580 || bundle
->getMaxSdkVersion() != NULL
) {
581 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
583 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
584 root
->insertChildAt(vers
, 0);
587 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
588 bundle
->getMinSdkVersion());
589 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
590 bundle
->getTargetSdkVersion());
591 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
592 bundle
->getMaxSdkVersion());
598 #define ASSIGN_IT(n) \
600 ssize_t index = resources->indexOfKey(String8(#n)); \
602 n ## s = resources->valueAt(index); \
606 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
608 // First, look for a package file to parse. This is required to
609 // be able to generate the resource information.
610 sp
<AaptGroup
> androidManifestFile
=
611 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
612 if (androidManifestFile
== NULL
) {
613 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
614 return UNKNOWN_ERROR
;
617 status_t err
= parsePackage(bundle
, assets
, androidManifestFile
);
618 if (err
!= NO_ERROR
) {
622 NOISY(printf("Creating resources for package %s\n",
623 assets
->getPackage().string()));
625 ResourceTable
table(bundle
, String16(assets
->getPackage()));
626 err
= table
.addIncludedResources(bundle
, assets
);
627 if (err
!= NO_ERROR
) {
631 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
633 // Standard flags for compiled XML and optional UTF-8 encoding
634 int xmlFlags
= XML_COMPILE_STANDARD_RESOURCE
;
635 if (bundle
->getUTF8()) {
636 xmlFlags
|= XML_COMPILE_UTF8
;
639 // --------------------------------------------------------------
640 // First, gather all resource information.
641 // --------------------------------------------------------------
643 // resType -> leafName -> group
644 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
645 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
646 collect_files(assets
, resources
);
648 sp
<ResourceTypeSet
> drawables
;
649 sp
<ResourceTypeSet
> layouts
;
650 sp
<ResourceTypeSet
> anims
;
651 sp
<ResourceTypeSet
> xmls
;
652 sp
<ResourceTypeSet
> raws
;
653 sp
<ResourceTypeSet
> colors
;
654 sp
<ResourceTypeSet
> menus
;
664 assets
->setResources(resources
);
665 // now go through any resource overlays and collect their files
666 sp
<AaptAssets
> current
= assets
->getOverlay();
667 while(current
.get()) {
668 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
669 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
670 current
->setResources(resources
);
671 collect_files(current
, resources
);
672 current
= current
->getOverlay();
674 // apply the overlay files to the base set
675 if (!applyFileOverlay(bundle
, assets
, drawables
, "drawable") ||
676 !applyFileOverlay(bundle
, assets
, layouts
, "layout") ||
677 !applyFileOverlay(bundle
, assets
, anims
, "anim") ||
678 !applyFileOverlay(bundle
, assets
, xmls
, "xml") ||
679 !applyFileOverlay(bundle
, assets
, raws
, "raw") ||
680 !applyFileOverlay(bundle
, assets
, colors
, "color") ||
681 !applyFileOverlay(bundle
, assets
, menus
, "menu")) {
682 return UNKNOWN_ERROR
;
685 bool hasErrors
= false;
687 if (drawables
!= NULL
) {
688 err
= preProcessImages(bundle
, assets
, drawables
);
689 if (err
== NO_ERROR
) {
690 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
691 if (err
!= NO_ERROR
) {
699 if (layouts
!= NULL
) {
700 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
701 if (err
!= NO_ERROR
) {
707 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
708 if (err
!= NO_ERROR
) {
714 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
715 if (err
!= NO_ERROR
) {
721 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
722 if (err
!= NO_ERROR
) {
729 while(current
.get()) {
730 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
731 current
->getResources();
733 ssize_t index
= resources
->indexOfKey(String8("values"));
735 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
737 while ((res
=it
.next()) == NO_ERROR
) {
738 sp
<AaptFile
> file
= it
.getFile();
739 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
740 (current
!=assets
), &table
);
741 if (res
!= NO_ERROR
) {
746 current
= current
->getOverlay();
749 if (colors
!= NULL
) {
750 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
751 if (err
!= NO_ERROR
) {
757 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
758 if (err
!= NO_ERROR
) {
763 // --------------------------------------------------------------------
764 // Assignment of resource IDs and initial generation of resource table.
765 // --------------------------------------------------------------------
767 if (table
.hasResources()) {
768 sp
<AaptFile
> resFile(getResourceFile(assets
));
769 if (resFile
== NULL
) {
770 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
771 return UNKNOWN_ERROR
;
774 err
= table
.assignResourceIds();
775 if (err
< NO_ERROR
) {
780 // --------------------------------------------------------------
781 // Finally, we can now we can compile XML files, which may reference
783 // --------------------------------------------------------------
785 if (layouts
!= NULL
) {
786 ResourceDirIterator
it(layouts
, String8("layout"));
787 while ((err
=it
.next()) == NO_ERROR
) {
788 String8 src
= it
.getFile()->getPrintableSource();
789 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
790 if (err
== NO_ERROR
) {
792 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
793 checkForIds(src
, block
);
799 if (err
< NO_ERROR
) {
806 ResourceDirIterator
it(anims
, String8("anim"));
807 while ((err
=it
.next()) == NO_ERROR
) {
808 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
809 if (err
!= NO_ERROR
) {
814 if (err
< NO_ERROR
) {
821 ResourceDirIterator
it(xmls
, String8("xml"));
822 while ((err
=it
.next()) == NO_ERROR
) {
823 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
824 if (err
!= NO_ERROR
) {
829 if (err
< NO_ERROR
) {
835 if (drawables
!= NULL
) {
836 err
= postProcessImages(assets
, &table
, drawables
);
837 if (err
!= NO_ERROR
) {
842 if (colors
!= NULL
) {
843 ResourceDirIterator
it(colors
, String8("color"));
844 while ((err
=it
.next()) == NO_ERROR
) {
845 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
846 if (err
!= NO_ERROR
) {
851 if (err
< NO_ERROR
) {
858 ResourceDirIterator
it(menus
, String8("menu"));
859 while ((err
=it
.next()) == NO_ERROR
) {
860 String8 src
= it
.getFile()->getPrintableSource();
861 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
862 if (err
!= NO_ERROR
) {
866 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
867 checkForIds(src
, block
);
870 if (err
< NO_ERROR
) {
876 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
877 String8
manifestPath(manifestFile
->getPrintableSource());
879 // Perform a basic validation of the manifest file. This time we
880 // parse it with the comments intact, so that we can use them to
881 // generate java docs... so we are not going to write this one
882 // back out to the final manifest data.
883 err
= compileXmlFile(assets
, manifestFile
, &table
,
884 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
885 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
886 if (err
< NO_ERROR
) {
890 block
.setTo(manifestFile
->getData(), manifestFile
->getSize(), true);
891 String16
manifest16("manifest");
892 String16
permission16("permission");
893 String16
permission_group16("permission-group");
894 String16
uses_permission16("uses-permission");
895 String16
instrumentation16("instrumentation");
896 String16
application16("application");
897 String16
provider16("provider");
898 String16
service16("service");
899 String16
receiver16("receiver");
900 String16
activity16("activity");
901 String16
action16("action");
902 String16
category16("category");
903 String16
data16("scheme");
904 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
905 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
906 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
907 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
908 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
909 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
910 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
911 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
912 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
913 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
914 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
915 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
916 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
917 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
918 ResXMLTree::event_code_t code
;
919 sp
<AaptSymbols
> permissionSymbols
;
920 sp
<AaptSymbols
> permissionGroupSymbols
;
921 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
922 && code
> ResXMLTree::BAD_DOCUMENT
) {
923 if (code
== ResXMLTree::START_TAG
) {
925 if (block
.getElementNamespace(&len
) != NULL
) {
928 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
929 if (validateAttr(manifestPath
, block
, NULL
, "package",
930 packageIdentChars
, true) != ATTR_OKAY
) {
933 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
934 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
935 const bool isGroup
= strcmp16(block
.getElementName(&len
),
936 permission_group16
.string()) == 0;
937 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
938 isGroup
? packageIdentCharsWithTheStupid
939 : packageIdentChars
, true) != ATTR_OKAY
) {
942 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
943 sp
<AaptSymbols
> syms
;
945 syms
= permissionSymbols
;
947 sp
<AaptSymbols
> symbols
=
948 assets
->getSymbolsFor(String8("Manifest"));
949 syms
= permissionSymbols
= symbols
->addNestedSymbol(
950 String8("permission"), srcPos
);
953 syms
= permissionGroupSymbols
;
955 sp
<AaptSymbols
> symbols
=
956 assets
->getSymbolsFor(String8("Manifest"));
957 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
958 String8("permission_group"), srcPos
);
962 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
963 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
965 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
966 manifestPath
.string(), block
.getLineNumber(),
967 String8(block
.getElementName(&len
)).string());
972 char* p
= idStr
.lockBuffer(idStr
.size());
973 char* e
= p
+ idStr
.size();
974 bool begins_with_digit
= true; // init to true so an empty string fails
977 if (*e
>= '0' && *e
<= '9') {
978 begins_with_digit
= true;
981 if ((*e
>= 'a' && *e
<= 'z') ||
982 (*e
>= 'A' && *e
<= 'Z') ||
984 begins_with_digit
= false;
987 if (isGroup
&& (*e
== '-')) {
989 begins_with_digit
= false;
995 idStr
.unlockBuffer();
996 // verify that we stopped because we hit a period or
997 // the beginning of the string, and that the
998 // identifier didn't begin with a digit.
999 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
1001 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
1002 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
1005 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
1006 const uint16_t* cmt
= block
.getComment(&len
);
1007 if (cmt
!= NULL
&& *cmt
!= 0) {
1008 //printf("Comment of %s: %s\n", String8(e).string(),
1009 // String8(cmt).string());
1010 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
1012 //printf("No comment for %s\n", String8(e).string());
1014 syms
->makeSymbolPublic(String8(e
), srcPos
);
1015 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
1016 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1017 packageIdentChars
, true) != ATTR_OKAY
) {
1020 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
1021 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1022 classIdentChars
, true) != ATTR_OKAY
) {
1025 if (validateAttr(manifestPath
, block
,
1026 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
1027 packageIdentChars
, true) != ATTR_OKAY
) {
1030 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
1031 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1032 classIdentChars
, false) != ATTR_OKAY
) {
1035 if (validateAttr(manifestPath
, block
,
1036 RESOURCES_ANDROID_NAMESPACE
, "permission",
1037 packageIdentChars
, false) != ATTR_OKAY
) {
1040 if (validateAttr(manifestPath
, block
,
1041 RESOURCES_ANDROID_NAMESPACE
, "process",
1042 processIdentChars
, false) != ATTR_OKAY
) {
1045 if (validateAttr(manifestPath
, block
,
1046 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1047 processIdentChars
, false) != ATTR_OKAY
) {
1050 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
1051 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1052 classIdentChars
, true) != ATTR_OKAY
) {
1055 if (validateAttr(manifestPath
, block
,
1056 RESOURCES_ANDROID_NAMESPACE
, "authorities",
1057 authoritiesIdentChars
, true) != ATTR_OKAY
) {
1060 if (validateAttr(manifestPath
, block
,
1061 RESOURCES_ANDROID_NAMESPACE
, "permission",
1062 packageIdentChars
, false) != ATTR_OKAY
) {
1065 if (validateAttr(manifestPath
, block
,
1066 RESOURCES_ANDROID_NAMESPACE
, "process",
1067 processIdentChars
, false) != ATTR_OKAY
) {
1070 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1071 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1072 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1073 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1074 classIdentChars
, true) != ATTR_OKAY
) {
1077 if (validateAttr(manifestPath
, block
,
1078 RESOURCES_ANDROID_NAMESPACE
, "permission",
1079 packageIdentChars
, false) != ATTR_OKAY
) {
1082 if (validateAttr(manifestPath
, block
,
1083 RESOURCES_ANDROID_NAMESPACE
, "process",
1084 processIdentChars
, false) != ATTR_OKAY
) {
1087 if (validateAttr(manifestPath
, block
,
1088 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1089 processIdentChars
, false) != ATTR_OKAY
) {
1092 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1093 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1094 if (validateAttr(manifestPath
, block
,
1095 RESOURCES_ANDROID_NAMESPACE
, "name",
1096 packageIdentChars
, true) != ATTR_OKAY
) {
1099 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1100 if (validateAttr(manifestPath
, block
,
1101 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1102 typeIdentChars
, true) != ATTR_OKAY
) {
1105 if (validateAttr(manifestPath
, block
,
1106 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1107 schemeIdentChars
, true) != ATTR_OKAY
) {
1114 if (table
.validateLocalizations()) {
1119 return UNKNOWN_ERROR
;
1122 // Generate final compiled manifest file.
1123 manifestFile
->clearData();
1124 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1125 if (manifestTree
== NULL
) {
1126 return UNKNOWN_ERROR
;
1128 err
= massageManifest(bundle
, manifestTree
);
1129 if (err
< NO_ERROR
) {
1132 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1133 if (err
< NO_ERROR
) {
1138 //printXMLBlock(&block);
1140 // --------------------------------------------------------------
1141 // Generate the final resource table.
1142 // Re-flatten because we may have added new resource IDs
1143 // --------------------------------------------------------------
1145 if (table
.hasResources()) {
1146 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1147 err
= table
.addSymbols(symbols
);
1148 if (err
< NO_ERROR
) {
1152 sp
<AaptFile
> resFile(getResourceFile(assets
));
1153 if (resFile
== NULL
) {
1154 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1155 return UNKNOWN_ERROR
;
1158 err
= table
.flatten(bundle
, resFile
);
1159 if (err
< NO_ERROR
) {
1163 if (bundle
->getPublicOutputFile()) {
1164 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1166 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1167 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1168 return UNKNOWN_ERROR
;
1170 if (bundle
->getVerbose()) {
1171 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1173 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1179 rt
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1180 printf("Generated resources:\n");
1184 // These resources are now considered to be a part of the included
1185 // resources, for others to reference.
1186 err
= assets
->addIncludedResources(resFile
);
1187 if (err
< NO_ERROR
) {
1188 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1195 static const char* getIndentSpace(int indent
)
1197 static const char whitespace
[] =
1200 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1203 static status_t
fixupSymbol(String16
* inoutSymbol
)
1205 inoutSymbol
->replaceAll('.', '_');
1206 inoutSymbol
->replaceAll(':', '_');
1210 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1211 const String8
& name
,
1212 String16
* outTypeComment
= NULL
)
1214 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1216 //printf("Got R symbols!\n");
1217 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1219 //printf("Got attrs symbols! comment %s=%s\n",
1220 // name.string(), String8(asym->getComment(name)).string());
1221 if (outTypeComment
!= NULL
) {
1222 *outTypeComment
= asym
->getTypeComment(name
);
1224 return asym
->getComment(name
);
1230 static status_t
writeLayoutClasses(
1231 FILE* fp
, const sp
<AaptAssets
>& assets
,
1232 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1234 const char* indentStr
= getIndentSpace(indent
);
1235 if (!includePrivate
) {
1236 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1238 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1241 String16
attr16("attr");
1242 String16
package16(assets
->getPackage());
1244 indentStr
= getIndentSpace(indent
);
1245 bool hasErrors
= false;
1248 size_t N
= symbols
->getNestedSymbols().size();
1249 for (i
=0; i
<N
; i
++) {
1250 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1251 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1252 String8
realClassName(nclassName16
);
1253 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1256 String8
nclassName(nclassName16
);
1258 SortedVector
<uint32_t> idents
;
1259 Vector
<uint32_t> origOrder
;
1260 Vector
<bool> publicFlags
;
1263 size_t NA
= nsymbols
->getSymbols().size();
1264 for (a
=0; a
<NA
; a
++) {
1265 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1266 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1268 bool isPublic
= true;
1270 String16
name16(sym
.name
);
1271 uint32_t typeSpecFlags
;
1272 code
= assets
->getIncludedResources().identifierForName(
1273 name16
.string(), name16
.size(),
1274 attr16
.string(), attr16
.size(),
1275 package16
.string(), package16
.size(), &typeSpecFlags
);
1277 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1278 nclassName
.string(), sym
.name
.string());
1281 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1284 origOrder
.add(code
);
1285 publicFlags
.add(isPublic
);
1290 bool deprecated
= false;
1292 String16 comment
= symbols
->getComment(realClassName
);
1293 fprintf(fp
, "%s/** ", indentStr
);
1294 if (comment
.size() > 0) {
1295 String8
cmt(comment
);
1296 fprintf(fp
, "%s\n", cmt
.string());
1297 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1301 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1303 bool hasTable
= false;
1304 for (a
=0; a
<NA
; a
++) {
1305 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1310 "%s <p>Includes the following attributes:</p>\n"
1312 "%s <colgroup align=\"left\" />\n"
1313 "%s <colgroup align=\"left\" />\n"
1314 "%s <tr><th>Attribute</th><th>Description</th></tr>\n",
1321 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1322 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1325 String8
name8(sym
.name
);
1326 String16
comment(sym
.comment
);
1327 if (comment
.size() <= 0) {
1328 comment
= getAttributeComment(assets
, name8
);
1330 if (comment
.size() > 0) {
1331 const char16_t* p
= comment
.string();
1332 while (*p
!= 0 && *p
!= '.') {
1334 while (*p
!= 0 && *p
!= '}') {
1344 comment
= String16(comment
.string(), p
-comment
.string());
1346 String16
name(name8
);
1348 fprintf(fp
, "%s <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n",
1349 indentStr
, nclassName
.string(),
1350 String8(name
).string(),
1351 assets
->getPackage().string(),
1352 String8(name
).string(),
1353 String8(comment
).string());
1357 fprintf(fp
, "%s </table>\n", indentStr
);
1359 for (a
=0; a
<NA
; a
++) {
1360 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1362 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1363 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1366 String16
name(sym
.name
);
1368 fprintf(fp
, "%s @see #%s_%s\n",
1369 indentStr
, nclassName
.string(),
1370 String8(name
).string());
1373 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1376 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1380 "%spublic static final int[] %s = {\n"
1382 indentStr
, nclassName
.string(),
1383 getIndentSpace(indent
+1));
1385 for (a
=0; a
<NA
; a
++) {
1388 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1393 fprintf(fp
, "0x%08x", idents
[a
]);
1396 fprintf(fp
, "\n%s};\n", indentStr
);
1398 for (a
=0; a
<NA
; a
++) {
1399 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1401 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1402 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1405 String8
name8(sym
.name
);
1406 String16
comment(sym
.comment
);
1407 String16 typeComment
;
1408 if (comment
.size() <= 0) {
1409 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1411 getAttributeComment(assets
, name8
, &typeComment
);
1413 String16
name(name8
);
1414 if (fixupSymbol(&name
) != NO_ERROR
) {
1418 uint32_t typeSpecFlags
= 0;
1419 String16
name16(sym
.name
);
1420 assets
->getIncludedResources().identifierForName(
1421 name16
.string(), name16
.size(),
1422 attr16
.string(), attr16
.size(),
1423 package16
.string(), package16
.size(), &typeSpecFlags
);
1424 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1425 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1426 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1428 bool deprecated
= false;
1430 fprintf(fp
, "%s/**\n", indentStr
);
1431 if (comment
.size() > 0) {
1432 String8
cmt(comment
);
1433 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1434 fprintf(fp
, "%s %s\n", indentStr
, cmt
.string());
1435 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1440 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1441 "%s attribute's value can be found in the {@link #%s} array.\n",
1443 pub
? assets
->getPackage().string()
1444 : assets
->getSymbolsPrivatePackage().string(),
1445 String8(name
).string(),
1446 indentStr
, nclassName
.string());
1448 if (typeComment
.size() > 0) {
1449 String8
cmt(typeComment
);
1450 fprintf(fp
, "\n\n%s %s\n", indentStr
, cmt
.string());
1451 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1455 if (comment
.size() > 0) {
1458 "%s <p>This corresponds to the global attribute"
1459 "%s resource symbol {@link %s.R.attr#%s}.\n",
1460 indentStr
, indentStr
,
1461 assets
->getPackage().string(),
1462 String8(name
).string());
1465 "%s <p>This is a private symbol.\n", indentStr
);
1468 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1469 "android", String8(name
).string());
1470 fprintf(fp
, "%s*/\n", indentStr
);
1472 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1475 "%spublic static final int %s_%s = %d;\n",
1476 indentStr
, nclassName
.string(),
1477 String8(name
).string(), (int)pos
);
1483 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1484 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1487 static status_t
writeSymbolClass(
1488 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1489 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
)
1491 fprintf(fp
, "%spublic %sfinal class %s {\n",
1492 getIndentSpace(indent
),
1493 indent
!= 0 ? "static " : "", className
.string());
1497 status_t err
= NO_ERROR
;
1499 size_t N
= symbols
->getSymbols().size();
1500 for (i
=0; i
<N
; i
++) {
1501 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1502 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1505 if (!includePrivate
&& !sym
.isPublic
) {
1508 String16
name(sym
.name
);
1509 String8
realName(name
);
1510 if (fixupSymbol(&name
) != NO_ERROR
) {
1511 return UNKNOWN_ERROR
;
1513 String16
comment(sym
.comment
);
1514 bool haveComment
= false;
1515 bool deprecated
= false;
1516 if (comment
.size() > 0) {
1518 String8
cmt(comment
);
1521 getIndentSpace(indent
), cmt
.string());
1522 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1525 } else if (sym
.isPublic
&& !includePrivate
) {
1526 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1527 assets
->getPackage().string(), className
.string(),
1528 String8(sym
.name
).string());
1530 String16
typeComment(sym
.typeComment
);
1531 if (typeComment
.size() > 0) {
1532 String8
cmt(typeComment
);
1536 "%s/** %s\n", getIndentSpace(indent
), cmt
.string());
1539 "%s %s\n", getIndentSpace(indent
), cmt
.string());
1541 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1546 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1549 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1551 fprintf(fp
, "%spublic static final int %s=0x%08x;\n",
1552 getIndentSpace(indent
),
1553 String8(name
).string(), (int)sym
.int32Val
);
1556 for (i
=0; i
<N
; i
++) {
1557 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1558 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1561 if (!includePrivate
&& !sym
.isPublic
) {
1564 String16
name(sym
.name
);
1565 if (fixupSymbol(&name
) != NO_ERROR
) {
1566 return UNKNOWN_ERROR
;
1568 String16
comment(sym
.comment
);
1569 bool deprecated
= false;
1570 if (comment
.size() > 0) {
1571 String8
cmt(comment
);
1575 getIndentSpace(indent
), cmt
.string(),
1576 getIndentSpace(indent
));
1577 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1580 } else if (sym
.isPublic
&& !includePrivate
) {
1581 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1582 assets
->getPackage().string(), className
.string(),
1583 String8(sym
.name
).string());
1586 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1588 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1589 getIndentSpace(indent
),
1590 String8(name
).string(), sym
.stringVal
.string());
1593 sp
<AaptSymbols
> styleableSymbols
;
1595 N
= symbols
->getNestedSymbols().size();
1596 for (i
=0; i
<N
; i
++) {
1597 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1598 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1599 if (nclassName
== "styleable") {
1600 styleableSymbols
= nsymbols
;
1602 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
);
1604 if (err
!= NO_ERROR
) {
1609 if (styleableSymbols
!= NULL
) {
1610 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1611 if (err
!= NO_ERROR
) {
1617 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1621 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1622 const String8
& package
, bool includePrivate
)
1624 if (!bundle
->getRClassDir()) {
1628 const size_t N
= assets
->getSymbols().size();
1629 for (size_t i
=0; i
<N
; i
++) {
1630 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1631 String8
className(assets
->getSymbols().keyAt(i
));
1632 String8
dest(bundle
->getRClassDir());
1633 if (bundle
->getMakePackageDirs()) {
1634 String8
pkg(package
);
1635 const char* last
= pkg
.string();
1636 const char* s
= last
-1;
1639 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1640 String8
part(last
, s
-last
);
1641 dest
.appendPath(part
);
1642 #ifdef HAVE_MS_C_RUNTIME
1643 _mkdir(dest
.string());
1645 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1651 dest
.appendPath(className
);
1652 dest
.append(".java");
1653 FILE* fp
= fopen(dest
.string(), "w+");
1655 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1656 dest
.string(), strerror(errno
));
1657 return UNKNOWN_ERROR
;
1659 if (bundle
->getVerbose()) {
1660 printf(" Writing symbols for class %s.\n", className
.string());
1664 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1666 " * This class was automatically generated by the\n"
1667 " * aapt tool from the resource data it found. It\n"
1668 " * should not be modified by hand.\n"
1671 "package %s;\n\n", package
.string());
1673 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0);
1674 if (err
!= NO_ERROR
) {
1685 class ProguardKeepSet
1688 // { rule --> { file locations } }
1689 KeyedVector
<String8
, SortedVector
<String8
> > rules
;
1691 void add(const String8
& rule
, const String8
& where
);
1694 void ProguardKeepSet::add(const String8
& rule
, const String8
& where
)
1696 ssize_t index
= rules
.indexOfKey(rule
);
1698 index
= rules
.add(rule
, SortedVector
<String8
>());
1700 rules
.editValueAt(index
).add(where
);
1704 writeProguardForAndroidManifest(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
1709 ResXMLTree::event_code_t code
;
1711 bool inApplication
= false;
1713 sp
<AaptGroup
> assGroup
;
1714 sp
<AaptFile
> assFile
;
1717 // First, look for a package file to parse. This is required to
1718 // be able to generate the resource information.
1719 assGroup
= assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
1720 if (assGroup
== NULL
) {
1721 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
1725 if (assGroup
->getFiles().size() != 1) {
1726 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
1727 assGroup
->getFiles().valueAt(0)->getPrintableSource().string());
1730 assFile
= assGroup
->getFiles().valueAt(0);
1732 err
= parseXMLResource(assFile
, &tree
);
1733 if (err
!= NO_ERROR
) {
1739 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1740 if (code
== ResXMLTree::END_TAG
) {
1741 if (/* name == "Application" && */ depth
== 2) {
1742 inApplication
= false;
1747 if (code
!= ResXMLTree::START_TAG
) {
1751 String8
tag(tree
.getElementName(&len
));
1752 // printf("Depth %d tag %s\n", depth, tag.string());
1754 if (tag
!= "manifest") {
1755 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
1758 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
1759 } else if (depth
== 2 && tag
== "application") {
1760 inApplication
= true;
1762 if (inApplication
) {
1763 if (tag
== "application" || tag
== "activity" || tag
== "service" || tag
== "receiver"
1764 || tag
== "provider") {
1765 String8 name
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
1768 fprintf(stderr
, "ERROR: %s\n", error
.string());
1771 if (name
.length() > 0) {
1772 // asdf --> package.asdf
1773 // .asdf .a.b --> package.asdf package.a.b
1774 // asdf.adsf --> asdf.asdf
1775 String8
rule("-keep class ");
1776 const char* p
= name
.string();
1777 const char* q
= strchr(p
, '.');
1781 } else if (q
== NULL
) {
1789 String8 location
= tag
;
1791 location
+= assFile
->getSourceFile();
1793 sprintf(lineno
, ":%d", tree
.getLineNumber());
1796 keep
->add(rule
, location
);
1806 writeProguardForLayout(ProguardKeepSet
* keep
, const sp
<AaptFile
>& layoutFile
)
1811 ResXMLTree::event_code_t code
;
1813 err
= parseXMLResource(layoutFile
, &tree
);
1814 if (err
!= NO_ERROR
) {
1820 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1821 if (code
!= ResXMLTree::START_TAG
) {
1824 String8
tag(tree
.getElementName(&len
));
1826 // If there is no '.', we'll assume that it's one of the built in names.
1827 if (strchr(tag
.string(), '.')) {
1828 String8
rule("-keep class ");
1830 rule
+= " { <init>(...); }";
1832 String8
location("view ");
1833 location
+= layoutFile
->getSourceFile();
1835 sprintf(lineno
, ":%d", tree
.getLineNumber());
1838 keep
->add(rule
, location
);
1846 writeProguardForLayouts(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
1849 sp
<AaptDir
> layout
= assets
->resDir(String8("layout"));
1851 if (layout
!= NULL
) {
1852 const KeyedVector
<String8
,sp
<AaptGroup
> > groups
= layout
->getFiles();
1853 const size_t N
= groups
.size();
1854 for (size_t i
=0; i
<N
; i
++) {
1855 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
1856 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
= group
->getFiles();
1857 const size_t M
= files
.size();
1858 for (size_t j
=0; j
<M
; j
++) {
1859 err
= writeProguardForLayout(keep
, files
.valueAt(j
));
1870 writeProguardFile(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
1874 if (!bundle
->getProguardFile()) {
1878 ProguardKeepSet keep
;
1880 err
= writeProguardForAndroidManifest(&keep
, assets
);
1885 err
= writeProguardForLayouts(&keep
, assets
);
1890 FILE* fp
= fopen(bundle
->getProguardFile(), "w+");
1892 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1893 bundle
->getProguardFile(), strerror(errno
));
1894 return UNKNOWN_ERROR
;
1897 const KeyedVector
<String8
, SortedVector
<String8
> >& rules
= keep
.rules
;
1898 const size_t N
= rules
.size();
1899 for (size_t i
=0; i
<N
; i
++) {
1900 const SortedVector
<String8
>& locations
= rules
.valueAt(i
);
1901 const size_t M
= locations
.size();
1902 for (size_t j
=0; j
<M
; j
++) {
1903 fprintf(fp
, "# %s\n", locations
.itemAt(j
).string());
1905 fprintf(fp
, "%s\n\n", rules
.keyAt(i
).string());