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 ui=%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] : '-',
110 mParams
.orientation
, mParams
.uiMode
,
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(RESOURCES_ANDROID_NAMESPACE
,
226 if (minSdkIndex
>= 0) {
227 const uint16_t* minSdk16
= block
.getAttributeStringValue(minSdkIndex
, &len
);
228 const char* minSdk8
= strdup(String8(minSdk16
).string());
229 bundle
->setMinSdkVersion(minSdk8
);
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 %ld has flavor %s\n", i
,
493 baseFiles
.keyAt(i
).toString().string());
495 for (size_t i
=0; i
< overlayFiles
.size(); i
++) {
496 printf("overlayFile %ld 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 (%ld) 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 static void fullyQualifyClassName(const String8
& package
, sp
<XMLNode
> node
,
566 const String16
& attrName
) {
567 XMLNode::attribute_entry
* attr
= node
->editAttribute(
568 String16("http://schemas.android.com/apk/res/android"), attrName
);
570 String8
name(attr
->string
);
572 // asdf --> package.asdf
573 // .asdf .a.b --> package.asdf package.a.b
574 // asdf.adsf --> asdf.asdf
576 const char* p
= name
.string();
577 const char* q
= strchr(p
, '.');
579 className
+= package
;
581 } else if (q
== NULL
) {
582 className
+= package
;
588 NOISY(printf("Qualifying class '%s' to '%s'", name
.string(), className
.string()));
589 attr
->string
.setTo(String16(className
));
593 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
595 root
= root
->searchElement(String16(), String16("manifest"));
597 fprintf(stderr
, "No <manifest> tag.\n");
598 return UNKNOWN_ERROR
;
601 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
602 bundle
->getVersionCode());
603 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
604 bundle
->getVersionName());
606 if (bundle
->getMinSdkVersion() != NULL
607 || bundle
->getTargetSdkVersion() != NULL
608 || bundle
->getMaxSdkVersion() != NULL
) {
609 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
611 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
612 root
->insertChildAt(vers
, 0);
615 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
616 bundle
->getMinSdkVersion());
617 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
618 bundle
->getTargetSdkVersion());
619 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
620 bundle
->getMaxSdkVersion());
623 // Deal with manifest package name overrides
624 const char* manifestPackageNameOverride
= bundle
->getManifestPackageNameOverride();
625 if (manifestPackageNameOverride
!= NULL
) {
626 // Update the actual package name
627 XMLNode::attribute_entry
* attr
= root
->editAttribute(String16(), String16("package"));
629 fprintf(stderr
, "package name is required with --rename-manifest-package.\n");
630 return UNKNOWN_ERROR
;
632 String8
origPackage(attr
->string
);
633 attr
->string
.setTo(String16(manifestPackageNameOverride
));
634 NOISY(printf("Overriding package '%s' to be '%s'\n", origPackage
.string(), manifestPackageNameOverride
));
636 // Make class names fully qualified
637 sp
<XMLNode
> application
= root
->getChildElement(String16(), String16("application"));
638 if (application
!= NULL
) {
639 fullyQualifyClassName(origPackage
, application
, String16("name"));
641 Vector
<sp
<XMLNode
> >& children
= const_cast<Vector
<sp
<XMLNode
> >&>(application
->getChildren());
642 for (size_t i
= 0; i
< children
.size(); i
++) {
643 sp
<XMLNode
> child
= children
.editItemAt(i
);
644 String8
tag(child
->getElementName());
645 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
646 fullyQualifyClassName(origPackage
, child
, String16("name"));
647 } else if (tag
== "activity-alias") {
648 fullyQualifyClassName(origPackage
, child
, String16("name"));
649 fullyQualifyClassName(origPackage
, child
, String16("targetActivity"));
655 // Deal with manifest package name overrides
656 const char* instrumentationPackageNameOverride
= bundle
->getInstrumentationPackageNameOverride();
657 if (instrumentationPackageNameOverride
!= NULL
) {
658 // Fix up instrumentation targets.
659 Vector
<sp
<XMLNode
> >& children
= const_cast<Vector
<sp
<XMLNode
> >&>(root
->getChildren());
660 for (size_t i
= 0; i
< children
.size(); i
++) {
661 sp
<XMLNode
> child
= children
.editItemAt(i
);
662 String8
tag(child
->getElementName());
663 if (tag
== "instrumentation") {
664 XMLNode::attribute_entry
* attr
= child
->editAttribute(
665 String16("http://schemas.android.com/apk/res/android"), String16("targetPackage"));
667 attr
->string
.setTo(String16(instrumentationPackageNameOverride
));
676 #define ASSIGN_IT(n) \
678 ssize_t index = resources->indexOfKey(String8(#n)); \
680 n ## s = resources->valueAt(index); \
684 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
686 // First, look for a package file to parse. This is required to
687 // be able to generate the resource information.
688 sp
<AaptGroup
> androidManifestFile
=
689 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
690 if (androidManifestFile
== NULL
) {
691 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
692 return UNKNOWN_ERROR
;
695 status_t err
= parsePackage(bundle
, assets
, androidManifestFile
);
696 if (err
!= NO_ERROR
) {
700 NOISY(printf("Creating resources for package %s\n",
701 assets
->getPackage().string()));
703 ResourceTable
table(bundle
, String16(assets
->getPackage()));
704 err
= table
.addIncludedResources(bundle
, assets
);
705 if (err
!= NO_ERROR
) {
709 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
711 // Standard flags for compiled XML and optional UTF-8 encoding
712 int xmlFlags
= XML_COMPILE_STANDARD_RESOURCE
;
713 if (bundle
->getUTF8()) {
714 xmlFlags
|= XML_COMPILE_UTF8
;
717 // --------------------------------------------------------------
718 // First, gather all resource information.
719 // --------------------------------------------------------------
721 // resType -> leafName -> group
722 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
723 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
724 collect_files(assets
, resources
);
726 sp
<ResourceTypeSet
> drawables
;
727 sp
<ResourceTypeSet
> layouts
;
728 sp
<ResourceTypeSet
> anims
;
729 sp
<ResourceTypeSet
> xmls
;
730 sp
<ResourceTypeSet
> raws
;
731 sp
<ResourceTypeSet
> colors
;
732 sp
<ResourceTypeSet
> menus
;
742 assets
->setResources(resources
);
743 // now go through any resource overlays and collect their files
744 sp
<AaptAssets
> current
= assets
->getOverlay();
745 while(current
.get()) {
746 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
747 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
748 current
->setResources(resources
);
749 collect_files(current
, resources
);
750 current
= current
->getOverlay();
752 // apply the overlay files to the base set
753 if (!applyFileOverlay(bundle
, assets
, drawables
, "drawable") ||
754 !applyFileOverlay(bundle
, assets
, layouts
, "layout") ||
755 !applyFileOverlay(bundle
, assets
, anims
, "anim") ||
756 !applyFileOverlay(bundle
, assets
, xmls
, "xml") ||
757 !applyFileOverlay(bundle
, assets
, raws
, "raw") ||
758 !applyFileOverlay(bundle
, assets
, colors
, "color") ||
759 !applyFileOverlay(bundle
, assets
, menus
, "menu")) {
760 return UNKNOWN_ERROR
;
763 bool hasErrors
= false;
765 if (drawables
!= NULL
) {
766 err
= preProcessImages(bundle
, assets
, drawables
);
767 if (err
== NO_ERROR
) {
768 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
769 if (err
!= NO_ERROR
) {
777 if (layouts
!= NULL
) {
778 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
779 if (err
!= NO_ERROR
) {
785 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
786 if (err
!= NO_ERROR
) {
792 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
793 if (err
!= NO_ERROR
) {
799 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
800 if (err
!= NO_ERROR
) {
807 while(current
.get()) {
808 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
809 current
->getResources();
811 ssize_t index
= resources
->indexOfKey(String8("values"));
813 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
815 while ((res
=it
.next()) == NO_ERROR
) {
816 sp
<AaptFile
> file
= it
.getFile();
817 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
818 (current
!=assets
), &table
);
819 if (res
!= NO_ERROR
) {
824 current
= current
->getOverlay();
827 if (colors
!= NULL
) {
828 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
829 if (err
!= NO_ERROR
) {
835 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
836 if (err
!= NO_ERROR
) {
841 // --------------------------------------------------------------------
842 // Assignment of resource IDs and initial generation of resource table.
843 // --------------------------------------------------------------------
845 if (table
.hasResources()) {
846 sp
<AaptFile
> resFile(getResourceFile(assets
));
847 if (resFile
== NULL
) {
848 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
849 return UNKNOWN_ERROR
;
852 err
= table
.assignResourceIds();
853 if (err
< NO_ERROR
) {
858 // --------------------------------------------------------------
859 // Finally, we can now we can compile XML files, which may reference
861 // --------------------------------------------------------------
863 if (layouts
!= NULL
) {
864 ResourceDirIterator
it(layouts
, String8("layout"));
865 while ((err
=it
.next()) == NO_ERROR
) {
866 String8 src
= it
.getFile()->getPrintableSource();
867 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
868 if (err
== NO_ERROR
) {
870 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
871 checkForIds(src
, block
);
877 if (err
< NO_ERROR
) {
884 ResourceDirIterator
it(anims
, String8("anim"));
885 while ((err
=it
.next()) == NO_ERROR
) {
886 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
887 if (err
!= NO_ERROR
) {
892 if (err
< NO_ERROR
) {
899 ResourceDirIterator
it(xmls
, String8("xml"));
900 while ((err
=it
.next()) == NO_ERROR
) {
901 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
902 if (err
!= NO_ERROR
) {
907 if (err
< NO_ERROR
) {
913 if (drawables
!= NULL
) {
914 err
= postProcessImages(assets
, &table
, drawables
);
915 if (err
!= NO_ERROR
) {
920 if (colors
!= NULL
) {
921 ResourceDirIterator
it(colors
, String8("color"));
922 while ((err
=it
.next()) == NO_ERROR
) {
923 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
924 if (err
!= NO_ERROR
) {
929 if (err
< NO_ERROR
) {
936 ResourceDirIterator
it(menus
, String8("menu"));
937 while ((err
=it
.next()) == NO_ERROR
) {
938 String8 src
= it
.getFile()->getPrintableSource();
939 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
940 if (err
!= NO_ERROR
) {
944 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
945 checkForIds(src
, block
);
948 if (err
< NO_ERROR
) {
954 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
955 String8
manifestPath(manifestFile
->getPrintableSource());
957 // Perform a basic validation of the manifest file. This time we
958 // parse it with the comments intact, so that we can use them to
959 // generate java docs... so we are not going to write this one
960 // back out to the final manifest data.
961 err
= compileXmlFile(assets
, manifestFile
, &table
,
962 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
963 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
964 if (err
< NO_ERROR
) {
968 block
.setTo(manifestFile
->getData(), manifestFile
->getSize(), true);
969 String16
manifest16("manifest");
970 String16
permission16("permission");
971 String16
permission_group16("permission-group");
972 String16
uses_permission16("uses-permission");
973 String16
instrumentation16("instrumentation");
974 String16
application16("application");
975 String16
provider16("provider");
976 String16
service16("service");
977 String16
receiver16("receiver");
978 String16
activity16("activity");
979 String16
action16("action");
980 String16
category16("category");
981 String16
data16("scheme");
982 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
983 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
984 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
985 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
986 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
987 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
988 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
989 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
990 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
991 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
992 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
993 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
994 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
995 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
996 ResXMLTree::event_code_t code
;
997 sp
<AaptSymbols
> permissionSymbols
;
998 sp
<AaptSymbols
> permissionGroupSymbols
;
999 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1000 && code
> ResXMLTree::BAD_DOCUMENT
) {
1001 if (code
== ResXMLTree::START_TAG
) {
1003 if (block
.getElementNamespace(&len
) != NULL
) {
1006 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
1007 if (validateAttr(manifestPath
, block
, NULL
, "package",
1008 packageIdentChars
, true) != ATTR_OKAY
) {
1011 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
1012 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
1013 const bool isGroup
= strcmp16(block
.getElementName(&len
),
1014 permission_group16
.string()) == 0;
1015 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1016 isGroup
? packageIdentCharsWithTheStupid
1017 : packageIdentChars
, true) != ATTR_OKAY
) {
1020 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
1021 sp
<AaptSymbols
> syms
;
1023 syms
= permissionSymbols
;
1025 sp
<AaptSymbols
> symbols
=
1026 assets
->getSymbolsFor(String8("Manifest"));
1027 syms
= permissionSymbols
= symbols
->addNestedSymbol(
1028 String8("permission"), srcPos
);
1031 syms
= permissionGroupSymbols
;
1033 sp
<AaptSymbols
> symbols
=
1034 assets
->getSymbolsFor(String8("Manifest"));
1035 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
1036 String8("permission_group"), srcPos
);
1040 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
1041 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
1043 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
1044 manifestPath
.string(), block
.getLineNumber(),
1045 String8(block
.getElementName(&len
)).string());
1050 char* p
= idStr
.lockBuffer(idStr
.size());
1051 char* e
= p
+ idStr
.size();
1052 bool begins_with_digit
= true; // init to true so an empty string fails
1055 if (*e
>= '0' && *e
<= '9') {
1056 begins_with_digit
= true;
1059 if ((*e
>= 'a' && *e
<= 'z') ||
1060 (*e
>= 'A' && *e
<= 'Z') ||
1062 begins_with_digit
= false;
1065 if (isGroup
&& (*e
== '-')) {
1067 begins_with_digit
= false;
1073 idStr
.unlockBuffer();
1074 // verify that we stopped because we hit a period or
1075 // the beginning of the string, and that the
1076 // identifier didn't begin with a digit.
1077 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
1079 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
1080 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
1083 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
1084 const uint16_t* cmt
= block
.getComment(&len
);
1085 if (cmt
!= NULL
&& *cmt
!= 0) {
1086 //printf("Comment of %s: %s\n", String8(e).string(),
1087 // String8(cmt).string());
1088 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
1090 //printf("No comment for %s\n", String8(e).string());
1092 syms
->makeSymbolPublic(String8(e
), srcPos
);
1093 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
1094 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1095 packageIdentChars
, true) != ATTR_OKAY
) {
1098 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
1099 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1100 classIdentChars
, true) != ATTR_OKAY
) {
1103 if (validateAttr(manifestPath
, block
,
1104 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
1105 packageIdentChars
, true) != ATTR_OKAY
) {
1108 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
1109 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1110 classIdentChars
, false) != ATTR_OKAY
) {
1113 if (validateAttr(manifestPath
, block
,
1114 RESOURCES_ANDROID_NAMESPACE
, "permission",
1115 packageIdentChars
, false) != ATTR_OKAY
) {
1118 if (validateAttr(manifestPath
, block
,
1119 RESOURCES_ANDROID_NAMESPACE
, "process",
1120 processIdentChars
, false) != ATTR_OKAY
) {
1123 if (validateAttr(manifestPath
, block
,
1124 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1125 processIdentChars
, false) != ATTR_OKAY
) {
1128 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
1129 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1130 classIdentChars
, true) != ATTR_OKAY
) {
1133 if (validateAttr(manifestPath
, block
,
1134 RESOURCES_ANDROID_NAMESPACE
, "authorities",
1135 authoritiesIdentChars
, true) != ATTR_OKAY
) {
1138 if (validateAttr(manifestPath
, block
,
1139 RESOURCES_ANDROID_NAMESPACE
, "permission",
1140 packageIdentChars
, false) != ATTR_OKAY
) {
1143 if (validateAttr(manifestPath
, block
,
1144 RESOURCES_ANDROID_NAMESPACE
, "process",
1145 processIdentChars
, false) != ATTR_OKAY
) {
1148 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1149 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1150 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1151 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name",
1152 classIdentChars
, true) != ATTR_OKAY
) {
1155 if (validateAttr(manifestPath
, block
,
1156 RESOURCES_ANDROID_NAMESPACE
, "permission",
1157 packageIdentChars
, false) != ATTR_OKAY
) {
1160 if (validateAttr(manifestPath
, block
,
1161 RESOURCES_ANDROID_NAMESPACE
, "process",
1162 processIdentChars
, false) != ATTR_OKAY
) {
1165 if (validateAttr(manifestPath
, block
,
1166 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1167 processIdentChars
, false) != ATTR_OKAY
) {
1170 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1171 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1172 if (validateAttr(manifestPath
, block
,
1173 RESOURCES_ANDROID_NAMESPACE
, "name",
1174 packageIdentChars
, true) != ATTR_OKAY
) {
1177 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1178 if (validateAttr(manifestPath
, block
,
1179 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1180 typeIdentChars
, true) != ATTR_OKAY
) {
1183 if (validateAttr(manifestPath
, block
,
1184 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1185 schemeIdentChars
, true) != ATTR_OKAY
) {
1192 if (table
.validateLocalizations()) {
1197 return UNKNOWN_ERROR
;
1200 // Generate final compiled manifest file.
1201 manifestFile
->clearData();
1202 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1203 if (manifestTree
== NULL
) {
1204 return UNKNOWN_ERROR
;
1206 err
= massageManifest(bundle
, manifestTree
);
1207 if (err
< NO_ERROR
) {
1210 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1211 if (err
< NO_ERROR
) {
1216 //printXMLBlock(&block);
1218 // --------------------------------------------------------------
1219 // Generate the final resource table.
1220 // Re-flatten because we may have added new resource IDs
1221 // --------------------------------------------------------------
1223 if (table
.hasResources()) {
1224 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1225 err
= table
.addSymbols(symbols
);
1226 if (err
< NO_ERROR
) {
1230 sp
<AaptFile
> resFile(getResourceFile(assets
));
1231 if (resFile
== NULL
) {
1232 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1233 return UNKNOWN_ERROR
;
1236 err
= table
.flatten(bundle
, resFile
);
1237 if (err
< NO_ERROR
) {
1241 if (bundle
->getPublicOutputFile()) {
1242 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1244 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1245 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1246 return UNKNOWN_ERROR
;
1248 if (bundle
->getVerbose()) {
1249 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1251 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1257 rt
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1258 printf("Generated resources:\n");
1262 // These resources are now considered to be a part of the included
1263 // resources, for others to reference.
1264 err
= assets
->addIncludedResources(resFile
);
1265 if (err
< NO_ERROR
) {
1266 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1273 static const char* getIndentSpace(int indent
)
1275 static const char whitespace
[] =
1278 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1281 static status_t
fixupSymbol(String16
* inoutSymbol
)
1283 inoutSymbol
->replaceAll('.', '_');
1284 inoutSymbol
->replaceAll(':', '_');
1288 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1289 const String8
& name
,
1290 String16
* outTypeComment
= NULL
)
1292 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1294 //printf("Got R symbols!\n");
1295 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1297 //printf("Got attrs symbols! comment %s=%s\n",
1298 // name.string(), String8(asym->getComment(name)).string());
1299 if (outTypeComment
!= NULL
) {
1300 *outTypeComment
= asym
->getTypeComment(name
);
1302 return asym
->getComment(name
);
1308 static status_t
writeLayoutClasses(
1309 FILE* fp
, const sp
<AaptAssets
>& assets
,
1310 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1312 const char* indentStr
= getIndentSpace(indent
);
1313 if (!includePrivate
) {
1314 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1316 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1319 String16
attr16("attr");
1320 String16
package16(assets
->getPackage());
1322 indentStr
= getIndentSpace(indent
);
1323 bool hasErrors
= false;
1326 size_t N
= symbols
->getNestedSymbols().size();
1327 for (i
=0; i
<N
; i
++) {
1328 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1329 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1330 String8
realClassName(nclassName16
);
1331 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1334 String8
nclassName(nclassName16
);
1336 SortedVector
<uint32_t> idents
;
1337 Vector
<uint32_t> origOrder
;
1338 Vector
<bool> publicFlags
;
1341 size_t NA
= nsymbols
->getSymbols().size();
1342 for (a
=0; a
<NA
; a
++) {
1343 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1344 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1346 bool isPublic
= true;
1348 String16
name16(sym
.name
);
1349 uint32_t typeSpecFlags
;
1350 code
= assets
->getIncludedResources().identifierForName(
1351 name16
.string(), name16
.size(),
1352 attr16
.string(), attr16
.size(),
1353 package16
.string(), package16
.size(), &typeSpecFlags
);
1355 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1356 nclassName
.string(), sym
.name
.string());
1359 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1362 origOrder
.add(code
);
1363 publicFlags
.add(isPublic
);
1368 bool deprecated
= false;
1370 String16 comment
= symbols
->getComment(realClassName
);
1371 fprintf(fp
, "%s/** ", indentStr
);
1372 if (comment
.size() > 0) {
1373 String8
cmt(comment
);
1374 fprintf(fp
, "%s\n", cmt
.string());
1375 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1379 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1381 bool hasTable
= false;
1382 for (a
=0; a
<NA
; a
++) {
1383 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1388 "%s <p>Includes the following attributes:</p>\n"
1390 "%s <colgroup align=\"left\" />\n"
1391 "%s <colgroup align=\"left\" />\n"
1392 "%s <tr><th>Attribute</th><th>Description</th></tr>\n",
1399 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1400 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1403 String8
name8(sym
.name
);
1404 String16
comment(sym
.comment
);
1405 if (comment
.size() <= 0) {
1406 comment
= getAttributeComment(assets
, name8
);
1408 if (comment
.size() > 0) {
1409 const char16_t* p
= comment
.string();
1410 while (*p
!= 0 && *p
!= '.') {
1412 while (*p
!= 0 && *p
!= '}') {
1422 comment
= String16(comment
.string(), p
-comment
.string());
1424 String16
name(name8
);
1426 fprintf(fp
, "%s <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n",
1427 indentStr
, nclassName
.string(),
1428 String8(name
).string(),
1429 assets
->getPackage().string(),
1430 String8(name
).string(),
1431 String8(comment
).string());
1435 fprintf(fp
, "%s </table>\n", indentStr
);
1437 for (a
=0; a
<NA
; a
++) {
1438 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1440 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1441 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1444 String16
name(sym
.name
);
1446 fprintf(fp
, "%s @see #%s_%s\n",
1447 indentStr
, nclassName
.string(),
1448 String8(name
).string());
1451 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1454 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1458 "%spublic static final int[] %s = {\n"
1460 indentStr
, nclassName
.string(),
1461 getIndentSpace(indent
+1));
1463 for (a
=0; a
<NA
; a
++) {
1466 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1471 fprintf(fp
, "0x%08x", idents
[a
]);
1474 fprintf(fp
, "\n%s};\n", indentStr
);
1476 for (a
=0; a
<NA
; a
++) {
1477 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1479 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1480 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1483 String8
name8(sym
.name
);
1484 String16
comment(sym
.comment
);
1485 String16 typeComment
;
1486 if (comment
.size() <= 0) {
1487 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1489 getAttributeComment(assets
, name8
, &typeComment
);
1491 String16
name(name8
);
1492 if (fixupSymbol(&name
) != NO_ERROR
) {
1496 uint32_t typeSpecFlags
= 0;
1497 String16
name16(sym
.name
);
1498 assets
->getIncludedResources().identifierForName(
1499 name16
.string(), name16
.size(),
1500 attr16
.string(), attr16
.size(),
1501 package16
.string(), package16
.size(), &typeSpecFlags
);
1502 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1503 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1504 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1506 bool deprecated
= false;
1508 fprintf(fp
, "%s/**\n", indentStr
);
1509 if (comment
.size() > 0) {
1510 String8
cmt(comment
);
1511 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1512 fprintf(fp
, "%s %s\n", indentStr
, cmt
.string());
1513 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1518 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1519 "%s attribute's value can be found in the {@link #%s} array.\n",
1521 pub
? assets
->getPackage().string()
1522 : assets
->getSymbolsPrivatePackage().string(),
1523 String8(name
).string(),
1524 indentStr
, nclassName
.string());
1526 if (typeComment
.size() > 0) {
1527 String8
cmt(typeComment
);
1528 fprintf(fp
, "\n\n%s %s\n", indentStr
, cmt
.string());
1529 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1533 if (comment
.size() > 0) {
1536 "%s <p>This corresponds to the global attribute"
1537 "%s resource symbol {@link %s.R.attr#%s}.\n",
1538 indentStr
, indentStr
,
1539 assets
->getPackage().string(),
1540 String8(name
).string());
1543 "%s <p>This is a private symbol.\n", indentStr
);
1546 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1547 "android", String8(name
).string());
1548 fprintf(fp
, "%s*/\n", indentStr
);
1550 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1553 "%spublic static final int %s_%s = %d;\n",
1554 indentStr
, nclassName
.string(),
1555 String8(name
).string(), (int)pos
);
1561 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1562 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1565 static status_t
writeSymbolClass(
1566 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1567 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
)
1569 fprintf(fp
, "%spublic %sfinal class %s {\n",
1570 getIndentSpace(indent
),
1571 indent
!= 0 ? "static " : "", className
.string());
1575 status_t err
= NO_ERROR
;
1577 size_t N
= symbols
->getSymbols().size();
1578 for (i
=0; i
<N
; i
++) {
1579 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1580 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1583 if (!includePrivate
&& !sym
.isPublic
) {
1586 String16
name(sym
.name
);
1587 String8
realName(name
);
1588 if (fixupSymbol(&name
) != NO_ERROR
) {
1589 return UNKNOWN_ERROR
;
1591 String16
comment(sym
.comment
);
1592 bool haveComment
= false;
1593 bool deprecated
= false;
1594 if (comment
.size() > 0) {
1596 String8
cmt(comment
);
1599 getIndentSpace(indent
), cmt
.string());
1600 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1603 } else if (sym
.isPublic
&& !includePrivate
) {
1604 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1605 assets
->getPackage().string(), className
.string(),
1606 String8(sym
.name
).string());
1608 String16
typeComment(sym
.typeComment
);
1609 if (typeComment
.size() > 0) {
1610 String8
cmt(typeComment
);
1614 "%s/** %s\n", getIndentSpace(indent
), cmt
.string());
1617 "%s %s\n", getIndentSpace(indent
), cmt
.string());
1619 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1624 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1627 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1629 fprintf(fp
, "%spublic static final int %s=0x%08x;\n",
1630 getIndentSpace(indent
),
1631 String8(name
).string(), (int)sym
.int32Val
);
1634 for (i
=0; i
<N
; i
++) {
1635 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1636 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1639 if (!includePrivate
&& !sym
.isPublic
) {
1642 String16
name(sym
.name
);
1643 if (fixupSymbol(&name
) != NO_ERROR
) {
1644 return UNKNOWN_ERROR
;
1646 String16
comment(sym
.comment
);
1647 bool deprecated
= false;
1648 if (comment
.size() > 0) {
1649 String8
cmt(comment
);
1653 getIndentSpace(indent
), cmt
.string(),
1654 getIndentSpace(indent
));
1655 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1658 } else if (sym
.isPublic
&& !includePrivate
) {
1659 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1660 assets
->getPackage().string(), className
.string(),
1661 String8(sym
.name
).string());
1664 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1666 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1667 getIndentSpace(indent
),
1668 String8(name
).string(), sym
.stringVal
.string());
1671 sp
<AaptSymbols
> styleableSymbols
;
1673 N
= symbols
->getNestedSymbols().size();
1674 for (i
=0; i
<N
; i
++) {
1675 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1676 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1677 if (nclassName
== "styleable") {
1678 styleableSymbols
= nsymbols
;
1680 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
);
1682 if (err
!= NO_ERROR
) {
1687 if (styleableSymbols
!= NULL
) {
1688 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1689 if (err
!= NO_ERROR
) {
1695 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1699 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1700 const String8
& package
, bool includePrivate
)
1702 if (!bundle
->getRClassDir()) {
1706 const size_t N
= assets
->getSymbols().size();
1707 for (size_t i
=0; i
<N
; i
++) {
1708 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1709 String8
className(assets
->getSymbols().keyAt(i
));
1710 String8
dest(bundle
->getRClassDir());
1711 if (bundle
->getMakePackageDirs()) {
1712 String8
pkg(package
);
1713 const char* last
= pkg
.string();
1714 const char* s
= last
-1;
1717 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1718 String8
part(last
, s
-last
);
1719 dest
.appendPath(part
);
1720 #ifdef HAVE_MS_C_RUNTIME
1721 _mkdir(dest
.string());
1723 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1729 dest
.appendPath(className
);
1730 dest
.append(".java");
1731 FILE* fp
= fopen(dest
.string(), "w+");
1733 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1734 dest
.string(), strerror(errno
));
1735 return UNKNOWN_ERROR
;
1737 if (bundle
->getVerbose()) {
1738 printf(" Writing symbols for class %s.\n", className
.string());
1742 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1744 " * This class was automatically generated by the\n"
1745 " * aapt tool from the resource data it found. It\n"
1746 " * should not be modified by hand.\n"
1749 "package %s;\n\n", package
.string());
1751 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0);
1752 if (err
!= NO_ERROR
) {
1763 class ProguardKeepSet
1766 // { rule --> { file locations } }
1767 KeyedVector
<String8
, SortedVector
<String8
> > rules
;
1769 void add(const String8
& rule
, const String8
& where
);
1772 void ProguardKeepSet::add(const String8
& rule
, const String8
& where
)
1774 ssize_t index
= rules
.indexOfKey(rule
);
1776 index
= rules
.add(rule
, SortedVector
<String8
>());
1778 rules
.editValueAt(index
).add(where
);
1782 writeProguardForAndroidManifest(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
1787 ResXMLTree::event_code_t code
;
1789 bool inApplication
= false;
1791 sp
<AaptGroup
> assGroup
;
1792 sp
<AaptFile
> assFile
;
1795 // First, look for a package file to parse. This is required to
1796 // be able to generate the resource information.
1797 assGroup
= assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
1798 if (assGroup
== NULL
) {
1799 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
1803 if (assGroup
->getFiles().size() != 1) {
1804 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
1805 assGroup
->getFiles().valueAt(0)->getPrintableSource().string());
1808 assFile
= assGroup
->getFiles().valueAt(0);
1810 err
= parseXMLResource(assFile
, &tree
);
1811 if (err
!= NO_ERROR
) {
1817 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1818 if (code
== ResXMLTree::END_TAG
) {
1819 if (/* name == "Application" && */ depth
== 2) {
1820 inApplication
= false;
1825 if (code
!= ResXMLTree::START_TAG
) {
1829 String8
tag(tree
.getElementName(&len
));
1830 // printf("Depth %d tag %s\n", depth, tag.string());
1831 bool keepTag
= false;
1833 if (tag
!= "manifest") {
1834 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
1837 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
1838 } else if (depth
== 2) {
1839 if (tag
== "application") {
1840 inApplication
= true;
1842 } else if (tag
== "instrumentation") {
1846 if (!keepTag
&& inApplication
&& depth
== 3) {
1847 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
1852 String8 name
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
1855 fprintf(stderr
, "ERROR: %s\n", error
.string());
1858 if (name
.length() > 0) {
1859 // asdf --> package.asdf
1860 // .asdf .a.b --> package.asdf package.a.b
1861 // asdf.adsf --> asdf.asdf
1862 String8
rule("-keep class ");
1863 const char* p
= name
.string();
1864 const char* q
= strchr(p
, '.');
1868 } else if (q
== NULL
) {
1876 String8 location
= tag
;
1878 location
+= assFile
->getSourceFile();
1880 sprintf(lineno
, ":%d", tree
.getLineNumber());
1883 keep
->add(rule
, location
);
1892 addProguardKeepRule(ProguardKeepSet
* keep
, const String8
& className
,
1893 const String8
& srcName
, int line
)
1895 String8
rule("-keep class ");
1897 rule
+= " { <init>(...); }";
1899 String8
location("view ");
1900 location
+= srcName
;
1902 sprintf(lineno
, ":%d", line
);
1905 keep
->add(rule
, location
);
1909 writeProguardForXml(ProguardKeepSet
* keep
, const sp
<AaptFile
>& layoutFile
,
1910 const char* startTag
, const char* altTag
)
1915 ResXMLTree::event_code_t code
;
1917 err
= parseXMLResource(layoutFile
, &tree
);
1918 if (err
!= NO_ERROR
) {
1924 if (startTag
!= NULL
) {
1925 bool haveStart
= false;
1926 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1927 if (code
!= ResXMLTree::START_TAG
) {
1930 String8
tag(tree
.getElementName(&len
));
1931 if (tag
== startTag
) {
1941 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
1942 if (code
!= ResXMLTree::START_TAG
) {
1945 String8
tag(tree
.getElementName(&len
));
1947 // If there is no '.', we'll assume that it's one of the built in names.
1948 if (strchr(tag
.string(), '.')) {
1949 addProguardKeepRule(keep
, tag
,
1950 layoutFile
->getPrintableSource(), tree
.getLineNumber());
1951 } else if (altTag
!= NULL
&& tag
== altTag
) {
1952 ssize_t classIndex
= tree
.indexOfAttribute(NULL
, "class");
1953 if (classIndex
< 0) {
1954 fprintf(stderr
, "%s:%d: <view> does not have class attribute.\n",
1955 layoutFile
->getPrintableSource().string(), tree
.getLineNumber());
1958 addProguardKeepRule(keep
,
1959 String8(tree
.getAttributeStringValue(classIndex
, &len
)),
1960 layoutFile
->getPrintableSource(), tree
.getLineNumber());
1969 writeProguardForLayouts(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
1972 const Vector
<sp
<AaptDir
> >& dirs
= assets
->resDirs();
1973 const size_t K
= dirs
.size();
1974 for (size_t k
=0; k
<K
; k
++) {
1975 const sp
<AaptDir
>& d
= dirs
.itemAt(k
);
1976 const String8
& dirName
= d
->getLeaf();
1977 const char* startTag
= NULL
;
1978 const char* altTag
= NULL
;
1979 if ((dirName
== String8("layout")) || (strncmp(dirName
.string(), "layout-", 7) == 0)) {
1981 } else if ((dirName
== String8("xml")) || (strncmp(dirName
.string(), "xml-", 4) == 0)) {
1982 startTag
= "PreferenceScreen";
1987 const KeyedVector
<String8
,sp
<AaptGroup
> > groups
= d
->getFiles();
1988 const size_t N
= groups
.size();
1989 for (size_t i
=0; i
<N
; i
++) {
1990 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
1991 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
= group
->getFiles();
1992 const size_t M
= files
.size();
1993 for (size_t j
=0; j
<M
; j
++) {
1994 err
= writeProguardForXml(keep
, files
.valueAt(j
), startTag
, altTag
);
2005 writeProguardFile(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
2009 if (!bundle
->getProguardFile()) {
2013 ProguardKeepSet keep
;
2015 err
= writeProguardForAndroidManifest(&keep
, assets
);
2020 err
= writeProguardForLayouts(&keep
, assets
);
2025 FILE* fp
= fopen(bundle
->getProguardFile(), "w+");
2027 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
2028 bundle
->getProguardFile(), strerror(errno
));
2029 return UNKNOWN_ERROR
;
2032 const KeyedVector
<String8
, SortedVector
<String8
> >& rules
= keep
.rules
;
2033 const size_t N
= rules
.size();
2034 for (size_t i
=0; i
<N
; i
++) {
2035 const SortedVector
<String8
>& locations
= rules
.valueAt(i
);
2036 const size_t M
= locations
.size();
2037 for (size_t j
=0; j
<M
; j
++) {
2038 fprintf(fp
, "# %s\n", locations
.itemAt(j
).string());
2040 fprintf(fp
, "%s\n\n", rules
.keyAt(i
).string());