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
== "animator" || type
== "interpolator"
152 || type
== "drawable" || type
== "layout"
153 || type
== "values" || type
== "xml" || type
== "raw"
154 || type
== "color" || type
== "menu" || type
== "mipmap";
157 static sp
<AaptFile
> getResourceFile(const sp
<AaptAssets
>& assets
, bool makeIfNecessary
=true)
159 sp
<AaptGroup
> group
= assets
->getFiles().valueFor(String8("resources.arsc"));
162 file
= group
->getFiles().valueFor(AaptGroupEntry());
168 if (!makeIfNecessary
) {
171 return assets
->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(),
175 static status_t
parsePackage(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
176 const sp
<AaptGroup
>& grp
)
178 if (grp
->getFiles().size() != 1) {
179 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
180 grp
->getFiles().valueAt(0)->getPrintableSource().string());
183 sp
<AaptFile
> file
= grp
->getFiles().valueAt(0);
186 status_t err
= parseXMLResource(file
, &block
);
187 if (err
!= NO_ERROR
) {
190 //printXMLBlock(&block);
192 ResXMLTree::event_code_t code
;
193 while ((code
=block
.next()) != ResXMLTree::START_TAG
194 && code
!= ResXMLTree::END_DOCUMENT
195 && code
!= ResXMLTree::BAD_DOCUMENT
) {
199 if (code
!= ResXMLTree::START_TAG
) {
200 fprintf(stderr
, "%s:%d: No start tag found\n",
201 file
->getPrintableSource().string(), block
.getLineNumber());
202 return UNKNOWN_ERROR
;
204 if (strcmp16(block
.getElementName(&len
), String16("manifest").string()) != 0) {
205 fprintf(stderr
, "%s:%d: Invalid start tag %s, expected <manifest>\n",
206 file
->getPrintableSource().string(), block
.getLineNumber(),
207 String8(block
.getElementName(&len
)).string());
208 return UNKNOWN_ERROR
;
211 ssize_t nameIndex
= block
.indexOfAttribute(NULL
, "package");
213 fprintf(stderr
, "%s:%d: <manifest> does not have package attribute.\n",
214 file
->getPrintableSource().string(), block
.getLineNumber());
215 return UNKNOWN_ERROR
;
218 assets
->setPackage(String8(block
.getAttributeStringValue(nameIndex
, &len
)));
220 String16
uses_sdk16("uses-sdk");
221 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
222 && code
!= ResXMLTree::BAD_DOCUMENT
) {
223 if (code
== ResXMLTree::START_TAG
) {
224 if (strcmp16(block
.getElementName(&len
), uses_sdk16
.string()) == 0) {
225 ssize_t minSdkIndex
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
,
227 if (minSdkIndex
>= 0) {
228 const uint16_t* minSdk16
= block
.getAttributeStringValue(minSdkIndex
, &len
);
229 const char* minSdk8
= strdup(String8(minSdk16
).string());
230 bundle
->setManifestMinSdkVersion(minSdk8
);
239 // ==========================================================================
240 // ==========================================================================
241 // ==========================================================================
243 static status_t
makeFileResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
244 ResourceTable
* table
,
245 const sp
<ResourceTypeSet
>& set
,
248 String8
type8(resType
);
249 String16
type16(resType
);
251 bool hasErrors
= false;
253 ResourceDirIterator
it(set
, String8(resType
));
255 while ((res
=it
.next()) == NO_ERROR
) {
256 if (bundle
->getVerbose()) {
257 printf(" (new resource id %s from %s)\n",
258 it
.getBaseName().string(), it
.getFile()->getPrintableSource().string());
260 String16
baseName(it
.getBaseName());
261 const char16_t* str
= baseName
.string();
262 const char16_t* const end
= str
+ baseName
.size();
264 if (!((*str
>= 'a' && *str
<= 'z')
265 || (*str
>= '0' && *str
<= '9')
266 || *str
== '_' || *str
== '.')) {
267 fprintf(stderr
, "%s: Invalid file name: must contain only [a-z0-9_.]\n",
268 it
.getPath().string());
273 String8 resPath
= it
.getPath();
274 resPath
.convertToResPath();
275 table
->addEntry(SourcePos(it
.getPath(), 0), String16(assets
->getPackage()),
281 assets
->addResource(it
.getLeafName(), resPath
, it
.getFile(), type8
);
284 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
287 static status_t
preProcessImages(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
288 const sp
<ResourceTypeSet
>& set
, const char* type
)
290 ResourceDirIterator
it(set
, String8(type
));
291 Vector
<sp
<AaptFile
> > newNameFiles
;
292 Vector
<String8
> newNamePaths
;
293 bool hasErrors
= false;
295 while ((res
=it
.next()) == NO_ERROR
) {
296 res
= preProcessImage(bundle
, assets
, it
.getFile(), NULL
);
297 if (res
< NO_ERROR
) {
302 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
305 status_t
postProcessImages(const sp
<AaptAssets
>& assets
,
306 ResourceTable
* table
,
307 const sp
<ResourceTypeSet
>& set
)
309 ResourceDirIterator
it(set
, String8("drawable"));
310 bool hasErrors
= false;
312 while ((res
=it
.next()) == NO_ERROR
) {
313 res
= postProcessImage(assets
, table
, it
.getFile());
314 if (res
< NO_ERROR
) {
319 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
322 static void collect_files(const sp
<AaptDir
>& dir
,
323 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
325 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& groups
= dir
->getFiles();
326 int N
= groups
.size();
327 for (int i
=0; i
<N
; i
++) {
328 String8 leafName
= groups
.keyAt(i
);
329 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
331 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
334 if (files
.size() == 0) {
338 String8 resType
= files
.valueAt(0)->getResourceType();
340 ssize_t index
= resources
->indexOfKey(resType
);
343 sp
<ResourceTypeSet
> set
= new ResourceTypeSet();
344 set
->add(leafName
, group
);
345 resources
->add(resType
, set
);
347 sp
<ResourceTypeSet
> set
= resources
->valueAt(index
);
348 index
= set
->indexOfKey(leafName
);
350 set
->add(leafName
, group
);
352 sp
<AaptGroup
> existingGroup
= set
->valueAt(index
);
353 int M
= files
.size();
354 for (int j
=0; j
<M
; j
++) {
355 existingGroup
->addFile(files
.valueAt(j
));
362 static void collect_files(const sp
<AaptAssets
>& ass
,
363 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
365 const Vector
<sp
<AaptDir
> >& dirs
= ass
->resDirs();
368 for (int i
=0; i
<N
; i
++) {
369 sp
<AaptDir
> d
= dirs
.itemAt(i
);
370 collect_files(d
, resources
);
372 // don't try to include the res dir
373 ass
->removeDir(d
->getLeaf());
380 ATTR_LEADING_SPACES
= -3,
381 ATTR_TRAILING_SPACES
= -4
383 static int validateAttr(const String8
& path
, const ResTable
& table
,
384 const ResXMLParser
& parser
,
385 const char* ns
, const char* attr
, const char* validChars
, bool required
)
389 ssize_t index
= parser
.indexOfAttribute(ns
, attr
);
392 if (index
>= 0 && parser
.getAttributeValue(index
, &value
) >= 0) {
393 const ResStringPool
* pool
= &parser
.getStrings();
394 if (value
.dataType
== Res_value::TYPE_REFERENCE
) {
395 uint32_t specFlags
= 0;
397 if ((strIdx
=table
.resolveReference(&value
, 0x10000000, NULL
, &specFlags
)) < 0) {
398 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s references unknown resid 0x%08x.\n",
399 path
.string(), parser
.getLineNumber(),
400 String8(parser
.getElementName(&len
)).string(), attr
,
402 return ATTR_NOT_FOUND
;
405 pool
= table
.getTableStringBlock(strIdx
);
408 str
= pool
->stringAt(value
.data
, &len
);
410 printf("***** RES ATTR: %s specFlags=0x%x strIdx=%d: %s\n", attr
,
411 specFlags
, strIdx
, str
!= NULL
? String8(str
).string() : "???");
413 if ((specFlags
&~ResTable_typeSpec::SPEC_PUBLIC
) != 0 && false) {
414 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s varies by configurations 0x%x.\n",
415 path
.string(), parser
.getLineNumber(),
416 String8(parser
.getElementName(&len
)).string(), attr
,
418 return ATTR_NOT_FOUND
;
421 if (value
.dataType
== Res_value::TYPE_STRING
) {
423 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has no string block.\n",
424 path
.string(), parser
.getLineNumber(),
425 String8(parser
.getElementName(&len
)).string(), attr
);
426 return ATTR_NOT_FOUND
;
428 if ((str
=pool
->stringAt(value
.data
, &len
)) == NULL
) {
429 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has corrupt string value.\n",
430 path
.string(), parser
.getLineNumber(),
431 String8(parser
.getElementName(&len
)).string(), attr
);
432 return ATTR_NOT_FOUND
;
435 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid type %d.\n",
436 path
.string(), parser
.getLineNumber(),
437 String8(parser
.getElementName(&len
)).string(), attr
,
439 return ATTR_NOT_FOUND
;
442 for (size_t i
=0; i
<len
; i
++) {
444 const char* p
= validChars
;
454 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n",
455 path
.string(), parser
.getLineNumber(),
456 String8(parser
.getElementName(&len
)).string(), attr
, (char)str
[i
]);
462 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not start with a space.\n",
463 path
.string(), parser
.getLineNumber(),
464 String8(parser
.getElementName(&len
)).string(), attr
);
465 return ATTR_LEADING_SPACES
;
467 if (str
[len
-1] == ' ') {
468 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not end with a space.\n",
469 path
.string(), parser
.getLineNumber(),
470 String8(parser
.getElementName(&len
)).string(), attr
);
471 return ATTR_TRAILING_SPACES
;
476 fprintf(stderr
, "%s:%d: Tag <%s> missing required attribute %s.\n",
477 path
.string(), parser
.getLineNumber(),
478 String8(parser
.getElementName(&len
)).string(), attr
);
479 return ATTR_NOT_FOUND
;
484 static void checkForIds(const String8
& path
, ResXMLParser
& parser
)
486 ResXMLTree::event_code_t code
;
487 while ((code
=parser
.next()) != ResXMLTree::END_DOCUMENT
488 && code
> ResXMLTree::BAD_DOCUMENT
) {
489 if (code
== ResXMLTree::START_TAG
) {
490 ssize_t index
= parser
.indexOfAttribute(NULL
, "id");
492 fprintf(stderr
, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n",
493 path
.string(), parser
.getLineNumber());
499 static bool applyFileOverlay(Bundle
*bundle
,
500 const sp
<AaptAssets
>& assets
,
501 sp
<ResourceTypeSet
> *baseSet
,
504 if (bundle
->getVerbose()) {
505 printf("applyFileOverlay for %s\n", resType
);
508 // Replace any base level files in this category with any found from the overlay
509 // Also add any found only in the overlay.
510 sp
<AaptAssets
> overlay
= assets
->getOverlay();
511 String8
resTypeString(resType
);
513 // work through the linked list of overlays
514 while (overlay
.get()) {
515 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* overlayRes
= overlay
->getResources();
517 // get the overlay resources of the requested type
518 ssize_t index
= overlayRes
->indexOfKey(resTypeString
);
520 sp
<ResourceTypeSet
> overlaySet
= overlayRes
->valueAt(index
);
522 // for each of the resources, check for a match in the previously built
523 // non-overlay "baseset".
524 size_t overlayCount
= overlaySet
->size();
525 for (size_t overlayIndex
=0; overlayIndex
<overlayCount
; overlayIndex
++) {
526 if (bundle
->getVerbose()) {
527 printf("trying overlaySet Key=%s\n",overlaySet
->keyAt(overlayIndex
).string());
529 size_t baseIndex
= UNKNOWN_ERROR
;
530 if (baseSet
->get() != NULL
) {
531 baseIndex
= (*baseSet
)->indexOfKey(overlaySet
->keyAt(overlayIndex
));
533 if (baseIndex
< UNKNOWN_ERROR
) {
534 // look for same flavor. For a given file (strings.xml, for example)
535 // there may be a locale specific or other flavors - we want to match
537 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
538 sp
<AaptGroup
> baseGroup
= (*baseSet
)->valueAt(baseIndex
);
540 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
541 overlayGroup
->getFiles();
542 if (bundle
->getVerbose()) {
543 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > baseFiles
=
544 baseGroup
->getFiles();
545 for (size_t i
=0; i
< baseFiles
.size(); i
++) {
546 printf("baseFile %zd has flavor %s\n", i
,
547 baseFiles
.keyAt(i
).toString().string());
549 for (size_t i
=0; i
< overlayFiles
.size(); i
++) {
550 printf("overlayFile %zd has flavor %s\n", i
,
551 overlayFiles
.keyAt(i
).toString().string());
555 size_t overlayGroupSize
= overlayFiles
.size();
556 for (size_t overlayGroupIndex
= 0;
557 overlayGroupIndex
<overlayGroupSize
;
558 overlayGroupIndex
++) {
559 size_t baseFileIndex
=
560 baseGroup
->getFiles().indexOfKey(overlayFiles
.
561 keyAt(overlayGroupIndex
));
562 if(baseFileIndex
< UNKNOWN_ERROR
) {
563 if (bundle
->getVerbose()) {
564 printf("found a match (%zd) for overlay file %s, for flavor %s\n",
566 overlayGroup
->getLeaf().string(),
567 overlayFiles
.keyAt(overlayGroupIndex
).toString().string());
569 baseGroup
->removeFile(baseFileIndex
);
571 // didn't find a match fall through and add it..
573 baseGroup
->addFile(overlayFiles
.valueAt(overlayGroupIndex
));
574 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
577 if (baseSet
->get() == NULL
) {
578 *baseSet
= new ResourceTypeSet();
579 assets
->getResources()->add(String8(resType
), *baseSet
);
581 // this group doesn't exist (a file that's only in the overlay)
582 (*baseSet
)->add(overlaySet
->keyAt(overlayIndex
),
583 overlaySet
->valueAt(overlayIndex
));
584 // make sure all flavors are defined in the resources.
585 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
586 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
587 overlayGroup
->getFiles();
588 size_t overlayGroupSize
= overlayFiles
.size();
589 for (size_t overlayGroupIndex
= 0;
590 overlayGroupIndex
<overlayGroupSize
;
591 overlayGroupIndex
++) {
592 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
596 // this overlay didn't have resources for this type
599 overlay
= overlay
->getOverlay();
604 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
,
605 const char* attr8
, const char* value
)
611 const String16
ns(ns8
);
612 const String16
attr(attr8
);
614 if (node
->getAttribute(ns
, attr
) != NULL
) {
615 fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s);"
616 " using existing value in manifest.\n",
617 String8(attr
).string(), String8(ns
).string());
621 node
->addAttribute(ns
, attr
, String16(value
));
624 static void fullyQualifyClassName(const String8
& package
, sp
<XMLNode
> node
,
625 const String16
& attrName
) {
626 XMLNode::attribute_entry
* attr
= node
->editAttribute(
627 String16("http://schemas.android.com/apk/res/android"), attrName
);
629 String8
name(attr
->string
);
631 // asdf --> package.asdf
632 // .asdf .a.b --> package.asdf package.a.b
633 // asdf.adsf --> asdf.asdf
635 const char* p
= name
.string();
636 const char* q
= strchr(p
, '.');
638 className
+= package
;
640 } else if (q
== NULL
) {
641 className
+= package
;
647 NOISY(printf("Qualifying class '%s' to '%s'", name
.string(), className
.string()));
648 attr
->string
.setTo(String16(className
));
652 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
654 root
= root
->searchElement(String16(), String16("manifest"));
656 fprintf(stderr
, "No <manifest> tag.\n");
657 return UNKNOWN_ERROR
;
660 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
661 bundle
->getVersionCode());
662 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
663 bundle
->getVersionName());
665 if (bundle
->getMinSdkVersion() != NULL
666 || bundle
->getTargetSdkVersion() != NULL
667 || bundle
->getMaxSdkVersion() != NULL
) {
668 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
670 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
671 root
->insertChildAt(vers
, 0);
674 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
675 bundle
->getMinSdkVersion());
676 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
677 bundle
->getTargetSdkVersion());
678 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
679 bundle
->getMaxSdkVersion());
682 if (bundle
->getDebugMode()) {
683 sp
<XMLNode
> application
= root
->getChildElement(String16(), String16("application"));
684 if (application
!= NULL
) {
685 addTagAttribute(application
, RESOURCES_ANDROID_NAMESPACE
, "debuggable", "true");
689 // Deal with manifest package name overrides
690 const char* manifestPackageNameOverride
= bundle
->getManifestPackageNameOverride();
691 if (manifestPackageNameOverride
!= NULL
) {
692 // Update the actual package name
693 XMLNode::attribute_entry
* attr
= root
->editAttribute(String16(), String16("package"));
695 fprintf(stderr
, "package name is required with --rename-manifest-package.\n");
696 return UNKNOWN_ERROR
;
698 String8
origPackage(attr
->string
);
699 attr
->string
.setTo(String16(manifestPackageNameOverride
));
700 NOISY(printf("Overriding package '%s' to be '%s'\n", origPackage
.string(), manifestPackageNameOverride
));
702 // Make class names fully qualified
703 sp
<XMLNode
> application
= root
->getChildElement(String16(), String16("application"));
704 if (application
!= NULL
) {
705 fullyQualifyClassName(origPackage
, application
, String16("name"));
706 fullyQualifyClassName(origPackage
, application
, String16("backupAgent"));
708 Vector
<sp
<XMLNode
> >& children
= const_cast<Vector
<sp
<XMLNode
> >&>(application
->getChildren());
709 for (size_t i
= 0; i
< children
.size(); i
++) {
710 sp
<XMLNode
> child
= children
.editItemAt(i
);
711 String8
tag(child
->getElementName());
712 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
713 fullyQualifyClassName(origPackage
, child
, String16("name"));
714 } else if (tag
== "activity-alias") {
715 fullyQualifyClassName(origPackage
, child
, String16("name"));
716 fullyQualifyClassName(origPackage
, child
, String16("targetActivity"));
722 // Deal with manifest package name overrides
723 const char* instrumentationPackageNameOverride
= bundle
->getInstrumentationPackageNameOverride();
724 if (instrumentationPackageNameOverride
!= NULL
) {
725 // Fix up instrumentation targets.
726 Vector
<sp
<XMLNode
> >& children
= const_cast<Vector
<sp
<XMLNode
> >&>(root
->getChildren());
727 for (size_t i
= 0; i
< children
.size(); i
++) {
728 sp
<XMLNode
> child
= children
.editItemAt(i
);
729 String8
tag(child
->getElementName());
730 if (tag
== "instrumentation") {
731 XMLNode::attribute_entry
* attr
= child
->editAttribute(
732 String16("http://schemas.android.com/apk/res/android"), String16("targetPackage"));
734 attr
->string
.setTo(String16(instrumentationPackageNameOverride
));
743 #define ASSIGN_IT(n) \
745 ssize_t index = resources->indexOfKey(String8(#n)); \
747 n ## s = resources->valueAt(index); \
751 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
753 // First, look for a package file to parse. This is required to
754 // be able to generate the resource information.
755 sp
<AaptGroup
> androidManifestFile
=
756 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
757 if (androidManifestFile
== NULL
) {
758 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
759 return UNKNOWN_ERROR
;
762 status_t err
= parsePackage(bundle
, assets
, androidManifestFile
);
763 if (err
!= NO_ERROR
) {
767 NOISY(printf("Creating resources for package %s\n",
768 assets
->getPackage().string()));
770 ResourceTable
table(bundle
, String16(assets
->getPackage()));
771 err
= table
.addIncludedResources(bundle
, assets
);
772 if (err
!= NO_ERROR
) {
776 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
778 // Standard flags for compiled XML and optional UTF-8 encoding
779 int xmlFlags
= XML_COMPILE_STANDARD_RESOURCE
;
781 /* Only enable UTF-8 if the caller of aapt didn't specifically
782 * request UTF-16 encoding and the parameters of this package
783 * allow UTF-8 to be used.
785 if (!bundle
->getWantUTF16()
786 && bundle
->isMinSdkAtLeast(SDK_FROYO
)) {
787 xmlFlags
|= XML_COMPILE_UTF8
;
790 // --------------------------------------------------------------
791 // First, gather all resource information.
792 // --------------------------------------------------------------
794 // resType -> leafName -> group
795 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
796 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
797 collect_files(assets
, resources
);
799 sp
<ResourceTypeSet
> drawables
;
800 sp
<ResourceTypeSet
> layouts
;
801 sp
<ResourceTypeSet
> anims
;
802 sp
<ResourceTypeSet
> animators
;
803 sp
<ResourceTypeSet
> interpolators
;
804 sp
<ResourceTypeSet
> xmls
;
805 sp
<ResourceTypeSet
> raws
;
806 sp
<ResourceTypeSet
> colors
;
807 sp
<ResourceTypeSet
> menus
;
808 sp
<ResourceTypeSet
> mipmaps
;
814 ASSIGN_IT(interpolator
);
821 assets
->setResources(resources
);
822 // now go through any resource overlays and collect their files
823 sp
<AaptAssets
> current
= assets
->getOverlay();
824 while(current
.get()) {
825 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
826 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
827 current
->setResources(resources
);
828 collect_files(current
, resources
);
829 current
= current
->getOverlay();
831 // apply the overlay files to the base set
832 if (!applyFileOverlay(bundle
, assets
, &drawables
, "drawable") ||
833 !applyFileOverlay(bundle
, assets
, &layouts
, "layout") ||
834 !applyFileOverlay(bundle
, assets
, &anims
, "anim") ||
835 !applyFileOverlay(bundle
, assets
, &animators
, "animator") ||
836 !applyFileOverlay(bundle
, assets
, &interpolators
, "interpolator") ||
837 !applyFileOverlay(bundle
, assets
, &xmls
, "xml") ||
838 !applyFileOverlay(bundle
, assets
, &raws
, "raw") ||
839 !applyFileOverlay(bundle
, assets
, &colors
, "color") ||
840 !applyFileOverlay(bundle
, assets
, &menus
, "menu") ||
841 !applyFileOverlay(bundle
, assets
, &mipmaps
, "mipmap")) {
842 return UNKNOWN_ERROR
;
845 bool hasErrors
= false;
847 if (drawables
!= NULL
) {
848 if (bundle
->getOutputAPKFile() != NULL
) {
849 err
= preProcessImages(bundle
, assets
, drawables
, "drawable");
851 if (err
== NO_ERROR
) {
852 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
853 if (err
!= NO_ERROR
) {
861 if (mipmaps
!= NULL
) {
862 if (bundle
->getOutputAPKFile() != NULL
) {
863 err
= preProcessImages(bundle
, assets
, mipmaps
, "mipmap");
865 if (err
== NO_ERROR
) {
866 err
= makeFileResources(bundle
, assets
, &table
, mipmaps
, "mipmap");
867 if (err
!= NO_ERROR
) {
875 if (layouts
!= NULL
) {
876 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
877 if (err
!= NO_ERROR
) {
883 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
884 if (err
!= NO_ERROR
) {
889 if (animators
!= NULL
) {
890 err
= makeFileResources(bundle
, assets
, &table
, animators
, "animator");
891 if (err
!= NO_ERROR
) {
896 if (interpolators
!= NULL
) {
897 err
= makeFileResources(bundle
, assets
, &table
, interpolators
, "interpolator");
898 if (err
!= NO_ERROR
) {
904 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
905 if (err
!= NO_ERROR
) {
911 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
912 if (err
!= NO_ERROR
) {
919 while(current
.get()) {
920 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
921 current
->getResources();
923 ssize_t index
= resources
->indexOfKey(String8("values"));
925 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
927 while ((res
=it
.next()) == NO_ERROR
) {
928 sp
<AaptFile
> file
= it
.getFile();
929 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
930 (current
!=assets
), &table
);
931 if (res
!= NO_ERROR
) {
936 current
= current
->getOverlay();
939 if (colors
!= NULL
) {
940 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
941 if (err
!= NO_ERROR
) {
947 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
948 if (err
!= NO_ERROR
) {
953 // --------------------------------------------------------------------
954 // Assignment of resource IDs and initial generation of resource table.
955 // --------------------------------------------------------------------
957 if (table
.hasResources()) {
958 sp
<AaptFile
> resFile(getResourceFile(assets
));
959 if (resFile
== NULL
) {
960 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
961 return UNKNOWN_ERROR
;
964 err
= table
.assignResourceIds();
965 if (err
< NO_ERROR
) {
970 // --------------------------------------------------------------
971 // Finally, we can now we can compile XML files, which may reference
973 // --------------------------------------------------------------
975 if (layouts
!= NULL
) {
976 ResourceDirIterator
it(layouts
, String8("layout"));
977 while ((err
=it
.next()) == NO_ERROR
) {
978 String8 src
= it
.getFile()->getPrintableSource();
979 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
980 if (err
== NO_ERROR
) {
982 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
983 checkForIds(src
, block
);
989 if (err
< NO_ERROR
) {
996 ResourceDirIterator
it(anims
, String8("anim"));
997 while ((err
=it
.next()) == NO_ERROR
) {
998 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
999 if (err
!= NO_ERROR
) {
1004 if (err
< NO_ERROR
) {
1010 if (animators
!= NULL
) {
1011 ResourceDirIterator
it(animators
, String8("animator"));
1012 while ((err
=it
.next()) == NO_ERROR
) {
1013 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1014 if (err
!= NO_ERROR
) {
1019 if (err
< NO_ERROR
) {
1025 if (interpolators
!= NULL
) {
1026 ResourceDirIterator
it(interpolators
, String8("interpolator"));
1027 while ((err
=it
.next()) == NO_ERROR
) {
1028 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1029 if (err
!= NO_ERROR
) {
1034 if (err
< NO_ERROR
) {
1041 ResourceDirIterator
it(xmls
, String8("xml"));
1042 while ((err
=it
.next()) == NO_ERROR
) {
1043 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1044 if (err
!= NO_ERROR
) {
1049 if (err
< NO_ERROR
) {
1055 if (drawables
!= NULL
) {
1056 err
= postProcessImages(assets
, &table
, drawables
);
1057 if (err
!= NO_ERROR
) {
1062 if (colors
!= NULL
) {
1063 ResourceDirIterator
it(colors
, String8("color"));
1064 while ((err
=it
.next()) == NO_ERROR
) {
1065 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1066 if (err
!= NO_ERROR
) {
1071 if (err
< NO_ERROR
) {
1077 if (menus
!= NULL
) {
1078 ResourceDirIterator
it(menus
, String8("menu"));
1079 while ((err
=it
.next()) == NO_ERROR
) {
1080 String8 src
= it
.getFile()->getPrintableSource();
1081 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1082 if (err
!= NO_ERROR
) {
1086 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
1087 checkForIds(src
, block
);
1090 if (err
< NO_ERROR
) {
1096 if (table
.validateLocalizations()) {
1101 return UNKNOWN_ERROR
;
1104 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
1105 String8
manifestPath(manifestFile
->getPrintableSource());
1107 // Generate final compiled manifest file.
1108 manifestFile
->clearData();
1109 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1110 if (manifestTree
== NULL
) {
1111 return UNKNOWN_ERROR
;
1113 err
= massageManifest(bundle
, manifestTree
);
1114 if (err
< NO_ERROR
) {
1117 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1118 if (err
< NO_ERROR
) {
1123 //printXMLBlock(&block);
1125 // --------------------------------------------------------------
1126 // Generate the final resource table.
1127 // Re-flatten because we may have added new resource IDs
1128 // --------------------------------------------------------------
1130 ResTable finalResTable
;
1131 sp
<AaptFile
> resFile
;
1133 if (table
.hasResources()) {
1134 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1135 err
= table
.addSymbols(symbols
);
1136 if (err
< NO_ERROR
) {
1140 resFile
= getResourceFile(assets
);
1141 if (resFile
== NULL
) {
1142 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1143 return UNKNOWN_ERROR
;
1146 err
= table
.flatten(bundle
, resFile
);
1147 if (err
< NO_ERROR
) {
1151 if (bundle
->getPublicOutputFile()) {
1152 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1154 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1155 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1156 return UNKNOWN_ERROR
;
1158 if (bundle
->getVerbose()) {
1159 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1161 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1165 // Read resources back in,
1166 finalResTable
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1170 printf("Generated resources:\n");
1171 finalResTable
.print();
1176 // Perform a basic validation of the manifest file. This time we
1177 // parse it with the comments intact, so that we can use them to
1178 // generate java docs... so we are not going to write this one
1179 // back out to the final manifest data.
1180 sp
<AaptFile
> outManifestFile
= new AaptFile(manifestFile
->getSourceFile(),
1181 manifestFile
->getGroupEntry(),
1182 manifestFile
->getResourceType());
1183 err
= compileXmlFile(assets
, manifestFile
,
1184 outManifestFile
, &table
,
1185 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
1186 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
1187 if (err
< NO_ERROR
) {
1191 block
.setTo(outManifestFile
->getData(), outManifestFile
->getSize(), true);
1192 String16
manifest16("manifest");
1193 String16
permission16("permission");
1194 String16
permission_group16("permission-group");
1195 String16
uses_permission16("uses-permission");
1196 String16
instrumentation16("instrumentation");
1197 String16
application16("application");
1198 String16
provider16("provider");
1199 String16
service16("service");
1200 String16
receiver16("receiver");
1201 String16
activity16("activity");
1202 String16
action16("action");
1203 String16
category16("category");
1204 String16
data16("scheme");
1205 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1206 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
1207 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
1208 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
1209 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1210 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
1211 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1212 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
1213 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1214 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
1215 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1216 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
1217 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1218 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
1219 ResXMLTree::event_code_t code
;
1220 sp
<AaptSymbols
> permissionSymbols
;
1221 sp
<AaptSymbols
> permissionGroupSymbols
;
1222 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1223 && code
> ResXMLTree::BAD_DOCUMENT
) {
1224 if (code
== ResXMLTree::START_TAG
) {
1226 if (block
.getElementNamespace(&len
) != NULL
) {
1229 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
1230 if (validateAttr(manifestPath
, finalResTable
, block
, NULL
, "package",
1231 packageIdentChars
, true) != ATTR_OKAY
) {
1234 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1235 "sharedUserId", packageIdentChars
, false) != ATTR_OKAY
) {
1238 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
1239 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
1240 const bool isGroup
= strcmp16(block
.getElementName(&len
),
1241 permission_group16
.string()) == 0;
1242 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1243 "name", isGroup
? packageIdentCharsWithTheStupid
1244 : packageIdentChars
, true) != ATTR_OKAY
) {
1247 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
1248 sp
<AaptSymbols
> syms
;
1250 syms
= permissionSymbols
;
1252 sp
<AaptSymbols
> symbols
=
1253 assets
->getSymbolsFor(String8("Manifest"));
1254 syms
= permissionSymbols
= symbols
->addNestedSymbol(
1255 String8("permission"), srcPos
);
1258 syms
= permissionGroupSymbols
;
1260 sp
<AaptSymbols
> symbols
=
1261 assets
->getSymbolsFor(String8("Manifest"));
1262 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
1263 String8("permission_group"), srcPos
);
1267 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
1268 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
1270 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
1271 manifestPath
.string(), block
.getLineNumber(),
1272 String8(block
.getElementName(&len
)).string());
1277 char* p
= idStr
.lockBuffer(idStr
.size());
1278 char* e
= p
+ idStr
.size();
1279 bool begins_with_digit
= true; // init to true so an empty string fails
1282 if (*e
>= '0' && *e
<= '9') {
1283 begins_with_digit
= true;
1286 if ((*e
>= 'a' && *e
<= 'z') ||
1287 (*e
>= 'A' && *e
<= 'Z') ||
1289 begins_with_digit
= false;
1292 if (isGroup
&& (*e
== '-')) {
1294 begins_with_digit
= false;
1300 idStr
.unlockBuffer();
1301 // verify that we stopped because we hit a period or
1302 // the beginning of the string, and that the
1303 // identifier didn't begin with a digit.
1304 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
1306 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
1307 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
1310 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
1311 const uint16_t* cmt
= block
.getComment(&len
);
1312 if (cmt
!= NULL
&& *cmt
!= 0) {
1313 //printf("Comment of %s: %s\n", String8(e).string(),
1314 // String8(cmt).string());
1315 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
1317 //printf("No comment for %s\n", String8(e).string());
1319 syms
->makeSymbolPublic(String8(e
), srcPos
);
1320 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
1321 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1322 "name", packageIdentChars
, true) != ATTR_OKAY
) {
1325 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
1326 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1327 "name", classIdentChars
, true) != ATTR_OKAY
) {
1330 if (validateAttr(manifestPath
, finalResTable
, block
,
1331 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
1332 packageIdentChars
, true) != ATTR_OKAY
) {
1335 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
1336 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1337 "name", classIdentChars
, false) != ATTR_OKAY
) {
1340 if (validateAttr(manifestPath
, finalResTable
, block
,
1341 RESOURCES_ANDROID_NAMESPACE
, "permission",
1342 packageIdentChars
, false) != ATTR_OKAY
) {
1345 if (validateAttr(manifestPath
, finalResTable
, block
,
1346 RESOURCES_ANDROID_NAMESPACE
, "process",
1347 processIdentChars
, false) != ATTR_OKAY
) {
1350 if (validateAttr(manifestPath
, finalResTable
, block
,
1351 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1352 processIdentChars
, false) != ATTR_OKAY
) {
1355 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
1356 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1357 "name", classIdentChars
, true) != ATTR_OKAY
) {
1360 if (validateAttr(manifestPath
, finalResTable
, block
,
1361 RESOURCES_ANDROID_NAMESPACE
, "authorities",
1362 authoritiesIdentChars
, true) != ATTR_OKAY
) {
1365 if (validateAttr(manifestPath
, finalResTable
, block
,
1366 RESOURCES_ANDROID_NAMESPACE
, "permission",
1367 packageIdentChars
, false) != ATTR_OKAY
) {
1370 if (validateAttr(manifestPath
, finalResTable
, block
,
1371 RESOURCES_ANDROID_NAMESPACE
, "process",
1372 processIdentChars
, false) != ATTR_OKAY
) {
1375 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1376 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1377 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1378 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1379 "name", classIdentChars
, true) != ATTR_OKAY
) {
1382 if (validateAttr(manifestPath
, finalResTable
, block
,
1383 RESOURCES_ANDROID_NAMESPACE
, "permission",
1384 packageIdentChars
, false) != ATTR_OKAY
) {
1387 if (validateAttr(manifestPath
, finalResTable
, block
,
1388 RESOURCES_ANDROID_NAMESPACE
, "process",
1389 processIdentChars
, false) != ATTR_OKAY
) {
1392 if (validateAttr(manifestPath
, finalResTable
, block
,
1393 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1394 processIdentChars
, false) != ATTR_OKAY
) {
1397 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1398 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1399 if (validateAttr(manifestPath
, finalResTable
, block
,
1400 RESOURCES_ANDROID_NAMESPACE
, "name",
1401 packageIdentChars
, true) != ATTR_OKAY
) {
1404 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1405 if (validateAttr(manifestPath
, finalResTable
, block
,
1406 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1407 typeIdentChars
, true) != ATTR_OKAY
) {
1410 if (validateAttr(manifestPath
, finalResTable
, block
,
1411 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1412 schemeIdentChars
, true) != ATTR_OKAY
) {
1419 if (resFile
!= NULL
) {
1420 // These resources are now considered to be a part of the included
1421 // resources, for others to reference.
1422 err
= assets
->addIncludedResources(resFile
);
1423 if (err
< NO_ERROR
) {
1424 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1432 static const char* getIndentSpace(int indent
)
1434 static const char whitespace
[] =
1437 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1440 static status_t
fixupSymbol(String16
* inoutSymbol
)
1442 inoutSymbol
->replaceAll('.', '_');
1443 inoutSymbol
->replaceAll(':', '_');
1447 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1448 const String8
& name
,
1449 String16
* outTypeComment
= NULL
)
1451 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1453 //printf("Got R symbols!\n");
1454 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1456 //printf("Got attrs symbols! comment %s=%s\n",
1457 // name.string(), String8(asym->getComment(name)).string());
1458 if (outTypeComment
!= NULL
) {
1459 *outTypeComment
= asym
->getTypeComment(name
);
1461 return asym
->getComment(name
);
1467 static status_t
writeLayoutClasses(
1468 FILE* fp
, const sp
<AaptAssets
>& assets
,
1469 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1471 const char* indentStr
= getIndentSpace(indent
);
1472 if (!includePrivate
) {
1473 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1475 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1478 String16
attr16("attr");
1479 String16
package16(assets
->getPackage());
1481 indentStr
= getIndentSpace(indent
);
1482 bool hasErrors
= false;
1485 size_t N
= symbols
->getNestedSymbols().size();
1486 for (i
=0; i
<N
; i
++) {
1487 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1488 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1489 String8
realClassName(nclassName16
);
1490 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1493 String8
nclassName(nclassName16
);
1495 SortedVector
<uint32_t> idents
;
1496 Vector
<uint32_t> origOrder
;
1497 Vector
<bool> publicFlags
;
1500 size_t NA
= nsymbols
->getSymbols().size();
1501 for (a
=0; a
<NA
; a
++) {
1502 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1503 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1505 bool isPublic
= true;
1507 String16
name16(sym
.name
);
1508 uint32_t typeSpecFlags
;
1509 code
= assets
->getIncludedResources().identifierForName(
1510 name16
.string(), name16
.size(),
1511 attr16
.string(), attr16
.size(),
1512 package16
.string(), package16
.size(), &typeSpecFlags
);
1514 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1515 nclassName
.string(), sym
.name
.string());
1518 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1521 origOrder
.add(code
);
1522 publicFlags
.add(isPublic
);
1527 bool deprecated
= false;
1529 String16 comment
= symbols
->getComment(realClassName
);
1530 fprintf(fp
, "%s/** ", indentStr
);
1531 if (comment
.size() > 0) {
1532 String8
cmt(comment
);
1533 fprintf(fp
, "%s\n", cmt
.string());
1534 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1538 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1540 bool hasTable
= false;
1541 for (a
=0; a
<NA
; a
++) {
1542 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1547 "%s <p>Includes the following attributes:</p>\n"
1549 "%s <colgroup align=\"left\" />\n"
1550 "%s <colgroup align=\"left\" />\n"
1551 "%s <tr><th>Attribute</th><th>Description</th></tr>\n",
1558 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1559 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1562 String8
name8(sym
.name
);
1563 String16
comment(sym
.comment
);
1564 if (comment
.size() <= 0) {
1565 comment
= getAttributeComment(assets
, name8
);
1567 if (comment
.size() > 0) {
1568 const char16_t* p
= comment
.string();
1569 while (*p
!= 0 && *p
!= '.') {
1571 while (*p
!= 0 && *p
!= '}') {
1581 comment
= String16(comment
.string(), p
-comment
.string());
1583 String16
name(name8
);
1585 fprintf(fp
, "%s <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n",
1586 indentStr
, nclassName
.string(),
1587 String8(name
).string(),
1588 assets
->getPackage().string(),
1589 String8(name
).string(),
1590 String8(comment
).string());
1594 fprintf(fp
, "%s </table>\n", indentStr
);
1596 for (a
=0; a
<NA
; a
++) {
1597 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1599 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1600 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1603 String16
name(sym
.name
);
1605 fprintf(fp
, "%s @see #%s_%s\n",
1606 indentStr
, nclassName
.string(),
1607 String8(name
).string());
1610 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1613 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1617 "%spublic static final int[] %s = {\n"
1619 indentStr
, nclassName
.string(),
1620 getIndentSpace(indent
+1));
1622 for (a
=0; a
<NA
; a
++) {
1625 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1630 fprintf(fp
, "0x%08x", idents
[a
]);
1633 fprintf(fp
, "\n%s};\n", indentStr
);
1635 for (a
=0; a
<NA
; a
++) {
1636 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1638 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1639 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1642 String8
name8(sym
.name
);
1643 String16
comment(sym
.comment
);
1644 String16 typeComment
;
1645 if (comment
.size() <= 0) {
1646 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1648 getAttributeComment(assets
, name8
, &typeComment
);
1650 String16
name(name8
);
1651 if (fixupSymbol(&name
) != NO_ERROR
) {
1655 uint32_t typeSpecFlags
= 0;
1656 String16
name16(sym
.name
);
1657 assets
->getIncludedResources().identifierForName(
1658 name16
.string(), name16
.size(),
1659 attr16
.string(), attr16
.size(),
1660 package16
.string(), package16
.size(), &typeSpecFlags
);
1661 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1662 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1663 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1665 bool deprecated
= false;
1667 fprintf(fp
, "%s/**\n", indentStr
);
1668 if (comment
.size() > 0) {
1669 String8
cmt(comment
);
1670 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1671 fprintf(fp
, "%s %s\n", indentStr
, cmt
.string());
1672 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1677 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1678 "%s attribute's value can be found in the {@link #%s} array.\n",
1680 pub
? assets
->getPackage().string()
1681 : assets
->getSymbolsPrivatePackage().string(),
1682 String8(name
).string(),
1683 indentStr
, nclassName
.string());
1685 if (typeComment
.size() > 0) {
1686 String8
cmt(typeComment
);
1687 fprintf(fp
, "\n\n%s %s\n", indentStr
, cmt
.string());
1688 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1692 if (comment
.size() > 0) {
1695 "%s <p>This corresponds to the global attribute"
1696 "%s resource symbol {@link %s.R.attr#%s}.\n",
1697 indentStr
, indentStr
,
1698 assets
->getPackage().string(),
1699 String8(name
).string());
1702 "%s <p>This is a private symbol.\n", indentStr
);
1705 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1706 "android", String8(name
).string());
1707 fprintf(fp
, "%s*/\n", indentStr
);
1709 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1712 "%spublic static final int %s_%s = %d;\n",
1713 indentStr
, nclassName
.string(),
1714 String8(name
).string(), (int)pos
);
1720 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1721 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1724 static status_t
writeSymbolClass(
1725 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1726 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
,
1729 fprintf(fp
, "%spublic %sfinal class %s {\n",
1730 getIndentSpace(indent
),
1731 indent
!= 0 ? "static " : "", className
.string());
1735 status_t err
= NO_ERROR
;
1737 const char * id_format
= nonConstantId
?
1738 "%spublic static int %s=0x%08x;\n" :
1739 "%spublic static final int %s=0x%08x;\n";
1741 size_t N
= symbols
->getSymbols().size();
1742 for (i
=0; i
<N
; i
++) {
1743 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1744 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1747 if (!includePrivate
&& !sym
.isPublic
) {
1750 String16
name(sym
.name
);
1751 String8
realName(name
);
1752 if (fixupSymbol(&name
) != NO_ERROR
) {
1753 return UNKNOWN_ERROR
;
1755 String16
comment(sym
.comment
);
1756 bool haveComment
= false;
1757 bool deprecated
= false;
1758 if (comment
.size() > 0) {
1760 String8
cmt(comment
);
1763 getIndentSpace(indent
), cmt
.string());
1764 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1767 } else if (sym
.isPublic
&& !includePrivate
) {
1768 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1769 assets
->getPackage().string(), className
.string(),
1770 String8(sym
.name
).string());
1772 String16
typeComment(sym
.typeComment
);
1773 if (typeComment
.size() > 0) {
1774 String8
cmt(typeComment
);
1778 "%s/** %s\n", getIndentSpace(indent
), cmt
.string());
1781 "%s %s\n", getIndentSpace(indent
), cmt
.string());
1783 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1788 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1791 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1793 fprintf(fp
, id_format
,
1794 getIndentSpace(indent
),
1795 String8(name
).string(), (int)sym
.int32Val
);
1798 for (i
=0; i
<N
; i
++) {
1799 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1800 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1803 if (!includePrivate
&& !sym
.isPublic
) {
1806 String16
name(sym
.name
);
1807 if (fixupSymbol(&name
) != NO_ERROR
) {
1808 return UNKNOWN_ERROR
;
1810 String16
comment(sym
.comment
);
1811 bool deprecated
= false;
1812 if (comment
.size() > 0) {
1813 String8
cmt(comment
);
1817 getIndentSpace(indent
), cmt
.string(),
1818 getIndentSpace(indent
));
1819 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1822 } else if (sym
.isPublic
&& !includePrivate
) {
1823 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1824 assets
->getPackage().string(), className
.string(),
1825 String8(sym
.name
).string());
1828 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1830 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1831 getIndentSpace(indent
),
1832 String8(name
).string(), sym
.stringVal
.string());
1835 sp
<AaptSymbols
> styleableSymbols
;
1837 N
= symbols
->getNestedSymbols().size();
1838 for (i
=0; i
<N
; i
++) {
1839 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1840 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1841 if (nclassName
== "styleable") {
1842 styleableSymbols
= nsymbols
;
1844 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
, nonConstantId
);
1846 if (err
!= NO_ERROR
) {
1851 if (styleableSymbols
!= NULL
) {
1852 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1853 if (err
!= NO_ERROR
) {
1859 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1863 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1864 const String8
& package
, bool includePrivate
)
1866 if (!bundle
->getRClassDir()) {
1870 const size_t N
= assets
->getSymbols().size();
1871 for (size_t i
=0; i
<N
; i
++) {
1872 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1873 String8
className(assets
->getSymbols().keyAt(i
));
1874 String8
dest(bundle
->getRClassDir());
1875 if (bundle
->getMakePackageDirs()) {
1876 String8
pkg(package
);
1877 const char* last
= pkg
.string();
1878 const char* s
= last
-1;
1881 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1882 String8
part(last
, s
-last
);
1883 dest
.appendPath(part
);
1884 #ifdef HAVE_MS_C_RUNTIME
1885 _mkdir(dest
.string());
1887 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1893 dest
.appendPath(className
);
1894 dest
.append(".java");
1895 FILE* fp
= fopen(dest
.string(), "w+");
1897 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1898 dest
.string(), strerror(errno
));
1899 return UNKNOWN_ERROR
;
1901 if (bundle
->getVerbose()) {
1902 printf(" Writing symbols for class %s.\n", className
.string());
1906 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1908 " * This class was automatically generated by the\n"
1909 " * aapt tool from the resource data it found. It\n"
1910 " * should not be modified by hand.\n"
1913 "package %s;\n\n", package
.string());
1915 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0, bundle
->getNonConstantId());
1916 if (err
!= NO_ERROR
) {
1927 class ProguardKeepSet
1930 // { rule --> { file locations } }
1931 KeyedVector
<String8
, SortedVector
<String8
> > rules
;
1933 void add(const String8
& rule
, const String8
& where
);
1936 void ProguardKeepSet::add(const String8
& rule
, const String8
& where
)
1938 ssize_t index
= rules
.indexOfKey(rule
);
1940 index
= rules
.add(rule
, SortedVector
<String8
>());
1942 rules
.editValueAt(index
).add(where
);
1946 addProguardKeepRule(ProguardKeepSet
* keep
, const String8
& inClassName
,
1947 const char* pkg
, const String8
& srcName
, int line
)
1949 String8
className(inClassName
);
1951 // asdf --> package.asdf
1952 // .asdf .a.b --> package.asdf package.a.b
1953 // asdf.adsf --> asdf.asdf
1954 const char* p
= className
.string();
1955 const char* q
= strchr(p
, '.');
1958 className
.append(inClassName
);
1959 } else if (q
== NULL
) {
1961 className
.append(".");
1962 className
.append(inClassName
);
1966 String8
rule("-keep class ");
1968 rule
+= " { <init>(...); }";
1970 String8
location("view ");
1971 location
+= srcName
;
1973 sprintf(lineno
, ":%d", line
);
1976 keep
->add(rule
, location
);
1980 writeProguardForAndroidManifest(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
1985 ResXMLTree::event_code_t code
;
1987 bool inApplication
= false;
1989 sp
<AaptGroup
> assGroup
;
1990 sp
<AaptFile
> assFile
;
1993 // First, look for a package file to parse. This is required to
1994 // be able to generate the resource information.
1995 assGroup
= assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
1996 if (assGroup
== NULL
) {
1997 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
2001 if (assGroup
->getFiles().size() != 1) {
2002 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
2003 assGroup
->getFiles().valueAt(0)->getPrintableSource().string());
2006 assFile
= assGroup
->getFiles().valueAt(0);
2008 err
= parseXMLResource(assFile
, &tree
);
2009 if (err
!= NO_ERROR
) {
2015 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2016 if (code
== ResXMLTree::END_TAG
) {
2017 if (/* name == "Application" && */ depth
== 2) {
2018 inApplication
= false;
2023 if (code
!= ResXMLTree::START_TAG
) {
2027 String8
tag(tree
.getElementName(&len
));
2028 // printf("Depth %d tag %s\n", depth, tag.string());
2029 bool keepTag
= false;
2031 if (tag
!= "manifest") {
2032 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
2035 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
2036 } else if (depth
== 2) {
2037 if (tag
== "application") {
2038 inApplication
= true;
2041 String8 agent
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
2042 "backupAgent", &error
);
2043 if (agent
.length() > 0) {
2044 addProguardKeepRule(keep
, agent
, pkg
.string(),
2045 assFile
->getPrintableSource(), tree
.getLineNumber());
2047 } else if (tag
== "instrumentation") {
2051 if (!keepTag
&& inApplication
&& depth
== 3) {
2052 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
2057 String8 name
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
2060 fprintf(stderr
, "ERROR: %s\n", error
.string());
2063 if (name
.length() > 0) {
2064 addProguardKeepRule(keep
, name
, pkg
.string(),
2065 assFile
->getPrintableSource(), tree
.getLineNumber());
2073 struct NamespaceAttributePair
{
2077 NamespaceAttributePair(const char* n
, const char* a
) : ns(n
), attr(a
) {}
2078 NamespaceAttributePair() : ns(NULL
), attr(NULL
) {}
2082 writeProguardForXml(ProguardKeepSet
* keep
, const sp
<AaptFile
>& layoutFile
,
2083 const char* startTag
, const KeyedVector
<String8
, NamespaceAttributePair
>* tagAttrPairs
)
2088 ResXMLTree::event_code_t code
;
2090 err
= parseXMLResource(layoutFile
, &tree
);
2091 if (err
!= NO_ERROR
) {
2097 if (startTag
!= NULL
) {
2098 bool haveStart
= false;
2099 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2100 if (code
!= ResXMLTree::START_TAG
) {
2103 String8
tag(tree
.getElementName(&len
));
2104 if (tag
== startTag
) {
2114 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2115 if (code
!= ResXMLTree::START_TAG
) {
2118 String8
tag(tree
.getElementName(&len
));
2120 // If there is no '.', we'll assume that it's one of the built in names.
2121 if (strchr(tag
.string(), '.')) {
2122 addProguardKeepRule(keep
, tag
, NULL
,
2123 layoutFile
->getPrintableSource(), tree
.getLineNumber());
2124 } else if (tagAttrPairs
!= NULL
) {
2125 ssize_t tagIndex
= tagAttrPairs
->indexOfKey(tag
);
2126 if (tagIndex
>= 0) {
2127 const NamespaceAttributePair
& nsAttr
= tagAttrPairs
->valueAt(tagIndex
);
2128 ssize_t attrIndex
= tree
.indexOfAttribute(nsAttr
.ns
, nsAttr
.attr
);
2129 if (attrIndex
< 0) {
2130 // fprintf(stderr, "%s:%d: <%s> does not have attribute %s:%s.\n",
2131 // layoutFile->getPrintableSource().string(), tree.getLineNumber(),
2132 // tag.string(), nsAttr.ns, nsAttr.attr);
2135 addProguardKeepRule(keep
,
2136 String8(tree
.getAttributeStringValue(attrIndex
, &len
)), NULL
,
2137 layoutFile
->getPrintableSource(), tree
.getLineNumber());
2146 static void addTagAttrPair(KeyedVector
<String8
, NamespaceAttributePair
>* dest
,
2147 const char* tag
, const char* ns
, const char* attr
) {
2148 dest
->add(String8(tag
), NamespaceAttributePair(ns
, attr
));
2152 writeProguardForLayouts(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
2156 // tag:attribute pairs that should be checked in layout files.
2157 KeyedVector
<String8
, NamespaceAttributePair
> kLayoutTagAttrPairs
;
2158 addTagAttrPair(&kLayoutTagAttrPairs
, "view", NULL
, "class");
2159 addTagAttrPair(&kLayoutTagAttrPairs
, "fragment", NULL
, "class");
2160 addTagAttrPair(&kLayoutTagAttrPairs
, "fragment", RESOURCES_ANDROID_NAMESPACE
, "name");
2162 // tag:attribute pairs that should be checked in xml files.
2163 KeyedVector
<String8
, NamespaceAttributePair
> kXmlTagAttrPairs
;
2164 addTagAttrPair(&kXmlTagAttrPairs
, "PreferenceScreen", RESOURCES_ANDROID_NAMESPACE
, "fragment");
2165 addTagAttrPair(&kXmlTagAttrPairs
, "header", RESOURCES_ANDROID_NAMESPACE
, "fragment");
2167 const Vector
<sp
<AaptDir
> >& dirs
= assets
->resDirs();
2168 const size_t K
= dirs
.size();
2169 for (size_t k
=0; k
<K
; k
++) {
2170 const sp
<AaptDir
>& d
= dirs
.itemAt(k
);
2171 const String8
& dirName
= d
->getLeaf();
2172 const char* startTag
= NULL
;
2173 const KeyedVector
<String8
, NamespaceAttributePair
>* tagAttrPairs
= NULL
;
2174 if ((dirName
== String8("layout")) || (strncmp(dirName
.string(), "layout-", 7) == 0)) {
2175 tagAttrPairs
= &kLayoutTagAttrPairs
;
2176 } else if ((dirName
== String8("xml")) || (strncmp(dirName
.string(), "xml-", 4) == 0)) {
2177 startTag
= "PreferenceScreen";
2178 tagAttrPairs
= &kXmlTagAttrPairs
;
2183 const KeyedVector
<String8
,sp
<AaptGroup
> > groups
= d
->getFiles();
2184 const size_t N
= groups
.size();
2185 for (size_t i
=0; i
<N
; i
++) {
2186 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
2187 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
= group
->getFiles();
2188 const size_t M
= files
.size();
2189 for (size_t j
=0; j
<M
; j
++) {
2190 err
= writeProguardForXml(keep
, files
.valueAt(j
), startTag
, tagAttrPairs
);
2197 // Handle the overlays
2198 sp
<AaptAssets
> overlay
= assets
->getOverlay();
2199 if (overlay
.get()) {
2200 return writeProguardForLayouts(keep
, overlay
);
2206 writeProguardFile(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
2210 if (!bundle
->getProguardFile()) {
2214 ProguardKeepSet keep
;
2216 err
= writeProguardForAndroidManifest(&keep
, assets
);
2221 err
= writeProguardForLayouts(&keep
, assets
);
2226 FILE* fp
= fopen(bundle
->getProguardFile(), "w+");
2228 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
2229 bundle
->getProguardFile(), strerror(errno
));
2230 return UNKNOWN_ERROR
;
2233 const KeyedVector
<String8
, SortedVector
<String8
> >& rules
= keep
.rules
;
2234 const size_t N
= rules
.size();
2235 for (size_t i
=0; i
<N
; i
++) {
2236 const SortedVector
<String8
>& locations
= rules
.valueAt(i
);
2237 const size_t M
= locations
.size();
2238 for (size_t j
=0; j
<M
; j
++) {
2239 fprintf(fp
, "# %s\n", locations
.itemAt(j
).string());
2241 fprintf(fp
, "%s\n\n", rules
.keyAt(i
).string());