2 // Copyright 2006 The Android Open Source Project
4 // Build resource files from raw assets.
7 #include "AaptAssets.h"
8 #include "StringPool.h"
10 #include "ResourceTable.h"
13 #include "CrunchCache.h"
14 #include "FileFinder.h"
15 #include "CacheUpdater.h"
19 // ==========================================================================
20 // ==========================================================================
21 // ==========================================================================
33 status_t
parsePackage(const sp
<AaptGroup
>& grp
);
36 // ==========================================================================
37 // ==========================================================================
38 // ==========================================================================
40 static String8
parseResourceName(const String8
& leaf
)
42 const char* firstDot
= strchr(leaf
.string(), '.');
43 const char* str
= leaf
.string();
46 return String8(str
, firstDot
-str
);
52 ResourceTypeSet::ResourceTypeSet()
54 KeyedVector
<String8
,sp
<AaptGroup
> >()
58 FilePathStore::FilePathStore()
64 class ResourceDirIterator
67 ResourceDirIterator(const sp
<ResourceTypeSet
>& set
, const String8
& resType
)
68 : mResType(resType
), mSet(set
), mSetPos(0), mGroupPos(0)
72 inline const sp
<AaptGroup
>& getGroup() const { return mGroup
; }
73 inline const sp
<AaptFile
>& getFile() const { return mFile
; }
75 inline const String8
& getBaseName() const { return mBaseName
; }
76 inline const String8
& getLeafName() const { return mLeafName
; }
77 inline String8
getPath() const { return mPath
; }
78 inline const ResTable_config
& getParams() const { return mParams
; }
90 // Try to get next file in this current group.
91 if (mGroup
!= NULL
&& mGroupPos
< mGroup
->getFiles().size()) {
93 file
= group
->getFiles().valueAt(mGroupPos
++);
95 // Try to get the next group/file in this directory
96 } else if (mSetPos
< mSet
->size()) {
97 mGroup
= group
= mSet
->valueAt(mSetPos
++);
98 if (group
->getFiles().size() < 1) {
101 file
= group
->getFiles().valueAt(0);
111 String8
leaf(group
->getLeaf());
112 mLeafName
= String8(leaf
);
113 mParams
= file
->getGroupEntry().toParams();
114 NOISY(printf("Dir %s: mcc=%d mnc=%d lang=%c%c cnt=%c%c orient=%d ui=%d density=%d touch=%d key=%d inp=%d nav=%d\n",
115 group
->getPath().string(), mParams
.mcc
, mParams
.mnc
,
116 mParams
.language
[0] ? mParams
.language
[0] : '-',
117 mParams
.language
[1] ? mParams
.language
[1] : '-',
118 mParams
.country
[0] ? mParams
.country
[0] : '-',
119 mParams
.country
[1] ? mParams
.country
[1] : '-',
120 mParams
.orientation
, mParams
.uiMode
,
121 mParams
.density
, mParams
.touchscreen
, mParams
.keyboard
,
122 mParams
.inputFlags
, mParams
.navigation
));
124 mPath
.appendPath(file
->getGroupEntry().toDirName(mResType
));
125 mPath
.appendPath(leaf
);
126 mBaseName
= parseResourceName(leaf
);
127 if (mBaseName
== "") {
128 fprintf(stderr
, "Error: malformed resource filename %s\n",
129 file
->getPrintableSource().string());
130 return UNKNOWN_ERROR
;
133 NOISY(printf("file name=%s\n", mBaseName
.string()));
142 const sp
<ResourceTypeSet
> mSet
;
145 sp
<AaptGroup
> mGroup
;
152 ResTable_config mParams
;
155 // ==========================================================================
156 // ==========================================================================
157 // ==========================================================================
159 bool isValidResourceType(const String8
& type
)
161 return type
== "anim" || type
== "animator" || type
== "interpolator"
162 || type
== "drawable" || type
== "layout"
163 || type
== "values" || type
== "xml" || type
== "raw"
164 || type
== "color" || type
== "menu" || type
== "mipmap";
167 static sp
<AaptFile
> getResourceFile(const sp
<AaptAssets
>& assets
, bool makeIfNecessary
=true)
169 sp
<AaptGroup
> group
= assets
->getFiles().valueFor(String8("resources.arsc"));
172 file
= group
->getFiles().valueFor(AaptGroupEntry());
178 if (!makeIfNecessary
) {
181 return assets
->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(),
185 static status_t
parsePackage(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
186 const sp
<AaptGroup
>& grp
)
188 if (grp
->getFiles().size() != 1) {
189 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
190 grp
->getFiles().valueAt(0)->getPrintableSource().string());
193 sp
<AaptFile
> file
= grp
->getFiles().valueAt(0);
196 status_t err
= parseXMLResource(file
, &block
);
197 if (err
!= NO_ERROR
) {
200 //printXMLBlock(&block);
202 ResXMLTree::event_code_t code
;
203 while ((code
=block
.next()) != ResXMLTree::START_TAG
204 && code
!= ResXMLTree::END_DOCUMENT
205 && code
!= ResXMLTree::BAD_DOCUMENT
) {
209 if (code
!= ResXMLTree::START_TAG
) {
210 fprintf(stderr
, "%s:%d: No start tag found\n",
211 file
->getPrintableSource().string(), block
.getLineNumber());
212 return UNKNOWN_ERROR
;
214 if (strcmp16(block
.getElementName(&len
), String16("manifest").string()) != 0) {
215 fprintf(stderr
, "%s:%d: Invalid start tag %s, expected <manifest>\n",
216 file
->getPrintableSource().string(), block
.getLineNumber(),
217 String8(block
.getElementName(&len
)).string());
218 return UNKNOWN_ERROR
;
221 ssize_t nameIndex
= block
.indexOfAttribute(NULL
, "package");
223 fprintf(stderr
, "%s:%d: <manifest> does not have package attribute.\n",
224 file
->getPrintableSource().string(), block
.getLineNumber());
225 return UNKNOWN_ERROR
;
228 assets
->setPackage(String8(block
.getAttributeStringValue(nameIndex
, &len
)));
230 String16
uses_sdk16("uses-sdk");
231 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
232 && code
!= ResXMLTree::BAD_DOCUMENT
) {
233 if (code
== ResXMLTree::START_TAG
) {
234 if (strcmp16(block
.getElementName(&len
), uses_sdk16
.string()) == 0) {
235 ssize_t minSdkIndex
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
,
237 if (minSdkIndex
>= 0) {
238 const uint16_t* minSdk16
= block
.getAttributeStringValue(minSdkIndex
, &len
);
239 const char* minSdk8
= strdup(String8(minSdk16
).string());
240 bundle
->setManifestMinSdkVersion(minSdk8
);
249 // ==========================================================================
250 // ==========================================================================
251 // ==========================================================================
253 static status_t
makeFileResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
254 ResourceTable
* table
,
255 const sp
<ResourceTypeSet
>& set
,
258 String8
type8(resType
);
259 String16
type16(resType
);
261 bool hasErrors
= false;
263 ResourceDirIterator
it(set
, String8(resType
));
265 while ((res
=it
.next()) == NO_ERROR
) {
266 if (bundle
->getVerbose()) {
267 printf(" (new resource id %s from %s)\n",
268 it
.getBaseName().string(), it
.getFile()->getPrintableSource().string());
270 String16
baseName(it
.getBaseName());
271 const char16_t* str
= baseName
.string();
272 const char16_t* const end
= str
+ baseName
.size();
274 if (!((*str
>= 'a' && *str
<= 'z')
275 || (*str
>= '0' && *str
<= '9')
276 || *str
== '_' || *str
== '.')) {
277 fprintf(stderr
, "%s: Invalid file name: must contain only [a-z0-9_.]\n",
278 it
.getPath().string());
283 String8 resPath
= it
.getPath();
284 resPath
.convertToResPath();
285 table
->addEntry(SourcePos(it
.getPath(), 0), String16(assets
->getPackage()),
291 assets
->addResource(it
.getLeafName(), resPath
, it
.getFile(), type8
);
294 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
297 static status_t
preProcessImages(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
298 const sp
<ResourceTypeSet
>& set
, const char* type
)
300 bool hasErrors
= false;
301 ssize_t res
= NO_ERROR
;
302 if (bundle
->getUseCrunchCache() == false) {
303 ResourceDirIterator
it(set
, String8(type
));
304 Vector
<sp
<AaptFile
> > newNameFiles
;
305 Vector
<String8
> newNamePaths
;
306 while ((res
=it
.next()) == NO_ERROR
) {
307 res
= preProcessImage(bundle
, assets
, it
.getFile(), NULL
);
308 if (res
< NO_ERROR
) {
313 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
316 status_t
postProcessImages(const sp
<AaptAssets
>& assets
,
317 ResourceTable
* table
,
318 const sp
<ResourceTypeSet
>& set
)
320 ResourceDirIterator
it(set
, String8("drawable"));
321 bool hasErrors
= false;
323 while ((res
=it
.next()) == NO_ERROR
) {
324 res
= postProcessImage(assets
, table
, it
.getFile());
325 if (res
< NO_ERROR
) {
330 return (hasErrors
|| (res
< NO_ERROR
)) ? UNKNOWN_ERROR
: NO_ERROR
;
333 static void collect_files(const sp
<AaptDir
>& dir
,
334 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
336 const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& groups
= dir
->getFiles();
337 int N
= groups
.size();
338 for (int i
=0; i
<N
; i
++) {
339 String8 leafName
= groups
.keyAt(i
);
340 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
342 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
345 if (files
.size() == 0) {
349 String8 resType
= files
.valueAt(0)->getResourceType();
351 ssize_t index
= resources
->indexOfKey(resType
);
354 sp
<ResourceTypeSet
> set
= new ResourceTypeSet();
355 set
->add(leafName
, group
);
356 resources
->add(resType
, set
);
358 sp
<ResourceTypeSet
> set
= resources
->valueAt(index
);
359 index
= set
->indexOfKey(leafName
);
361 set
->add(leafName
, group
);
363 sp
<AaptGroup
> existingGroup
= set
->valueAt(index
);
364 int M
= files
.size();
365 for (int j
=0; j
<M
; j
++) {
366 existingGroup
->addFile(files
.valueAt(j
));
373 static void collect_files(const sp
<AaptAssets
>& ass
,
374 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
)
376 const Vector
<sp
<AaptDir
> >& dirs
= ass
->resDirs();
379 for (int i
=0; i
<N
; i
++) {
380 sp
<AaptDir
> d
= dirs
.itemAt(i
);
381 collect_files(d
, resources
);
383 // don't try to include the res dir
384 ass
->removeDir(d
->getLeaf());
391 ATTR_LEADING_SPACES
= -3,
392 ATTR_TRAILING_SPACES
= -4
394 static int validateAttr(const String8
& path
, const ResTable
& table
,
395 const ResXMLParser
& parser
,
396 const char* ns
, const char* attr
, const char* validChars
, bool required
)
400 ssize_t index
= parser
.indexOfAttribute(ns
, attr
);
403 if (index
>= 0 && parser
.getAttributeValue(index
, &value
) >= 0) {
404 const ResStringPool
* pool
= &parser
.getStrings();
405 if (value
.dataType
== Res_value::TYPE_REFERENCE
) {
406 uint32_t specFlags
= 0;
408 if ((strIdx
=table
.resolveReference(&value
, 0x10000000, NULL
, &specFlags
)) < 0) {
409 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s references unknown resid 0x%08x.\n",
410 path
.string(), parser
.getLineNumber(),
411 String8(parser
.getElementName(&len
)).string(), attr
,
413 return ATTR_NOT_FOUND
;
416 pool
= table
.getTableStringBlock(strIdx
);
419 str
= pool
->stringAt(value
.data
, &len
);
421 printf("***** RES ATTR: %s specFlags=0x%x strIdx=%d: %s\n", attr
,
422 specFlags
, strIdx
, str
!= NULL
? String8(str
).string() : "???");
424 if ((specFlags
&~ResTable_typeSpec::SPEC_PUBLIC
) != 0 && false) {
425 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s varies by configurations 0x%x.\n",
426 path
.string(), parser
.getLineNumber(),
427 String8(parser
.getElementName(&len
)).string(), attr
,
429 return ATTR_NOT_FOUND
;
432 if (value
.dataType
== Res_value::TYPE_STRING
) {
434 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has no string block.\n",
435 path
.string(), parser
.getLineNumber(),
436 String8(parser
.getElementName(&len
)).string(), attr
);
437 return ATTR_NOT_FOUND
;
439 if ((str
=pool
->stringAt(value
.data
, &len
)) == NULL
) {
440 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has corrupt string value.\n",
441 path
.string(), parser
.getLineNumber(),
442 String8(parser
.getElementName(&len
)).string(), attr
);
443 return ATTR_NOT_FOUND
;
446 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid type %d.\n",
447 path
.string(), parser
.getLineNumber(),
448 String8(parser
.getElementName(&len
)).string(), attr
,
450 return ATTR_NOT_FOUND
;
453 for (size_t i
=0; i
<len
; i
++) {
455 const char* p
= validChars
;
465 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n",
466 path
.string(), parser
.getLineNumber(),
467 String8(parser
.getElementName(&len
)).string(), attr
, (char)str
[i
]);
473 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not start with a space.\n",
474 path
.string(), parser
.getLineNumber(),
475 String8(parser
.getElementName(&len
)).string(), attr
);
476 return ATTR_LEADING_SPACES
;
478 if (str
[len
-1] == ' ') {
479 fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not end with a space.\n",
480 path
.string(), parser
.getLineNumber(),
481 String8(parser
.getElementName(&len
)).string(), attr
);
482 return ATTR_TRAILING_SPACES
;
487 fprintf(stderr
, "%s:%d: Tag <%s> missing required attribute %s.\n",
488 path
.string(), parser
.getLineNumber(),
489 String8(parser
.getElementName(&len
)).string(), attr
);
490 return ATTR_NOT_FOUND
;
495 static void checkForIds(const String8
& path
, ResXMLParser
& parser
)
497 ResXMLTree::event_code_t code
;
498 while ((code
=parser
.next()) != ResXMLTree::END_DOCUMENT
499 && code
> ResXMLTree::BAD_DOCUMENT
) {
500 if (code
== ResXMLTree::START_TAG
) {
501 ssize_t index
= parser
.indexOfAttribute(NULL
, "id");
503 fprintf(stderr
, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n",
504 path
.string(), parser
.getLineNumber());
510 static bool applyFileOverlay(Bundle
*bundle
,
511 const sp
<AaptAssets
>& assets
,
512 sp
<ResourceTypeSet
> *baseSet
,
515 if (bundle
->getVerbose()) {
516 printf("applyFileOverlay for %s\n", resType
);
519 // Replace any base level files in this category with any found from the overlay
520 // Also add any found only in the overlay.
521 sp
<AaptAssets
> overlay
= assets
->getOverlay();
522 String8
resTypeString(resType
);
524 // work through the linked list of overlays
525 while (overlay
.get()) {
526 KeyedVector
<String8
, sp
<ResourceTypeSet
> >* overlayRes
= overlay
->getResources();
528 // get the overlay resources of the requested type
529 ssize_t index
= overlayRes
->indexOfKey(resTypeString
);
531 sp
<ResourceTypeSet
> overlaySet
= overlayRes
->valueAt(index
);
533 // for each of the resources, check for a match in the previously built
534 // non-overlay "baseset".
535 size_t overlayCount
= overlaySet
->size();
536 for (size_t overlayIndex
=0; overlayIndex
<overlayCount
; overlayIndex
++) {
537 if (bundle
->getVerbose()) {
538 printf("trying overlaySet Key=%s\n",overlaySet
->keyAt(overlayIndex
).string());
540 size_t baseIndex
= UNKNOWN_ERROR
;
541 if (baseSet
->get() != NULL
) {
542 baseIndex
= (*baseSet
)->indexOfKey(overlaySet
->keyAt(overlayIndex
));
544 if (baseIndex
< UNKNOWN_ERROR
) {
545 // look for same flavor. For a given file (strings.xml, for example)
546 // there may be a locale specific or other flavors - we want to match
548 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
549 sp
<AaptGroup
> baseGroup
= (*baseSet
)->valueAt(baseIndex
);
551 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
552 overlayGroup
->getFiles();
553 if (bundle
->getVerbose()) {
554 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > baseFiles
=
555 baseGroup
->getFiles();
556 for (size_t i
=0; i
< baseFiles
.size(); i
++) {
557 printf("baseFile %zd has flavor %s\n", i
,
558 baseFiles
.keyAt(i
).toString().string());
560 for (size_t i
=0; i
< overlayFiles
.size(); i
++) {
561 printf("overlayFile %zd has flavor %s\n", i
,
562 overlayFiles
.keyAt(i
).toString().string());
566 size_t overlayGroupSize
= overlayFiles
.size();
567 for (size_t overlayGroupIndex
= 0;
568 overlayGroupIndex
<overlayGroupSize
;
569 overlayGroupIndex
++) {
570 size_t baseFileIndex
=
571 baseGroup
->getFiles().indexOfKey(overlayFiles
.
572 keyAt(overlayGroupIndex
));
573 if(baseFileIndex
< UNKNOWN_ERROR
) {
574 if (bundle
->getVerbose()) {
575 printf("found a match (%zd) for overlay file %s, for flavor %s\n",
577 overlayGroup
->getLeaf().string(),
578 overlayFiles
.keyAt(overlayGroupIndex
).toString().string());
580 baseGroup
->removeFile(baseFileIndex
);
582 // didn't find a match fall through and add it..
584 baseGroup
->addFile(overlayFiles
.valueAt(overlayGroupIndex
));
585 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
588 if (baseSet
->get() == NULL
) {
589 *baseSet
= new ResourceTypeSet();
590 assets
->getResources()->add(String8(resType
), *baseSet
);
592 // this group doesn't exist (a file that's only in the overlay)
593 (*baseSet
)->add(overlaySet
->keyAt(overlayIndex
),
594 overlaySet
->valueAt(overlayIndex
));
595 // make sure all flavors are defined in the resources.
596 sp
<AaptGroup
> overlayGroup
= overlaySet
->valueAt(overlayIndex
);
597 DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles
=
598 overlayGroup
->getFiles();
599 size_t overlayGroupSize
= overlayFiles
.size();
600 for (size_t overlayGroupIndex
= 0;
601 overlayGroupIndex
<overlayGroupSize
;
602 overlayGroupIndex
++) {
603 assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
));
607 // this overlay didn't have resources for this type
610 overlay
= overlay
->getOverlay();
615 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
,
616 const char* attr8
, const char* value
)
622 const String16
ns(ns8
);
623 const String16
attr(attr8
);
625 if (node
->getAttribute(ns
, attr
) != NULL
) {
626 fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s);"
627 " using existing value in manifest.\n",
628 String8(attr
).string(), String8(ns
).string());
632 node
->addAttribute(ns
, attr
, String16(value
));
635 static void fullyQualifyClassName(const String8
& package
, sp
<XMLNode
> node
,
636 const String16
& attrName
) {
637 XMLNode::attribute_entry
* attr
= node
->editAttribute(
638 String16("http://schemas.android.com/apk/res/android"), attrName
);
640 String8
name(attr
->string
);
642 // asdf --> package.asdf
643 // .asdf .a.b --> package.asdf package.a.b
644 // asdf.adsf --> asdf.asdf
646 const char* p
= name
.string();
647 const char* q
= strchr(p
, '.');
649 className
+= package
;
651 } else if (q
== NULL
) {
652 className
+= package
;
658 NOISY(printf("Qualifying class '%s' to '%s'", name
.string(), className
.string()));
659 attr
->string
.setTo(String16(className
));
663 status_t
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
)
665 root
= root
->searchElement(String16(), String16("manifest"));
667 fprintf(stderr
, "No <manifest> tag.\n");
668 return UNKNOWN_ERROR
;
671 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode",
672 bundle
->getVersionCode());
673 addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName",
674 bundle
->getVersionName());
676 if (bundle
->getMinSdkVersion() != NULL
677 || bundle
->getTargetSdkVersion() != NULL
678 || bundle
->getMaxSdkVersion() != NULL
) {
679 sp
<XMLNode
> vers
= root
->getChildElement(String16(), String16("uses-sdk"));
681 vers
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk"));
682 root
->insertChildAt(vers
, 0);
685 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion",
686 bundle
->getMinSdkVersion());
687 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion",
688 bundle
->getTargetSdkVersion());
689 addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion",
690 bundle
->getMaxSdkVersion());
693 if (bundle
->getDebugMode()) {
694 sp
<XMLNode
> application
= root
->getChildElement(String16(), String16("application"));
695 if (application
!= NULL
) {
696 addTagAttribute(application
, RESOURCES_ANDROID_NAMESPACE
, "debuggable", "true");
700 // Deal with manifest package name overrides
701 const char* manifestPackageNameOverride
= bundle
->getManifestPackageNameOverride();
702 if (manifestPackageNameOverride
!= NULL
) {
703 // Update the actual package name
704 XMLNode::attribute_entry
* attr
= root
->editAttribute(String16(), String16("package"));
706 fprintf(stderr
, "package name is required with --rename-manifest-package.\n");
707 return UNKNOWN_ERROR
;
709 String8
origPackage(attr
->string
);
710 attr
->string
.setTo(String16(manifestPackageNameOverride
));
711 NOISY(printf("Overriding package '%s' to be '%s'\n", origPackage
.string(), manifestPackageNameOverride
));
713 // Make class names fully qualified
714 sp
<XMLNode
> application
= root
->getChildElement(String16(), String16("application"));
715 if (application
!= NULL
) {
716 fullyQualifyClassName(origPackage
, application
, String16("name"));
717 fullyQualifyClassName(origPackage
, application
, String16("backupAgent"));
719 Vector
<sp
<XMLNode
> >& children
= const_cast<Vector
<sp
<XMLNode
> >&>(application
->getChildren());
720 for (size_t i
= 0; i
< children
.size(); i
++) {
721 sp
<XMLNode
> child
= children
.editItemAt(i
);
722 String8
tag(child
->getElementName());
723 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
724 fullyQualifyClassName(origPackage
, child
, String16("name"));
725 } else if (tag
== "activity-alias") {
726 fullyQualifyClassName(origPackage
, child
, String16("name"));
727 fullyQualifyClassName(origPackage
, child
, String16("targetActivity"));
733 // Deal with manifest package name overrides
734 const char* instrumentationPackageNameOverride
= bundle
->getInstrumentationPackageNameOverride();
735 if (instrumentationPackageNameOverride
!= NULL
) {
736 // Fix up instrumentation targets.
737 Vector
<sp
<XMLNode
> >& children
= const_cast<Vector
<sp
<XMLNode
> >&>(root
->getChildren());
738 for (size_t i
= 0; i
< children
.size(); i
++) {
739 sp
<XMLNode
> child
= children
.editItemAt(i
);
740 String8
tag(child
->getElementName());
741 if (tag
== "instrumentation") {
742 XMLNode::attribute_entry
* attr
= child
->editAttribute(
743 String16("http://schemas.android.com/apk/res/android"), String16("targetPackage"));
745 attr
->string
.setTo(String16(instrumentationPackageNameOverride
));
754 #define ASSIGN_IT(n) \
756 ssize_t index = resources->indexOfKey(String8(#n)); \
758 n ## s = resources->valueAt(index); \
762 status_t
updatePreProcessedCache(Bundle
* bundle
)
765 fprintf(stdout
, "BENCHMARK: Starting PNG PreProcessing \n");
766 long startPNGTime
= clock();
767 #endif /* BENCHMARK */
769 String8
source(bundle
->getResourceSourceDirs()[0]);
770 String8
dest(bundle
->getCrunchedOutputDir());
772 FileFinder
* ff
= new SystemFileFinder();
773 CrunchCache
cc(source
,dest
,ff
);
775 CacheUpdater
* cu
= new SystemCacheUpdater(bundle
);
776 size_t numFiles
= cc
.crunch(cu
);
778 if (bundle
->getVerbose())
779 fprintf(stdout
, "Crunched %d PNG files to update cache\n", (int)numFiles
);
785 fprintf(stdout
, "BENCHMARK: End PNG PreProcessing. Time Elapsed: %f ms \n"
786 ,(clock() - startPNGTime
)/1000.0);
787 #endif /* BENCHMARK */
791 status_t
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
793 // First, look for a package file to parse. This is required to
794 // be able to generate the resource information.
795 sp
<AaptGroup
> androidManifestFile
=
796 assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
797 if (androidManifestFile
== NULL
) {
798 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
799 return UNKNOWN_ERROR
;
802 status_t err
= parsePackage(bundle
, assets
, androidManifestFile
);
803 if (err
!= NO_ERROR
) {
807 NOISY(printf("Creating resources for package %s\n",
808 assets
->getPackage().string()));
810 ResourceTable
table(bundle
, String16(assets
->getPackage()));
811 err
= table
.addIncludedResources(bundle
, assets
);
812 if (err
!= NO_ERROR
) {
816 NOISY(printf("Found %d included resource packages\n", (int)table
.size()));
818 // Standard flags for compiled XML and optional UTF-8 encoding
819 int xmlFlags
= XML_COMPILE_STANDARD_RESOURCE
;
821 /* Only enable UTF-8 if the caller of aapt didn't specifically
822 * request UTF-16 encoding and the parameters of this package
823 * allow UTF-8 to be used.
825 if (!bundle
->getWantUTF16()
826 && bundle
->isMinSdkAtLeast(SDK_FROYO
)) {
827 xmlFlags
|= XML_COMPILE_UTF8
;
830 // --------------------------------------------------------------
831 // First, gather all resource information.
832 // --------------------------------------------------------------
834 // resType -> leafName -> group
835 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
836 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
837 collect_files(assets
, resources
);
839 sp
<ResourceTypeSet
> drawables
;
840 sp
<ResourceTypeSet
> layouts
;
841 sp
<ResourceTypeSet
> anims
;
842 sp
<ResourceTypeSet
> animators
;
843 sp
<ResourceTypeSet
> interpolators
;
844 sp
<ResourceTypeSet
> xmls
;
845 sp
<ResourceTypeSet
> raws
;
846 sp
<ResourceTypeSet
> colors
;
847 sp
<ResourceTypeSet
> menus
;
848 sp
<ResourceTypeSet
> mipmaps
;
854 ASSIGN_IT(interpolator
);
861 assets
->setResources(resources
);
862 // now go through any resource overlays and collect their files
863 sp
<AaptAssets
> current
= assets
->getOverlay();
864 while(current
.get()) {
865 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
866 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >;
867 current
->setResources(resources
);
868 collect_files(current
, resources
);
869 current
= current
->getOverlay();
871 // apply the overlay files to the base set
872 if (!applyFileOverlay(bundle
, assets
, &drawables
, "drawable") ||
873 !applyFileOverlay(bundle
, assets
, &layouts
, "layout") ||
874 !applyFileOverlay(bundle
, assets
, &anims
, "anim") ||
875 !applyFileOverlay(bundle
, assets
, &animators
, "animator") ||
876 !applyFileOverlay(bundle
, assets
, &interpolators
, "interpolator") ||
877 !applyFileOverlay(bundle
, assets
, &xmls
, "xml") ||
878 !applyFileOverlay(bundle
, assets
, &raws
, "raw") ||
879 !applyFileOverlay(bundle
, assets
, &colors
, "color") ||
880 !applyFileOverlay(bundle
, assets
, &menus
, "menu") ||
881 !applyFileOverlay(bundle
, assets
, &mipmaps
, "mipmap")) {
882 return UNKNOWN_ERROR
;
885 bool hasErrors
= false;
887 if (drawables
!= NULL
) {
888 if (bundle
->getOutputAPKFile() != NULL
) {
889 err
= preProcessImages(bundle
, assets
, drawables
, "drawable");
891 if (err
== NO_ERROR
) {
892 err
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable");
893 if (err
!= NO_ERROR
) {
901 if (mipmaps
!= NULL
) {
902 if (bundle
->getOutputAPKFile() != NULL
) {
903 err
= preProcessImages(bundle
, assets
, mipmaps
, "mipmap");
905 if (err
== NO_ERROR
) {
906 err
= makeFileResources(bundle
, assets
, &table
, mipmaps
, "mipmap");
907 if (err
!= NO_ERROR
) {
915 if (layouts
!= NULL
) {
916 err
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout");
917 if (err
!= NO_ERROR
) {
923 err
= makeFileResources(bundle
, assets
, &table
, anims
, "anim");
924 if (err
!= NO_ERROR
) {
929 if (animators
!= NULL
) {
930 err
= makeFileResources(bundle
, assets
, &table
, animators
, "animator");
931 if (err
!= NO_ERROR
) {
936 if (interpolators
!= NULL
) {
937 err
= makeFileResources(bundle
, assets
, &table
, interpolators
, "interpolator");
938 if (err
!= NO_ERROR
) {
944 err
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml");
945 if (err
!= NO_ERROR
) {
951 err
= makeFileResources(bundle
, assets
, &table
, raws
, "raw");
952 if (err
!= NO_ERROR
) {
959 while(current
.get()) {
960 KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources
=
961 current
->getResources();
963 ssize_t index
= resources
->indexOfKey(String8("values"));
965 ResourceDirIterator
it(resources
->valueAt(index
), String8("values"));
967 while ((res
=it
.next()) == NO_ERROR
) {
968 sp
<AaptFile
> file
= it
.getFile();
969 res
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),
970 (current
!=assets
), &table
);
971 if (res
!= NO_ERROR
) {
976 current
= current
->getOverlay();
979 if (colors
!= NULL
) {
980 err
= makeFileResources(bundle
, assets
, &table
, colors
, "color");
981 if (err
!= NO_ERROR
) {
987 err
= makeFileResources(bundle
, assets
, &table
, menus
, "menu");
988 if (err
!= NO_ERROR
) {
993 // --------------------------------------------------------------------
994 // Assignment of resource IDs and initial generation of resource table.
995 // --------------------------------------------------------------------
997 if (table
.hasResources()) {
998 sp
<AaptFile
> resFile(getResourceFile(assets
));
999 if (resFile
== NULL
) {
1000 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1001 return UNKNOWN_ERROR
;
1004 err
= table
.assignResourceIds();
1005 if (err
< NO_ERROR
) {
1010 // --------------------------------------------------------------
1011 // Finally, we can now we can compile XML files, which may reference
1013 // --------------------------------------------------------------
1015 if (layouts
!= NULL
) {
1016 ResourceDirIterator
it(layouts
, String8("layout"));
1017 while ((err
=it
.next()) == NO_ERROR
) {
1018 String8 src
= it
.getFile()->getPrintableSource();
1019 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1020 if (err
== NO_ERROR
) {
1022 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
1023 checkForIds(src
, block
);
1029 if (err
< NO_ERROR
) {
1035 if (anims
!= NULL
) {
1036 ResourceDirIterator
it(anims
, String8("anim"));
1037 while ((err
=it
.next()) == NO_ERROR
) {
1038 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1039 if (err
!= NO_ERROR
) {
1044 if (err
< NO_ERROR
) {
1050 if (animators
!= NULL
) {
1051 ResourceDirIterator
it(animators
, String8("animator"));
1052 while ((err
=it
.next()) == NO_ERROR
) {
1053 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1054 if (err
!= NO_ERROR
) {
1059 if (err
< NO_ERROR
) {
1065 if (interpolators
!= NULL
) {
1066 ResourceDirIterator
it(interpolators
, String8("interpolator"));
1067 while ((err
=it
.next()) == NO_ERROR
) {
1068 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1069 if (err
!= NO_ERROR
) {
1074 if (err
< NO_ERROR
) {
1081 ResourceDirIterator
it(xmls
, String8("xml"));
1082 while ((err
=it
.next()) == NO_ERROR
) {
1083 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1084 if (err
!= NO_ERROR
) {
1089 if (err
< NO_ERROR
) {
1095 if (drawables
!= NULL
) {
1096 err
= postProcessImages(assets
, &table
, drawables
);
1097 if (err
!= NO_ERROR
) {
1102 if (colors
!= NULL
) {
1103 ResourceDirIterator
it(colors
, String8("color"));
1104 while ((err
=it
.next()) == NO_ERROR
) {
1105 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1106 if (err
!= NO_ERROR
) {
1111 if (err
< NO_ERROR
) {
1117 if (menus
!= NULL
) {
1118 ResourceDirIterator
it(menus
, String8("menu"));
1119 while ((err
=it
.next()) == NO_ERROR
) {
1120 String8 src
= it
.getFile()->getPrintableSource();
1121 err
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
);
1122 if (err
!= NO_ERROR
) {
1126 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true);
1127 checkForIds(src
, block
);
1130 if (err
< NO_ERROR
) {
1136 if (table
.validateLocalizations()) {
1141 return UNKNOWN_ERROR
;
1144 const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0));
1145 String8
manifestPath(manifestFile
->getPrintableSource());
1147 // Generate final compiled manifest file.
1148 manifestFile
->clearData();
1149 sp
<XMLNode
> manifestTree
= XMLNode::parse(manifestFile
);
1150 if (manifestTree
== NULL
) {
1151 return UNKNOWN_ERROR
;
1153 err
= massageManifest(bundle
, manifestTree
);
1154 if (err
< NO_ERROR
) {
1157 err
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
);
1158 if (err
< NO_ERROR
) {
1163 //printXMLBlock(&block);
1165 // --------------------------------------------------------------
1166 // Generate the final resource table.
1167 // Re-flatten because we may have added new resource IDs
1168 // --------------------------------------------------------------
1170 ResTable finalResTable
;
1171 sp
<AaptFile
> resFile
;
1173 if (table
.hasResources()) {
1174 sp
<AaptSymbols
> symbols
= assets
->getSymbolsFor(String8("R"));
1175 err
= table
.addSymbols(symbols
);
1176 if (err
< NO_ERROR
) {
1180 resFile
= getResourceFile(assets
);
1181 if (resFile
== NULL
) {
1182 fprintf(stderr
, "Error: unable to generate entry for resource data\n");
1183 return UNKNOWN_ERROR
;
1186 err
= table
.flatten(bundle
, resFile
);
1187 if (err
< NO_ERROR
) {
1191 if (bundle
->getPublicOutputFile()) {
1192 FILE* fp
= fopen(bundle
->getPublicOutputFile(), "w+");
1194 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n",
1195 (const char*)bundle
->getPublicOutputFile(), strerror(errno
));
1196 return UNKNOWN_ERROR
;
1198 if (bundle
->getVerbose()) {
1199 printf(" Writing public definitions to %s.\n", bundle
->getPublicOutputFile());
1201 table
.writePublicDefinitions(String16(assets
->getPackage()), fp
);
1205 // Read resources back in,
1206 finalResTable
.add(resFile
->getData(), resFile
->getSize(), NULL
);
1210 printf("Generated resources:\n");
1211 finalResTable
.print();
1216 // Perform a basic validation of the manifest file. This time we
1217 // parse it with the comments intact, so that we can use them to
1218 // generate java docs... so we are not going to write this one
1219 // back out to the final manifest data.
1220 sp
<AaptFile
> outManifestFile
= new AaptFile(manifestFile
->getSourceFile(),
1221 manifestFile
->getGroupEntry(),
1222 manifestFile
->getResourceType());
1223 err
= compileXmlFile(assets
, manifestFile
,
1224 outManifestFile
, &table
,
1225 XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
1226 | XML_COMPILE_STRIP_WHITESPACE
| XML_COMPILE_STRIP_RAW_VALUES
);
1227 if (err
< NO_ERROR
) {
1231 block
.setTo(outManifestFile
->getData(), outManifestFile
->getSize(), true);
1232 String16
manifest16("manifest");
1233 String16
permission16("permission");
1234 String16
permission_group16("permission-group");
1235 String16
uses_permission16("uses-permission");
1236 String16
instrumentation16("instrumentation");
1237 String16
application16("application");
1238 String16
provider16("provider");
1239 String16
service16("service");
1240 String16
receiver16("receiver");
1241 String16
activity16("activity");
1242 String16
action16("action");
1243 String16
category16("category");
1244 String16
data16("scheme");
1245 const char* packageIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1246 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789";
1247 const char* packageIdentCharsWithTheStupid
= "abcdefghijklmnopqrstuvwxyz"
1248 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
1249 const char* classIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1250 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$";
1251 const char* processIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1252 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:";
1253 const char* authoritiesIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1254 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;";
1255 const char* typeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1256 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+";
1257 const char* schemeIdentChars
= "abcdefghijklmnopqrstuvwxyz"
1258 "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-";
1259 ResXMLTree::event_code_t code
;
1260 sp
<AaptSymbols
> permissionSymbols
;
1261 sp
<AaptSymbols
> permissionGroupSymbols
;
1262 while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
1263 && code
> ResXMLTree::BAD_DOCUMENT
) {
1264 if (code
== ResXMLTree::START_TAG
) {
1266 if (block
.getElementNamespace(&len
) != NULL
) {
1269 if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) {
1270 if (validateAttr(manifestPath
, finalResTable
, block
, NULL
, "package",
1271 packageIdentChars
, true) != ATTR_OKAY
) {
1274 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1275 "sharedUserId", packageIdentChars
, false) != ATTR_OKAY
) {
1278 } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0
1279 || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) {
1280 const bool isGroup
= strcmp16(block
.getElementName(&len
),
1281 permission_group16
.string()) == 0;
1282 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1283 "name", isGroup
? packageIdentCharsWithTheStupid
1284 : packageIdentChars
, true) != ATTR_OKAY
) {
1287 SourcePos
srcPos(manifestPath
, block
.getLineNumber());
1288 sp
<AaptSymbols
> syms
;
1290 syms
= permissionSymbols
;
1292 sp
<AaptSymbols
> symbols
=
1293 assets
->getSymbolsFor(String8("Manifest"));
1294 syms
= permissionSymbols
= symbols
->addNestedSymbol(
1295 String8("permission"), srcPos
);
1298 syms
= permissionGroupSymbols
;
1300 sp
<AaptSymbols
> symbols
=
1301 assets
->getSymbolsFor(String8("Manifest"));
1302 syms
= permissionGroupSymbols
= symbols
->addNestedSymbol(
1303 String8("permission_group"), srcPos
);
1307 ssize_t index
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name");
1308 const uint16_t* id
= block
.getAttributeStringValue(index
, &len
);
1310 fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",
1311 manifestPath
.string(), block
.getLineNumber(),
1312 String8(block
.getElementName(&len
)).string());
1317 char* p
= idStr
.lockBuffer(idStr
.size());
1318 char* e
= p
+ idStr
.size();
1319 bool begins_with_digit
= true; // init to true so an empty string fails
1322 if (*e
>= '0' && *e
<= '9') {
1323 begins_with_digit
= true;
1326 if ((*e
>= 'a' && *e
<= 'z') ||
1327 (*e
>= 'A' && *e
<= 'Z') ||
1329 begins_with_digit
= false;
1332 if (isGroup
&& (*e
== '-')) {
1334 begins_with_digit
= false;
1340 idStr
.unlockBuffer();
1341 // verify that we stopped because we hit a period or
1342 // the beginning of the string, and that the
1343 // identifier didn't begin with a digit.
1344 if (begins_with_digit
|| (e
!= p
&& *(e
-1) != '.')) {
1346 "%s:%d: Permission name <%s> is not a valid Java symbol\n",
1347 manifestPath
.string(), block
.getLineNumber(), idStr
.string());
1350 syms
->addStringSymbol(String8(e
), idStr
, srcPos
);
1351 const uint16_t* cmt
= block
.getComment(&len
);
1352 if (cmt
!= NULL
&& *cmt
!= 0) {
1353 //printf("Comment of %s: %s\n", String8(e).string(),
1354 // String8(cmt).string());
1355 syms
->appendComment(String8(e
), String16(cmt
), srcPos
);
1357 //printf("No comment for %s\n", String8(e).string());
1359 syms
->makeSymbolPublic(String8(e
), srcPos
);
1360 } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) {
1361 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1362 "name", packageIdentChars
, true) != ATTR_OKAY
) {
1365 } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) {
1366 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1367 "name", classIdentChars
, true) != ATTR_OKAY
) {
1370 if (validateAttr(manifestPath
, finalResTable
, block
,
1371 RESOURCES_ANDROID_NAMESPACE
, "targetPackage",
1372 packageIdentChars
, true) != ATTR_OKAY
) {
1375 } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) {
1376 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1377 "name", classIdentChars
, false) != ATTR_OKAY
) {
1380 if (validateAttr(manifestPath
, finalResTable
, block
,
1381 RESOURCES_ANDROID_NAMESPACE
, "permission",
1382 packageIdentChars
, false) != ATTR_OKAY
) {
1385 if (validateAttr(manifestPath
, finalResTable
, block
,
1386 RESOURCES_ANDROID_NAMESPACE
, "process",
1387 processIdentChars
, false) != ATTR_OKAY
) {
1390 if (validateAttr(manifestPath
, finalResTable
, block
,
1391 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1392 processIdentChars
, false) != ATTR_OKAY
) {
1395 } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) {
1396 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1397 "name", classIdentChars
, true) != ATTR_OKAY
) {
1400 if (validateAttr(manifestPath
, finalResTable
, block
,
1401 RESOURCES_ANDROID_NAMESPACE
, "authorities",
1402 authoritiesIdentChars
, true) != ATTR_OKAY
) {
1405 if (validateAttr(manifestPath
, finalResTable
, block
,
1406 RESOURCES_ANDROID_NAMESPACE
, "permission",
1407 packageIdentChars
, false) != ATTR_OKAY
) {
1410 if (validateAttr(manifestPath
, finalResTable
, block
,
1411 RESOURCES_ANDROID_NAMESPACE
, "process",
1412 processIdentChars
, false) != ATTR_OKAY
) {
1415 } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0
1416 || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0
1417 || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) {
1418 if (validateAttr(manifestPath
, finalResTable
, block
, RESOURCES_ANDROID_NAMESPACE
,
1419 "name", classIdentChars
, true) != ATTR_OKAY
) {
1422 if (validateAttr(manifestPath
, finalResTable
, block
,
1423 RESOURCES_ANDROID_NAMESPACE
, "permission",
1424 packageIdentChars
, false) != ATTR_OKAY
) {
1427 if (validateAttr(manifestPath
, finalResTable
, block
,
1428 RESOURCES_ANDROID_NAMESPACE
, "process",
1429 processIdentChars
, false) != ATTR_OKAY
) {
1432 if (validateAttr(manifestPath
, finalResTable
, block
,
1433 RESOURCES_ANDROID_NAMESPACE
, "taskAffinity",
1434 processIdentChars
, false) != ATTR_OKAY
) {
1437 } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0
1438 || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) {
1439 if (validateAttr(manifestPath
, finalResTable
, block
,
1440 RESOURCES_ANDROID_NAMESPACE
, "name",
1441 packageIdentChars
, true) != ATTR_OKAY
) {
1444 } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) {
1445 if (validateAttr(manifestPath
, finalResTable
, block
,
1446 RESOURCES_ANDROID_NAMESPACE
, "mimeType",
1447 typeIdentChars
, true) != ATTR_OKAY
) {
1450 if (validateAttr(manifestPath
, finalResTable
, block
,
1451 RESOURCES_ANDROID_NAMESPACE
, "scheme",
1452 schemeIdentChars
, true) != ATTR_OKAY
) {
1459 if (resFile
!= NULL
) {
1460 // These resources are now considered to be a part of the included
1461 // resources, for others to reference.
1462 err
= assets
->addIncludedResources(resFile
);
1463 if (err
< NO_ERROR
) {
1464 fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n");
1472 static const char* getIndentSpace(int indent
)
1474 static const char whitespace
[] =
1477 return whitespace
+ sizeof(whitespace
) - 1 - indent
*4;
1480 static status_t
fixupSymbol(String16
* inoutSymbol
)
1482 inoutSymbol
->replaceAll('.', '_');
1483 inoutSymbol
->replaceAll(':', '_');
1487 static String16
getAttributeComment(const sp
<AaptAssets
>& assets
,
1488 const String8
& name
,
1489 String16
* outTypeComment
= NULL
)
1491 sp
<AaptSymbols
> asym
= assets
->getSymbolsFor(String8("R"));
1493 //printf("Got R symbols!\n");
1494 asym
= asym
->getNestedSymbols().valueFor(String8("attr"));
1496 //printf("Got attrs symbols! comment %s=%s\n",
1497 // name.string(), String8(asym->getComment(name)).string());
1498 if (outTypeComment
!= NULL
) {
1499 *outTypeComment
= asym
->getTypeComment(name
);
1501 return asym
->getComment(name
);
1507 static status_t
writeLayoutClasses(
1508 FILE* fp
, const sp
<AaptAssets
>& assets
,
1509 const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
)
1511 const char* indentStr
= getIndentSpace(indent
);
1512 if (!includePrivate
) {
1513 fprintf(fp
, "%s/** @doconly */\n", indentStr
);
1515 fprintf(fp
, "%spublic static final class styleable {\n", indentStr
);
1518 String16
attr16("attr");
1519 String16
package16(assets
->getPackage());
1521 indentStr
= getIndentSpace(indent
);
1522 bool hasErrors
= false;
1525 size_t N
= symbols
->getNestedSymbols().size();
1526 for (i
=0; i
<N
; i
++) {
1527 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1528 String16
nclassName16(symbols
->getNestedSymbols().keyAt(i
));
1529 String8
realClassName(nclassName16
);
1530 if (fixupSymbol(&nclassName16
) != NO_ERROR
) {
1533 String8
nclassName(nclassName16
);
1535 SortedVector
<uint32_t> idents
;
1536 Vector
<uint32_t> origOrder
;
1537 Vector
<bool> publicFlags
;
1540 size_t NA
= nsymbols
->getSymbols().size();
1541 for (a
=0; a
<NA
; a
++) {
1542 const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
));
1543 int32_t code
= sym
.typeCode
== AaptSymbolEntry::TYPE_INT32
1545 bool isPublic
= true;
1547 String16
name16(sym
.name
);
1548 uint32_t typeSpecFlags
;
1549 code
= assets
->getIncludedResources().identifierForName(
1550 name16
.string(), name16
.size(),
1551 attr16
.string(), attr16
.size(),
1552 package16
.string(), package16
.size(), &typeSpecFlags
);
1554 fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n",
1555 nclassName
.string(), sym
.name
.string());
1558 isPublic
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1561 origOrder
.add(code
);
1562 publicFlags
.add(isPublic
);
1567 bool deprecated
= false;
1569 String16 comment
= symbols
->getComment(realClassName
);
1570 fprintf(fp
, "%s/** ", indentStr
);
1571 if (comment
.size() > 0) {
1572 String8
cmt(comment
);
1573 fprintf(fp
, "%s\n", cmt
.string());
1574 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1578 fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string());
1580 bool hasTable
= false;
1581 for (a
=0; a
<NA
; a
++) {
1582 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1587 "%s <p>Includes the following attributes:</p>\n"
1589 "%s <colgroup align=\"left\" />\n"
1590 "%s <colgroup align=\"left\" />\n"
1591 "%s <tr><th>Attribute</th><th>Description</th></tr>\n",
1598 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1599 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1602 String8
name8(sym
.name
);
1603 String16
comment(sym
.comment
);
1604 if (comment
.size() <= 0) {
1605 comment
= getAttributeComment(assets
, name8
);
1607 if (comment
.size() > 0) {
1608 const char16_t* p
= comment
.string();
1609 while (*p
!= 0 && *p
!= '.') {
1611 while (*p
!= 0 && *p
!= '}') {
1621 comment
= String16(comment
.string(), p
-comment
.string());
1623 String16
name(name8
);
1625 fprintf(fp
, "%s <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n",
1626 indentStr
, nclassName
.string(),
1627 String8(name
).string(),
1628 assets
->getPackage().string(),
1629 String8(name
).string(),
1630 String8(comment
).string());
1634 fprintf(fp
, "%s </table>\n", indentStr
);
1636 for (a
=0; a
<NA
; a
++) {
1637 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1639 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1640 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1643 String16
name(sym
.name
);
1645 fprintf(fp
, "%s @see #%s_%s\n",
1646 indentStr
, nclassName
.string(),
1647 String8(name
).string());
1650 fprintf(fp
, "%s */\n", getIndentSpace(indent
));
1653 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1657 "%spublic static final int[] %s = {\n"
1659 indentStr
, nclassName
.string(),
1660 getIndentSpace(indent
+1));
1662 for (a
=0; a
<NA
; a
++) {
1665 fprintf(fp
, ",\n%s", getIndentSpace(indent
+1));
1670 fprintf(fp
, "0x%08x", idents
[a
]);
1673 fprintf(fp
, "\n%s};\n", indentStr
);
1675 for (a
=0; a
<NA
; a
++) {
1676 ssize_t pos
= idents
.indexOf(origOrder
.itemAt(a
));
1678 const AaptSymbolEntry
& sym
= nsymbols
->getSymbols().valueAt(a
);
1679 if (!publicFlags
.itemAt(a
) && !includePrivate
) {
1682 String8
name8(sym
.name
);
1683 String16
comment(sym
.comment
);
1684 String16 typeComment
;
1685 if (comment
.size() <= 0) {
1686 comment
= getAttributeComment(assets
, name8
, &typeComment
);
1688 getAttributeComment(assets
, name8
, &typeComment
);
1690 String16
name(name8
);
1691 if (fixupSymbol(&name
) != NO_ERROR
) {
1695 uint32_t typeSpecFlags
= 0;
1696 String16
name16(sym
.name
);
1697 assets
->getIncludedResources().identifierForName(
1698 name16
.string(), name16
.size(),
1699 attr16
.string(), attr16
.size(),
1700 package16
.string(), package16
.size(), &typeSpecFlags
);
1701 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(),
1702 // String8(attr16).string(), String8(name16).string(), typeSpecFlags);
1703 const bool pub
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0;
1705 bool deprecated
= false;
1707 fprintf(fp
, "%s/**\n", indentStr
);
1708 if (comment
.size() > 0) {
1709 String8
cmt(comment
);
1710 fprintf(fp
, "%s <p>\n%s @attr description\n", indentStr
, indentStr
);
1711 fprintf(fp
, "%s %s\n", indentStr
, cmt
.string());
1712 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1717 "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n"
1718 "%s attribute's value can be found in the {@link #%s} array.\n",
1720 pub
? assets
->getPackage().string()
1721 : assets
->getSymbolsPrivatePackage().string(),
1722 String8(name
).string(),
1723 indentStr
, nclassName
.string());
1725 if (typeComment
.size() > 0) {
1726 String8
cmt(typeComment
);
1727 fprintf(fp
, "\n\n%s %s\n", indentStr
, cmt
.string());
1728 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1732 if (comment
.size() > 0) {
1735 "%s <p>This corresponds to the global attribute"
1736 "%s resource symbol {@link %s.R.attr#%s}.\n",
1737 indentStr
, indentStr
,
1738 assets
->getPackage().string(),
1739 String8(name
).string());
1742 "%s <p>This is a private symbol.\n", indentStr
);
1745 fprintf(fp
, "%s @attr name %s:%s\n", indentStr
,
1746 "android", String8(name
).string());
1747 fprintf(fp
, "%s*/\n", indentStr
);
1749 fprintf(fp
, "%s@Deprecated\n", indentStr
);
1752 "%spublic static final int %s_%s = %d;\n",
1753 indentStr
, nclassName
.string(),
1754 String8(name
).string(), (int)pos
);
1760 fprintf(fp
, "%s};\n", getIndentSpace(indent
));
1761 return hasErrors
? UNKNOWN_ERROR
: NO_ERROR
;
1764 static status_t
writeSymbolClass(
1765 FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
,
1766 const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
,
1769 fprintf(fp
, "%spublic %sfinal class %s {\n",
1770 getIndentSpace(indent
),
1771 indent
!= 0 ? "static " : "", className
.string());
1775 status_t err
= NO_ERROR
;
1777 const char * id_format
= nonConstantId
?
1778 "%spublic static int %s=0x%08x;\n" :
1779 "%spublic static final int %s=0x%08x;\n";
1781 size_t N
= symbols
->getSymbols().size();
1782 for (i
=0; i
<N
; i
++) {
1783 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1784 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_INT32
) {
1787 if (!includePrivate
&& !sym
.isPublic
) {
1790 String16
name(sym
.name
);
1791 String8
realName(name
);
1792 if (fixupSymbol(&name
) != NO_ERROR
) {
1793 return UNKNOWN_ERROR
;
1795 String16
comment(sym
.comment
);
1796 bool haveComment
= false;
1797 bool deprecated
= false;
1798 if (comment
.size() > 0) {
1800 String8
cmt(comment
);
1803 getIndentSpace(indent
), cmt
.string());
1804 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1807 } else if (sym
.isPublic
&& !includePrivate
) {
1808 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1809 assets
->getPackage().string(), className
.string(),
1810 String8(sym
.name
).string());
1812 String16
typeComment(sym
.typeComment
);
1813 if (typeComment
.size() > 0) {
1814 String8
cmt(typeComment
);
1818 "%s/** %s\n", getIndentSpace(indent
), cmt
.string());
1821 "%s %s\n", getIndentSpace(indent
), cmt
.string());
1823 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1828 fprintf(fp
,"%s */\n", getIndentSpace(indent
));
1831 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1833 fprintf(fp
, id_format
,
1834 getIndentSpace(indent
),
1835 String8(name
).string(), (int)sym
.int32Val
);
1838 for (i
=0; i
<N
; i
++) {
1839 const AaptSymbolEntry
& sym
= symbols
->getSymbols().valueAt(i
);
1840 if (sym
.typeCode
!= AaptSymbolEntry::TYPE_STRING
) {
1843 if (!includePrivate
&& !sym
.isPublic
) {
1846 String16
name(sym
.name
);
1847 if (fixupSymbol(&name
) != NO_ERROR
) {
1848 return UNKNOWN_ERROR
;
1850 String16
comment(sym
.comment
);
1851 bool deprecated
= false;
1852 if (comment
.size() > 0) {
1853 String8
cmt(comment
);
1857 getIndentSpace(indent
), cmt
.string(),
1858 getIndentSpace(indent
));
1859 if (strstr(cmt
.string(), "@deprecated") != NULL
) {
1862 } else if (sym
.isPublic
&& !includePrivate
) {
1863 sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s",
1864 assets
->getPackage().string(), className
.string(),
1865 String8(sym
.name
).string());
1868 fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
));
1870 fprintf(fp
, "%spublic static final String %s=\"%s\";\n",
1871 getIndentSpace(indent
),
1872 String8(name
).string(), sym
.stringVal
.string());
1875 sp
<AaptSymbols
> styleableSymbols
;
1877 N
= symbols
->getNestedSymbols().size();
1878 for (i
=0; i
<N
; i
++) {
1879 sp
<AaptSymbols
> nsymbols
= symbols
->getNestedSymbols().valueAt(i
);
1880 String8
nclassName(symbols
->getNestedSymbols().keyAt(i
));
1881 if (nclassName
== "styleable") {
1882 styleableSymbols
= nsymbols
;
1884 err
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
, nonConstantId
);
1886 if (err
!= NO_ERROR
) {
1891 if (styleableSymbols
!= NULL
) {
1892 err
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
);
1893 if (err
!= NO_ERROR
) {
1899 fprintf(fp
, "%s}\n", getIndentSpace(indent
));
1903 status_t
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
,
1904 const String8
& package
, bool includePrivate
)
1906 if (!bundle
->getRClassDir()) {
1910 const size_t N
= assets
->getSymbols().size();
1911 for (size_t i
=0; i
<N
; i
++) {
1912 sp
<AaptSymbols
> symbols
= assets
->getSymbols().valueAt(i
);
1913 String8
className(assets
->getSymbols().keyAt(i
));
1914 String8
dest(bundle
->getRClassDir());
1915 if (bundle
->getMakePackageDirs()) {
1916 String8
pkg(package
);
1917 const char* last
= pkg
.string();
1918 const char* s
= last
-1;
1921 if (s
> last
&& (*s
== '.' || *s
== 0)) {
1922 String8
part(last
, s
-last
);
1923 dest
.appendPath(part
);
1924 #ifdef HAVE_MS_C_RUNTIME
1925 _mkdir(dest
.string());
1927 mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
);
1933 dest
.appendPath(className
);
1934 dest
.append(".java");
1935 FILE* fp
= fopen(dest
.string(), "w+");
1937 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
1938 dest
.string(), strerror(errno
));
1939 return UNKNOWN_ERROR
;
1941 if (bundle
->getVerbose()) {
1942 printf(" Writing symbols for class %s.\n", className
.string());
1946 "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n"
1948 " * This class was automatically generated by the\n"
1949 " * aapt tool from the resource data it found. It\n"
1950 " * should not be modified by hand.\n"
1953 "package %s;\n\n", package
.string());
1955 status_t err
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0, bundle
->getNonConstantId());
1956 if (err
!= NO_ERROR
) {
1961 if (bundle
->getGenDependencies()) {
1962 // Add this R.java to the dependency file
1963 String8
dependencyFile(bundle
->getRClassDir());
1964 dependencyFile
.appendPath("R.d");
1966 fp
= fopen(dependencyFile
.string(), "a");
1967 fprintf(fp
,"%s \\\n", dest
.string());
1977 class ProguardKeepSet
1980 // { rule --> { file locations } }
1981 KeyedVector
<String8
, SortedVector
<String8
> > rules
;
1983 void add(const String8
& rule
, const String8
& where
);
1986 void ProguardKeepSet::add(const String8
& rule
, const String8
& where
)
1988 ssize_t index
= rules
.indexOfKey(rule
);
1990 index
= rules
.add(rule
, SortedVector
<String8
>());
1992 rules
.editValueAt(index
).add(where
);
1996 addProguardKeepRule(ProguardKeepSet
* keep
, const String8
& inClassName
,
1997 const char* pkg
, const String8
& srcName
, int line
)
1999 String8
className(inClassName
);
2001 // asdf --> package.asdf
2002 // .asdf .a.b --> package.asdf package.a.b
2003 // asdf.adsf --> asdf.asdf
2004 const char* p
= className
.string();
2005 const char* q
= strchr(p
, '.');
2008 className
.append(inClassName
);
2009 } else if (q
== NULL
) {
2011 className
.append(".");
2012 className
.append(inClassName
);
2016 String8
rule("-keep class ");
2018 rule
+= " { <init>(...); }";
2020 String8
location("view ");
2021 location
+= srcName
;
2023 sprintf(lineno
, ":%d", line
);
2026 keep
->add(rule
, location
);
2030 writeProguardForAndroidManifest(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
2035 ResXMLTree::event_code_t code
;
2037 bool inApplication
= false;
2039 sp
<AaptGroup
> assGroup
;
2040 sp
<AaptFile
> assFile
;
2043 // First, look for a package file to parse. This is required to
2044 // be able to generate the resource information.
2045 assGroup
= assets
->getFiles().valueFor(String8("AndroidManifest.xml"));
2046 if (assGroup
== NULL
) {
2047 fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n");
2051 if (assGroup
->getFiles().size() != 1) {
2052 fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n",
2053 assGroup
->getFiles().valueAt(0)->getPrintableSource().string());
2056 assFile
= assGroup
->getFiles().valueAt(0);
2058 err
= parseXMLResource(assFile
, &tree
);
2059 if (err
!= NO_ERROR
) {
2065 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2066 if (code
== ResXMLTree::END_TAG
) {
2067 if (/* name == "Application" && */ depth
== 2) {
2068 inApplication
= false;
2073 if (code
!= ResXMLTree::START_TAG
) {
2077 String8
tag(tree
.getElementName(&len
));
2078 // printf("Depth %d tag %s\n", depth, tag.string());
2079 bool keepTag
= false;
2081 if (tag
!= "manifest") {
2082 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n");
2085 pkg
= getAttribute(tree
, NULL
, "package", NULL
);
2086 } else if (depth
== 2) {
2087 if (tag
== "application") {
2088 inApplication
= true;
2091 String8 agent
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
2092 "backupAgent", &error
);
2093 if (agent
.length() > 0) {
2094 addProguardKeepRule(keep
, agent
, pkg
.string(),
2095 assFile
->getPrintableSource(), tree
.getLineNumber());
2097 } else if (tag
== "instrumentation") {
2101 if (!keepTag
&& inApplication
&& depth
== 3) {
2102 if (tag
== "activity" || tag
== "service" || tag
== "receiver" || tag
== "provider") {
2107 String8 name
= getAttribute(tree
, "http://schemas.android.com/apk/res/android",
2110 fprintf(stderr
, "ERROR: %s\n", error
.string());
2113 if (name
.length() > 0) {
2114 addProguardKeepRule(keep
, name
, pkg
.string(),
2115 assFile
->getPrintableSource(), tree
.getLineNumber());
2123 struct NamespaceAttributePair
{
2127 NamespaceAttributePair(const char* n
, const char* a
) : ns(n
), attr(a
) {}
2128 NamespaceAttributePair() : ns(NULL
), attr(NULL
) {}
2132 writeProguardForXml(ProguardKeepSet
* keep
, const sp
<AaptFile
>& layoutFile
,
2133 const char* startTag
, const KeyedVector
<String8
, NamespaceAttributePair
>* tagAttrPairs
)
2138 ResXMLTree::event_code_t code
;
2140 err
= parseXMLResource(layoutFile
, &tree
);
2141 if (err
!= NO_ERROR
) {
2147 if (startTag
!= NULL
) {
2148 bool haveStart
= false;
2149 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2150 if (code
!= ResXMLTree::START_TAG
) {
2153 String8
tag(tree
.getElementName(&len
));
2154 if (tag
== startTag
) {
2164 while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT
&& code
!= ResXMLTree::BAD_DOCUMENT
) {
2165 if (code
!= ResXMLTree::START_TAG
) {
2168 String8
tag(tree
.getElementName(&len
));
2170 // If there is no '.', we'll assume that it's one of the built in names.
2171 if (strchr(tag
.string(), '.')) {
2172 addProguardKeepRule(keep
, tag
, NULL
,
2173 layoutFile
->getPrintableSource(), tree
.getLineNumber());
2174 } else if (tagAttrPairs
!= NULL
) {
2175 ssize_t tagIndex
= tagAttrPairs
->indexOfKey(tag
);
2176 if (tagIndex
>= 0) {
2177 const NamespaceAttributePair
& nsAttr
= tagAttrPairs
->valueAt(tagIndex
);
2178 ssize_t attrIndex
= tree
.indexOfAttribute(nsAttr
.ns
, nsAttr
.attr
);
2179 if (attrIndex
< 0) {
2180 // fprintf(stderr, "%s:%d: <%s> does not have attribute %s:%s.\n",
2181 // layoutFile->getPrintableSource().string(), tree.getLineNumber(),
2182 // tag.string(), nsAttr.ns, nsAttr.attr);
2185 addProguardKeepRule(keep
,
2186 String8(tree
.getAttributeStringValue(attrIndex
, &len
)), NULL
,
2187 layoutFile
->getPrintableSource(), tree
.getLineNumber());
2196 static void addTagAttrPair(KeyedVector
<String8
, NamespaceAttributePair
>* dest
,
2197 const char* tag
, const char* ns
, const char* attr
) {
2198 dest
->add(String8(tag
), NamespaceAttributePair(ns
, attr
));
2202 writeProguardForLayouts(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
)
2206 // tag:attribute pairs that should be checked in layout files.
2207 KeyedVector
<String8
, NamespaceAttributePair
> kLayoutTagAttrPairs
;
2208 addTagAttrPair(&kLayoutTagAttrPairs
, "view", NULL
, "class");
2209 addTagAttrPair(&kLayoutTagAttrPairs
, "fragment", NULL
, "class");
2210 addTagAttrPair(&kLayoutTagAttrPairs
, "fragment", RESOURCES_ANDROID_NAMESPACE
, "name");
2212 // tag:attribute pairs that should be checked in xml files.
2213 KeyedVector
<String8
, NamespaceAttributePair
> kXmlTagAttrPairs
;
2214 addTagAttrPair(&kXmlTagAttrPairs
, "PreferenceScreen", RESOURCES_ANDROID_NAMESPACE
, "fragment");
2215 addTagAttrPair(&kXmlTagAttrPairs
, "header", RESOURCES_ANDROID_NAMESPACE
, "fragment");
2217 const Vector
<sp
<AaptDir
> >& dirs
= assets
->resDirs();
2218 const size_t K
= dirs
.size();
2219 for (size_t k
=0; k
<K
; k
++) {
2220 const sp
<AaptDir
>& d
= dirs
.itemAt(k
);
2221 const String8
& dirName
= d
->getLeaf();
2222 const char* startTag
= NULL
;
2223 const KeyedVector
<String8
, NamespaceAttributePair
>* tagAttrPairs
= NULL
;
2224 if ((dirName
== String8("layout")) || (strncmp(dirName
.string(), "layout-", 7) == 0)) {
2225 tagAttrPairs
= &kLayoutTagAttrPairs
;
2226 } else if ((dirName
== String8("xml")) || (strncmp(dirName
.string(), "xml-", 4) == 0)) {
2227 startTag
= "PreferenceScreen";
2228 tagAttrPairs
= &kXmlTagAttrPairs
;
2233 const KeyedVector
<String8
,sp
<AaptGroup
> > groups
= d
->getFiles();
2234 const size_t N
= groups
.size();
2235 for (size_t i
=0; i
<N
; i
++) {
2236 const sp
<AaptGroup
>& group
= groups
.valueAt(i
);
2237 const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
= group
->getFiles();
2238 const size_t M
= files
.size();
2239 for (size_t j
=0; j
<M
; j
++) {
2240 err
= writeProguardForXml(keep
, files
.valueAt(j
), startTag
, tagAttrPairs
);
2251 writeProguardFile(Bundle
* bundle
, const sp
<AaptAssets
>& assets
)
2255 if (!bundle
->getProguardFile()) {
2259 ProguardKeepSet keep
;
2261 err
= writeProguardForAndroidManifest(&keep
, assets
);
2266 err
= writeProguardForLayouts(&keep
, assets
);
2271 FILE* fp
= fopen(bundle
->getProguardFile(), "w+");
2273 fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n",
2274 bundle
->getProguardFile(), strerror(errno
));
2275 return UNKNOWN_ERROR
;
2278 const KeyedVector
<String8
, SortedVector
<String8
> >& rules
= keep
.rules
;
2279 const size_t N
= rules
.size();
2280 for (size_t i
=0; i
<N
; i
++) {
2281 const SortedVector
<String8
>& locations
= rules
.valueAt(i
);
2282 const size_t M
= locations
.size();
2283 for (size_t j
=0; j
<M
; j
++) {
2284 fprintf(fp
, "# %s\n", locations
.itemAt(j
).string());
2286 fprintf(fp
, "%s\n\n", rules
.keyAt(i
).string());
2293 // Loops through the string paths and writes them to the file pointer
2294 // Each file path is written on its own line with a terminating backslash.
2295 status_t
writePathsToFile(const sp
<FilePathStore
>& files
, FILE* fp
)
2298 for (size_t file_i
= 0; file_i
< files
->size(); ++file_i
) {
2299 // Add the full file path to the dependency file
2300 fprintf(fp
, "%s \\\n", files
->itemAt(file_i
).string());
2307 writeDependencyPreReqs(Bundle
* bundle
, const sp
<AaptAssets
>& assets
, FILE* fp
, bool includeRaw
)
2310 deps
+= writePathsToFile(assets
->getFullResPaths(), fp
);
2312 deps
+= writePathsToFile(assets
->getFullAssetPaths(), fp
);