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"
13 #include "CrunchCache.h"
14 #include "FileFinder.h"
15 #include "CacheUpdater.h"
19 // ==========================================================================
20 // ==========================================================================
21 // ==========================================================================
33 status_t
parsePackage(const sp
<AaptGroup
>& grp
);
36 // ==========================================================================
37 // ==========================================================================
38 // ==========================================================================
40 static String8
parseResourceName(const String8
& leaf
)
42 const char* firstDot
= strchr(leaf
.string(), '.');
43 const char* str
= leaf
.string();
46 return String8(str
, firstDot
-str
);
52 ResourceTypeSet::ResourceTypeSet()
54 KeyedVector
<String8
,sp
<AaptGroup
> >()
58 FilePathStore::FilePathStore()
64 class ResourceDirIterator
67 ResourceDirIterator(const sp
<ResourceTypeSet
>& set
, const String8
& resType
)
68 : mResType(resType
), mSet(set
), mSetPos(0), mGroupPos(0)
72 inline const sp
<AaptGroup
>& getGroup() const { return mGroup
; }
73 inline const sp
<AaptFile
>& getFile() const { return mFile
; }
75 inline const String8
& getBaseName() const { return mBaseName
; }
76 inline const String8
& getLeafName() const { return mLeafName
; }
77 inline String8
getPath() const { return mPath
; }
78 inline const ResTable_config
& getParams() const { return mParams
; }
90 // Try to get next file in this current group.
91 if (mGroup
!= NULL
&& mGroupPos
< mGroup
->getFiles().size()) {
93 file
= group
->getFiles().valueAt(mGroupPos
++);
95 // Try to get the next group/file in this directory
96 } else if (mSetPos
< mSet
->size()) {
97 mGroup
= group
= mSet
->valueAt(mSetPos
++);
98 if (group
->getFiles().size() < 1) {
101 file
= group
->getFiles().valueAt(0);
111 String8
leaf(group
->getLeaf());
112 mLeafName
= String8(leaf
);
113 mParams
= file
->getGroupEntry().toParams();
114 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",
115 group
->getPath().string(), mParams
.mcc
, mParams
.mnc
,
116 mParams
.language
[0] ? mParams
.language
[0] : '-',
117 mParams
.language
[1] ? mParams
.language
[1] : '-',
118 mParams
.country
[0] ? mParams
.country
[0] : '-',
119 mParams
.country
[1] ? mParams
.country
[1] : '-',
120 mParams
.orientation
, mParams
.uiMode
,
121 mParams
.density
, mParams
.touchscreen
, mParams
.keyboard
,
122 mParams
.inputFlags
, mParams
.navigation
));
124 mPath
.appendPath(file
->getGroupEntry().toDirName(mResType
));
125 mPath
.appendPath(leaf
);
126 mBaseName
= parseResourceName(leaf
);
127 if (mBaseName
== "") {
128 fprintf(stderr
, "Error: malformed resource filename %s\n",
129 file
->getPrintableSource().string());
130 return UNKNOWN_ERROR
;
133 NOISY(printf("file name=%s\n", mBaseName
.string()));
142 const sp
<ResourceTypeSet
> mSet
;
145 sp
<AaptGroup
> mGroup
;
152 ResTable_config mParams
;
155 // ==========================================================================
156 // ==========================================================================
157 // ==========================================================================
159 bool isValidResourceType(const String8
& type
)
161 return type
== "anim" || type
== "animator" || type
== "interpolator"
162 || type
== "drawable" || type
== "layout"
163 || type
== "values" || type
== "xml" || type
== "raw"
164 || type
== "color" || type
== "menu" || type
== "mipmap";
167 static sp
<AaptFile
> getResourceFile(const sp
<AaptAssets
>& assets
, bool makeIfNecessary
=true)
169 sp
<AaptGroup
> group
= assets
->getFiles().valueFor(String8("resources.arsc"));
172 file
= group
->getFiles().valueFor(AaptGroupEntry());
178 if (!makeIfNecessary
) {
181 return assets
->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(),
185 static status_t
parsePackage(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
186 const sp
<AaptGroup
>& grp
)
188 if (grp
->getFiles().size() != 1) {
189 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
190 grp
->getFiles().valueAt(0)->getPrintableSource().string());
193 sp
<AaptFile
> file
= grp
->getFiles().valueAt(0);
196 status_t err
= parseXMLResource(file
, &block
);
197 if (err
!= NO_ERROR
) {
200 //printXMLBlock(&block);
202 ResXMLTree::event_code_t code
;
203 while ((code
=block
.next()) != ResXMLTree::START_TAG
204 && code
!= ResXMLTree::END_DOCUMENT
205 && code
!= ResXMLTree::BAD_DOCUMENT
) {
209 if (code
!= ResXMLTree::START_TAG
) {
210 fprintf(stderr
, "%s:%d: No start tag found\n",
211 file
->getPrintableSource().string(), block
.getLineNumber());
212 return UNKNOWN_ERROR
;
214 if (strcmp16(block
.getElementName(&len
), String16("manifest").string()) != 0) {
215 fprintf(stderr
, "%s:%d: Invalid start tag %s, expected <manifest>\n",
216 file
->getPrintableSource().string(), block
.getLineNumber(),
217 String8(block
.getElementName(&len
)).string());
218 return UNKNOWN_ERROR
;
221 ssize_t nameIndex
= block
.indexOfAttribute(NULL
, "package");
223 fprintf(stderr
, "%s:%d: <manifest> does not have package attribute.\n",
224 file
->getPrintableSource().string(), block
.getLineNumber());
225 return UNKNOWN_ERROR
;
228 assets
->setPackage(String8(block
.getAttributeStringValue(nameIndex
, &len
)));
230 String16
uses_sdk16("uses-sdk");
231 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
232 && code
!= ResXMLTree::BAD_DOCUMENT
) {
233 if (code
== ResXMLTree::START_TAG
) {
234 if (strcmp16(block
.getElementName(&len
), uses_sdk16
.string()) == 0) {
235 ssize_t minSdkIndex
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
,
237 if (minSdkIndex
>= 0) {
238 const uint16_t* minSdk16
= block
.getAttributeStringValue(minSdkIndex
, &len
);
239 const char* minSdk8
= strdup(String8(minSdk16
).string());
240 bundle
->setManifestMinSdkVersion(minSdk8
);
249 // ==========================================================================
250 // ==========================================================================
251 // ==========================================================================
253 static status_t
makeFileResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
254 ResourceTable
* table
,
255 const sp
<ResourceTypeSet
>& set
,
258 String8
type8(resType
);
259 String16
type16(resType
);
261 bool hasErrors
= false;
263 ResourceDirIterator
it(set
, String8(resType
));
265 while ((res
=it
.next()) == NO_ERROR
) {
266 if (bundle
->getVerbose()) {
267 printf(" (new resource id %s from %s)\n",
268 it
.getBaseName().string(), it
.getFile()->getPrintableSource().string());
270 String16
baseName(it
.getBaseName());
271 const char16_t* str
= baseName
.string();
272 const char16_t* const end
= str
+ baseName
.size();
274 if (!((*str
>= 'a' && *str
<= 'z')
275 || (*str
>= '0' && *str
<= '9')
276 || *str
== '_' || *str
== '.')) {
277 fprintf(stderr
, "%s: Invalid file name: must contain only [a-z0-9_.]\n",
278 it
.getPath().string());
283 String8 resPath
= it
.getPath();
284 resPath
.convertToResPath();
285 table
->addEntry(SourcePos(it
.getPath(), 0), String16(assets
->getPackage()),
291 assets
->addResource(it
.getLeafName(), resPath
, it
.getFile(), type8
);
294 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
297 static status_t
preProcessImages(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
298 const sp
<ResourceTypeSet
>& set
, const char* type
)
300 bool hasErrors
= false;
301 ssize_t res
= NO_ERROR
;
302 if (bundle
->getUseCrunchCache() == false) {
303 ResourceDirIterator
it(set
, String8(type
));
304 Vector
<sp
<AaptFile
> > newNameFiles
;
305 Vector
<String8
> newNamePaths
;
306 while ((res
=it
.next()) == NO_ERROR
) {
307 res
= preProcessImage(bundle
, assets
, it
.getFile(), NULL
);
308 if (res
< NO_ERROR
) {
313 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
316 status_t
postProcessImages(const sp
<AaptAssets
>& assets
,
317 ResourceTable
* table
,
318 const sp
<ResourceTypeSet
>& set
)
320 ResourceDirIterator
it(set
, String8("drawable"));
321 bool hasErrors
= false;
323 while ((res
=it
.next()) == NO_ERROR
) {
324 res
= postProcessImage(assets
, table
, it
.getFile());
325 if (res
< NO_ERROR
) {
330 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
333 static void collect_files(const sp
<AaptDir
>& dir
,
334 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
336 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& groups
= dir
->getFiles();
337 int N
= groups
.size();
338 for (int i
=0; i
<N
; i
++) {
339 String8 leafName
= groups
.keyAt(i
);
340 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
342 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
345 if (files
.size() == 0) {
349 String8 resType
= files
.valueAt(0)->getResourceType();
351 ssize_t index
= resources
->indexOfKey(resType
);
354 sp
<ResourceTypeSet
> set
= new ResourceTypeSet();
355 NOISY(printf("Creating new resource type set for leaf %s with group %s (%p)\n",
356 leafName
.string(), group
->getPath().string(), group
.get()));
357 set
->add(leafName
, group
);
358 resources
->add(resType
, set
);
360 sp
<ResourceTypeSet
> set
= resources
->valueAt(index
);
361 index
= set
->indexOfKey(leafName
);
363 NOISY(printf("Adding to resource type set for leaf %s group %s (%p)\n",
364 leafName
.string(), group
->getPath().string(), group
.get()));
365 set
->add(leafName
, group
);
367 sp
<AaptGroup
> existingGroup
= set
->valueAt(index
);
368 NOISY(printf("Extending to resource type set for leaf %s group %s (%p)\n",
369 leafName
.string(), group
->getPath().string(), group
.get()));
370 for (size_t j
=0; j
<files
.size(); j
++) {
371 NOISY(printf("Adding file %s in group %s resType %s\n",
372 files
.valueAt(j
)->getSourceFile().string(),
373 files
.keyAt(j
).toDirName(String8()).string(),
375 status_t err
= existingGroup
->addFile(files
.valueAt(j
));
382 static void collect_files(const sp
<AaptAssets
>& ass
,
383 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
385 const Vector
<sp
<AaptDir
> >& dirs
= ass
->resDirs();
388 for (int i
=0; i
<N
; i
++) {
389 sp
<AaptDir
> d
= dirs
.itemAt(i
);
390 NOISY(printf("Collecting dir #%d %p: %s, leaf %s\n", i
, d
.get(), d
->getPath().string(),
391 d
->getLeaf().string()));
392 collect_files(d
, resources
);
394 // don't try to include the res dir
395 NOISY(printf("Removing dir leaf %s\n", d
->getLeaf().string()));
396 ass
->removeDir(d
->getLeaf());
403 ATTR_LEADING_SPACES
= -3,
404 ATTR_TRAILING_SPACES
= -4
406 static int validateAttr(const String8
& path
, const ResTable
& table
,
407 const ResXMLParser
& parser
,
408 const char* ns
, const char* attr
, const char* validChars
, bool required
)
412 ssize_t index
= parser
.indexOfAttribute(ns
, attr
);
415 if (index
>= 0 && parser
.getAttributeValue(index
, &value
) >= 0) {
416 const ResStringPool
* pool
= &parser
.getStrings();
417 if (value
.dataType
== Res_value::TYPE_REFERENCE
) {
418 uint32_t specFlags
= 0;
420 if ((strIdx
=table
.resolveReference(&value
, 0x10000000, NULL
, &specFlags
)) < 0) {
421 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s references unknown resid 0x%08x.\n",
422 path
.string(), parser
.getLineNumber(),
423 String8(parser
.getElementName(&len
)).string(), attr
,
425 return ATTR_NOT_FOUND
;
428 pool
= table
.getTableStringBlock(strIdx
);
431 str
= pool
->stringAt(value
.data
, &len
);
433 printf("***** RES ATTR: %s specFlags=0x%x strIdx=%d: %s\n", attr
,
434 specFlags
, strIdx
, str
!= NULL
? String8(str
).string() : "???");
436 if ((specFlags
&~ResTable_typeSpec::SPEC_PUBLIC
) != 0 && false) {
437 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s varies by configurations 0x%x.\n",
438 path
.string(), parser
.getLineNumber(),
439 String8(parser
.getElementName(&len
)).string(), attr
,
441 return ATTR_NOT_FOUND
;
444 if (value
.dataType
== Res_value::TYPE_STRING
) {
446 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has no string block.\n",
447 path
.string(), parser
.getLineNumber(),
448 String8(parser
.getElementName(&len
)).string(), attr
);
449 return ATTR_NOT_FOUND
;
451 if ((str
=pool
->stringAt(value
.data
, &len
)) == NULL
) {
452 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has corrupt string value.\n",
453 path
.string(), parser
.getLineNumber(),
454 String8(parser
.getElementName(&len
)).string(), attr
);
455 return ATTR_NOT_FOUND
;
458 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid type %d.\n",
459 path
.string(), parser
.getLineNumber(),
460 String8(parser
.getElementName(&len
)).string(), attr
,
462 return ATTR_NOT_FOUND
;
465 for (size_t i
=0; i
<len
; i
++) {
467 const char* p
= validChars
;
477 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n",
478 path
.string(), parser
.getLineNumber(),
479 String8(parser
.getElementName(&len
)).string(), attr
, (char)str
[i
]);
485 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not start with a space.\n",
486 path
.string(), parser
.getLineNumber(),
487 String8(parser
.getElementName(&len
)).string(), attr
);
488 return ATTR_LEADING_SPACES
;
490 if (str
[len
-1] == ' ') {
491 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not end with a space.\n",
492 path
.string(), parser
.getLineNumber(),
493 String8(parser
.getElementName(&len
)).string(), attr
);
494 return ATTR_TRAILING_SPACES
;
499 fprintf(stderr
, "%s:%d: Tag <%s> missing required attribute %s.\n",
500 path
.string(), parser
.getLineNumber(),
501 String8(parser
.getElementName(&len
)).string(), attr
);
502 return ATTR_NOT_FOUND
;
507 static void checkForIds(const String8
& path
, ResXMLParser
& parser
)
509 ResXMLTree::event_code_t code
;
510 while ((code
=parser
.next()) != ResXMLTree::END_DOCUMENT
511 && code
> ResXMLTree::BAD_DOCUMENT
) {
512 if (code
== ResXMLTree::START_TAG
) {
513 ssize_t index
= parser
.indexOfAttribute(NULL
, "id");
515 fprintf(stderr
, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n",
516 path
.string(), parser
.getLineNumber());
522 static bool applyFileOverlay(Bundle
*bundle
,
523 const sp
<AaptAssets
>& assets
,
524 sp
<ResourceTypeSet
> *baseSet
,
527 if (bundle
->getVerbose()) {
528 printf("applyFileOverlay for %s\n", resType
);
531 // Replace any base level files in this category with any found from the overlay
532 // Also add any found only in the overlay.
533 sp
<AaptAssets
> overlay
= assets
->getOverlay();
534 String8
resTypeString(resType
);
536 // work through the linked list of overlays
537 while (overlay
.get()) {
538 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* overlayRes
= overlay
->getResources();
540 // get the overlay resources of the requested type
541 ssize_t index
= overlayRes
->indexOfKey(resTypeString
);
543 sp
<ResourceTypeSet
> overlaySet
= overlayRes
->valueAt(index
);
545 // for each of the resources, check for a match in the previously built
546 // non-overlay "baseset".
547 size_t overlayCount
= overlaySet
->size();
548 for (size_t overlayIndex
=0; overlayIndex
<overlayCount
; overlayIndex
++) {
549 if (bundle
->getVerbose()) {
550 printf("trying overlaySet Key=%s\n",overlaySet
->keyAt(overlayIndex
).string());
552 size_t baseIndex
= UNKNOWN_ERROR
;
553 if (baseSet
->get() != NULL
) {
554 baseIndex
= (*baseSet
)->indexOfKey(overlaySet
->keyAt(overlayIndex
));
556 if (baseIndex
< UNKNOWN_ERROR
) {
557 // look for same flavor. For a given file (strings.xml, for example)
558 // there may be a locale specific or other flavors - we want to match
560 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
561 sp
<AaptGroup
> baseGroup
= (*baseSet
)->valueAt(baseIndex
);
563 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
564 overlayGroup
->getFiles();
565 if (bundle
->getVerbose()) {
566 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > baseFiles
=
567 baseGroup
->getFiles();
568 for (size_t i
=0; i
< baseFiles
.size(); i
++) {
569 printf("baseFile %zd has flavor %s\n", i
,
570 baseFiles
.keyAt(i
).toString().string());
572 for (size_t i
=0; i
< overlayFiles
.size(); i
++) {
573 printf("overlayFile %zd has flavor %s\n", i
,
574 overlayFiles
.keyAt(i
).toString().string());
578 size_t overlayGroupSize
= overlayFiles
.size();
579 for (size_t overlayGroupIndex
= 0;
580 overlayGroupIndex
<overlayGroupSize
;
581 overlayGroupIndex
++) {
582 size_t baseFileIndex
=
583 baseGroup
->getFiles().indexOfKey(overlayFiles
.
584 keyAt(overlayGroupIndex
));
585 if (baseFileIndex
< UNKNOWN_ERROR
) {
586 if (bundle
->getVerbose()) {
587 printf("found a match (%zd) for overlay file %s, for flavor %s\n",
589 overlayGroup
->getLeaf().string(),
590 overlayFiles
.keyAt(overlayGroupIndex
).toString().string());
592 baseGroup
->removeFile(baseFileIndex
);
594 // didn't find a match fall through and add it..
595 if (true || bundle
->getVerbose()) {
596 printf("nothing matches overlay file %s, for flavor %s\n",
597 overlayGroup
->getLeaf().string(),
598 overlayFiles
.keyAt(overlayGroupIndex
).toString().string());
601 baseGroup
->addFile(overlayFiles
.valueAt(overlayGroupIndex
));
602 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
605 if (baseSet
->get() == NULL
) {
606 *baseSet
= new ResourceTypeSet();
607 assets
->getResources()->add(String8(resType
), *baseSet
);
609 // this group doesn't exist (a file that's only in the overlay)
610 (*baseSet
)->add(overlaySet
->keyAt(overlayIndex
),
611 overlaySet
->valueAt(overlayIndex
));
612 // make sure all flavors are defined in the resources.
613 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
614 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
615 overlayGroup
->getFiles();
616 size_t overlayGroupSize
= overlayFiles
.size();
617 for (size_t overlayGroupIndex
= 0;
618 overlayGroupIndex
<overlayGroupSize
;
619 overlayGroupIndex
++) {
620 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
624 // this overlay didn't have resources for this type
627 overlay
= overlay
->getOverlay();
632 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
,
633 const char* attr8
, const char* value
)
639 const String16
ns(ns8
);
640 const String16
attr(attr8
);
642 if (node
->getAttribute(ns
, attr
) != NULL
) {
643 fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s);"
644 " using existing value in manifest.\n",
645 String8(attr
).string(), String8(ns
).string());
649 node
->addAttribute(ns
, attr
, String16(value
));
652 static void fullyQualifyClassName(const String8
& package
, sp
<XMLNode
> node
,
653 const String16
& attrName
) {
654 XMLNode::attribute_entry
* attr
= node
->editAttribute(
655 String16("http://schemas.android.com/apk/res/android"), attrName
);
657 String8
name(attr
->string
);
659 // asdf --> package.asdf
660 // .asdf .a.b --> package.asdf package.a.b
661 // asdf.adsf --> asdf.asdf
663 const char* p
= name
.string();
664 const char* q
= strchr(p
, '.');
666 className
+= package
;
668 } else if (q
== NULL
) {
669 className
+= package
;
675 NOISY(printf("Qualifying class '%s' to '%s'", name
.string(), className
.string()));
676 attr
->string
.setTo(String16(className
));
680 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
682 root
= root
->searchElement(String16(), String16("manifest"));
684 fprintf(stderr
, "No <manifest> tag.\n");
685 return UNKNOWN_ERROR
;
688 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
689 bundle
->getVersionCode());
690 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
691 bundle
->getVersionName());
693 if (bundle
->getMinSdkVersion() != NULL
694 || bundle
->getTargetSdkVersion() != NULL
695 || bundle
->getMaxSdkVersion() != NULL
) {
696 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
698 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
699 root
->insertChildAt(vers
, 0);
702 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
703 bundle
->getMinSdkVersion());
704 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
705 bundle
->getTargetSdkVersion());
706 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
707 bundle
->getMaxSdkVersion());
710 if (bundle
->getDebugMode()) {
711 sp
<XMLNode
> application
= root
->getChildElement(String16(), String16("application"));
712 if (application
!= NULL
) {
713 addTagAttribute(application
, RESOURCES_ANDROID_NAMESPACE
, "debuggable", "true");
717 // Deal with manifest package name overrides
718 const char* manifestPackageNameOverride
= bundle
->getManifestPackageNameOverride();
719 if (manifestPackageNameOverride
!= NULL
) {
720 // Update the actual package name
721 XMLNode::attribute_entry
* attr
= root
->editAttribute(String16(), String16("package"));
723 fprintf(stderr
, "package name is required with --rename-manifest-package.\n");
724 return UNKNOWN_ERROR
;
726 String8
origPackage(attr
->string
);
727 attr
->string
.setTo(String16(manifestPackageNameOverride
));
728 NOISY(printf("Overriding package '%s' to be '%s'\n", origPackage
.string(), manifestPackageNameOverride
));
730 // Make class names fully qualified
731 sp
<XMLNode
> application
= root
->getChildElement(String16(), String16("application"));
732 if (application
!= NULL
) {
733 fullyQualifyClassName(origPackage
, application
, String16("name"));
734 fullyQualifyClassName(origPackage
, application
, String16("backupAgent"));
736 Vector
<sp
<XMLNode
> >& children
= const_cast<Vector
<sp
<XMLNode
> >&>(application
->getChildren());
737 for (size_t i
= 0; i
< children
.size(); i
++) {
738 sp
<XMLNode
> child
= children
.editItemAt(i
);
739 String8
tag(child
->getElementName());
740 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
741 fullyQualifyClassName(origPackage
, child
, String16("name"));
742 } else if (tag
== "activity-alias") {
743 fullyQualifyClassName(origPackage
, child
, String16("name"));
744 fullyQualifyClassName(origPackage
, child
, String16("targetActivity"));
750 // Deal with manifest package name overrides
751 const char* instrumentationPackageNameOverride
= bundle
->getInstrumentationPackageNameOverride();
752 if (instrumentationPackageNameOverride
!= NULL
) {
753 // Fix up instrumentation targets.
754 Vector
<sp
<XMLNode
> >& children
= const_cast<Vector
<sp
<XMLNode
> >&>(root
->getChildren());
755 for (size_t i
= 0; i
< children
.size(); i
++) {
756 sp
<XMLNode
> child
= children
.editItemAt(i
);
757 String8
tag(child
->getElementName());
758 if (tag
== "instrumentation") {
759 XMLNode::attribute_entry
* attr
= child
->editAttribute(
760 String16("http://schemas.android.com/apk/res/android"), String16("targetPackage"));
762 attr
->string
.setTo(String16(instrumentationPackageNameOverride
));
771 #define ASSIGN_IT(n) \
773 ssize_t index = resources->indexOfKey(String8(#n)); \
775 n ## s = resources->valueAt(index); \
779 status_t
updatePreProcessedCache(Bundle
* bundle
)
782 fprintf(stdout
, "BENCHMARK: Starting PNG PreProcessing \n");
783 long startPNGTime
= clock();
784 #endif /* BENCHMARK */
786 String8
source(bundle
->getResourceSourceDirs()[0]);
787 String8
dest(bundle
->getCrunchedOutputDir());
789 FileFinder
* ff
= new SystemFileFinder();
790 CrunchCache
cc(source
,dest
,ff
);
792 CacheUpdater
* cu
= new SystemCacheUpdater(bundle
);
793 size_t numFiles
= cc
.crunch(cu
);
795 if (bundle
->getVerbose())
796 fprintf(stdout
, "Crunched %d PNG files to update cache\n", (int)numFiles
);
802 fprintf(stdout
, "BENCHMARK: End PNG PreProcessing. Time Elapsed: %f ms \n"
803 ,(clock() - startPNGTime
)/1000.0);
804 #endif /* BENCHMARK */
808 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
810 // First, look for a package file to parse. This is required to
811 // be able to generate the resource information.
812 sp
<AaptGroup
> androidManifestFile
=
813 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
814 if (androidManifestFile
== NULL
) {
815 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
816 return UNKNOWN_ERROR
;
819 status_t err
= parsePackage(bundle
, assets
, androidManifestFile
);
820 if (err
!= NO_ERROR
) {
824 NOISY(printf("Creating resources for package %s\n",
825 assets
->getPackage().string()));
827 ResourceTable
table(bundle
, String16(assets
->getPackage()));
828 err
= table
.addIncludedResources(bundle
, assets
);
829 if (err
!= NO_ERROR
) {
833 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
835 // Standard flags for compiled XML and optional UTF-8 encoding
836 int xmlFlags
= XML_COMPILE_STANDARD_RESOURCE
;
838 /* Only enable UTF-8 if the caller of aapt didn't specifically
839 * request UTF-16 encoding and the parameters of this package
840 * allow UTF-8 to be used.
842 if (!bundle
->getWantUTF16()
843 && bundle
->isMinSdkAtLeast(SDK_FROYO
)) {
844 xmlFlags
|= XML_COMPILE_UTF8
;
847 // --------------------------------------------------------------
848 // First, gather all resource information.
849 // --------------------------------------------------------------
851 // resType -> leafName -> group
852 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
853 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
854 collect_files(assets
, resources
);
856 sp
<ResourceTypeSet
> drawables
;
857 sp
<ResourceTypeSet
> layouts
;
858 sp
<ResourceTypeSet
> anims
;
859 sp
<ResourceTypeSet
> animators
;
860 sp
<ResourceTypeSet
> interpolators
;
861 sp
<ResourceTypeSet
> xmls
;
862 sp
<ResourceTypeSet
> raws
;
863 sp
<ResourceTypeSet
> colors
;
864 sp
<ResourceTypeSet
> menus
;
865 sp
<ResourceTypeSet
> mipmaps
;
871 ASSIGN_IT(interpolator
);
878 assets
->setResources(resources
);
879 // now go through any resource overlays and collect their files
880 sp
<AaptAssets
> current
= assets
->getOverlay();
881 while(current
.get()) {
882 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
883 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
884 current
->setResources(resources
);
885 collect_files(current
, resources
);
886 current
= current
->getOverlay();
888 // apply the overlay files to the base set
889 if (!applyFileOverlay(bundle
, assets
, &drawables
, "drawable") ||
890 !applyFileOverlay(bundle
, assets
, &layouts
, "layout") ||
891 !applyFileOverlay(bundle
, assets
, &anims
, "anim") ||
892 !applyFileOverlay(bundle
, assets
, &animators
, "animator") ||
893 !applyFileOverlay(bundle
, assets
, &interpolators
, "interpolator") ||
894 !applyFileOverlay(bundle
, assets
, &xmls
, "xml") ||
895 !applyFileOverlay(bundle
, assets
, &raws
, "raw") ||
896 !applyFileOverlay(bundle
, assets
, &colors
, "color") ||
897 !applyFileOverlay(bundle
, assets
, &menus
, "menu") ||
898 !applyFileOverlay(bundle
, assets
, &mipmaps
, "mipmap")) {
899 return UNKNOWN_ERROR
;
902 bool hasErrors
= false;
904 if (drawables
!= NULL
) {
905 if (bundle
->getOutputAPKFile() != NULL
) {
906 err
= preProcessImages(bundle
, assets
, drawables
, "drawable");
908 if (err
== NO_ERROR
) {
909 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
910 if (err
!= NO_ERROR
) {
918 if (mipmaps
!= NULL
) {
919 if (bundle
->getOutputAPKFile() != NULL
) {
920 err
= preProcessImages(bundle
, assets
, mipmaps
, "mipmap");
922 if (err
== NO_ERROR
) {
923 err
= makeFileResources(bundle
, assets
, &table
, mipmaps
, "mipmap");
924 if (err
!= NO_ERROR
) {
932 if (layouts
!= NULL
) {
933 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
934 if (err
!= NO_ERROR
) {
940 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
941 if (err
!= NO_ERROR
) {
946 if (animators
!= NULL
) {
947 err
= makeFileResources(bundle
, assets
, &table
, animators
, "animator");
948 if (err
!= NO_ERROR
) {
953 if (interpolators
!= NULL
) {
954 err
= makeFileResources(bundle
, assets
, &table
, interpolators
, "interpolator");
955 if (err
!= NO_ERROR
) {
961 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
962 if (err
!= NO_ERROR
) {
968 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
969 if (err
!= NO_ERROR
) {
976 while(current
.get()) {
977 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
978 current
->getResources();
980 ssize_t index
= resources
->indexOfKey(String8("values"));
982 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
984 while ((res
=it
.next()) == NO_ERROR
) {
985 sp
<AaptFile
> file
= it
.getFile();
986 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
987 (current
!=assets
), &table
);
988 if (res
!= NO_ERROR
) {
993 current
= current
->getOverlay();
996 if (colors
!= NULL
) {
997 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
998 if (err
!= NO_ERROR
) {
1003 if (menus
!= NULL
) {
1004 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
1005 if (err
!= NO_ERROR
) {
1010 // --------------------------------------------------------------------
1011 // Assignment of resource IDs and initial generation of resource table.
1012 // --------------------------------------------------------------------
1014 if (table
.hasResources()) {
1015 sp
<AaptFile
> resFile(getResourceFile(assets
));
1016 if (resFile
== NULL
) {
1017 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1018 return UNKNOWN_ERROR
;
1021 err
= table
.assignResourceIds();
1022 if (err
< NO_ERROR
) {
1027 // --------------------------------------------------------------
1028 // Finally, we can now we can compile XML files, which may reference
1030 // --------------------------------------------------------------
1032 if (layouts
!= NULL
) {
1033 ResourceDirIterator
it(layouts
, String8("layout"));
1034 while ((err
=it
.next()) == NO_ERROR
) {
1035 String8 src
= it
.getFile()->getPrintableSource();
1036 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1037 if (err
== NO_ERROR
) {
1039 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
1040 checkForIds(src
, block
);
1046 if (err
< NO_ERROR
) {
1052 if (anims
!= NULL
) {
1053 ResourceDirIterator
it(anims
, String8("anim"));
1054 while ((err
=it
.next()) == NO_ERROR
) {
1055 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1056 if (err
!= NO_ERROR
) {
1061 if (err
< NO_ERROR
) {
1067 if (animators
!= NULL
) {
1068 ResourceDirIterator
it(animators
, String8("animator"));
1069 while ((err
=it
.next()) == NO_ERROR
) {
1070 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1071 if (err
!= NO_ERROR
) {
1076 if (err
< NO_ERROR
) {
1082 if (interpolators
!= NULL
) {
1083 ResourceDirIterator
it(interpolators
, String8("interpolator"));
1084 while ((err
=it
.next()) == NO_ERROR
) {
1085 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1086 if (err
!= NO_ERROR
) {
1091 if (err
< NO_ERROR
) {
1098 ResourceDirIterator
it(xmls
, String8("xml"));
1099 while ((err
=it
.next()) == NO_ERROR
) {
1100 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1101 if (err
!= NO_ERROR
) {
1106 if (err
< NO_ERROR
) {
1112 if (drawables
!= NULL
) {
1113 err
= postProcessImages(assets
, &table
, drawables
);
1114 if (err
!= NO_ERROR
) {
1119 if (colors
!= NULL
) {
1120 ResourceDirIterator
it(colors
, String8("color"));
1121 while ((err
=it
.next()) == NO_ERROR
) {
1122 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1123 if (err
!= NO_ERROR
) {
1128 if (err
< NO_ERROR
) {
1134 if (menus
!= NULL
) {
1135 ResourceDirIterator
it(menus
, String8("menu"));
1136 while ((err
=it
.next()) == NO_ERROR
) {
1137 String8 src
= it
.getFile()->getPrintableSource();
1138 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1139 if (err
!= NO_ERROR
) {
1143 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
1144 checkForIds(src
, block
);
1147 if (err
< NO_ERROR
) {
1153 if (table
.validateLocalizations()) {
1158 return UNKNOWN_ERROR
;
1161 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
1162 String8
manifestPath(manifestFile
->getPrintableSource());
1164 // Generate final compiled manifest file.
1165 manifestFile
->clearData();
1166 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1167 if (manifestTree
== NULL
) {
1168 return UNKNOWN_ERROR
;
1170 err
= massageManifest(bundle
, manifestTree
);
1171 if (err
< NO_ERROR
) {
1174 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1175 if (err
< NO_ERROR
) {
1180 //printXMLBlock(&block);
1182 // --------------------------------------------------------------
1183 // Generate the final resource table.
1184 // Re-flatten because we may have added new resource IDs
1185 // --------------------------------------------------------------
1187 ResTable finalResTable
;
1188 sp
<AaptFile
> resFile
;
1190 if (table
.hasResources()) {
1191 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1192 err
= table
.addSymbols(symbols
);
1193 if (err
< NO_ERROR
) {
1197 resFile
= getResourceFile(assets
);
1198 if (resFile
== NULL
) {
1199 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1200 return UNKNOWN_ERROR
;
1203 err
= table
.flatten(bundle
, resFile
);
1204 if (err
< NO_ERROR
) {
1208 if (bundle
->getPublicOutputFile()) {
1209 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1211 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1212 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1213 return UNKNOWN_ERROR
;
1215 if (bundle
->getVerbose()) {
1216 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1218 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1222 // Read resources back in,
1223 finalResTable
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1227 printf("Generated resources:\n");
1228 finalResTable
.print();
1233 // Perform a basic validation of the manifest file. This time we
1234 // parse it with the comments intact, so that we can use them to
1235 // generate java docs... so we are not going to write this one
1236 // back out to the final manifest data.
1237 sp
<AaptFile
> outManifestFile
= new AaptFile(manifestFile
->getSourceFile(),
1238 manifestFile
->getGroupEntry(),
1239 manifestFile
->getResourceType());
1240 err
= compileXmlFile(assets
, manifestFile
,
1241 outManifestFile
, &table
,
1242 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
1243 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
1244 if (err
< NO_ERROR
) {
1248 block
.setTo(outManifestFile
->getData(), outManifestFile
->getSize(), true);
1249 String16
manifest16("manifest");
1250 String16
permission16("permission");
1251 String16
permission_group16("permission-group");
1252 String16
uses_permission16("uses-permission");
1253 String16
instrumentation16("instrumentation");
1254 String16
application16("application");
1255 String16
provider16("provider");
1256 String16
service16("service");
1257 String16
receiver16("receiver");
1258 String16
activity16("activity");
1259 String16
action16("action");
1260 String16
category16("category");
1261 String16
data16("scheme");
1262 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1263 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
1264 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
1265 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
1266 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1267 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
1268 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1269 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
1270 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1271 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
1272 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1273 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
1274 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1275 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
1276 ResXMLTree::event_code_t code
;
1277 sp
<AaptSymbols
> permissionSymbols
;
1278 sp
<AaptSymbols
> permissionGroupSymbols
;
1279 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1280 && code
> ResXMLTree::BAD_DOCUMENT
) {
1281 if (code
== ResXMLTree::START_TAG
) {
1283 if (block
.getElementNamespace(&len
) != NULL
) {
1286 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
1287 if (validateAttr(manifestPath
, finalResTable
, block
, NULL
, "package",
1288 packageIdentChars
, true) != ATTR_OKAY
) {
1291 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1292 "sharedUserId", packageIdentChars
, false) != ATTR_OKAY
) {
1295 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
1296 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
1297 const bool isGroup
= strcmp16(block
.getElementName(&len
),
1298 permission_group16
.string()) == 0;
1299 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1300 "name", isGroup
? packageIdentCharsWithTheStupid
1301 : packageIdentChars
, true) != ATTR_OKAY
) {
1304 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
1305 sp
<AaptSymbols
> syms
;
1307 syms
= permissionSymbols
;
1309 sp
<AaptSymbols
> symbols
=
1310 assets
->getSymbolsFor(String8("Manifest"));
1311 syms
= permissionSymbols
= symbols
->addNestedSymbol(
1312 String8("permission"), srcPos
);
1315 syms
= permissionGroupSymbols
;
1317 sp
<AaptSymbols
> symbols
=
1318 assets
->getSymbolsFor(String8("Manifest"));
1319 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
1320 String8("permission_group"), srcPos
);
1324 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
1325 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
1327 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
1328 manifestPath
.string(), block
.getLineNumber(),
1329 String8(block
.getElementName(&len
)).string());
1334 char* p
= idStr
.lockBuffer(idStr
.size());
1335 char* e
= p
+ idStr
.size();
1336 bool begins_with_digit
= true; // init to true so an empty string fails
1339 if (*e
>= '0' && *e
<= '9') {
1340 begins_with_digit
= true;
1343 if ((*e
>= 'a' && *e
<= 'z') ||
1344 (*e
>= 'A' && *e
<= 'Z') ||
1346 begins_with_digit
= false;
1349 if (isGroup
&& (*e
== '-')) {
1351 begins_with_digit
= false;
1357 idStr
.unlockBuffer();
1358 // verify that we stopped because we hit a period or
1359 // the beginning of the string, and that the
1360 // identifier didn't begin with a digit.
1361 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
1363 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
1364 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
1367 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
1368 const uint16_t* cmt
= block
.getComment(&len
);
1369 if (cmt
!= NULL
&& *cmt
!= 0) {
1370 //printf("Comment of %s: %s\n", String8(e).string(),
1371 // String8(cmt).string());
1372 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
1374 //printf("No comment for %s\n", String8(e).string());
1376 syms
->makeSymbolPublic(String8(e
), srcPos
);
1377 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
1378 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1379 "name", packageIdentChars
, true) != ATTR_OKAY
) {
1382 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
1383 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1384 "name", classIdentChars
, true) != ATTR_OKAY
) {
1387 if (validateAttr(manifestPath
, finalResTable
, block
,
1388 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
1389 packageIdentChars
, true) != ATTR_OKAY
) {
1392 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
1393 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1394 "name", classIdentChars
, false) != ATTR_OKAY
) {
1397 if (validateAttr(manifestPath
, finalResTable
, block
,
1398 RESOURCES_ANDROID_NAMESPACE
, "permission",
1399 packageIdentChars
, false) != ATTR_OKAY
) {
1402 if (validateAttr(manifestPath
, finalResTable
, block
,
1403 RESOURCES_ANDROID_NAMESPACE
, "process",
1404 processIdentChars
, false) != ATTR_OKAY
) {
1407 if (validateAttr(manifestPath
, finalResTable
, block
,
1408 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1409 processIdentChars
, false) != ATTR_OKAY
) {
1412 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
1413 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1414 "name", classIdentChars
, true) != ATTR_OKAY
) {
1417 if (validateAttr(manifestPath
, finalResTable
, block
,
1418 RESOURCES_ANDROID_NAMESPACE
, "authorities",
1419 authoritiesIdentChars
, true) != ATTR_OKAY
) {
1422 if (validateAttr(manifestPath
, finalResTable
, block
,
1423 RESOURCES_ANDROID_NAMESPACE
, "permission",
1424 packageIdentChars
, false) != ATTR_OKAY
) {
1427 if (validateAttr(manifestPath
, finalResTable
, block
,
1428 RESOURCES_ANDROID_NAMESPACE
, "process",
1429 processIdentChars
, false) != ATTR_OKAY
) {
1432 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1433 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1434 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1435 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1436 "name", classIdentChars
, true) != ATTR_OKAY
) {
1439 if (validateAttr(manifestPath
, finalResTable
, block
,
1440 RESOURCES_ANDROID_NAMESPACE
, "permission",
1441 packageIdentChars
, false) != ATTR_OKAY
) {
1444 if (validateAttr(manifestPath
, finalResTable
, block
,
1445 RESOURCES_ANDROID_NAMESPACE
, "process",
1446 processIdentChars
, false) != ATTR_OKAY
) {
1449 if (validateAttr(manifestPath
, finalResTable
, block
,
1450 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1451 processIdentChars
, false) != ATTR_OKAY
) {
1454 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1455 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1456 if (validateAttr(manifestPath
, finalResTable
, block
,
1457 RESOURCES_ANDROID_NAMESPACE
, "name",
1458 packageIdentChars
, true) != ATTR_OKAY
) {
1461 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1462 if (validateAttr(manifestPath
, finalResTable
, block
,
1463 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1464 typeIdentChars
, true) != ATTR_OKAY
) {
1467 if (validateAttr(manifestPath
, finalResTable
, block
,
1468 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1469 schemeIdentChars
, true) != ATTR_OKAY
) {
1476 if (resFile
!= NULL
) {
1477 // These resources are now considered to be a part of the included
1478 // resources, for others to reference.
1479 err
= assets
->addIncludedResources(resFile
);
1480 if (err
< NO_ERROR
) {
1481 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1489 static const char* getIndentSpace(int indent
)
1491 static const char whitespace
[] =
1494 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1497 static status_t
fixupSymbol(String16
* inoutSymbol
)
1499 inoutSymbol
->replaceAll('.', '_');
1500 inoutSymbol
->replaceAll(':', '_');
1504 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1505 const String8
& name
,
1506 String16
* outTypeComment
= NULL
)
1508 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1510 //printf("Got R symbols!\n");
1511 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1513 //printf("Got attrs symbols! comment %s=%s\n",
1514 // name.string(), String8(asym->getComment(name)).string());
1515 if (outTypeComment
!= NULL
) {
1516 *outTypeComment
= asym
->getTypeComment(name
);
1518 return asym
->getComment(name
);
1524 static status_t
writeLayoutClasses(
1525 FILE* fp
, const sp
<AaptAssets
>& assets
,
1526 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1528 const char* indentStr
= getIndentSpace(indent
);
1529 if (!includePrivate
) {
1530 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1532 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1535 String16
attr16("attr");
1536 String16
package16(assets
->getPackage());
1538 indentStr
= getIndentSpace(indent
);
1539 bool hasErrors
= false;
1542 size_t N
= symbols
->getNestedSymbols().size();
1543 for (i
=0; i
<N
; i
++) {
1544 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1545 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1546 String8
realClassName(nclassName16
);
1547 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1550 String8
nclassName(nclassName16
);
1552 SortedVector
<uint32_t> idents
;
1553 Vector
<uint32_t> origOrder
;
1554 Vector
<bool> publicFlags
;
1557 size_t NA
= nsymbols
->getSymbols().size();
1558 for (a
=0; a
<NA
; a
++) {
1559 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1560 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1562 bool isPublic
= true;
1564 String16
name16(sym
.name
);
1565 uint32_t typeSpecFlags
;
1566 code
= assets
->getIncludedResources().identifierForName(
1567 name16
.string(), name16
.size(),
1568 attr16
.string(), attr16
.size(),
1569 package16
.string(), package16
.size(), &typeSpecFlags
);
1571 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1572 nclassName
.string(), sym
.name
.string());
1575 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1578 origOrder
.add(code
);
1579 publicFlags
.add(isPublic
);
1584 bool deprecated
= false;
1586 String16 comment
= symbols
->getComment(realClassName
);
1587 fprintf(fp
, "%s/** ", indentStr
);
1588 if (comment
.size() > 0) {
1589 String8
cmt(comment
);
1590 fprintf(fp
, "%s\n", cmt
.string());
1591 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1595 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1597 bool hasTable
= false;
1598 for (a
=0; a
<NA
; a
++) {
1599 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1604 "%s <p>Includes the following attributes:</p>\n"
1606 "%s <colgroup align=\"left\" />\n"
1607 "%s <colgroup align=\"left\" />\n"
1608 "%s <tr><th>Attribute</th><th>Description</th></tr>\n",
1615 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1616 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1619 String8
name8(sym
.name
);
1620 String16
comment(sym
.comment
);
1621 if (comment
.size() <= 0) {
1622 comment
= getAttributeComment(assets
, name8
);
1624 if (comment
.size() > 0) {
1625 const char16_t* p
= comment
.string();
1626 while (*p
!= 0 && *p
!= '.') {
1628 while (*p
!= 0 && *p
!= '}') {
1638 comment
= String16(comment
.string(), p
-comment
.string());
1640 String16
name(name8
);
1642 fprintf(fp
, "%s <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n",
1643 indentStr
, nclassName
.string(),
1644 String8(name
).string(),
1645 assets
->getPackage().string(),
1646 String8(name
).string(),
1647 String8(comment
).string());
1651 fprintf(fp
, "%s </table>\n", indentStr
);
1653 for (a
=0; a
<NA
; a
++) {
1654 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1656 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1657 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1660 String16
name(sym
.name
);
1662 fprintf(fp
, "%s @see #%s_%s\n",
1663 indentStr
, nclassName
.string(),
1664 String8(name
).string());
1667 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1670 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1674 "%spublic static final int[] %s = {\n"
1676 indentStr
, nclassName
.string(),
1677 getIndentSpace(indent
+1));
1679 for (a
=0; a
<NA
; a
++) {
1682 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1687 fprintf(fp
, "0x%08x", idents
[a
]);
1690 fprintf(fp
, "\n%s};\n", indentStr
);
1692 for (a
=0; a
<NA
; a
++) {
1693 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1695 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1696 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1699 String8
name8(sym
.name
);
1700 String16
comment(sym
.comment
);
1701 String16 typeComment
;
1702 if (comment
.size() <= 0) {
1703 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1705 getAttributeComment(assets
, name8
, &typeComment
);
1707 String16
name(name8
);
1708 if (fixupSymbol(&name
) != NO_ERROR
) {
1712 uint32_t typeSpecFlags
= 0;
1713 String16
name16(sym
.name
);
1714 assets
->getIncludedResources().identifierForName(
1715 name16
.string(), name16
.size(),
1716 attr16
.string(), attr16
.size(),
1717 package16
.string(), package16
.size(), &typeSpecFlags
);
1718 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1719 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1720 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1722 bool deprecated
= false;
1724 fprintf(fp
, "%s/**\n", indentStr
);
1725 if (comment
.size() > 0) {
1726 String8
cmt(comment
);
1727 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1728 fprintf(fp
, "%s %s\n", indentStr
, cmt
.string());
1729 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1734 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1735 "%s attribute's value can be found in the {@link #%s} array.\n",
1737 pub
? assets
->getPackage().string()
1738 : assets
->getSymbolsPrivatePackage().string(),
1739 String8(name
).string(),
1740 indentStr
, nclassName
.string());
1742 if (typeComment
.size() > 0) {
1743 String8
cmt(typeComment
);
1744 fprintf(fp
, "\n\n%s %s\n", indentStr
, cmt
.string());
1745 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1749 if (comment
.size() > 0) {
1752 "%s <p>This corresponds to the global attribute"
1753 "%s resource symbol {@link %s.R.attr#%s}.\n",
1754 indentStr
, indentStr
,
1755 assets
->getPackage().string(),
1756 String8(name
).string());
1759 "%s <p>This is a private symbol.\n", indentStr
);
1762 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1763 "android", String8(name
).string());
1764 fprintf(fp
, "%s*/\n", indentStr
);
1766 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1769 "%spublic static final int %s_%s = %d;\n",
1770 indentStr
, nclassName
.string(),
1771 String8(name
).string(), (int)pos
);
1777 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1778 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1781 static status_t
writeSymbolClass(
1782 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1783 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
,
1786 fprintf(fp
, "%spublic %sfinal class %s {\n",
1787 getIndentSpace(indent
),
1788 indent
!= 0 ? "static " : "", className
.string());
1792 status_t err
= NO_ERROR
;
1794 const char * id_format
= nonConstantId
?
1795 "%spublic static int %s=0x%08x;\n" :
1796 "%spublic static final int %s=0x%08x;\n";
1798 size_t N
= symbols
->getSymbols().size();
1799 for (i
=0; i
<N
; i
++) {
1800 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1801 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1804 if (!includePrivate
&& !sym
.isPublic
) {
1807 String16
name(sym
.name
);
1808 String8
realName(name
);
1809 if (fixupSymbol(&name
) != NO_ERROR
) {
1810 return UNKNOWN_ERROR
;
1812 String16
comment(sym
.comment
);
1813 bool haveComment
= false;
1814 bool deprecated
= false;
1815 if (comment
.size() > 0) {
1817 String8
cmt(comment
);
1820 getIndentSpace(indent
), cmt
.string());
1821 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1824 } else if (sym
.isPublic
&& !includePrivate
) {
1825 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1826 assets
->getPackage().string(), className
.string(),
1827 String8(sym
.name
).string());
1829 String16
typeComment(sym
.typeComment
);
1830 if (typeComment
.size() > 0) {
1831 String8
cmt(typeComment
);
1835 "%s/** %s\n", getIndentSpace(indent
), cmt
.string());
1838 "%s %s\n", getIndentSpace(indent
), cmt
.string());
1840 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1845 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1848 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1850 fprintf(fp
, id_format
,
1851 getIndentSpace(indent
),
1852 String8(name
).string(), (int)sym
.int32Val
);
1855 for (i
=0; i
<N
; i
++) {
1856 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1857 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1860 if (!includePrivate
&& !sym
.isPublic
) {
1863 String16
name(sym
.name
);
1864 if (fixupSymbol(&name
) != NO_ERROR
) {
1865 return UNKNOWN_ERROR
;
1867 String16
comment(sym
.comment
);
1868 bool deprecated
= false;
1869 if (comment
.size() > 0) {
1870 String8
cmt(comment
);
1874 getIndentSpace(indent
), cmt
.string(),
1875 getIndentSpace(indent
));
1876 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1879 } else if (sym
.isPublic
&& !includePrivate
) {
1880 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1881 assets
->getPackage().string(), className
.string(),
1882 String8(sym
.name
).string());
1885 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1887 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1888 getIndentSpace(indent
),
1889 String8(name
).string(), sym
.stringVal
.string());
1892 sp
<AaptSymbols
> styleableSymbols
;
1894 N
= symbols
->getNestedSymbols().size();
1895 for (i
=0; i
<N
; i
++) {
1896 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1897 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1898 if (nclassName
== "styleable") {
1899 styleableSymbols
= nsymbols
;
1901 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
, nonConstantId
);
1903 if (err
!= NO_ERROR
) {
1908 if (styleableSymbols
!= NULL
) {
1909 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1910 if (err
!= NO_ERROR
) {
1916 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1920 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1921 const String8
& package
, bool includePrivate
)
1923 if (!bundle
->getRClassDir()) {
1927 const size_t N
= assets
->getSymbols().size();
1928 for (size_t i
=0; i
<N
; i
++) {
1929 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1930 String8
className(assets
->getSymbols().keyAt(i
));
1931 String8
dest(bundle
->getRClassDir());
1932 if (bundle
->getMakePackageDirs()) {
1933 String8
pkg(package
);
1934 const char* last
= pkg
.string();
1935 const char* s
= last
-1;
1938 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1939 String8
part(last
, s
-last
);
1940 dest
.appendPath(part
);
1941 #ifdef HAVE_MS_C_RUNTIME
1942 _mkdir(dest
.string());
1944 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1950 dest
.appendPath(className
);
1951 dest
.append(".java");
1952 FILE* fp
= fopen(dest
.string(), "w+");
1954 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1955 dest
.string(), strerror(errno
));
1956 return UNKNOWN_ERROR
;
1958 if (bundle
->getVerbose()) {
1959 printf(" Writing symbols for class %s.\n", className
.string());
1963 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1965 " * This class was automatically generated by the\n"
1966 " * aapt tool from the resource data it found. It\n"
1967 " * should not be modified by hand.\n"
1970 "package %s;\n\n", package
.string());
1972 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0, bundle
->getNonConstantId());
1973 if (err
!= NO_ERROR
) {
1978 // If we were asked to generate a dependency file, we'll go ahead and add this R.java
1979 // as a target in the dependency file right next to it.
1980 if (bundle
->getGenDependencies()) {
1981 // Add this R.java to the dependency file
1982 String8
dependencyFile(bundle
->getRClassDir());
1983 dependencyFile
.appendPath("R.java.d");
1985 fp
= fopen(dependencyFile
.string(), "a");
1986 fprintf(fp
,"%s \\\n", dest
.string());
1996 class ProguardKeepSet
1999 // { rule --> { file locations } }
2000 KeyedVector
<String8
, SortedVector
<String8
> > rules
;
2002 void add(const String8
& rule
, const String8
& where
);
2005 void ProguardKeepSet::add(const String8
& rule
, const String8
& where
)
2007 ssize_t index
= rules
.indexOfKey(rule
);
2009 index
= rules
.add(rule
, SortedVector
<String8
>());
2011 rules
.editValueAt(index
).add(where
);
2015 addProguardKeepRule(ProguardKeepSet
* keep
, const String8
& inClassName
,
2016 const char* pkg
, const String8
& srcName
, int line
)
2018 String8
className(inClassName
);
2020 // asdf --> package.asdf
2021 // .asdf .a.b --> package.asdf package.a.b
2022 // asdf.adsf --> asdf.asdf
2023 const char* p
= className
.string();
2024 const char* q
= strchr(p
, '.');
2027 className
.append(inClassName
);
2028 } else if (q
== NULL
) {
2030 className
.append(".");
2031 className
.append(inClassName
);
2035 String8
rule("-keep class ");
2037 rule
+= " { <init>(...); }";
2039 String8
location("view ");
2040 location
+= srcName
;
2042 sprintf(lineno
, ":%d", line
);
2045 keep
->add(rule
, location
);
2049 writeProguardForAndroidManifest(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
2054 ResXMLTree::event_code_t code
;
2056 bool inApplication
= false;
2058 sp
<AaptGroup
> assGroup
;
2059 sp
<AaptFile
> assFile
;
2062 // First, look for a package file to parse. This is required to
2063 // be able to generate the resource information.
2064 assGroup
= assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
2065 if (assGroup
== NULL
) {
2066 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
2070 if (assGroup
->getFiles().size() != 1) {
2071 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
2072 assGroup
->getFiles().valueAt(0)->getPrintableSource().string());
2075 assFile
= assGroup
->getFiles().valueAt(0);
2077 err
= parseXMLResource(assFile
, &tree
);
2078 if (err
!= NO_ERROR
) {
2084 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2085 if (code
== ResXMLTree::END_TAG
) {
2086 if (/* name == "Application" && */ depth
== 2) {
2087 inApplication
= false;
2092 if (code
!= ResXMLTree::START_TAG
) {
2096 String8
tag(tree
.getElementName(&len
));
2097 // printf("Depth %d tag %s\n", depth, tag.string());
2098 bool keepTag
= false;
2100 if (tag
!= "manifest") {
2101 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
2104 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
2105 } else if (depth
== 2) {
2106 if (tag
== "application") {
2107 inApplication
= true;
2110 String8 agent
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
2111 "backupAgent", &error
);
2112 if (agent
.length() > 0) {
2113 addProguardKeepRule(keep
, agent
, pkg
.string(),
2114 assFile
->getPrintableSource(), tree
.getLineNumber());
2116 } else if (tag
== "instrumentation") {
2120 if (!keepTag
&& inApplication
&& depth
== 3) {
2121 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
2126 String8 name
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
2129 fprintf(stderr
, "ERROR: %s\n", error
.string());
2132 if (name
.length() > 0) {
2133 addProguardKeepRule(keep
, name
, pkg
.string(),
2134 assFile
->getPrintableSource(), tree
.getLineNumber());
2142 struct NamespaceAttributePair
{
2146 NamespaceAttributePair(const char* n
, const char* a
) : ns(n
), attr(a
) {}
2147 NamespaceAttributePair() : ns(NULL
), attr(NULL
) {}
2151 writeProguardForXml(ProguardKeepSet
* keep
, const sp
<AaptFile
>& layoutFile
,
2152 const char* startTag
, const KeyedVector
<String8
, NamespaceAttributePair
>* tagAttrPairs
)
2157 ResXMLTree::event_code_t code
;
2159 err
= parseXMLResource(layoutFile
, &tree
);
2160 if (err
!= NO_ERROR
) {
2166 if (startTag
!= NULL
) {
2167 bool haveStart
= false;
2168 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2169 if (code
!= ResXMLTree::START_TAG
) {
2172 String8
tag(tree
.getElementName(&len
));
2173 if (tag
== startTag
) {
2183 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2184 if (code
!= ResXMLTree::START_TAG
) {
2187 String8
tag(tree
.getElementName(&len
));
2189 // If there is no '.', we'll assume that it's one of the built in names.
2190 if (strchr(tag
.string(), '.')) {
2191 addProguardKeepRule(keep
, tag
, NULL
,
2192 layoutFile
->getPrintableSource(), tree
.getLineNumber());
2193 } else if (tagAttrPairs
!= NULL
) {
2194 ssize_t tagIndex
= tagAttrPairs
->indexOfKey(tag
);
2195 if (tagIndex
>= 0) {
2196 const NamespaceAttributePair
& nsAttr
= tagAttrPairs
->valueAt(tagIndex
);
2197 ssize_t attrIndex
= tree
.indexOfAttribute(nsAttr
.ns
, nsAttr
.attr
);
2198 if (attrIndex
< 0) {
2199 // fprintf(stderr, "%s:%d: <%s> does not have attribute %s:%s.\n",
2200 // layoutFile->getPrintableSource().string(), tree.getLineNumber(),
2201 // tag.string(), nsAttr.ns, nsAttr.attr);
2204 addProguardKeepRule(keep
,
2205 String8(tree
.getAttributeStringValue(attrIndex
, &len
)), NULL
,
2206 layoutFile
->getPrintableSource(), tree
.getLineNumber());
2215 static void addTagAttrPair(KeyedVector
<String8
, NamespaceAttributePair
>* dest
,
2216 const char* tag
, const char* ns
, const char* attr
) {
2217 dest
->add(String8(tag
), NamespaceAttributePair(ns
, attr
));
2221 writeProguardForLayouts(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
2225 // tag:attribute pairs that should be checked in layout files.
2226 KeyedVector
<String8
, NamespaceAttributePair
> kLayoutTagAttrPairs
;
2227 addTagAttrPair(&kLayoutTagAttrPairs
, "view", NULL
, "class");
2228 addTagAttrPair(&kLayoutTagAttrPairs
, "fragment", NULL
, "class");
2229 addTagAttrPair(&kLayoutTagAttrPairs
, "fragment", RESOURCES_ANDROID_NAMESPACE
, "name");
2231 // tag:attribute pairs that should be checked in xml files.
2232 KeyedVector
<String8
, NamespaceAttributePair
> kXmlTagAttrPairs
;
2233 addTagAttrPair(&kXmlTagAttrPairs
, "PreferenceScreen", RESOURCES_ANDROID_NAMESPACE
, "fragment");
2234 addTagAttrPair(&kXmlTagAttrPairs
, "header", RESOURCES_ANDROID_NAMESPACE
, "fragment");
2236 const Vector
<sp
<AaptDir
> >& dirs
= assets
->resDirs();
2237 const size_t K
= dirs
.size();
2238 for (size_t k
=0; k
<K
; k
++) {
2239 const sp
<AaptDir
>& d
= dirs
.itemAt(k
);
2240 const String8
& dirName
= d
->getLeaf();
2241 const char* startTag
= NULL
;
2242 const KeyedVector
<String8
, NamespaceAttributePair
>* tagAttrPairs
= NULL
;
2243 if ((dirName
== String8("layout")) || (strncmp(dirName
.string(), "layout-", 7) == 0)) {
2244 tagAttrPairs
= &kLayoutTagAttrPairs
;
2245 } else if ((dirName
== String8("xml")) || (strncmp(dirName
.string(), "xml-", 4) == 0)) {
2246 startTag
= "PreferenceScreen";
2247 tagAttrPairs
= &kXmlTagAttrPairs
;
2252 const KeyedVector
<String8
,sp
<AaptGroup
> > groups
= d
->getFiles();
2253 const size_t N
= groups
.size();
2254 for (size_t i
=0; i
<N
; i
++) {
2255 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
2256 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
= group
->getFiles();
2257 const size_t M
= files
.size();
2258 for (size_t j
=0; j
<M
; j
++) {
2259 err
= writeProguardForXml(keep
, files
.valueAt(j
), startTag
, tagAttrPairs
);
2266 // Handle the overlays
2267 sp
<AaptAssets
> overlay
= assets
->getOverlay();
2268 if (overlay
.get()) {
2269 return writeProguardForLayouts(keep
, overlay
);
2275 writeProguardFile(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
2279 if (!bundle
->getProguardFile()) {
2283 ProguardKeepSet keep
;
2285 err
= writeProguardForAndroidManifest(&keep
, assets
);
2290 err
= writeProguardForLayouts(&keep
, assets
);
2295 FILE* fp
= fopen(bundle
->getProguardFile(), "w+");
2297 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
2298 bundle
->getProguardFile(), strerror(errno
));
2299 return UNKNOWN_ERROR
;
2302 const KeyedVector
<String8
, SortedVector
<String8
> >& rules
= keep
.rules
;
2303 const size_t N
= rules
.size();
2304 for (size_t i
=0; i
<N
; i
++) {
2305 const SortedVector
<String8
>& locations
= rules
.valueAt(i
);
2306 const size_t M
= locations
.size();
2307 for (size_t j
=0; j
<M
; j
++) {
2308 fprintf(fp
, "# %s\n", locations
.itemAt(j
).string());
2310 fprintf(fp
, "%s\n\n", rules
.keyAt(i
).string());
2317 // Loops through the string paths and writes them to the file pointer
2318 // Each file path is written on its own line with a terminating backslash.
2319 status_t
writePathsToFile(const sp
<FilePathStore
>& files
, FILE* fp
)
2322 for (size_t file_i
= 0; file_i
< files
->size(); ++file_i
) {
2323 // Add the full file path to the dependency file
2324 fprintf(fp
, "%s \\\n", files
->itemAt(file_i
).string());
2331 writeDependencyPreReqs(Bundle
* bundle
, const sp
<AaptAssets
>& assets
, FILE* fp
, bool includeRaw
)
2334 deps
+= writePathsToFile(assets
->getFullResPaths(), fp
);
2336 deps
+= writePathsToFile(assets
->getFullAssetPaths(), fp
);