2 // Copyright 2006 The Android Open Source Project 
   4 // Build resource files from raw assets. 
   7 #include "AaptAssets.h" 
   8 #include "StringPool.h" 
  10 #include "ResourceTable.h" 
  15 // ========================================================================== 
  16 // ========================================================================== 
  17 // ========================================================================== 
  29     status_t 
parsePackage(const sp
<AaptGroup
>& grp
); 
  32 // ========================================================================== 
  33 // ========================================================================== 
  34 // ========================================================================== 
  36 static String8 
parseResourceName(const String8
& leaf
) 
  38     const char* firstDot 
= strchr(leaf
.string(), '.'); 
  39     const char* str 
= leaf
.string(); 
  42         return String8(str
, firstDot
-str
); 
  48 ResourceTypeSet::ResourceTypeSet() 
  50      KeyedVector
<String8
,sp
<AaptGroup
> >() 
  54 class ResourceDirIterator
 
  57     ResourceDirIterator(const sp
<ResourceTypeSet
>& set
, const String8
& resType
) 
  58         : mResType(resType
), mSet(set
), mSetPos(0), mGroupPos(0) 
  62     inline const sp
<AaptGroup
>& getGroup() const { return mGroup
; } 
  63     inline const sp
<AaptFile
>& getFile() const { return mFile
; } 
  65     inline const String8
& getBaseName() const { return mBaseName
; } 
  66     inline const String8
& getLeafName() const { return mLeafName
; } 
  67     inline String8 
getPath() const { return mPath
; } 
  68     inline const ResTable_config
& getParams() const { return mParams
; } 
  80             // Try to get next file in this current group. 
  81             if (mGroup 
!= NULL 
&& mGroupPos 
< mGroup
->getFiles().size()) { 
  83                 file 
= group
->getFiles().valueAt(mGroupPos
++); 
  85             // Try to get the next group/file in this directory 
  86             } else if (mSetPos 
< mSet
->size()) { 
  87                 mGroup 
= group 
= mSet
->valueAt(mSetPos
++); 
  88                 if (group
->getFiles().size() < 1) { 
  91                 file 
= group
->getFiles().valueAt(0); 
 101             String8 
leaf(group
->getLeaf()); 
 102             mLeafName 
= String8(leaf
); 
 103             mParams 
= file
->getGroupEntry().toParams(); 
 104             NOISY(printf("Dir %s: mcc=%d mnc=%d lang=%c%c cnt=%c%c orient=%d ui=%d density=%d touch=%d key=%d inp=%d nav=%d\n", 
 105                    group
->getPath().string(), mParams
.mcc
, mParams
.mnc
, 
 106                    mParams
.language
[0] ? mParams
.language
[0] : '-', 
 107                    mParams
.language
[1] ? mParams
.language
[1] : '-', 
 108                    mParams
.country
[0] ? mParams
.country
[0] : '-', 
 109                    mParams
.country
[1] ? mParams
.country
[1] : '-', 
 110                    mParams
.orientation
, mParams
.uiMode
, 
 111                    mParams
.density
, mParams
.touchscreen
, mParams
.keyboard
, 
 112                    mParams
.inputFlags
, mParams
.navigation
)); 
 114             mPath
.appendPath(file
->getGroupEntry().toDirName(mResType
)); 
 115             mPath
.appendPath(leaf
); 
 116             mBaseName 
= parseResourceName(leaf
); 
 117             if (mBaseName 
== "") { 
 118                 fprintf(stderr
, "Error: malformed resource filename %s\n", 
 119                         file
->getPrintableSource().string()); 
 120                 return UNKNOWN_ERROR
; 
 123             NOISY(printf("file name=%s\n", mBaseName
.string())); 
 132     const sp
<ResourceTypeSet
> mSet
; 
 135     sp
<AaptGroup
> mGroup
; 
 142     ResTable_config mParams
; 
 145 // ========================================================================== 
 146 // ========================================================================== 
 147 // ========================================================================== 
 149 bool isValidResourceType(const String8
& type
) 
 151     return type 
== "anim" || type 
== "drawable" || type 
== "layout" 
 152         || type 
== "values" || type 
== "xml" || type 
== "raw" 
 153         || type 
== "color" || type 
== "menu"; 
 156 static sp
<AaptFile
> getResourceFile(const sp
<AaptAssets
>& assets
, bool makeIfNecessary
=true) 
 158     sp
<AaptGroup
> group 
= assets
->getFiles().valueFor(String8("resources.arsc")); 
 161         file 
= group
->getFiles().valueFor(AaptGroupEntry()); 
 167     if (!makeIfNecessary
) { 
 170     return assets
->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(), 
 174 static status_t 
parsePackage(Bundle
* bundle
, const sp
<AaptAssets
>& assets
, 
 175     const sp
<AaptGroup
>& grp
) 
 177     if (grp
->getFiles().size() != 1) { 
 178         fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n", 
 179                 grp
->getFiles().valueAt(0)->getPrintableSource().string()); 
 182     sp
<AaptFile
> file 
= grp
->getFiles().valueAt(0); 
 185     status_t err 
= parseXMLResource(file
, &block
); 
 186     if (err 
!= NO_ERROR
) { 
 189     //printXMLBlock(&block); 
 191     ResXMLTree::event_code_t code
; 
 192     while ((code
=block
.next()) != ResXMLTree::START_TAG
 
 193            && code 
!= ResXMLTree::END_DOCUMENT
 
 194            && code 
!= ResXMLTree::BAD_DOCUMENT
) { 
 198     if (code 
!= ResXMLTree::START_TAG
) { 
 199         fprintf(stderr
, "%s:%d: No start tag found\n", 
 200                 file
->getPrintableSource().string(), block
.getLineNumber()); 
 201         return UNKNOWN_ERROR
; 
 203     if (strcmp16(block
.getElementName(&len
), String16("manifest").string()) != 0) { 
 204         fprintf(stderr
, "%s:%d: Invalid start tag %s, expected <manifest>\n", 
 205                 file
->getPrintableSource().string(), block
.getLineNumber(), 
 206                 String8(block
.getElementName(&len
)).string()); 
 207         return UNKNOWN_ERROR
; 
 210     ssize_t nameIndex 
= block
.indexOfAttribute(NULL
, "package"); 
 212         fprintf(stderr
, "%s:%d: <manifest> does not have package attribute.\n", 
 213                 file
->getPrintableSource().string(), block
.getLineNumber()); 
 214         return UNKNOWN_ERROR
; 
 217     assets
->setPackage(String8(block
.getAttributeStringValue(nameIndex
, &len
))); 
 219     String16 
uses_sdk16("uses-sdk"); 
 220     while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
 
 221            && code 
!= ResXMLTree::BAD_DOCUMENT
) { 
 222         if (code 
== ResXMLTree::START_TAG
) { 
 223             if (strcmp16(block
.getElementName(&len
), uses_sdk16
.string()) == 0) { 
 224                 ssize_t minSdkIndex 
= block
.indexOfAttribute("android", 
 226                 if (minSdkIndex 
>= 0) { 
 227                     String8 minSdkString 
= String8( 
 228                         block
.getAttributeStringValue(minSdkIndex
, &len
)); 
 229                     bundle
->setMinSdkVersion(minSdkString
.string()); 
 238 // ========================================================================== 
 239 // ========================================================================== 
 240 // ========================================================================== 
 242 static status_t 
makeFileResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
, 
 243                                   ResourceTable
* table
, 
 244                                   const sp
<ResourceTypeSet
>& set
, 
 247     String8 
type8(resType
); 
 248     String16 
type16(resType
); 
 250     bool hasErrors 
= false; 
 252     ResourceDirIterator 
it(set
, String8(resType
)); 
 254     while ((res
=it
.next()) == NO_ERROR
) { 
 255         if (bundle
->getVerbose()) { 
 256             printf("    (new resource id %s from %s)\n", 
 257                    it
.getBaseName().string(), it
.getFile()->getPrintableSource().string()); 
 259         String16 
baseName(it
.getBaseName()); 
 260         const char16_t* str 
= baseName
.string(); 
 261         const char16_t* const end 
= str 
+ baseName
.size(); 
 263             if (!((*str 
>= 'a' && *str 
<= 'z') 
 264                     || (*str 
>= '0' && *str 
<= '9') 
 265                     || *str 
== '_' || *str 
== '.')) { 
 266                 fprintf(stderr
, "%s: Invalid file name: must contain only [a-z0-9_.]\n", 
 267                         it
.getPath().string()); 
 272         String8 resPath 
= it
.getPath(); 
 273         resPath
.convertToResPath(); 
 274         table
->addEntry(SourcePos(it
.getPath(), 0), String16(assets
->getPackage()), 
 280         assets
->addResource(it
.getLeafName(), resPath
, it
.getFile(), type8
); 
 283     return hasErrors 
? UNKNOWN_ERROR 
: NO_ERROR
; 
 286 static status_t 
preProcessImages(Bundle
* bundle
, const sp
<AaptAssets
>& assets
, 
 287                           const sp
<ResourceTypeSet
>& set
) 
 289     ResourceDirIterator 
it(set
, String8("drawable")); 
 290     Vector
<sp
<AaptFile
> > newNameFiles
; 
 291     Vector
<String8
> newNamePaths
; 
 292     bool hasErrors 
= false; 
 294     while ((res
=it
.next()) == NO_ERROR
) { 
 295         res 
= preProcessImage(bundle
, assets
, it
.getFile(), NULL
); 
 296         if (res 
< NO_ERROR
) { 
 301     return (hasErrors 
|| (res 
< NO_ERROR
)) ? UNKNOWN_ERROR 
: NO_ERROR
; 
 304 status_t 
postProcessImages(const sp
<AaptAssets
>& assets
, 
 305                            ResourceTable
* table
, 
 306                            const sp
<ResourceTypeSet
>& set
) 
 308     ResourceDirIterator 
it(set
, String8("drawable")); 
 309     bool hasErrors 
= false; 
 311     while ((res
=it
.next()) == NO_ERROR
) { 
 312         res 
= postProcessImage(assets
, table
, it
.getFile()); 
 313         if (res 
< NO_ERROR
) { 
 318     return (hasErrors 
|| (res 
< NO_ERROR
)) ? UNKNOWN_ERROR 
: NO_ERROR
; 
 321 static void collect_files(const sp
<AaptDir
>& dir
, 
 322         KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
) 
 324     const DefaultKeyedVector
<String8
, sp
<AaptGroup
> >& groups 
= dir
->getFiles(); 
 325     int N 
= groups
.size(); 
 326     for (int i
=0; i
<N
; i
++) { 
 327         String8 leafName 
= groups
.keyAt(i
); 
 328         const sp
<AaptGroup
>& group 
= groups
.valueAt(i
); 
 330         const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files
 
 333         if (files
.size() == 0) { 
 337         String8 resType 
= files
.valueAt(0)->getResourceType(); 
 339         ssize_t index 
= resources
->indexOfKey(resType
); 
 342             sp
<ResourceTypeSet
> set 
= new ResourceTypeSet(); 
 343             set
->add(leafName
, group
); 
 344             resources
->add(resType
, set
); 
 346             sp
<ResourceTypeSet
> set 
= resources
->valueAt(index
); 
 347             index 
= set
->indexOfKey(leafName
); 
 349                 set
->add(leafName
, group
); 
 351                 sp
<AaptGroup
> existingGroup 
= set
->valueAt(index
); 
 352                 int M 
= files
.size(); 
 353                 for (int j
=0; j
<M
; j
++) { 
 354                     existingGroup
->addFile(files
.valueAt(j
)); 
 361 static void collect_files(const sp
<AaptAssets
>& ass
, 
 362         KeyedVector
<String8
, sp
<ResourceTypeSet
> >* resources
) 
 364     const Vector
<sp
<AaptDir
> >& dirs 
= ass
->resDirs(); 
 367     for (int i
=0; i
<N
; i
++) { 
 368         sp
<AaptDir
> d 
= dirs
.itemAt(i
); 
 369         collect_files(d
, resources
); 
 371         // don't try to include the res dir 
 372         ass
->removeDir(d
->getLeaf()); 
 379     ATTR_LEADING_SPACES 
= -3, 
 380     ATTR_TRAILING_SPACES 
= -4 
 382 static int validateAttr(const String8
& path
, const ResXMLParser
& parser
, 
 383         const char* ns
, const char* attr
, const char* validChars
, bool required
) 
 387     ssize_t index 
= parser
.indexOfAttribute(ns
, attr
); 
 389     if (index 
>= 0 && (str
=parser
.getAttributeStringValue(index
, &len
)) != NULL
) { 
 391             for (size_t i
=0; i
<len
; i
++) { 
 393                 const char* p 
= validChars
; 
 403                     fprintf(stderr
, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n", 
 404                             path
.string(), parser
.getLineNumber(), 
 405                             String8(parser
.getElementName(&len
)).string(), attr
, (char)str
[i
]); 
 411             fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not start with a space.\n", 
 412                     path
.string(), parser
.getLineNumber(), 
 413                     String8(parser
.getElementName(&len
)).string(), attr
); 
 414             return ATTR_LEADING_SPACES
; 
 416         if (str
[len
-1] == ' ') { 
 417             fprintf(stderr
, "%s:%d: Tag <%s> attribute %s can not end with a space.\n", 
 418                     path
.string(), parser
.getLineNumber(), 
 419                     String8(parser
.getElementName(&len
)).string(), attr
); 
 420             return ATTR_TRAILING_SPACES
; 
 425         fprintf(stderr
, "%s:%d: Tag <%s> missing required attribute %s.\n", 
 426                 path
.string(), parser
.getLineNumber(), 
 427                 String8(parser
.getElementName(&len
)).string(), attr
); 
 428         return ATTR_NOT_FOUND
; 
 433 static void checkForIds(const String8
& path
, ResXMLParser
& parser
) 
 435     ResXMLTree::event_code_t code
; 
 436     while ((code
=parser
.next()) != ResXMLTree::END_DOCUMENT
 
 437            && code 
> ResXMLTree::BAD_DOCUMENT
) { 
 438         if (code 
== ResXMLTree::START_TAG
) { 
 439             ssize_t index 
= parser
.indexOfAttribute(NULL
, "id"); 
 441                 fprintf(stderr
, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n", 
 442                         path
.string(), parser
.getLineNumber()); 
 448 static bool applyFileOverlay(Bundle 
*bundle
, 
 449                              const sp
<AaptAssets
>& assets
, 
 450                              const sp
<ResourceTypeSet
>& baseSet
, 
 453     if (bundle
->getVerbose()) { 
 454         printf("applyFileOverlay for %s\n", resType
); 
 457     // Replace any base level files in this category with any found from the overlay 
 458     // Also add any found only in the overlay. 
 459     sp
<AaptAssets
> overlay 
= assets
->getOverlay(); 
 460     String8 
resTypeString(resType
); 
 462     // work through the linked list of overlays 
 463     while (overlay
.get()) { 
 464         KeyedVector
<String8
, sp
<ResourceTypeSet
> >* overlayRes 
= overlay
->getResources(); 
 466         // get the overlay resources of the requested type 
 467         ssize_t index 
= overlayRes
->indexOfKey(resTypeString
); 
 469             sp
<ResourceTypeSet
> overlaySet 
= overlayRes
->valueAt(index
); 
 471             // for each of the resources, check for a match in the previously built 
 472             // non-overlay "baseset". 
 473             size_t overlayCount 
= overlaySet
->size(); 
 474             for (size_t overlayIndex
=0; overlayIndex
<overlayCount
; overlayIndex
++) { 
 475                 if (bundle
->getVerbose()) { 
 476                     printf("trying overlaySet Key=%s\n",overlaySet
->keyAt(overlayIndex
).string()); 
 478                 size_t baseIndex 
= baseSet
->indexOfKey(overlaySet
->keyAt(overlayIndex
)); 
 479                 if (baseIndex 
< UNKNOWN_ERROR
) { 
 480                     // look for same flavor.  For a given file (strings.xml, for example) 
 481                     // there may be a locale specific or other flavors - we want to match 
 483                     sp
<AaptGroup
> overlayGroup 
= overlaySet
->valueAt(overlayIndex
); 
 484                     sp
<AaptGroup
> baseGroup 
= baseSet
->valueAt(baseIndex
); 
 486                     DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles 
= 
 487                             overlayGroup
->getFiles(); 
 488                     if (bundle
->getVerbose()) { 
 489                         DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > baseFiles 
= 
 490                                 baseGroup
->getFiles(); 
 491                         for (size_t i
=0; i 
< baseFiles
.size(); i
++) { 
 492                             printf("baseFile %ld has flavor %s\n", i
, 
 493                                     baseFiles
.keyAt(i
).toString().string()); 
 495                         for (size_t i
=0; i 
< overlayFiles
.size(); i
++) { 
 496                             printf("overlayFile %ld has flavor %s\n", i
, 
 497                                     overlayFiles
.keyAt(i
).toString().string()); 
 501                     size_t overlayGroupSize 
= overlayFiles
.size(); 
 502                     for (size_t overlayGroupIndex 
= 0; 
 503                             overlayGroupIndex
<overlayGroupSize
; 
 504                             overlayGroupIndex
++) { 
 505                         size_t baseFileIndex 
= 
 506                                 baseGroup
->getFiles().indexOfKey(overlayFiles
. 
 507                                 keyAt(overlayGroupIndex
)); 
 508                         if(baseFileIndex 
< UNKNOWN_ERROR
) { 
 509                             if (bundle
->getVerbose()) { 
 510                                 printf("found a match (%ld) for overlay file %s, for flavor %s\n", 
 512                                         overlayGroup
->getLeaf().string(), 
 513                                         overlayFiles
.keyAt(overlayGroupIndex
).toString().string()); 
 515                             baseGroup
->removeFile(baseFileIndex
); 
 517                             // didn't find a match fall through and add it.. 
 519                         baseGroup
->addFile(overlayFiles
.valueAt(overlayGroupIndex
)); 
 520                         assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
)); 
 523                     // this group doesn't exist (a file that's only in the overlay) 
 524                     baseSet
->add(overlaySet
->keyAt(overlayIndex
), 
 525                             overlaySet
->valueAt(overlayIndex
)); 
 526                     // make sure all flavors are defined in the resources. 
 527                     sp
<AaptGroup
> overlayGroup 
= overlaySet
->valueAt(overlayIndex
); 
 528                     DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> > overlayFiles 
= 
 529                             overlayGroup
->getFiles(); 
 530                     size_t overlayGroupSize 
= overlayFiles
.size(); 
 531                     for (size_t overlayGroupIndex 
= 0; 
 532                             overlayGroupIndex
<overlayGroupSize
; 
 533                             overlayGroupIndex
++) { 
 534                         assets
->addGroupEntry(overlayFiles
.keyAt(overlayGroupIndex
)); 
 538             // this overlay didn't have resources for this type 
 541         overlay 
= overlay
->getOverlay(); 
 546 void addTagAttribute(const sp
<XMLNode
>& node
, const char* ns8
, 
 547         const char* attr8
, const char* value
) 
 553     const String16 
ns(ns8
); 
 554     const String16 
attr(attr8
); 
 556     if (node
->getAttribute(ns
, attr
) != NULL
) { 
 557         fprintf(stderr
, "Warning: AndroidManifest.xml already defines %s (in %s)\n", 
 558                 String8(attr
).string(), String8(ns
).string()); 
 562     node
->addAttribute(ns
, attr
, String16(value
)); 
 565 static void fullyQualifyClassName(String8
& package
, sp
<XMLNode
> node
) { 
 566     XMLNode::attribute_entry
* attr 
= node
->editAttribute( 
 567             String16("http://schemas.android.com/apk/res/android"), String16("name")); 
 569         String8 
name(attr
->string
); 
 571         // asdf     --> package.asdf 
 572         // .asdf  .a.b  --> package.asdf package.a.b 
 573         // asdf.adsf --> asdf.asdf 
 575         const char* p 
= name
.string(); 
 576         const char* q 
= strchr(p
, '.'); 
 578             className 
+= package
; 
 580         } else if (q 
== NULL
) { 
 581             className 
+= package
; 
 587         NOISY(printf("Qualifying class '%s' to '%s'", name
.string(), className
.string())); 
 588         attr
->string
.setTo(String16(className
)); 
 592 status_t 
massageManifest(Bundle
* bundle
, sp
<XMLNode
> root
) 
 594     root 
= root
->searchElement(String16(), String16("manifest")); 
 596         fprintf(stderr
, "No <manifest> tag.\n"); 
 597         return UNKNOWN_ERROR
; 
 600     addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionCode", 
 601             bundle
->getVersionCode()); 
 602     addTagAttribute(root
, RESOURCES_ANDROID_NAMESPACE
, "versionName", 
 603             bundle
->getVersionName()); 
 605     if (bundle
->getMinSdkVersion() != NULL
 
 606             || bundle
->getTargetSdkVersion() != NULL
 
 607             || bundle
->getMaxSdkVersion() != NULL
) { 
 608         sp
<XMLNode
> vers 
= root
->getChildElement(String16(), String16("uses-sdk")); 
 610             vers 
= XMLNode::newElement(root
->getFilename(), String16(), String16("uses-sdk")); 
 611             root
->insertChildAt(vers
, 0); 
 614         addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "minSdkVersion", 
 615                 bundle
->getMinSdkVersion()); 
 616         addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "targetSdkVersion", 
 617                 bundle
->getTargetSdkVersion()); 
 618         addTagAttribute(vers
, RESOURCES_ANDROID_NAMESPACE
, "maxSdkVersion", 
 619                 bundle
->getMaxSdkVersion()); 
 622     // Deal with manifest package name overrides 
 623     const char* manifestPackageNameOverride 
= bundle
->getManifestPackageNameOverride(); 
 624     if (manifestPackageNameOverride 
!= NULL
) { 
 625         // Update the actual package name 
 626         XMLNode::attribute_entry
* attr 
= root
->editAttribute(String16(), String16("package")); 
 628             fprintf(stderr
, "package name is required with --rename-manifest-package.\n"); 
 629             return UNKNOWN_ERROR
; 
 631         String8 
origPackage(attr
->string
); 
 632         attr
->string
.setTo(String16(manifestPackageNameOverride
)); 
 633         NOISY(printf("Overriding package '%s' to be '%s'\n", origPackage
.string(), manifestPackageNameOverride
)); 
 635         // Make class names fully qualified 
 636         sp
<XMLNode
> application 
= root
->getChildElement(String16(), String16("application")); 
 637         if (application 
!= NULL
) { 
 638             fullyQualifyClassName(origPackage
, application
); 
 640             Vector
<sp
<XMLNode
> >& children 
= const_cast<Vector
<sp
<XMLNode
> >&>(application
->getChildren()); 
 641             for (size_t i 
= 0; i 
< children
.size(); i
++) { 
 642                 sp
<XMLNode
> child 
= children
.editItemAt(i
); 
 643                 String8 
tag(child
->getElementName()); 
 644                 if (tag 
== "activity" || tag 
== "service" || tag 
== "receiver" || tag 
== "provider") { 
 645                     fullyQualifyClassName(origPackage
, child
); 
 654 #define ASSIGN_IT(n) \ 
 656             ssize_t index = resources->indexOfKey(String8(#n)); \ 
 658                 n ## s = resources->valueAt(index); \ 
 662 status_t 
buildResources(Bundle
* bundle
, const sp
<AaptAssets
>& assets
) 
 664     // First, look for a package file to parse.  This is required to 
 665     // be able to generate the resource information. 
 666     sp
<AaptGroup
> androidManifestFile 
= 
 667             assets
->getFiles().valueFor(String8("AndroidManifest.xml")); 
 668     if (androidManifestFile 
== NULL
) { 
 669         fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n"); 
 670         return UNKNOWN_ERROR
; 
 673     status_t err 
= parsePackage(bundle
, assets
, androidManifestFile
); 
 674     if (err 
!= NO_ERROR
) { 
 678     NOISY(printf("Creating resources for package %s\n", 
 679                  assets
->getPackage().string())); 
 681     ResourceTable 
table(bundle
, String16(assets
->getPackage())); 
 682     err 
= table
.addIncludedResources(bundle
, assets
); 
 683     if (err 
!= NO_ERROR
) { 
 687     NOISY(printf("Found %d included resource packages\n", (int)table
.size())); 
 689     // Standard flags for compiled XML and optional UTF-8 encoding 
 690     int xmlFlags 
= XML_COMPILE_STANDARD_RESOURCE
; 
 691     if (bundle
->getUTF8()) { 
 692         xmlFlags 
|= XML_COMPILE_UTF8
; 
 695     // -------------------------------------------------------------- 
 696     // First, gather all resource information. 
 697     // -------------------------------------------------------------- 
 699     // resType -> leafName -> group 
 700     KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources 
=  
 701             new KeyedVector
<String8
, sp
<ResourceTypeSet
> >; 
 702     collect_files(assets
, resources
); 
 704     sp
<ResourceTypeSet
> drawables
; 
 705     sp
<ResourceTypeSet
> layouts
; 
 706     sp
<ResourceTypeSet
> anims
; 
 707     sp
<ResourceTypeSet
> xmls
; 
 708     sp
<ResourceTypeSet
> raws
; 
 709     sp
<ResourceTypeSet
> colors
; 
 710     sp
<ResourceTypeSet
> menus
; 
 720     assets
->setResources(resources
); 
 721     // now go through any resource overlays and collect their files 
 722     sp
<AaptAssets
> current 
= assets
->getOverlay(); 
 723     while(current
.get()) { 
 724         KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources 
=  
 725                 new KeyedVector
<String8
, sp
<ResourceTypeSet
> >; 
 726         current
->setResources(resources
); 
 727         collect_files(current
, resources
); 
 728         current 
= current
->getOverlay(); 
 730     // apply the overlay files to the base set 
 731     if (!applyFileOverlay(bundle
, assets
, drawables
, "drawable") || 
 732             !applyFileOverlay(bundle
, assets
, layouts
, "layout") || 
 733             !applyFileOverlay(bundle
, assets
, anims
, "anim") || 
 734             !applyFileOverlay(bundle
, assets
, xmls
, "xml") || 
 735             !applyFileOverlay(bundle
, assets
, raws
, "raw") || 
 736             !applyFileOverlay(bundle
, assets
, colors
, "color") || 
 737             !applyFileOverlay(bundle
, assets
, menus
, "menu")) { 
 738         return UNKNOWN_ERROR
; 
 741     bool hasErrors 
= false; 
 743     if (drawables 
!= NULL
) { 
 744         err 
= preProcessImages(bundle
, assets
, drawables
); 
 745         if (err 
== NO_ERROR
) { 
 746             err 
= makeFileResources(bundle
, assets
, &table
, drawables
, "drawable"); 
 747             if (err 
!= NO_ERROR
) { 
 755     if (layouts 
!= NULL
) { 
 756         err 
= makeFileResources(bundle
, assets
, &table
, layouts
, "layout"); 
 757         if (err 
!= NO_ERROR
) { 
 763         err 
= makeFileResources(bundle
, assets
, &table
, anims
, "anim"); 
 764         if (err 
!= NO_ERROR
) { 
 770         err 
= makeFileResources(bundle
, assets
, &table
, xmls
, "xml"); 
 771         if (err 
!= NO_ERROR
) { 
 777         err 
= makeFileResources(bundle
, assets
, &table
, raws
, "raw"); 
 778         if (err 
!= NO_ERROR
) { 
 785     while(current
.get()) { 
 786         KeyedVector
<String8
, sp
<ResourceTypeSet
> > *resources 
=  
 787                 current
->getResources(); 
 789         ssize_t index 
= resources
->indexOfKey(String8("values")); 
 791             ResourceDirIterator 
it(resources
->valueAt(index
), String8("values")); 
 793             while ((res
=it
.next()) == NO_ERROR
) { 
 794                 sp
<AaptFile
> file 
= it
.getFile(); 
 795                 res 
= compileResourceFile(bundle
, assets
, file
, it
.getParams(),  
 796                                           (current
!=assets
), &table
); 
 797                 if (res 
!= NO_ERROR
) { 
 802         current 
= current
->getOverlay(); 
 805     if (colors 
!= NULL
) { 
 806         err 
= makeFileResources(bundle
, assets
, &table
, colors
, "color"); 
 807         if (err 
!= NO_ERROR
) { 
 813         err 
= makeFileResources(bundle
, assets
, &table
, menus
, "menu"); 
 814         if (err 
!= NO_ERROR
) { 
 819     // -------------------------------------------------------------------- 
 820     // Assignment of resource IDs and initial generation of resource table. 
 821     // -------------------------------------------------------------------- 
 823     if (table
.hasResources()) { 
 824         sp
<AaptFile
> resFile(getResourceFile(assets
)); 
 825         if (resFile 
== NULL
) { 
 826             fprintf(stderr
, "Error: unable to generate entry for resource data\n"); 
 827             return UNKNOWN_ERROR
; 
 830         err 
= table
.assignResourceIds(); 
 831         if (err 
< NO_ERROR
) { 
 836     // -------------------------------------------------------------- 
 837     // Finally, we can now we can compile XML files, which may reference 
 839     // -------------------------------------------------------------- 
 841     if (layouts 
!= NULL
) { 
 842         ResourceDirIterator 
it(layouts
, String8("layout")); 
 843         while ((err
=it
.next()) == NO_ERROR
) { 
 844             String8 src 
= it
.getFile()->getPrintableSource(); 
 845             err 
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
); 
 846             if (err 
== NO_ERROR
) { 
 848                 block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true); 
 849                 checkForIds(src
, block
); 
 855         if (err 
< NO_ERROR
) { 
 862         ResourceDirIterator 
it(anims
, String8("anim")); 
 863         while ((err
=it
.next()) == NO_ERROR
) { 
 864             err 
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
); 
 865             if (err 
!= NO_ERROR
) { 
 870         if (err 
< NO_ERROR
) { 
 877         ResourceDirIterator 
it(xmls
, String8("xml")); 
 878         while ((err
=it
.next()) == NO_ERROR
) { 
 879             err 
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
); 
 880             if (err 
!= NO_ERROR
) { 
 885         if (err 
< NO_ERROR
) { 
 891     if (drawables 
!= NULL
) { 
 892         err 
= postProcessImages(assets
, &table
, drawables
); 
 893         if (err 
!= NO_ERROR
) { 
 898     if (colors 
!= NULL
) { 
 899         ResourceDirIterator 
it(colors
, String8("color")); 
 900         while ((err
=it
.next()) == NO_ERROR
) { 
 901           err 
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
); 
 902             if (err 
!= NO_ERROR
) { 
 907         if (err 
< NO_ERROR
) { 
 914         ResourceDirIterator 
it(menus
, String8("menu")); 
 915         while ((err
=it
.next()) == NO_ERROR
) { 
 916             String8 src 
= it
.getFile()->getPrintableSource(); 
 917             err 
= compileXmlFile(assets
, it
.getFile(), &table
, xmlFlags
); 
 918             if (err 
!= NO_ERROR
) { 
 922             block
.setTo(it
.getFile()->getData(), it
.getFile()->getSize(), true); 
 923             checkForIds(src
, block
); 
 926         if (err 
< NO_ERROR
) { 
 932     const sp
<AaptFile
> manifestFile(androidManifestFile
->getFiles().valueAt(0)); 
 933     String8 
manifestPath(manifestFile
->getPrintableSource()); 
 935     // Perform a basic validation of the manifest file.  This time we 
 936     // parse it with the comments intact, so that we can use them to 
 937     // generate java docs...  so we are not going to write this one 
 938     // back out to the final manifest data. 
 939     err 
= compileXmlFile(assets
, manifestFile
, &table
, 
 940             XML_COMPILE_ASSIGN_ATTRIBUTE_IDS
 
 941             | XML_COMPILE_STRIP_WHITESPACE 
| XML_COMPILE_STRIP_RAW_VALUES
); 
 942     if (err 
< NO_ERROR
) { 
 946     block
.setTo(manifestFile
->getData(), manifestFile
->getSize(), true); 
 947     String16 
manifest16("manifest"); 
 948     String16 
permission16("permission"); 
 949     String16 
permission_group16("permission-group"); 
 950     String16 
uses_permission16("uses-permission"); 
 951     String16 
instrumentation16("instrumentation"); 
 952     String16 
application16("application"); 
 953     String16 
provider16("provider"); 
 954     String16 
service16("service"); 
 955     String16 
receiver16("receiver"); 
 956     String16 
activity16("activity"); 
 957     String16 
action16("action"); 
 958     String16 
category16("category"); 
 959     String16 
data16("scheme"); 
 960     const char* packageIdentChars 
= "abcdefghijklmnopqrstuvwxyz" 
 961         "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789"; 
 962     const char* packageIdentCharsWithTheStupid 
= "abcdefghijklmnopqrstuvwxyz" 
 963         "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; 
 964     const char* classIdentChars 
= "abcdefghijklmnopqrstuvwxyz" 
 965         "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$"; 
 966     const char* processIdentChars 
= "abcdefghijklmnopqrstuvwxyz" 
 967         "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:"; 
 968     const char* authoritiesIdentChars 
= "abcdefghijklmnopqrstuvwxyz" 
 969         "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;"; 
 970     const char* typeIdentChars 
= "abcdefghijklmnopqrstuvwxyz" 
 971         "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+"; 
 972     const char* schemeIdentChars 
= "abcdefghijklmnopqrstuvwxyz" 
 973         "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; 
 974     ResXMLTree::event_code_t code
; 
 975     sp
<AaptSymbols
> permissionSymbols
; 
 976     sp
<AaptSymbols
> permissionGroupSymbols
; 
 977     while ((code
=block
.next()) != ResXMLTree::END_DOCUMENT
 
 978            && code 
> ResXMLTree::BAD_DOCUMENT
) { 
 979         if (code 
== ResXMLTree::START_TAG
) { 
 981             if (block
.getElementNamespace(&len
) != NULL
) { 
 984             if (strcmp16(block
.getElementName(&len
), manifest16
.string()) == 0) { 
 985                 if (validateAttr(manifestPath
, block
, NULL
, "package", 
 986                                  packageIdentChars
, true) != ATTR_OKAY
) { 
 989             } else if (strcmp16(block
.getElementName(&len
), permission16
.string()) == 0 
 990                     || strcmp16(block
.getElementName(&len
), permission_group16
.string()) == 0) { 
 991                 const bool isGroup 
= strcmp16(block
.getElementName(&len
), 
 992                         permission_group16
.string()) == 0; 
 993                 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name", 
 994                                  isGroup 
? packageIdentCharsWithTheStupid
 
 995                                  : packageIdentChars
, true) != ATTR_OKAY
) { 
 998                 SourcePos 
srcPos(manifestPath
, block
.getLineNumber()); 
 999                 sp
<AaptSymbols
> syms
; 
1001                     syms 
= permissionSymbols
; 
1003                         sp
<AaptSymbols
> symbols 
= 
1004                                 assets
->getSymbolsFor(String8("Manifest")); 
1005                         syms 
= permissionSymbols 
= symbols
->addNestedSymbol( 
1006                                 String8("permission"), srcPos
); 
1009                     syms 
= permissionGroupSymbols
; 
1011                         sp
<AaptSymbols
> symbols 
= 
1012                                 assets
->getSymbolsFor(String8("Manifest")); 
1013                         syms 
= permissionGroupSymbols 
= symbols
->addNestedSymbol( 
1014                                 String8("permission_group"), srcPos
); 
1018                 ssize_t index 
= block
.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE
, "name"); 
1019                 const uint16_t* id 
= block
.getAttributeStringValue(index
, &len
); 
1021                     fprintf(stderr
, "%s:%d: missing name attribute in element <%s>.\n",  
1022                             manifestPath
.string(), block
.getLineNumber(), 
1023                             String8(block
.getElementName(&len
)).string()); 
1028                 char* p 
= idStr
.lockBuffer(idStr
.size()); 
1029                 char* e 
= p 
+ idStr
.size(); 
1030                 bool begins_with_digit 
= true;  // init to true so an empty string fails 
1033                     if (*e 
>= '0' && *e 
<= '9') { 
1034                       begins_with_digit 
= true; 
1037                     if ((*e 
>= 'a' && *e 
<= 'z') || 
1038                         (*e 
>= 'A' && *e 
<= 'Z') || 
1040                       begins_with_digit 
= false; 
1043                     if (isGroup 
&& (*e 
== '-')) { 
1045                         begins_with_digit 
= false; 
1051                 idStr
.unlockBuffer(); 
1052                 // verify that we stopped because we hit a period or 
1053                 // the beginning of the string, and that the 
1054                 // identifier didn't begin with a digit. 
1055                 if (begins_with_digit 
|| (e 
!= p 
&& *(e
-1) != '.')) { 
1057                           "%s:%d: Permission name <%s> is not a valid Java symbol\n", 
1058                           manifestPath
.string(), block
.getLineNumber(), idStr
.string()); 
1061                 syms
->addStringSymbol(String8(e
), idStr
, srcPos
); 
1062                 const uint16_t* cmt 
= block
.getComment(&len
); 
1063                 if (cmt 
!= NULL 
&& *cmt 
!= 0) { 
1064                     //printf("Comment of %s: %s\n", String8(e).string(), 
1065                     //        String8(cmt).string()); 
1066                     syms
->appendComment(String8(e
), String16(cmt
), srcPos
); 
1068                     //printf("No comment for %s\n", String8(e).string()); 
1070                 syms
->makeSymbolPublic(String8(e
), srcPos
); 
1071             } else if (strcmp16(block
.getElementName(&len
), uses_permission16
.string()) == 0) { 
1072                 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name", 
1073                                  packageIdentChars
, true) != ATTR_OKAY
) { 
1076             } else if (strcmp16(block
.getElementName(&len
), instrumentation16
.string()) == 0) { 
1077                 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name", 
1078                                  classIdentChars
, true) != ATTR_OKAY
) { 
1081                 if (validateAttr(manifestPath
, block
, 
1082                                  RESOURCES_ANDROID_NAMESPACE
, "targetPackage", 
1083                                  packageIdentChars
, true) != ATTR_OKAY
) { 
1086             } else if (strcmp16(block
.getElementName(&len
), application16
.string()) == 0) { 
1087                 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name", 
1088                                  classIdentChars
, false) != ATTR_OKAY
) { 
1091                 if (validateAttr(manifestPath
, block
, 
1092                                  RESOURCES_ANDROID_NAMESPACE
, "permission", 
1093                                  packageIdentChars
, false) != ATTR_OKAY
) { 
1096                 if (validateAttr(manifestPath
, block
, 
1097                                  RESOURCES_ANDROID_NAMESPACE
, "process", 
1098                                  processIdentChars
, false) != ATTR_OKAY
) { 
1101                 if (validateAttr(manifestPath
, block
, 
1102                                  RESOURCES_ANDROID_NAMESPACE
, "taskAffinity", 
1103                                  processIdentChars
, false) != ATTR_OKAY
) { 
1106             } else if (strcmp16(block
.getElementName(&len
), provider16
.string()) == 0) { 
1107                 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name", 
1108                                  classIdentChars
, true) != ATTR_OKAY
) { 
1111                 if (validateAttr(manifestPath
, block
, 
1112                                  RESOURCES_ANDROID_NAMESPACE
, "authorities", 
1113                                  authoritiesIdentChars
, true) != ATTR_OKAY
) { 
1116                 if (validateAttr(manifestPath
, block
, 
1117                                  RESOURCES_ANDROID_NAMESPACE
, "permission", 
1118                                  packageIdentChars
, false) != ATTR_OKAY
) { 
1121                 if (validateAttr(manifestPath
, block
, 
1122                                  RESOURCES_ANDROID_NAMESPACE
, "process", 
1123                                  processIdentChars
, false) != ATTR_OKAY
) { 
1126             } else if (strcmp16(block
.getElementName(&len
), service16
.string()) == 0 
1127                        || strcmp16(block
.getElementName(&len
), receiver16
.string()) == 0 
1128                        || strcmp16(block
.getElementName(&len
), activity16
.string()) == 0) { 
1129                 if (validateAttr(manifestPath
, block
, RESOURCES_ANDROID_NAMESPACE
, "name", 
1130                                  classIdentChars
, true) != ATTR_OKAY
) { 
1133                 if (validateAttr(manifestPath
, block
, 
1134                                  RESOURCES_ANDROID_NAMESPACE
, "permission", 
1135                                  packageIdentChars
, false) != ATTR_OKAY
) { 
1138                 if (validateAttr(manifestPath
, block
, 
1139                                  RESOURCES_ANDROID_NAMESPACE
, "process", 
1140                                  processIdentChars
, false) != ATTR_OKAY
) { 
1143                 if (validateAttr(manifestPath
, block
, 
1144                                  RESOURCES_ANDROID_NAMESPACE
, "taskAffinity", 
1145                                  processIdentChars
, false) != ATTR_OKAY
) { 
1148             } else if (strcmp16(block
.getElementName(&len
), action16
.string()) == 0 
1149                        || strcmp16(block
.getElementName(&len
), category16
.string()) == 0) { 
1150                 if (validateAttr(manifestPath
, block
, 
1151                                  RESOURCES_ANDROID_NAMESPACE
, "name", 
1152                                  packageIdentChars
, true) != ATTR_OKAY
) { 
1155             } else if (strcmp16(block
.getElementName(&len
), data16
.string()) == 0) { 
1156                 if (validateAttr(manifestPath
, block
, 
1157                                  RESOURCES_ANDROID_NAMESPACE
, "mimeType", 
1158                                  typeIdentChars
, true) != ATTR_OKAY
) { 
1161                 if (validateAttr(manifestPath
, block
, 
1162                                  RESOURCES_ANDROID_NAMESPACE
, "scheme", 
1163                                  schemeIdentChars
, true) != ATTR_OKAY
) { 
1170     if (table
.validateLocalizations()) { 
1175         return UNKNOWN_ERROR
; 
1178     // Generate final compiled manifest file. 
1179     manifestFile
->clearData(); 
1180     sp
<XMLNode
> manifestTree 
= XMLNode::parse(manifestFile
); 
1181     if (manifestTree 
== NULL
) { 
1182         return UNKNOWN_ERROR
; 
1184     err 
= massageManifest(bundle
, manifestTree
); 
1185     if (err 
< NO_ERROR
) { 
1188     err 
= compileXmlFile(assets
, manifestTree
, manifestFile
, &table
); 
1189     if (err 
< NO_ERROR
) { 
1194     //printXMLBlock(&block); 
1196     // -------------------------------------------------------------- 
1197     // Generate the final resource table. 
1198     // Re-flatten because we may have added new resource IDs 
1199     // -------------------------------------------------------------- 
1201     if (table
.hasResources()) { 
1202         sp
<AaptSymbols
> symbols 
= assets
->getSymbolsFor(String8("R")); 
1203         err 
= table
.addSymbols(symbols
); 
1204         if (err 
< NO_ERROR
) { 
1208         sp
<AaptFile
> resFile(getResourceFile(assets
)); 
1209         if (resFile 
== NULL
) { 
1210             fprintf(stderr
, "Error: unable to generate entry for resource data\n"); 
1211             return UNKNOWN_ERROR
; 
1214         err 
= table
.flatten(bundle
, resFile
); 
1215         if (err 
< NO_ERROR
) { 
1219         if (bundle
->getPublicOutputFile()) { 
1220             FILE* fp 
= fopen(bundle
->getPublicOutputFile(), "w+"); 
1222                 fprintf(stderr
, "ERROR: Unable to open public definitions output file %s: %s\n", 
1223                         (const char*)bundle
->getPublicOutputFile(), strerror(errno
)); 
1224                 return UNKNOWN_ERROR
; 
1226             if (bundle
->getVerbose()) { 
1227                 printf("  Writing public definitions to %s.\n", bundle
->getPublicOutputFile()); 
1229             table
.writePublicDefinitions(String16(assets
->getPackage()), fp
); 
1235               rt
.add(resFile
->getData(), resFile
->getSize(), NULL
); 
1236               printf("Generated resources:\n"); 
1240         // These resources are now considered to be a part of the included 
1241         // resources, for others to reference. 
1242         err 
= assets
->addIncludedResources(resFile
); 
1243         if (err 
< NO_ERROR
) { 
1244             fprintf(stderr
, "ERROR: Unable to parse generated resources, aborting.\n"); 
1251 static const char* getIndentSpace(int indent
) 
1253 static const char whitespace
[] = 
1256     return whitespace 
+ sizeof(whitespace
) - 1 - indent
*4; 
1259 static status_t 
fixupSymbol(String16
* inoutSymbol
) 
1261     inoutSymbol
->replaceAll('.', '_'); 
1262     inoutSymbol
->replaceAll(':', '_'); 
1266 static String16 
getAttributeComment(const sp
<AaptAssets
>& assets
, 
1267                                     const String8
& name
, 
1268                                     String16
* outTypeComment 
= NULL
) 
1270     sp
<AaptSymbols
> asym 
= assets
->getSymbolsFor(String8("R")); 
1272         //printf("Got R symbols!\n"); 
1273         asym 
= asym
->getNestedSymbols().valueFor(String8("attr")); 
1275             //printf("Got attrs symbols! comment %s=%s\n", 
1276             //     name.string(), String8(asym->getComment(name)).string()); 
1277             if (outTypeComment 
!= NULL
) { 
1278                 *outTypeComment 
= asym
->getTypeComment(name
); 
1280             return asym
->getComment(name
); 
1286 static status_t 
writeLayoutClasses( 
1287     FILE* fp
, const sp
<AaptAssets
>& assets
, 
1288     const sp
<AaptSymbols
>& symbols
, int indent
, bool includePrivate
) 
1290     const char* indentStr 
= getIndentSpace(indent
); 
1291     if (!includePrivate
) { 
1292         fprintf(fp
, "%s/** @doconly */\n", indentStr
); 
1294     fprintf(fp
, "%spublic static final class styleable {\n", indentStr
); 
1297     String16 
attr16("attr"); 
1298     String16 
package16(assets
->getPackage()); 
1300     indentStr 
= getIndentSpace(indent
); 
1301     bool hasErrors 
= false; 
1304     size_t N 
= symbols
->getNestedSymbols().size(); 
1305     for (i
=0; i
<N
; i
++) { 
1306         sp
<AaptSymbols
> nsymbols 
= symbols
->getNestedSymbols().valueAt(i
); 
1307         String16 
nclassName16(symbols
->getNestedSymbols().keyAt(i
)); 
1308         String8 
realClassName(nclassName16
); 
1309         if (fixupSymbol(&nclassName16
) != NO_ERROR
) { 
1312         String8 
nclassName(nclassName16
); 
1314         SortedVector
<uint32_t> idents
; 
1315         Vector
<uint32_t> origOrder
; 
1316         Vector
<bool> publicFlags
; 
1319         size_t NA 
= nsymbols
->getSymbols().size(); 
1320         for (a
=0; a
<NA
; a
++) { 
1321             const AaptSymbolEntry
& sym(nsymbols
->getSymbols().valueAt(a
)); 
1322             int32_t code 
= sym
.typeCode 
== AaptSymbolEntry::TYPE_INT32
 
1324             bool isPublic 
= true; 
1326                 String16 
name16(sym
.name
); 
1327                 uint32_t typeSpecFlags
; 
1328                 code 
= assets
->getIncludedResources().identifierForName( 
1329                     name16
.string(), name16
.size(), 
1330                     attr16
.string(), attr16
.size(), 
1331                     package16
.string(), package16
.size(), &typeSpecFlags
); 
1333                     fprintf(stderr
, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n", 
1334                             nclassName
.string(), sym
.name
.string()); 
1337                 isPublic 
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0; 
1340             origOrder
.add(code
); 
1341             publicFlags
.add(isPublic
); 
1346         bool deprecated 
= false; 
1348         String16 comment 
= symbols
->getComment(realClassName
); 
1349         fprintf(fp
, "%s/** ", indentStr
); 
1350         if (comment
.size() > 0) { 
1351             String8 
cmt(comment
); 
1352             fprintf(fp
, "%s\n", cmt
.string()); 
1353             if (strstr(cmt
.string(), "@deprecated") != NULL
) { 
1357             fprintf(fp
, "Attributes that can be used with a %s.\n", nclassName
.string()); 
1359         bool hasTable 
= false; 
1360         for (a
=0; a
<NA
; a
++) { 
1361             ssize_t pos 
= idents
.indexOf(origOrder
.itemAt(a
)); 
1366                             "%s   <p>Includes the following attributes:</p>\n" 
1368                             "%s   <colgroup align=\"left\" />\n" 
1369                             "%s   <colgroup align=\"left\" />\n" 
1370                             "%s   <tr><th>Attribute</th><th>Description</th></tr>\n", 
1377                 const AaptSymbolEntry
& sym 
= nsymbols
->getSymbols().valueAt(a
); 
1378                 if (!publicFlags
.itemAt(a
) && !includePrivate
) { 
1381                 String8 
name8(sym
.name
); 
1382                 String16 
comment(sym
.comment
); 
1383                 if (comment
.size() <= 0) { 
1384                     comment 
= getAttributeComment(assets
, name8
); 
1386                 if (comment
.size() > 0) { 
1387                     const char16_t* p 
= comment
.string(); 
1388                     while (*p 
!= 0 && *p 
!= '.') { 
1390                             while (*p 
!= 0 && *p 
!= '}') { 
1400                     comment 
= String16(comment
.string(), p
-comment
.string()); 
1402                 String16 
name(name8
); 
1404                 fprintf(fp
, "%s   <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n", 
1405                         indentStr
, nclassName
.string(), 
1406                         String8(name
).string(), 
1407                         assets
->getPackage().string(), 
1408                         String8(name
).string(), 
1409                         String8(comment
).string()); 
1413             fprintf(fp
, "%s   </table>\n", indentStr
); 
1415         for (a
=0; a
<NA
; a
++) { 
1416             ssize_t pos 
= idents
.indexOf(origOrder
.itemAt(a
)); 
1418                 const AaptSymbolEntry
& sym 
= nsymbols
->getSymbols().valueAt(a
); 
1419                 if (!publicFlags
.itemAt(a
) && !includePrivate
) { 
1422                 String16 
name(sym
.name
); 
1424                 fprintf(fp
, "%s   @see #%s_%s\n", 
1425                         indentStr
, nclassName
.string(), 
1426                         String8(name
).string()); 
1429         fprintf(fp
, "%s */\n", getIndentSpace(indent
)); 
1432             fprintf(fp
, "%s@Deprecated\n", indentStr
); 
1436                 "%spublic static final int[] %s = {\n" 
1438                 indentStr
, nclassName
.string(), 
1439                 getIndentSpace(indent
+1)); 
1441         for (a
=0; a
<NA
; a
++) { 
1444                     fprintf(fp
, ",\n%s", getIndentSpace(indent
+1)); 
1449             fprintf(fp
, "0x%08x", idents
[a
]); 
1452         fprintf(fp
, "\n%s};\n", indentStr
); 
1454         for (a
=0; a
<NA
; a
++) { 
1455             ssize_t pos 
= idents
.indexOf(origOrder
.itemAt(a
)); 
1457                 const AaptSymbolEntry
& sym 
= nsymbols
->getSymbols().valueAt(a
); 
1458                 if (!publicFlags
.itemAt(a
) && !includePrivate
) { 
1461                 String8 
name8(sym
.name
); 
1462                 String16 
comment(sym
.comment
); 
1463                 String16 typeComment
; 
1464                 if (comment
.size() <= 0) { 
1465                     comment 
= getAttributeComment(assets
, name8
, &typeComment
); 
1467                     getAttributeComment(assets
, name8
, &typeComment
); 
1469                 String16 
name(name8
); 
1470                 if (fixupSymbol(&name
) != NO_ERROR
) { 
1474                 uint32_t typeSpecFlags 
= 0; 
1475                 String16 
name16(sym
.name
); 
1476                 assets
->getIncludedResources().identifierForName( 
1477                     name16
.string(), name16
.size(), 
1478                     attr16
.string(), attr16
.size(), 
1479                     package16
.string(), package16
.size(), &typeSpecFlags
); 
1480                 //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(), 
1481                 //    String8(attr16).string(), String8(name16).string(), typeSpecFlags); 
1482                 const bool pub 
= (typeSpecFlags
&ResTable_typeSpec::SPEC_PUBLIC
) != 0; 
1484                 bool deprecated 
= false; 
1486                 fprintf(fp
, "%s/**\n", indentStr
); 
1487                 if (comment
.size() > 0) { 
1488                     String8 
cmt(comment
); 
1489                     fprintf(fp
, "%s  <p>\n%s  @attr description\n", indentStr
, indentStr
); 
1490                     fprintf(fp
, "%s  %s\n", indentStr
, cmt
.string()); 
1491                     if (strstr(cmt
.string(), "@deprecated") != NULL
) { 
1496                             "%s  <p>This symbol is the offset where the {@link %s.R.attr#%s}\n" 
1497                             "%s  attribute's value can be found in the {@link #%s} array.\n", 
1499                             pub 
? assets
->getPackage().string() 
1500                                 : assets
->getSymbolsPrivatePackage().string(), 
1501                             String8(name
).string(), 
1502                             indentStr
, nclassName
.string()); 
1504                 if (typeComment
.size() > 0) { 
1505                     String8 
cmt(typeComment
); 
1506                     fprintf(fp
, "\n\n%s  %s\n", indentStr
, cmt
.string()); 
1507                     if (strstr(cmt
.string(), "@deprecated") != NULL
) { 
1511                 if (comment
.size() > 0) { 
1514                                 "%s  <p>This corresponds to the global attribute" 
1515                                 "%s  resource symbol {@link %s.R.attr#%s}.\n", 
1516                                 indentStr
, indentStr
, 
1517                                 assets
->getPackage().string(), 
1518                                 String8(name
).string()); 
1521                                 "%s  <p>This is a private symbol.\n", indentStr
); 
1524                 fprintf(fp
, "%s  @attr name %s:%s\n", indentStr
, 
1525                         "android", String8(name
).string()); 
1526                 fprintf(fp
, "%s*/\n", indentStr
); 
1528                     fprintf(fp
, "%s@Deprecated\n", indentStr
); 
1531                         "%spublic static final int %s_%s = %d;\n", 
1532                         indentStr
, nclassName
.string(), 
1533                         String8(name
).string(), (int)pos
); 
1539     fprintf(fp
, "%s};\n", getIndentSpace(indent
)); 
1540     return hasErrors 
? UNKNOWN_ERROR 
: NO_ERROR
; 
1543 static status_t 
writeSymbolClass( 
1544     FILE* fp
, const sp
<AaptAssets
>& assets
, bool includePrivate
, 
1545     const sp
<AaptSymbols
>& symbols
, const String8
& className
, int indent
) 
1547     fprintf(fp
, "%spublic %sfinal class %s {\n", 
1548             getIndentSpace(indent
), 
1549             indent 
!= 0 ? "static " : "", className
.string()); 
1553     status_t err 
= NO_ERROR
; 
1555     size_t N 
= symbols
->getSymbols().size(); 
1556     for (i
=0; i
<N
; i
++) { 
1557         const AaptSymbolEntry
& sym 
= symbols
->getSymbols().valueAt(i
); 
1558         if (sym
.typeCode 
!= AaptSymbolEntry::TYPE_INT32
) { 
1561         if (!includePrivate 
&& !sym
.isPublic
) { 
1564         String16 
name(sym
.name
); 
1565         String8 
realName(name
); 
1566         if (fixupSymbol(&name
) != NO_ERROR
) { 
1567             return UNKNOWN_ERROR
; 
1569         String16 
comment(sym
.comment
); 
1570         bool haveComment 
= false; 
1571         bool deprecated 
= false; 
1572         if (comment
.size() > 0) { 
1574             String8 
cmt(comment
); 
1577                     getIndentSpace(indent
), cmt
.string()); 
1578             if (strstr(cmt
.string(), "@deprecated") != NULL
) { 
1581         } else if (sym
.isPublic 
&& !includePrivate
) { 
1582             sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s", 
1583                 assets
->getPackage().string(), className
.string(), 
1584                 String8(sym
.name
).string()); 
1586         String16 
typeComment(sym
.typeComment
); 
1587         if (typeComment
.size() > 0) { 
1588             String8 
cmt(typeComment
); 
1592                         "%s/** %s\n", getIndentSpace(indent
), cmt
.string()); 
1595                         "%s %s\n", getIndentSpace(indent
), cmt
.string()); 
1597             if (strstr(cmt
.string(), "@deprecated") != NULL
) { 
1602             fprintf(fp
,"%s */\n", getIndentSpace(indent
)); 
1605             fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
)); 
1607         fprintf(fp
, "%spublic static final int %s=0x%08x;\n", 
1608                 getIndentSpace(indent
), 
1609                 String8(name
).string(), (int)sym
.int32Val
); 
1612     for (i
=0; i
<N
; i
++) { 
1613         const AaptSymbolEntry
& sym 
= symbols
->getSymbols().valueAt(i
); 
1614         if (sym
.typeCode 
!= AaptSymbolEntry::TYPE_STRING
) { 
1617         if (!includePrivate 
&& !sym
.isPublic
) { 
1620         String16 
name(sym
.name
); 
1621         if (fixupSymbol(&name
) != NO_ERROR
) { 
1622             return UNKNOWN_ERROR
; 
1624         String16 
comment(sym
.comment
); 
1625         bool deprecated 
= false; 
1626         if (comment
.size() > 0) { 
1627             String8 
cmt(comment
); 
1631                     getIndentSpace(indent
), cmt
.string(), 
1632                     getIndentSpace(indent
)); 
1633             if (strstr(cmt
.string(), "@deprecated") != NULL
) { 
1636         } else if (sym
.isPublic 
&& !includePrivate
) { 
1637             sym
.sourcePos
.warning("No comment for public symbol %s:%s/%s", 
1638                 assets
->getPackage().string(), className
.string(), 
1639                 String8(sym
.name
).string()); 
1642             fprintf(fp
, "%s@Deprecated\n", getIndentSpace(indent
)); 
1644         fprintf(fp
, "%spublic static final String %s=\"%s\";\n", 
1645                 getIndentSpace(indent
), 
1646                 String8(name
).string(), sym
.stringVal
.string()); 
1649     sp
<AaptSymbols
> styleableSymbols
; 
1651     N 
= symbols
->getNestedSymbols().size(); 
1652     for (i
=0; i
<N
; i
++) { 
1653         sp
<AaptSymbols
> nsymbols 
= symbols
->getNestedSymbols().valueAt(i
); 
1654         String8 
nclassName(symbols
->getNestedSymbols().keyAt(i
)); 
1655         if (nclassName 
== "styleable") { 
1656             styleableSymbols 
= nsymbols
; 
1658             err 
= writeSymbolClass(fp
, assets
, includePrivate
, nsymbols
, nclassName
, indent
); 
1660         if (err 
!= NO_ERROR
) { 
1665     if (styleableSymbols 
!= NULL
) { 
1666         err 
= writeLayoutClasses(fp
, assets
, styleableSymbols
, indent
, includePrivate
); 
1667         if (err 
!= NO_ERROR
) { 
1673     fprintf(fp
, "%s}\n", getIndentSpace(indent
)); 
1677 status_t 
writeResourceSymbols(Bundle
* bundle
, const sp
<AaptAssets
>& assets
, 
1678     const String8
& package
, bool includePrivate
) 
1680     if (!bundle
->getRClassDir()) { 
1684     const size_t N 
= assets
->getSymbols().size(); 
1685     for (size_t i
=0; i
<N
; i
++) { 
1686         sp
<AaptSymbols
> symbols 
= assets
->getSymbols().valueAt(i
); 
1687         String8 
className(assets
->getSymbols().keyAt(i
)); 
1688         String8 
dest(bundle
->getRClassDir()); 
1689         if (bundle
->getMakePackageDirs()) { 
1690             String8 
pkg(package
); 
1691             const char* last 
= pkg
.string(); 
1692             const char* s 
= last
-1; 
1695                 if (s 
> last 
&& (*s 
== '.' || *s 
== 0)) { 
1696                     String8 
part(last
, s
-last
); 
1697                     dest
.appendPath(part
); 
1698 #ifdef HAVE_MS_C_RUNTIME 
1699                     _mkdir(dest
.string()); 
1701                     mkdir(dest
.string(), S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
); 
1707         dest
.appendPath(className
); 
1708         dest
.append(".java"); 
1709         FILE* fp 
= fopen(dest
.string(), "w+"); 
1711             fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n", 
1712                     dest
.string(), strerror(errno
)); 
1713             return UNKNOWN_ERROR
; 
1715         if (bundle
->getVerbose()) { 
1716             printf("  Writing symbols for class %s.\n", className
.string()); 
1720         "/* AUTO-GENERATED FILE.  DO NOT MODIFY.\n" 
1722         " * This class was automatically generated by the\n" 
1723         " * aapt tool from the resource data it found.  It\n" 
1724         " * should not be modified by hand.\n" 
1727         "package %s;\n\n", package
.string()); 
1729         status_t err 
= writeSymbolClass(fp
, assets
, includePrivate
, symbols
, className
, 0); 
1730         if (err 
!= NO_ERROR
) { 
1741 class ProguardKeepSet
 
1744     // { rule --> { file locations } } 
1745     KeyedVector
<String8
, SortedVector
<String8
> > rules
; 
1747     void add(const String8
& rule
, const String8
& where
); 
1750 void ProguardKeepSet::add(const String8
& rule
, const String8
& where
) 
1752     ssize_t index 
= rules
.indexOfKey(rule
); 
1754         index 
= rules
.add(rule
, SortedVector
<String8
>()); 
1756     rules
.editValueAt(index
).add(where
); 
1760 writeProguardForAndroidManifest(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
) 
1765     ResXMLTree::event_code_t code
; 
1767     bool inApplication 
= false; 
1769     sp
<AaptGroup
> assGroup
; 
1770     sp
<AaptFile
> assFile
; 
1773     // First, look for a package file to parse.  This is required to 
1774     // be able to generate the resource information. 
1775     assGroup 
= assets
->getFiles().valueFor(String8("AndroidManifest.xml")); 
1776     if (assGroup 
== NULL
) { 
1777         fprintf(stderr
, "ERROR: No AndroidManifest.xml file found.\n"); 
1781     if (assGroup
->getFiles().size() != 1) { 
1782         fprintf(stderr
, "warning: Multiple AndroidManifest.xml files found, using %s\n", 
1783                 assGroup
->getFiles().valueAt(0)->getPrintableSource().string()); 
1786     assFile 
= assGroup
->getFiles().valueAt(0); 
1788     err 
= parseXMLResource(assFile
, &tree
); 
1789     if (err 
!= NO_ERROR
) { 
1795     while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT 
&& code 
!= ResXMLTree::BAD_DOCUMENT
) { 
1796         if (code 
== ResXMLTree::END_TAG
) { 
1797             if (/* name == "Application" && */ depth 
== 2) { 
1798                 inApplication 
= false; 
1803         if (code 
!= ResXMLTree::START_TAG
) { 
1807         String8 
tag(tree
.getElementName(&len
)); 
1808         // printf("Depth %d tag %s\n", depth, tag.string()); 
1809         bool keepTag 
= false; 
1811             if (tag 
!= "manifest") { 
1812                 fprintf(stderr
, "ERROR: manifest does not start with <manifest> tag\n"); 
1815             pkg 
= getAttribute(tree
, NULL
, "package", NULL
); 
1816         } else if (depth 
== 2) { 
1817             if (tag 
== "application") { 
1818                 inApplication 
= true; 
1820             } else if (tag 
== "instrumentation") { 
1824         if (!keepTag 
&& inApplication 
&& depth 
== 3) { 
1825             if (tag 
== "activity" || tag 
== "service" || tag 
== "receiver" || tag 
== "provider") { 
1830             String8 name 
= getAttribute(tree
, "http://schemas.android.com/apk/res/android", 
1833                 fprintf(stderr
, "ERROR: %s\n", error
.string()); 
1836             if (name
.length() > 0) { 
1837                 // asdf     --> package.asdf 
1838                 // .asdf  .a.b  --> package.asdf package.a.b 
1839                 // asdf.adsf --> asdf.asdf 
1840                 String8 
rule("-keep class "); 
1841                 const char* p 
= name
.string(); 
1842                 const char* q 
= strchr(p
, '.'); 
1846                 } else if (q 
== NULL
) { 
1854                 String8 location 
= tag
; 
1856                 location 
+= assFile
->getSourceFile(); 
1858                 sprintf(lineno
, ":%d", tree
.getLineNumber()); 
1861                 keep
->add(rule
, location
); 
1870 writeProguardForLayout(ProguardKeepSet
* keep
, const sp
<AaptFile
>& layoutFile
) 
1875     ResXMLTree::event_code_t code
; 
1877     err 
= parseXMLResource(layoutFile
, &tree
); 
1878     if (err 
!= NO_ERROR
) { 
1884     while ((code
=tree
.next()) != ResXMLTree::END_DOCUMENT 
&& code 
!= ResXMLTree::BAD_DOCUMENT
) { 
1885         if (code 
!= ResXMLTree::START_TAG
) { 
1888         String8 
tag(tree
.getElementName(&len
)); 
1890         // If there is no '.', we'll assume that it's one of the built in names. 
1891         if (strchr(tag
.string(), '.')) { 
1892             String8 
rule("-keep class "); 
1894             rule 
+= " { <init>(...); }"; 
1896             String8 
location("view "); 
1897             location 
+= layoutFile
->getSourceFile(); 
1899             sprintf(lineno
, ":%d", tree
.getLineNumber()); 
1902             keep
->add(rule
, location
); 
1910 writeProguardForLayouts(ProguardKeepSet
* keep
, const sp
<AaptAssets
>& assets
) 
1913     const Vector
<sp
<AaptDir
> >& dirs 
= assets
->resDirs(); 
1914     const size_t K 
= dirs
.size(); 
1915     for (size_t k
=0; k
<K
; k
++) { 
1916         const sp
<AaptDir
>& d 
= dirs
.itemAt(k
); 
1917         const String8
& dirName 
= d
->getLeaf(); 
1918         if ((dirName 
!= String8("layout")) && (strncmp(dirName
.string(), "layout-", 7) != 0)) { 
1922         const KeyedVector
<String8
,sp
<AaptGroup
> > groups 
= d
->getFiles(); 
1923         const size_t N 
= groups
.size(); 
1924         for (size_t i
=0; i
<N
; i
++) { 
1925             const sp
<AaptGroup
>& group 
= groups
.valueAt(i
); 
1926             const DefaultKeyedVector
<AaptGroupEntry
, sp
<AaptFile
> >& files 
= group
->getFiles(); 
1927             const size_t M 
= files
.size(); 
1928             for (size_t j
=0; j
<M
; j
++) { 
1929                 err 
= writeProguardForLayout(keep
, files
.valueAt(j
)); 
1940 writeProguardFile(Bundle
* bundle
, const sp
<AaptAssets
>& assets
) 
1944     if (!bundle
->getProguardFile()) { 
1948     ProguardKeepSet keep
; 
1950     err 
= writeProguardForAndroidManifest(&keep
, assets
); 
1955     err 
= writeProguardForLayouts(&keep
, assets
); 
1960     FILE* fp 
= fopen(bundle
->getProguardFile(), "w+"); 
1962         fprintf(stderr
, "ERROR: Unable to open class file %s: %s\n", 
1963                 bundle
->getProguardFile(), strerror(errno
)); 
1964         return UNKNOWN_ERROR
; 
1967     const KeyedVector
<String8
, SortedVector
<String8
> >& rules 
= keep
.rules
; 
1968     const size_t N 
= rules
.size(); 
1969     for (size_t i
=0; i
<N
; i
++) { 
1970         const SortedVector
<String8
>& locations 
= rules
.valueAt(i
); 
1971         const size_t M 
= locations
.size(); 
1972         for (size_t j
=0; j
<M
; j
++) { 
1973             fprintf(fp
, "# %s\n", locations
.itemAt(j
).string()); 
1975         fprintf(fp
, "%s\n\n", rules
.keyAt(i
).string());