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
)
1728 fprintf(fp
, "%spublic %sfinal class %s {\n",
1729 getIndentSpace(indent
),
1730 indent
!= 0 ? "static " : "", className
.string());
1734 status_t err
= NO_ERROR
;
1736 size_t N
= symbols
->getSymbols().size();
1737 for (i
=0; i
<N
; i
++) {
1738 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1739 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1742 if (!includePrivate
&& !sym
.isPublic
) {
1745 String16
name(sym
.name
);
1746 String8
realName(name
);
1747 if (fixupSymbol(&name
) != NO_ERROR
) {
1748 return UNKNOWN_ERROR
;
1750 String16
comment(sym
.comment
);
1751 bool haveComment
= false;
1752 bool deprecated
= false;
1753 if (comment
.size() > 0) {
1755 String8
cmt(comment
);
1758 getIndentSpace(indent
), cmt
.string());
1759 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1762 } else if (sym
.isPublic
&& !includePrivate
) {
1763 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1764 assets
->getPackage().string(), className
.string(),
1765 String8(sym
.name
).string());
1767 String16
typeComment(sym
.typeComment
);
1768 if (typeComment
.size() > 0) {
1769 String8
cmt(typeComment
);
1773 "%s/** %s\n", getIndentSpace(indent
), cmt
.string());
1776 "%s %s\n", getIndentSpace(indent
), cmt
.string());
1778 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1783 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1786 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1788 fprintf(fp
, "%spublic static final int %s=0x%08x;\n",
1789 getIndentSpace(indent
),
1790 String8(name
).string(), (int)sym
.int32Val
);
1793 for (i
=0; i
<N
; i
++) {
1794 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1795 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1798 if (!includePrivate
&& !sym
.isPublic
) {
1801 String16
name(sym
.name
);
1802 if (fixupSymbol(&name
) != NO_ERROR
) {
1803 return UNKNOWN_ERROR
;
1805 String16
comment(sym
.comment
);
1806 bool deprecated
= false;
1807 if (comment
.size() > 0) {
1808 String8
cmt(comment
);
1812 getIndentSpace(indent
), cmt
.string(),
1813 getIndentSpace(indent
));
1814 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1817 } else if (sym
.isPublic
&& !includePrivate
) {
1818 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1819 assets
->getPackage().string(), className
.string(),
1820 String8(sym
.name
).string());
1823 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1825 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1826 getIndentSpace(indent
),
1827 String8(name
).string(), sym
.stringVal
.string());
1830 sp
<AaptSymbols
> styleableSymbols
;
1832 N
= symbols
->getNestedSymbols().size();
1833 for (i
=0; i
<N
; i
++) {
1834 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1835 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1836 if (nclassName
== "styleable") {
1837 styleableSymbols
= nsymbols
;
1839 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
);
1841 if (err
!= NO_ERROR
) {
1846 if (styleableSymbols
!= NULL
) {
1847 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1848 if (err
!= NO_ERROR
) {
1854 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1858 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1859 const String8
& package
, bool includePrivate
)
1861 if (!bundle
->getRClassDir()) {
1865 const size_t N
= assets
->getSymbols().size();
1866 for (size_t i
=0; i
<N
; i
++) {
1867 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1868 String8
className(assets
->getSymbols().keyAt(i
));
1869 String8
dest(bundle
->getRClassDir());
1870 if (bundle
->getMakePackageDirs()) {
1871 String8
pkg(package
);
1872 const char* last
= pkg
.string();
1873 const char* s
= last
-1;
1876 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1877 String8
part(last
, s
-last
);
1878 dest
.appendPath(part
);
1879 #ifdef HAVE_MS_C_RUNTIME
1880 _mkdir(dest
.string());
1882 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1888 dest
.appendPath(className
);
1889 dest
.append(".java");
1890 FILE* fp
= fopen(dest
.string(), "w+");
1892 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1893 dest
.string(), strerror(errno
));
1894 return UNKNOWN_ERROR
;
1896 if (bundle
->getVerbose()) {
1897 printf(" Writing symbols for class %s.\n", className
.string());
1901 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1903 " * This class was automatically generated by the\n"
1904 " * aapt tool from the resource data it found. It\n"
1905 " * should not be modified by hand.\n"
1908 "package %s;\n\n", package
.string());
1910 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0);
1911 if (err
!= NO_ERROR
) {
1922 class ProguardKeepSet
1925 // { rule --> { file locations } }
1926 KeyedVector
<String8
, SortedVector
<String8
> > rules
;
1928 void add(const String8
& rule
, const String8
& where
);
1931 void ProguardKeepSet::add(const String8
& rule
, const String8
& where
)
1933 ssize_t index
= rules
.indexOfKey(rule
);
1935 index
= rules
.add(rule
, SortedVector
<String8
>());
1937 rules
.editValueAt(index
).add(where
);
1941 addProguardKeepRule(ProguardKeepSet
* keep
, const String8
& inClassName
,
1942 const char* pkg
, const String8
& srcName
, int line
)
1944 String8
className(inClassName
);
1946 // asdf --> package.asdf
1947 // .asdf .a.b --> package.asdf package.a.b
1948 // asdf.adsf --> asdf.asdf
1949 const char* p
= className
.string();
1950 const char* q
= strchr(p
, '.');
1953 className
.append(inClassName
);
1954 } else if (q
== NULL
) {
1956 className
.append(".");
1957 className
.append(inClassName
);
1961 String8
rule("-keep class ");
1963 rule
+= " { <init>(...); }";
1965 String8
location("view ");
1966 location
+= srcName
;
1968 sprintf(lineno
, ":%d", line
);
1971 keep
->add(rule
, location
);
1975 writeProguardForAndroidManifest(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
1980 ResXMLTree::event_code_t code
;
1982 bool inApplication
= false;
1984 sp
<AaptGroup
> assGroup
;
1985 sp
<AaptFile
> assFile
;
1988 // First, look for a package file to parse. This is required to
1989 // be able to generate the resource information.
1990 assGroup
= assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
1991 if (assGroup
== NULL
) {
1992 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
1996 if (assGroup
->getFiles().size() != 1) {
1997 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
1998 assGroup
->getFiles().valueAt(0)->getPrintableSource().string());
2001 assFile
= assGroup
->getFiles().valueAt(0);
2003 err
= parseXMLResource(assFile
, &tree
);
2004 if (err
!= NO_ERROR
) {
2010 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2011 if (code
== ResXMLTree::END_TAG
) {
2012 if (/* name == "Application" && */ depth
== 2) {
2013 inApplication
= false;
2018 if (code
!= ResXMLTree::START_TAG
) {
2022 String8
tag(tree
.getElementName(&len
));
2023 // printf("Depth %d tag %s\n", depth, tag.string());
2024 bool keepTag
= false;
2026 if (tag
!= "manifest") {
2027 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
2030 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
2031 } else if (depth
== 2) {
2032 if (tag
== "application") {
2033 inApplication
= true;
2036 String8 agent
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
2037 "backupAgent", &error
);
2038 if (agent
.length() > 0) {
2039 addProguardKeepRule(keep
, agent
, pkg
.string(),
2040 assFile
->getPrintableSource(), tree
.getLineNumber());
2042 } else if (tag
== "instrumentation") {
2046 if (!keepTag
&& inApplication
&& depth
== 3) {
2047 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
2052 String8 name
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
2055 fprintf(stderr
, "ERROR: %s\n", error
.string());
2058 if (name
.length() > 0) {
2059 addProguardKeepRule(keep
, name
, pkg
.string(),
2060 assFile
->getPrintableSource(), tree
.getLineNumber());
2068 struct NamespaceAttributePair
{
2072 NamespaceAttributePair(const char* n
, const char* a
) : ns(n
), attr(a
) {}
2073 NamespaceAttributePair() : ns(NULL
), attr(NULL
) {}
2077 writeProguardForXml(ProguardKeepSet
* keep
, const sp
<AaptFile
>& layoutFile
,
2078 const char* startTag
, const KeyedVector
<String8
, NamespaceAttributePair
>* tagAttrPairs
)
2083 ResXMLTree::event_code_t code
;
2085 err
= parseXMLResource(layoutFile
, &tree
);
2086 if (err
!= NO_ERROR
) {
2092 if (startTag
!= NULL
) {
2093 bool haveStart
= false;
2094 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2095 if (code
!= ResXMLTree::START_TAG
) {
2098 String8
tag(tree
.getElementName(&len
));
2099 if (tag
== startTag
) {
2109 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2110 if (code
!= ResXMLTree::START_TAG
) {
2113 String8
tag(tree
.getElementName(&len
));
2115 // If there is no '.', we'll assume that it's one of the built in names.
2116 if (strchr(tag
.string(), '.')) {
2117 addProguardKeepRule(keep
, tag
, NULL
,
2118 layoutFile
->getPrintableSource(), tree
.getLineNumber());
2119 } else if (tagAttrPairs
!= NULL
) {
2120 ssize_t tagIndex
= tagAttrPairs
->indexOfKey(tag
);
2121 if (tagIndex
>= 0) {
2122 const NamespaceAttributePair
& nsAttr
= tagAttrPairs
->valueAt(tagIndex
);
2123 ssize_t attrIndex
= tree
.indexOfAttribute(nsAttr
.ns
, nsAttr
.attr
);
2124 if (attrIndex
< 0) {
2125 // fprintf(stderr, "%s:%d: <%s> does not have attribute %s:%s.\n",
2126 // layoutFile->getPrintableSource().string(), tree.getLineNumber(),
2127 // tag.string(), nsAttr.ns, nsAttr.attr);
2130 addProguardKeepRule(keep
,
2131 String8(tree
.getAttributeStringValue(attrIndex
, &len
)), NULL
,
2132 layoutFile
->getPrintableSource(), tree
.getLineNumber());
2141 static void addTagAttrPair(KeyedVector
<String8
, NamespaceAttributePair
>* dest
,
2142 const char* tag
, const char* ns
, const char* attr
) {
2143 dest
->add(String8(tag
), NamespaceAttributePair(ns
, attr
));
2147 writeProguardForLayouts(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
2151 // tag:attribute pairs that should be checked in layout files.
2152 KeyedVector
<String8
, NamespaceAttributePair
> kLayoutTagAttrPairs
;
2153 addTagAttrPair(&kLayoutTagAttrPairs
, "view", NULL
, "class");
2154 addTagAttrPair(&kLayoutTagAttrPairs
, "fragment", NULL
, "class");
2155 addTagAttrPair(&kLayoutTagAttrPairs
, "fragment", RESOURCES_ANDROID_NAMESPACE
, "name");
2157 // tag:attribute pairs that should be checked in xml files.
2158 KeyedVector
<String8
, NamespaceAttributePair
> kXmlTagAttrPairs
;
2159 addTagAttrPair(&kXmlTagAttrPairs
, "PreferenceScreen", RESOURCES_ANDROID_NAMESPACE
, "fragment");
2160 addTagAttrPair(&kXmlTagAttrPairs
, "header", RESOURCES_ANDROID_NAMESPACE
, "fragment");
2162 const Vector
<sp
<AaptDir
> >& dirs
= assets
->resDirs();
2163 const size_t K
= dirs
.size();
2164 for (size_t k
=0; k
<K
; k
++) {
2165 const sp
<AaptDir
>& d
= dirs
.itemAt(k
);
2166 const String8
& dirName
= d
->getLeaf();
2167 const char* startTag
= NULL
;
2168 const KeyedVector
<String8
, NamespaceAttributePair
>* tagAttrPairs
= NULL
;
2169 if ((dirName
== String8("layout")) || (strncmp(dirName
.string(), "layout-", 7) == 0)) {
2170 tagAttrPairs
= &kLayoutTagAttrPairs
;
2171 } else if ((dirName
== String8("xml")) || (strncmp(dirName
.string(), "xml-", 4) == 0)) {
2172 startTag
= "PreferenceScreen";
2173 tagAttrPairs
= &kXmlTagAttrPairs
;
2178 const KeyedVector
<String8
,sp
<AaptGroup
> > groups
= d
->getFiles();
2179 const size_t N
= groups
.size();
2180 for (size_t i
=0; i
<N
; i
++) {
2181 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
2182 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
= group
->getFiles();
2183 const size_t M
= files
.size();
2184 for (size_t j
=0; j
<M
; j
++) {
2185 err
= writeProguardForXml(keep
, files
.valueAt(j
), startTag
, tagAttrPairs
);
2196 writeProguardFile(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
2200 if (!bundle
->getProguardFile()) {
2204 ProguardKeepSet keep
;
2206 err
= writeProguardForAndroidManifest(&keep
, assets
);
2211 err
= writeProguardForLayouts(&keep
, assets
);
2216 FILE* fp
= fopen(bundle
->getProguardFile(), "w+");
2218 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
2219 bundle
->getProguardFile(), strerror(errno
));
2220 return UNKNOWN_ERROR
;
2223 const KeyedVector
<String8
, SortedVector
<String8
> >& rules
= keep
.rules
;
2224 const size_t N
= rules
.size();
2225 for (size_t i
=0; i
<N
; i
++) {
2226 const SortedVector
<String8
>& locations
= rules
.valueAt(i
);
2227 const size_t M
= locations
.size();
2228 for (size_t j
=0; j
<M
; j
++) {
2229 fprintf(fp
, "# %s\n", locations
.itemAt(j
).string());
2231 fprintf(fp
, "%s\n\n", rules
.keyAt(i
).string());