The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2006 The Android Open Source Project |
| 3 | // |
| 4 | // Build resource files from raw assets. |
| 5 | // |
| 6 | #include "Main.h" |
| 7 | #include "AaptAssets.h" |
| 8 | #include "StringPool.h" |
| 9 | #include "XMLNode.h" |
| 10 | #include "ResourceTable.h" |
| 11 | #include "Images.h" |
| 12 | |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 13 | #include "CrunchCache.h" |
| 14 | #include "FileFinder.h" |
| 15 | #include "CacheUpdater.h" |
| 16 | |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 17 | #include "WorkQueue.h" |
Jeff Brown | c0f7366 | 2012-03-16 22:17:41 -0700 | [diff] [blame] | 18 | |
Raphael | f51125d | 2011-10-27 17:01:31 -0700 | [diff] [blame] | 19 | #if HAVE_PRINTF_ZD |
| 20 | # define ZD "%zd" |
| 21 | # define ZD_TYPE ssize_t |
| 22 | #else |
| 23 | # define ZD "%ld" |
| 24 | # define ZD_TYPE long |
| 25 | #endif |
| 26 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #define NOISY(x) // x |
| 28 | |
Jeff Brown | c0f7366 | 2012-03-16 22:17:41 -0700 | [diff] [blame] | 29 | // Number of threads to use for preprocessing images. |
| 30 | static const size_t MAX_THREADS = 4; |
| 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | // ========================================================================== |
| 33 | // ========================================================================== |
| 34 | // ========================================================================== |
| 35 | |
| 36 | class PackageInfo |
| 37 | { |
| 38 | public: |
| 39 | PackageInfo() |
| 40 | { |
| 41 | } |
| 42 | ~PackageInfo() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | status_t parsePackage(const sp<AaptGroup>& grp); |
| 47 | }; |
| 48 | |
| 49 | // ========================================================================== |
| 50 | // ========================================================================== |
| 51 | // ========================================================================== |
| 52 | |
| 53 | static String8 parseResourceName(const String8& leaf) |
| 54 | { |
| 55 | const char* firstDot = strchr(leaf.string(), '.'); |
| 56 | const char* str = leaf.string(); |
| 57 | |
| 58 | if (firstDot) { |
| 59 | return String8(str, firstDot-str); |
| 60 | } else { |
| 61 | return String8(str); |
| 62 | } |
| 63 | } |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | ResourceTypeSet::ResourceTypeSet() |
| 66 | :RefBase(), |
| 67 | KeyedVector<String8,sp<AaptGroup> >() |
| 68 | { |
| 69 | } |
| 70 | |
Josiah Gaskin | 9bf34ca | 2011-06-14 13:57:09 -0700 | [diff] [blame] | 71 | FilePathStore::FilePathStore() |
| 72 | :RefBase(), |
| 73 | Vector<String8>() |
| 74 | { |
| 75 | } |
| 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | class ResourceDirIterator |
| 78 | { |
| 79 | public: |
| 80 | ResourceDirIterator(const sp<ResourceTypeSet>& set, const String8& resType) |
| 81 | : mResType(resType), mSet(set), mSetPos(0), mGroupPos(0) |
| 82 | { |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 83 | memset(&mParams, 0, sizeof(ResTable_config)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | inline const sp<AaptGroup>& getGroup() const { return mGroup; } |
| 87 | inline const sp<AaptFile>& getFile() const { return mFile; } |
| 88 | |
| 89 | inline const String8& getBaseName() const { return mBaseName; } |
| 90 | inline const String8& getLeafName() const { return mLeafName; } |
| 91 | inline String8 getPath() const { return mPath; } |
| 92 | inline const ResTable_config& getParams() const { return mParams; } |
| 93 | |
| 94 | enum { |
| 95 | EOD = 1 |
| 96 | }; |
| 97 | |
| 98 | ssize_t next() |
| 99 | { |
| 100 | while (true) { |
| 101 | sp<AaptGroup> group; |
| 102 | sp<AaptFile> file; |
| 103 | |
| 104 | // Try to get next file in this current group. |
| 105 | if (mGroup != NULL && mGroupPos < mGroup->getFiles().size()) { |
| 106 | group = mGroup; |
| 107 | file = group->getFiles().valueAt(mGroupPos++); |
| 108 | |
| 109 | // Try to get the next group/file in this directory |
| 110 | } else if (mSetPos < mSet->size()) { |
| 111 | mGroup = group = mSet->valueAt(mSetPos++); |
| 112 | if (group->getFiles().size() < 1) { |
| 113 | continue; |
| 114 | } |
| 115 | file = group->getFiles().valueAt(0); |
| 116 | mGroupPos = 1; |
| 117 | |
| 118 | // All done! |
| 119 | } else { |
| 120 | return EOD; |
| 121 | } |
| 122 | |
| 123 | mFile = file; |
| 124 | |
| 125 | String8 leaf(group->getLeaf()); |
| 126 | mLeafName = String8(leaf); |
| 127 | mParams = file->getGroupEntry().toParams(); |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 128 | 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", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | group->getPath().string(), mParams.mcc, mParams.mnc, |
| 130 | mParams.language[0] ? mParams.language[0] : '-', |
| 131 | mParams.language[1] ? mParams.language[1] : '-', |
| 132 | mParams.country[0] ? mParams.country[0] : '-', |
| 133 | mParams.country[1] ? mParams.country[1] : '-', |
Tobias Haamel | 27b28b3 | 2010-02-09 23:09:17 +0100 | [diff] [blame] | 134 | mParams.orientation, mParams.uiMode, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | mParams.density, mParams.touchscreen, mParams.keyboard, |
| 136 | mParams.inputFlags, mParams.navigation)); |
| 137 | mPath = "res"; |
| 138 | mPath.appendPath(file->getGroupEntry().toDirName(mResType)); |
| 139 | mPath.appendPath(leaf); |
| 140 | mBaseName = parseResourceName(leaf); |
| 141 | if (mBaseName == "") { |
| 142 | fprintf(stderr, "Error: malformed resource filename %s\n", |
| 143 | file->getPrintableSource().string()); |
| 144 | return UNKNOWN_ERROR; |
| 145 | } |
| 146 | |
| 147 | NOISY(printf("file name=%s\n", mBaseName.string())); |
| 148 | |
| 149 | return NO_ERROR; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | private: |
| 154 | String8 mResType; |
| 155 | |
| 156 | const sp<ResourceTypeSet> mSet; |
| 157 | size_t mSetPos; |
| 158 | |
| 159 | sp<AaptGroup> mGroup; |
| 160 | size_t mGroupPos; |
| 161 | |
| 162 | sp<AaptFile> mFile; |
| 163 | String8 mBaseName; |
| 164 | String8 mLeafName; |
| 165 | String8 mPath; |
| 166 | ResTable_config mParams; |
| 167 | }; |
| 168 | |
| 169 | // ========================================================================== |
| 170 | // ========================================================================== |
| 171 | // ========================================================================== |
| 172 | |
| 173 | bool isValidResourceType(const String8& type) |
| 174 | { |
Dianne Hackborn | f31161a | 2011-01-04 21:02:48 -0800 | [diff] [blame] | 175 | return type == "anim" || type == "animator" || type == "interpolator" |
Chet Haase | d82c8ac | 2013-08-26 14:20:16 -0700 | [diff] [blame] | 176 | || type == "transition" |
Dianne Hackborn | f31161a | 2011-01-04 21:02:48 -0800 | [diff] [blame] | 177 | || type == "drawable" || type == "layout" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | || type == "values" || type == "xml" || type == "raw" |
Kenny Root | 7c71023 | 2010-11-22 22:28:37 -0800 | [diff] [blame] | 179 | || type == "color" || type == "menu" || type == "mipmap"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static sp<AaptFile> getResourceFile(const sp<AaptAssets>& assets, bool makeIfNecessary=true) |
| 183 | { |
| 184 | sp<AaptGroup> group = assets->getFiles().valueFor(String8("resources.arsc")); |
| 185 | sp<AaptFile> file; |
| 186 | if (group != NULL) { |
| 187 | file = group->getFiles().valueFor(AaptGroupEntry()); |
| 188 | if (file != NULL) { |
| 189 | return file; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | if (!makeIfNecessary) { |
| 194 | return NULL; |
| 195 | } |
| 196 | return assets->addFile(String8("resources.arsc"), AaptGroupEntry(), String8(), |
| 197 | NULL, String8()); |
| 198 | } |
| 199 | |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 200 | static status_t parsePackage(Bundle* bundle, const sp<AaptAssets>& assets, |
| 201 | const sp<AaptGroup>& grp) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | { |
| 203 | if (grp->getFiles().size() != 1) { |
Marco Nelissen | dd93186 | 2009-07-13 13:02:33 -0700 | [diff] [blame] | 204 | fprintf(stderr, "warning: Multiple AndroidManifest.xml files found, using %s\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | grp->getFiles().valueAt(0)->getPrintableSource().string()); |
| 206 | } |
| 207 | |
| 208 | sp<AaptFile> file = grp->getFiles().valueAt(0); |
| 209 | |
| 210 | ResXMLTree block; |
| 211 | status_t err = parseXMLResource(file, &block); |
| 212 | if (err != NO_ERROR) { |
| 213 | return err; |
| 214 | } |
| 215 | //printXMLBlock(&block); |
| 216 | |
| 217 | ResXMLTree::event_code_t code; |
| 218 | while ((code=block.next()) != ResXMLTree::START_TAG |
| 219 | && code != ResXMLTree::END_DOCUMENT |
| 220 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 221 | } |
| 222 | |
| 223 | size_t len; |
| 224 | if (code != ResXMLTree::START_TAG) { |
| 225 | fprintf(stderr, "%s:%d: No start tag found\n", |
| 226 | file->getPrintableSource().string(), block.getLineNumber()); |
| 227 | return UNKNOWN_ERROR; |
| 228 | } |
| 229 | if (strcmp16(block.getElementName(&len), String16("manifest").string()) != 0) { |
| 230 | fprintf(stderr, "%s:%d: Invalid start tag %s, expected <manifest>\n", |
| 231 | file->getPrintableSource().string(), block.getLineNumber(), |
| 232 | String8(block.getElementName(&len)).string()); |
| 233 | return UNKNOWN_ERROR; |
| 234 | } |
| 235 | |
| 236 | ssize_t nameIndex = block.indexOfAttribute(NULL, "package"); |
| 237 | if (nameIndex < 0) { |
| 238 | fprintf(stderr, "%s:%d: <manifest> does not have package attribute.\n", |
| 239 | file->getPrintableSource().string(), block.getLineNumber()); |
| 240 | return UNKNOWN_ERROR; |
| 241 | } |
| 242 | |
| 243 | assets->setPackage(String8(block.getAttributeStringValue(nameIndex, &len))); |
| 244 | |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 245 | String16 uses_sdk16("uses-sdk"); |
| 246 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 247 | && code != ResXMLTree::BAD_DOCUMENT) { |
| 248 | if (code == ResXMLTree::START_TAG) { |
| 249 | if (strcmp16(block.getElementName(&len), uses_sdk16.string()) == 0) { |
Kenny Root | 5a8ec76 | 2010-02-24 20:00:03 -0800 | [diff] [blame] | 250 | ssize_t minSdkIndex = block.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE, |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 251 | "minSdkVersion"); |
| 252 | if (minSdkIndex >= 0) { |
Kenny Root | 7ff20e3 | 2010-02-24 23:49:59 -0800 | [diff] [blame] | 253 | const uint16_t* minSdk16 = block.getAttributeStringValue(minSdkIndex, &len); |
| 254 | const char* minSdk8 = strdup(String8(minSdk16).string()); |
Kenny Root | 1741cd4 | 2010-03-18 12:12:11 -0700 | [diff] [blame] | 255 | bundle->setManifestMinSdkVersion(minSdk8); |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | return NO_ERROR; |
| 262 | } |
| 263 | |
| 264 | // ========================================================================== |
| 265 | // ========================================================================== |
| 266 | // ========================================================================== |
| 267 | |
| 268 | static status_t makeFileResources(Bundle* bundle, const sp<AaptAssets>& assets, |
| 269 | ResourceTable* table, |
| 270 | const sp<ResourceTypeSet>& set, |
| 271 | const char* resType) |
| 272 | { |
| 273 | String8 type8(resType); |
| 274 | String16 type16(resType); |
| 275 | |
| 276 | bool hasErrors = false; |
| 277 | |
| 278 | ResourceDirIterator it(set, String8(resType)); |
| 279 | ssize_t res; |
| 280 | while ((res=it.next()) == NO_ERROR) { |
| 281 | if (bundle->getVerbose()) { |
| 282 | printf(" (new resource id %s from %s)\n", |
| 283 | it.getBaseName().string(), it.getFile()->getPrintableSource().string()); |
| 284 | } |
| 285 | String16 baseName(it.getBaseName()); |
| 286 | const char16_t* str = baseName.string(); |
| 287 | const char16_t* const end = str + baseName.size(); |
| 288 | while (str < end) { |
| 289 | if (!((*str >= 'a' && *str <= 'z') |
| 290 | || (*str >= '0' && *str <= '9') |
| 291 | || *str == '_' || *str == '.')) { |
| 292 | fprintf(stderr, "%s: Invalid file name: must contain only [a-z0-9_.]\n", |
| 293 | it.getPath().string()); |
| 294 | hasErrors = true; |
| 295 | } |
| 296 | str++; |
| 297 | } |
| 298 | String8 resPath = it.getPath(); |
| 299 | resPath.convertToResPath(); |
| 300 | table->addEntry(SourcePos(it.getPath(), 0), String16(assets->getPackage()), |
| 301 | type16, |
| 302 | baseName, |
| 303 | String16(resPath), |
| 304 | NULL, |
| 305 | &it.getParams()); |
| 306 | assets->addResource(it.getLeafName(), resPath, it.getFile(), type8); |
| 307 | } |
| 308 | |
| 309 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; |
| 310 | } |
| 311 | |
Jeff Brown | c0f7366 | 2012-03-16 22:17:41 -0700 | [diff] [blame] | 312 | class PreProcessImageWorkUnit : public WorkQueue::WorkUnit { |
| 313 | public: |
| 314 | PreProcessImageWorkUnit(const Bundle* bundle, const sp<AaptAssets>& assets, |
| 315 | const sp<AaptFile>& file, volatile bool* hasErrors) : |
| 316 | mBundle(bundle), mAssets(assets), mFile(file), mHasErrors(hasErrors) { |
| 317 | } |
| 318 | |
| 319 | virtual bool run() { |
| 320 | status_t status = preProcessImage(mBundle, mAssets, mFile, NULL); |
| 321 | if (status) { |
| 322 | *mHasErrors = true; |
| 323 | } |
| 324 | return true; // continue even if there are errors |
| 325 | } |
| 326 | |
| 327 | private: |
| 328 | const Bundle* mBundle; |
| 329 | sp<AaptAssets> mAssets; |
| 330 | sp<AaptFile> mFile; |
| 331 | volatile bool* mHasErrors; |
| 332 | }; |
| 333 | |
| 334 | static status_t preProcessImages(const Bundle* bundle, const sp<AaptAssets>& assets, |
Kenny Root | 7c71023 | 2010-11-22 22:28:37 -0800 | [diff] [blame] | 335 | const sp<ResourceTypeSet>& set, const char* type) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | { |
Jeff Brown | c0f7366 | 2012-03-16 22:17:41 -0700 | [diff] [blame] | 337 | volatile bool hasErrors = false; |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 338 | ssize_t res = NO_ERROR; |
| 339 | if (bundle->getUseCrunchCache() == false) { |
Jeff Brown | c0f7366 | 2012-03-16 22:17:41 -0700 | [diff] [blame] | 340 | WorkQueue wq(MAX_THREADS, false); |
Xavier Ducrohet | 84be06e | 2011-07-20 17:45:11 -0700 | [diff] [blame] | 341 | ResourceDirIterator it(set, String8(type)); |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 342 | while ((res=it.next()) == NO_ERROR) { |
Jeff Brown | c0f7366 | 2012-03-16 22:17:41 -0700 | [diff] [blame] | 343 | PreProcessImageWorkUnit* w = new PreProcessImageWorkUnit( |
| 344 | bundle, assets, it.getFile(), &hasErrors); |
| 345 | status_t status = wq.schedule(w); |
| 346 | if (status) { |
| 347 | fprintf(stderr, "preProcessImages failed: schedule() returned %d\n", status); |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 348 | hasErrors = true; |
Jeff Brown | c0f7366 | 2012-03-16 22:17:41 -0700 | [diff] [blame] | 349 | delete w; |
| 350 | break; |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 351 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 | } |
Jeff Brown | c0f7366 | 2012-03-16 22:17:41 -0700 | [diff] [blame] | 353 | status_t status = wq.finish(); |
| 354 | if (status) { |
| 355 | fprintf(stderr, "preProcessImages failed: finish() returned %d\n", status); |
| 356 | hasErrors = true; |
| 357 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | } |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 359 | return (hasErrors || (res < NO_ERROR)) ? UNKNOWN_ERROR : NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | status_t postProcessImages(const sp<AaptAssets>& assets, |
| 363 | ResourceTable* table, |
| 364 | const sp<ResourceTypeSet>& set) |
| 365 | { |
| 366 | ResourceDirIterator it(set, String8("drawable")); |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 367 | bool hasErrors = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | ssize_t res; |
| 369 | while ((res=it.next()) == NO_ERROR) { |
| 370 | res = postProcessImage(assets, table, it.getFile()); |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 371 | if (res < NO_ERROR) { |
| 372 | hasErrors = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Daniel Sandler | 3547f85 | 2009-08-14 13:47:30 -0700 | [diff] [blame] | 376 | return (hasErrors || (res < NO_ERROR)) ? UNKNOWN_ERROR : NO_ERROR; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void collect_files(const sp<AaptDir>& dir, |
| 380 | KeyedVector<String8, sp<ResourceTypeSet> >* resources) |
| 381 | { |
| 382 | const DefaultKeyedVector<String8, sp<AaptGroup> >& groups = dir->getFiles(); |
| 383 | int N = groups.size(); |
| 384 | for (int i=0; i<N; i++) { |
| 385 | String8 leafName = groups.keyAt(i); |
| 386 | const sp<AaptGroup>& group = groups.valueAt(i); |
| 387 | |
| 388 | const DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> >& files |
| 389 | = group->getFiles(); |
| 390 | |
| 391 | if (files.size() == 0) { |
| 392 | continue; |
| 393 | } |
| 394 | |
| 395 | String8 resType = files.valueAt(0)->getResourceType(); |
| 396 | |
| 397 | ssize_t index = resources->indexOfKey(resType); |
| 398 | |
| 399 | if (index < 0) { |
| 400 | sp<ResourceTypeSet> set = new ResourceTypeSet(); |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 401 | NOISY(printf("Creating new resource type set for leaf %s with group %s (%p)\n", |
| 402 | leafName.string(), group->getPath().string(), group.get())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | set->add(leafName, group); |
| 404 | resources->add(resType, set); |
| 405 | } else { |
| 406 | sp<ResourceTypeSet> set = resources->valueAt(index); |
| 407 | index = set->indexOfKey(leafName); |
| 408 | if (index < 0) { |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 409 | NOISY(printf("Adding to resource type set for leaf %s group %s (%p)\n", |
| 410 | leafName.string(), group->getPath().string(), group.get())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 411 | set->add(leafName, group); |
| 412 | } else { |
| 413 | sp<AaptGroup> existingGroup = set->valueAt(index); |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 414 | NOISY(printf("Extending to resource type set for leaf %s group %s (%p)\n", |
| 415 | leafName.string(), group->getPath().string(), group.get())); |
| 416 | for (size_t j=0; j<files.size(); j++) { |
| 417 | NOISY(printf("Adding file %s in group %s resType %s\n", |
| 418 | files.valueAt(j)->getSourceFile().string(), |
| 419 | files.keyAt(j).toDirName(String8()).string(), |
| 420 | resType.string())); |
| 421 | status_t err = existingGroup->addFile(files.valueAt(j)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | static void collect_files(const sp<AaptAssets>& ass, |
| 429 | KeyedVector<String8, sp<ResourceTypeSet> >* resources) |
| 430 | { |
| 431 | const Vector<sp<AaptDir> >& dirs = ass->resDirs(); |
| 432 | int N = dirs.size(); |
| 433 | |
| 434 | for (int i=0; i<N; i++) { |
| 435 | sp<AaptDir> d = dirs.itemAt(i); |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 436 | NOISY(printf("Collecting dir #%d %p: %s, leaf %s\n", i, d.get(), d->getPath().string(), |
| 437 | d->getLeaf().string())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 438 | collect_files(d, resources); |
| 439 | |
| 440 | // don't try to include the res dir |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 441 | NOISY(printf("Removing dir leaf %s\n", d->getLeaf().string())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | ass->removeDir(d->getLeaf()); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | enum { |
| 447 | ATTR_OKAY = -1, |
| 448 | ATTR_NOT_FOUND = -2, |
| 449 | ATTR_LEADING_SPACES = -3, |
| 450 | ATTR_TRAILING_SPACES = -4 |
| 451 | }; |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 452 | static int validateAttr(const String8& path, const ResTable& table, |
| 453 | const ResXMLParser& parser, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | const char* ns, const char* attr, const char* validChars, bool required) |
| 455 | { |
| 456 | size_t len; |
| 457 | |
| 458 | ssize_t index = parser.indexOfAttribute(ns, attr); |
| 459 | const uint16_t* str; |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 460 | Res_value value; |
| 461 | if (index >= 0 && parser.getAttributeValue(index, &value) >= 0) { |
| 462 | const ResStringPool* pool = &parser.getStrings(); |
| 463 | if (value.dataType == Res_value::TYPE_REFERENCE) { |
| 464 | uint32_t specFlags = 0; |
| 465 | int strIdx; |
| 466 | if ((strIdx=table.resolveReference(&value, 0x10000000, NULL, &specFlags)) < 0) { |
| 467 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s references unknown resid 0x%08x.\n", |
| 468 | path.string(), parser.getLineNumber(), |
| 469 | String8(parser.getElementName(&len)).string(), attr, |
| 470 | value.data); |
| 471 | return ATTR_NOT_FOUND; |
| 472 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 473 | |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 474 | pool = table.getTableStringBlock(strIdx); |
| 475 | #if 0 |
| 476 | if (pool != NULL) { |
| 477 | str = pool->stringAt(value.data, &len); |
| 478 | } |
| 479 | printf("***** RES ATTR: %s specFlags=0x%x strIdx=%d: %s\n", attr, |
| 480 | specFlags, strIdx, str != NULL ? String8(str).string() : "???"); |
| 481 | #endif |
| 482 | if ((specFlags&~ResTable_typeSpec::SPEC_PUBLIC) != 0 && false) { |
| 483 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s varies by configurations 0x%x.\n", |
| 484 | path.string(), parser.getLineNumber(), |
| 485 | String8(parser.getElementName(&len)).string(), attr, |
| 486 | specFlags); |
| 487 | return ATTR_NOT_FOUND; |
| 488 | } |
| 489 | } |
| 490 | if (value.dataType == Res_value::TYPE_STRING) { |
| 491 | if (pool == NULL) { |
| 492 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s has no string block.\n", |
| 493 | path.string(), parser.getLineNumber(), |
| 494 | String8(parser.getElementName(&len)).string(), attr); |
| 495 | return ATTR_NOT_FOUND; |
| 496 | } |
| 497 | if ((str=pool->stringAt(value.data, &len)) == NULL) { |
| 498 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s has corrupt string value.\n", |
| 499 | path.string(), parser.getLineNumber(), |
| 500 | String8(parser.getElementName(&len)).string(), attr); |
| 501 | return ATTR_NOT_FOUND; |
| 502 | } |
| 503 | } else { |
| 504 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s has invalid type %d.\n", |
| 505 | path.string(), parser.getLineNumber(), |
| 506 | String8(parser.getElementName(&len)).string(), attr, |
| 507 | value.dataType); |
| 508 | return ATTR_NOT_FOUND; |
| 509 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | if (validChars) { |
| 511 | for (size_t i=0; i<len; i++) { |
| 512 | uint16_t c = str[i]; |
| 513 | const char* p = validChars; |
| 514 | bool okay = false; |
| 515 | while (*p) { |
| 516 | if (c == *p) { |
| 517 | okay = true; |
| 518 | break; |
| 519 | } |
| 520 | p++; |
| 521 | } |
| 522 | if (!okay) { |
| 523 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s has invalid character '%c'.\n", |
| 524 | path.string(), parser.getLineNumber(), |
| 525 | String8(parser.getElementName(&len)).string(), attr, (char)str[i]); |
| 526 | return (int)i; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | if (*str == ' ') { |
| 531 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s can not start with a space.\n", |
| 532 | path.string(), parser.getLineNumber(), |
| 533 | String8(parser.getElementName(&len)).string(), attr); |
| 534 | return ATTR_LEADING_SPACES; |
| 535 | } |
| 536 | if (str[len-1] == ' ') { |
| 537 | fprintf(stderr, "%s:%d: Tag <%s> attribute %s can not end with a space.\n", |
| 538 | path.string(), parser.getLineNumber(), |
| 539 | String8(parser.getElementName(&len)).string(), attr); |
| 540 | return ATTR_TRAILING_SPACES; |
| 541 | } |
| 542 | return ATTR_OKAY; |
| 543 | } |
| 544 | if (required) { |
| 545 | fprintf(stderr, "%s:%d: Tag <%s> missing required attribute %s.\n", |
| 546 | path.string(), parser.getLineNumber(), |
| 547 | String8(parser.getElementName(&len)).string(), attr); |
| 548 | return ATTR_NOT_FOUND; |
| 549 | } |
| 550 | return ATTR_OKAY; |
| 551 | } |
| 552 | |
| 553 | static void checkForIds(const String8& path, ResXMLParser& parser) |
| 554 | { |
| 555 | ResXMLTree::event_code_t code; |
| 556 | while ((code=parser.next()) != ResXMLTree::END_DOCUMENT |
| 557 | && code > ResXMLTree::BAD_DOCUMENT) { |
| 558 | if (code == ResXMLTree::START_TAG) { |
| 559 | ssize_t index = parser.indexOfAttribute(NULL, "id"); |
| 560 | if (index >= 0) { |
Marco Nelissen | dd93186 | 2009-07-13 13:02:33 -0700 | [diff] [blame] | 561 | fprintf(stderr, "%s:%d: warning: found plain 'id' attribute; did you mean the new 'android:id' name?\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | path.string(), parser.getLineNumber()); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 568 | static bool applyFileOverlay(Bundle *bundle, |
| 569 | const sp<AaptAssets>& assets, |
Xavier Ducrohet | 83f4c09 | 2010-03-04 15:21:59 -0800 | [diff] [blame] | 570 | sp<ResourceTypeSet> *baseSet, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | const char *resType) |
| 572 | { |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 573 | if (bundle->getVerbose()) { |
| 574 | printf("applyFileOverlay for %s\n", resType); |
| 575 | } |
| 576 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | // Replace any base level files in this category with any found from the overlay |
| 578 | // Also add any found only in the overlay. |
| 579 | sp<AaptAssets> overlay = assets->getOverlay(); |
| 580 | String8 resTypeString(resType); |
Robert Greenwalt | fa5c7e1 | 2009-06-05 18:53:26 -0700 | [diff] [blame] | 581 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | // work through the linked list of overlays |
| 583 | while (overlay.get()) { |
| 584 | KeyedVector<String8, sp<ResourceTypeSet> >* overlayRes = overlay->getResources(); |
| 585 | |
| 586 | // get the overlay resources of the requested type |
| 587 | ssize_t index = overlayRes->indexOfKey(resTypeString); |
| 588 | if (index >= 0) { |
| 589 | sp<ResourceTypeSet> overlaySet = overlayRes->valueAt(index); |
| 590 | |
| 591 | // for each of the resources, check for a match in the previously built |
| 592 | // non-overlay "baseset". |
| 593 | size_t overlayCount = overlaySet->size(); |
| 594 | for (size_t overlayIndex=0; overlayIndex<overlayCount; overlayIndex++) { |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 595 | if (bundle->getVerbose()) { |
| 596 | printf("trying overlaySet Key=%s\n",overlaySet->keyAt(overlayIndex).string()); |
| 597 | } |
Xavier Ducrohet | 83f4c09 | 2010-03-04 15:21:59 -0800 | [diff] [blame] | 598 | size_t baseIndex = UNKNOWN_ERROR; |
| 599 | if (baseSet->get() != NULL) { |
| 600 | baseIndex = (*baseSet)->indexOfKey(overlaySet->keyAt(overlayIndex)); |
| 601 | } |
Robert Greenwalt | fa5c7e1 | 2009-06-05 18:53:26 -0700 | [diff] [blame] | 602 | if (baseIndex < UNKNOWN_ERROR) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | // look for same flavor. For a given file (strings.xml, for example) |
| 604 | // there may be a locale specific or other flavors - we want to match |
| 605 | // the same flavor. |
| 606 | sp<AaptGroup> overlayGroup = overlaySet->valueAt(overlayIndex); |
Xavier Ducrohet | 83f4c09 | 2010-03-04 15:21:59 -0800 | [diff] [blame] | 607 | sp<AaptGroup> baseGroup = (*baseSet)->valueAt(baseIndex); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 608 | |
| 609 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > overlayFiles = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | overlayGroup->getFiles(); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 611 | if (bundle->getVerbose()) { |
| 612 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > baseFiles = |
| 613 | baseGroup->getFiles(); |
| 614 | for (size_t i=0; i < baseFiles.size(); i++) { |
Raphael | f51125d | 2011-10-27 17:01:31 -0700 | [diff] [blame] | 615 | printf("baseFile " ZD " has flavor %s\n", (ZD_TYPE) i, |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 616 | baseFiles.keyAt(i).toString().string()); |
| 617 | } |
| 618 | for (size_t i=0; i < overlayFiles.size(); i++) { |
Raphael | f51125d | 2011-10-27 17:01:31 -0700 | [diff] [blame] | 619 | printf("overlayFile " ZD " has flavor %s\n", (ZD_TYPE) i, |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 620 | overlayFiles.keyAt(i).toString().string()); |
| 621 | } |
| 622 | } |
| 623 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | size_t overlayGroupSize = overlayFiles.size(); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 625 | for (size_t overlayGroupIndex = 0; |
| 626 | overlayGroupIndex<overlayGroupSize; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | overlayGroupIndex++) { |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 628 | size_t baseFileIndex = |
| 629 | baseGroup->getFiles().indexOfKey(overlayFiles. |
| 630 | keyAt(overlayGroupIndex)); |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 631 | if (baseFileIndex < UNKNOWN_ERROR) { |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 632 | if (bundle->getVerbose()) { |
Raphael | f51125d | 2011-10-27 17:01:31 -0700 | [diff] [blame] | 633 | printf("found a match (" ZD ") for overlay file %s, for flavor %s\n", |
| 634 | (ZD_TYPE) baseFileIndex, |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 635 | overlayGroup->getLeaf().string(), |
| 636 | overlayFiles.keyAt(overlayGroupIndex).toString().string()); |
| 637 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | baseGroup->removeFile(baseFileIndex); |
| 639 | } else { |
| 640 | // didn't find a match fall through and add it.. |
Dianne Hackborn | e6b6803 | 2011-10-13 16:26:02 -0700 | [diff] [blame] | 641 | if (true || bundle->getVerbose()) { |
| 642 | printf("nothing matches overlay file %s, for flavor %s\n", |
| 643 | overlayGroup->getLeaf().string(), |
| 644 | overlayFiles.keyAt(overlayGroupIndex).toString().string()); |
| 645 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | } |
| 647 | baseGroup->addFile(overlayFiles.valueAt(overlayGroupIndex)); |
Dianne Hackborn | 64551b2 | 2009-08-15 00:00:33 -0700 | [diff] [blame] | 648 | assets->addGroupEntry(overlayFiles.keyAt(overlayGroupIndex)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | } |
| 650 | } else { |
Xavier Ducrohet | 83f4c09 | 2010-03-04 15:21:59 -0800 | [diff] [blame] | 651 | if (baseSet->get() == NULL) { |
| 652 | *baseSet = new ResourceTypeSet(); |
| 653 | assets->getResources()->add(String8(resType), *baseSet); |
| 654 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 655 | // this group doesn't exist (a file that's only in the overlay) |
Xavier Ducrohet | 83f4c09 | 2010-03-04 15:21:59 -0800 | [diff] [blame] | 656 | (*baseSet)->add(overlaySet->keyAt(overlayIndex), |
Dianne Hackborn | 58c27a0 | 2009-08-13 13:36:00 -0700 | [diff] [blame] | 657 | overlaySet->valueAt(overlayIndex)); |
Dianne Hackborn | 64551b2 | 2009-08-15 00:00:33 -0700 | [diff] [blame] | 658 | // make sure all flavors are defined in the resources. |
| 659 | sp<AaptGroup> overlayGroup = overlaySet->valueAt(overlayIndex); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 660 | DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> > overlayFiles = |
Dianne Hackborn | 64551b2 | 2009-08-15 00:00:33 -0700 | [diff] [blame] | 661 | overlayGroup->getFiles(); |
| 662 | size_t overlayGroupSize = overlayFiles.size(); |
Robert Greenwalt | 832528f | 2009-08-31 14:48:20 -0700 | [diff] [blame] | 663 | for (size_t overlayGroupIndex = 0; |
| 664 | overlayGroupIndex<overlayGroupSize; |
Dianne Hackborn | 64551b2 | 2009-08-15 00:00:33 -0700 | [diff] [blame] | 665 | overlayGroupIndex++) { |
| 666 | assets->addGroupEntry(overlayFiles.keyAt(overlayGroupIndex)); |
| 667 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | // this overlay didn't have resources for this type |
| 671 | } |
| 672 | // try next overlay |
| 673 | overlay = overlay->getOverlay(); |
| 674 | } |
Robert Greenwalt | fa5c7e1 | 2009-06-05 18:53:26 -0700 | [diff] [blame] | 675 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 678 | /* |
| 679 | * Inserts an attribute in a given node, only if the attribute does not |
| 680 | * exist. |
| 681 | * If errorOnFailedInsert is true, and the attribute already exists, returns false. |
| 682 | * Returns true otherwise, even if the attribute already exists. |
| 683 | */ |
| 684 | bool addTagAttribute(const sp<XMLNode>& node, const char* ns8, |
| 685 | const char* attr8, const char* value, bool errorOnFailedInsert) |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 686 | { |
| 687 | if (value == NULL) { |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 688 | return true; |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 689 | } |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 690 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 691 | const String16 ns(ns8); |
| 692 | const String16 attr(attr8); |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 693 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 694 | if (node->getAttribute(ns, attr) != NULL) { |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 695 | if (errorOnFailedInsert) { |
| 696 | fprintf(stderr, "Error: AndroidManifest.xml already defines %s (in %s);" |
| 697 | " cannot insert new value %s.\n", |
| 698 | String8(attr).string(), String8(ns).string(), value); |
| 699 | return false; |
| 700 | } |
| 701 | |
Kenny Root | ed98309 | 2010-03-18 14:14:49 -0700 | [diff] [blame] | 702 | fprintf(stderr, "Warning: AndroidManifest.xml already defines %s (in %s);" |
| 703 | " using existing value in manifest.\n", |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 704 | String8(attr).string(), String8(ns).string()); |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 705 | |
| 706 | // don't stop the build. |
| 707 | return true; |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 708 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 709 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 710 | node->addAttribute(ns, attr, String16(value)); |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 711 | return true; |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Dianne Hackborn | ef05e07 | 2010-03-01 17:43:39 -0800 | [diff] [blame] | 714 | static void fullyQualifyClassName(const String8& package, sp<XMLNode> node, |
| 715 | const String16& attrName) { |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 716 | XMLNode::attribute_entry* attr = node->editAttribute( |
Dianne Hackborn | ef05e07 | 2010-03-01 17:43:39 -0800 | [diff] [blame] | 717 | String16("http://schemas.android.com/apk/res/android"), attrName); |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 718 | if (attr != NULL) { |
| 719 | String8 name(attr->string); |
| 720 | |
| 721 | // asdf --> package.asdf |
| 722 | // .asdf .a.b --> package.asdf package.a.b |
| 723 | // asdf.adsf --> asdf.asdf |
| 724 | String8 className; |
| 725 | const char* p = name.string(); |
| 726 | const char* q = strchr(p, '.'); |
| 727 | if (p == q) { |
| 728 | className += package; |
| 729 | className += name; |
| 730 | } else if (q == NULL) { |
| 731 | className += package; |
| 732 | className += "."; |
| 733 | className += name; |
| 734 | } else { |
| 735 | className += name; |
| 736 | } |
| 737 | NOISY(printf("Qualifying class '%s' to '%s'", name.string(), className.string())); |
| 738 | attr->string.setTo(String16(className)); |
| 739 | } |
| 740 | } |
| 741 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 742 | status_t massageManifest(Bundle* bundle, sp<XMLNode> root) |
| 743 | { |
| 744 | root = root->searchElement(String16(), String16("manifest")); |
| 745 | if (root == NULL) { |
| 746 | fprintf(stderr, "No <manifest> tag.\n"); |
| 747 | return UNKNOWN_ERROR; |
| 748 | } |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 749 | |
| 750 | bool errorOnFailedInsert = bundle->getErrorOnFailedInsert(); |
| 751 | |
| 752 | if (!addTagAttribute(root, RESOURCES_ANDROID_NAMESPACE, "versionCode", |
| 753 | bundle->getVersionCode(), errorOnFailedInsert)) { |
| 754 | return UNKNOWN_ERROR; |
| 755 | } |
| 756 | if (!addTagAttribute(root, RESOURCES_ANDROID_NAMESPACE, "versionName", |
| 757 | bundle->getVersionName(), errorOnFailedInsert)) { |
| 758 | return UNKNOWN_ERROR; |
| 759 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 760 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 761 | if (bundle->getMinSdkVersion() != NULL |
| 762 | || bundle->getTargetSdkVersion() != NULL |
| 763 | || bundle->getMaxSdkVersion() != NULL) { |
| 764 | sp<XMLNode> vers = root->getChildElement(String16(), String16("uses-sdk")); |
| 765 | if (vers == NULL) { |
| 766 | vers = XMLNode::newElement(root->getFilename(), String16(), String16("uses-sdk")); |
| 767 | root->insertChildAt(vers, 0); |
| 768 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 769 | |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 770 | if (!addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "minSdkVersion", |
| 771 | bundle->getMinSdkVersion(), errorOnFailedInsert)) { |
| 772 | return UNKNOWN_ERROR; |
| 773 | } |
| 774 | if (!addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "targetSdkVersion", |
| 775 | bundle->getTargetSdkVersion(), errorOnFailedInsert)) { |
| 776 | return UNKNOWN_ERROR; |
| 777 | } |
| 778 | if (!addTagAttribute(vers, RESOURCES_ANDROID_NAMESPACE, "maxSdkVersion", |
| 779 | bundle->getMaxSdkVersion(), errorOnFailedInsert)) { |
| 780 | return UNKNOWN_ERROR; |
| 781 | } |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 782 | } |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 783 | |
Xavier Ducrohet | 6487b09 | 2010-08-31 10:45:31 -0700 | [diff] [blame] | 784 | if (bundle->getDebugMode()) { |
| 785 | sp<XMLNode> application = root->getChildElement(String16(), String16("application")); |
| 786 | if (application != NULL) { |
Xavier Ducrohet | 7714a24 | 2012-09-05 17:49:21 -0700 | [diff] [blame] | 787 | if (!addTagAttribute(application, RESOURCES_ANDROID_NAMESPACE, "debuggable", "true", |
| 788 | errorOnFailedInsert)) { |
| 789 | return UNKNOWN_ERROR; |
| 790 | } |
Xavier Ducrohet | 6487b09 | 2010-08-31 10:45:31 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 794 | // Deal with manifest package name overrides |
| 795 | const char* manifestPackageNameOverride = bundle->getManifestPackageNameOverride(); |
| 796 | if (manifestPackageNameOverride != NULL) { |
| 797 | // Update the actual package name |
| 798 | XMLNode::attribute_entry* attr = root->editAttribute(String16(), String16("package")); |
| 799 | if (attr == NULL) { |
| 800 | fprintf(stderr, "package name is required with --rename-manifest-package.\n"); |
| 801 | return UNKNOWN_ERROR; |
| 802 | } |
| 803 | String8 origPackage(attr->string); |
| 804 | attr->string.setTo(String16(manifestPackageNameOverride)); |
| 805 | NOISY(printf("Overriding package '%s' to be '%s'\n", origPackage.string(), manifestPackageNameOverride)); |
| 806 | |
| 807 | // Make class names fully qualified |
| 808 | sp<XMLNode> application = root->getChildElement(String16(), String16("application")); |
| 809 | if (application != NULL) { |
Dianne Hackborn | ef05e07 | 2010-03-01 17:43:39 -0800 | [diff] [blame] | 810 | fullyQualifyClassName(origPackage, application, String16("name")); |
Dianne Hackborn | b0381ef | 2010-03-03 13:36:35 -0800 | [diff] [blame] | 811 | fullyQualifyClassName(origPackage, application, String16("backupAgent")); |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 812 | |
| 813 | Vector<sp<XMLNode> >& children = const_cast<Vector<sp<XMLNode> >&>(application->getChildren()); |
| 814 | for (size_t i = 0; i < children.size(); i++) { |
| 815 | sp<XMLNode> child = children.editItemAt(i); |
| 816 | String8 tag(child->getElementName()); |
| 817 | if (tag == "activity" || tag == "service" || tag == "receiver" || tag == "provider") { |
Dianne Hackborn | ef05e07 | 2010-03-01 17:43:39 -0800 | [diff] [blame] | 818 | fullyQualifyClassName(origPackage, child, String16("name")); |
| 819 | } else if (tag == "activity-alias") { |
| 820 | fullyQualifyClassName(origPackage, child, String16("name")); |
| 821 | fullyQualifyClassName(origPackage, child, String16("targetActivity")); |
Jeff Hamilton | 2fee0ed | 2010-01-06 15:46:38 -0600 | [diff] [blame] | 822 | } |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | |
Dianne Hackborn | ef05e07 | 2010-03-01 17:43:39 -0800 | [diff] [blame] | 827 | // Deal with manifest package name overrides |
| 828 | const char* instrumentationPackageNameOverride = bundle->getInstrumentationPackageNameOverride(); |
| 829 | if (instrumentationPackageNameOverride != NULL) { |
| 830 | // Fix up instrumentation targets. |
| 831 | Vector<sp<XMLNode> >& children = const_cast<Vector<sp<XMLNode> >&>(root->getChildren()); |
| 832 | for (size_t i = 0; i < children.size(); i++) { |
| 833 | sp<XMLNode> child = children.editItemAt(i); |
| 834 | String8 tag(child->getElementName()); |
| 835 | if (tag == "instrumentation") { |
| 836 | XMLNode::attribute_entry* attr = child->editAttribute( |
| 837 | String16("http://schemas.android.com/apk/res/android"), String16("targetPackage")); |
| 838 | if (attr != NULL) { |
| 839 | attr->string.setTo(String16(instrumentationPackageNameOverride)); |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 844 | |
Dianne Hackborn | 62da846 | 2009-05-13 15:06:13 -0700 | [diff] [blame] | 845 | return NO_ERROR; |
| 846 | } |
| 847 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | #define ASSIGN_IT(n) \ |
| 849 | do { \ |
| 850 | ssize_t index = resources->indexOfKey(String8(#n)); \ |
| 851 | if (index >= 0) { \ |
| 852 | n ## s = resources->valueAt(index); \ |
| 853 | } \ |
| 854 | } while (0) |
| 855 | |
Josiah Gaskin | 8a39da8 | 2011-06-06 17:00:35 -0700 | [diff] [blame] | 856 | status_t updatePreProcessedCache(Bundle* bundle) |
| 857 | { |
| 858 | #if BENCHMARK |
| 859 | fprintf(stdout, "BENCHMARK: Starting PNG PreProcessing \n"); |
| 860 | long startPNGTime = clock(); |
| 861 | #endif /* BENCHMARK */ |
| 862 | |
| 863 | String8 source(bundle->getResourceSourceDirs()[0]); |
| 864 | String8 dest(bundle->getCrunchedOutputDir()); |
| 865 | |
| 866 | FileFinder* ff = new SystemFileFinder(); |
| 867 | CrunchCache cc(source,dest,ff); |
| 868 | |
| 869 | CacheUpdater* cu = new SystemCacheUpdater(bundle); |
| 870 | size_t numFiles = cc.crunch(cu); |
| 871 | |
| 872 | if (bundle->getVerbose()) |
| 873 | fprintf(stdout, "Crunched %d PNG files to update cache\n", (int)numFiles); |
| 874 | |
| 875 | delete ff; |
| 876 | delete cu; |
| 877 | |
| 878 | #if BENCHMARK |
| 879 | fprintf(stdout, "BENCHMARK: End PNG PreProcessing. Time Elapsed: %f ms \n" |
| 880 | ,(clock() - startPNGTime)/1000.0); |
| 881 | #endif /* BENCHMARK */ |
| 882 | return 0; |
| 883 | } |
| 884 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | status_t buildResources(Bundle* bundle, const sp<AaptAssets>& assets) |
| 886 | { |
| 887 | // First, look for a package file to parse. This is required to |
| 888 | // be able to generate the resource information. |
| 889 | sp<AaptGroup> androidManifestFile = |
| 890 | assets->getFiles().valueFor(String8("AndroidManifest.xml")); |
| 891 | if (androidManifestFile == NULL) { |
| 892 | fprintf(stderr, "ERROR: No AndroidManifest.xml file found.\n"); |
| 893 | return UNKNOWN_ERROR; |
| 894 | } |
| 895 | |
Kenny Root | b5ef7ee | 2009-12-10 13:52:53 -0800 | [diff] [blame] | 896 | status_t err = parsePackage(bundle, assets, androidManifestFile); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | if (err != NO_ERROR) { |
| 898 | return err; |
| 899 | } |
| 900 | |
| 901 | NOISY(printf("Creating resources for package %s\n", |
| 902 | assets->getPackage().string())); |
| 903 | |
| 904 | ResourceTable table(bundle, String16(assets->getPackage())); |
| 905 | err = table.addIncludedResources(bundle, assets); |
| 906 | if (err != NO_ERROR) { |
| 907 | return err; |
| 908 | } |
| 909 | |
| 910 | NOISY(printf("Found %d included resource packages\n", (int)table.size())); |
| 911 | |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 912 | // Standard flags for compiled XML and optional UTF-8 encoding |
| 913 | int xmlFlags = XML_COMPILE_STANDARD_RESOURCE; |
Kenny Root | 1741cd4 | 2010-03-18 12:12:11 -0700 | [diff] [blame] | 914 | |
| 915 | /* Only enable UTF-8 if the caller of aapt didn't specifically |
| 916 | * request UTF-16 encoding and the parameters of this package |
| 917 | * allow UTF-8 to be used. |
| 918 | */ |
Dianne Hackborn | 6c997a9 | 2012-01-31 11:27:43 -0800 | [diff] [blame] | 919 | if (!bundle->getUTF16StringsOption()) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 920 | xmlFlags |= XML_COMPILE_UTF8; |
| 921 | } |
| 922 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | // -------------------------------------------------------------- |
| 924 | // First, gather all resource information. |
| 925 | // -------------------------------------------------------------- |
| 926 | |
| 927 | // resType -> leafName -> group |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 928 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | new KeyedVector<String8, sp<ResourceTypeSet> >; |
| 930 | collect_files(assets, resources); |
| 931 | |
| 932 | sp<ResourceTypeSet> drawables; |
| 933 | sp<ResourceTypeSet> layouts; |
| 934 | sp<ResourceTypeSet> anims; |
Dianne Hackborn | f31161a | 2011-01-04 21:02:48 -0800 | [diff] [blame] | 935 | sp<ResourceTypeSet> animators; |
| 936 | sp<ResourceTypeSet> interpolators; |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 937 | sp<ResourceTypeSet> transitions; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 938 | sp<ResourceTypeSet> xmls; |
| 939 | sp<ResourceTypeSet> raws; |
| 940 | sp<ResourceTypeSet> colors; |
| 941 | sp<ResourceTypeSet> menus; |
Kenny Root | 7c71023 | 2010-11-22 22:28:37 -0800 | [diff] [blame] | 942 | sp<ResourceTypeSet> mipmaps; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | |
| 944 | ASSIGN_IT(drawable); |
| 945 | ASSIGN_IT(layout); |
| 946 | ASSIGN_IT(anim); |
Dianne Hackborn | f31161a | 2011-01-04 21:02:48 -0800 | [diff] [blame] | 947 | ASSIGN_IT(animator); |
| 948 | ASSIGN_IT(interpolator); |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 949 | ASSIGN_IT(transition); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | ASSIGN_IT(xml); |
| 951 | ASSIGN_IT(raw); |
| 952 | ASSIGN_IT(color); |
| 953 | ASSIGN_IT(menu); |
Kenny Root | 7c71023 | 2010-11-22 22:28:37 -0800 | [diff] [blame] | 954 | ASSIGN_IT(mipmap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 955 | |
| 956 | assets->setResources(resources); |
| 957 | // now go through any resource overlays and collect their files |
| 958 | sp<AaptAssets> current = assets->getOverlay(); |
| 959 | while(current.get()) { |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 960 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | new KeyedVector<String8, sp<ResourceTypeSet> >; |
| 962 | current->setResources(resources); |
| 963 | collect_files(current, resources); |
| 964 | current = current->getOverlay(); |
| 965 | } |
| 966 | // apply the overlay files to the base set |
Xavier Ducrohet | 83f4c09 | 2010-03-04 15:21:59 -0800 | [diff] [blame] | 967 | if (!applyFileOverlay(bundle, assets, &drawables, "drawable") || |
| 968 | !applyFileOverlay(bundle, assets, &layouts, "layout") || |
| 969 | !applyFileOverlay(bundle, assets, &anims, "anim") || |
Dianne Hackborn | f31161a | 2011-01-04 21:02:48 -0800 | [diff] [blame] | 970 | !applyFileOverlay(bundle, assets, &animators, "animator") || |
| 971 | !applyFileOverlay(bundle, assets, &interpolators, "interpolator") || |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 972 | !applyFileOverlay(bundle, assets, &transitions, "transition") || |
Xavier Ducrohet | 83f4c09 | 2010-03-04 15:21:59 -0800 | [diff] [blame] | 973 | !applyFileOverlay(bundle, assets, &xmls, "xml") || |
| 974 | !applyFileOverlay(bundle, assets, &raws, "raw") || |
| 975 | !applyFileOverlay(bundle, assets, &colors, "color") || |
Kenny Root | 7c71023 | 2010-11-22 22:28:37 -0800 | [diff] [blame] | 976 | !applyFileOverlay(bundle, assets, &menus, "menu") || |
| 977 | !applyFileOverlay(bundle, assets, &mipmaps, "mipmap")) { |
Robert Greenwalt | fa5c7e1 | 2009-06-05 18:53:26 -0700 | [diff] [blame] | 978 | return UNKNOWN_ERROR; |
| 979 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | |
| 981 | bool hasErrors = false; |
| 982 | |
| 983 | if (drawables != NULL) { |
Anthony Newnam | 578a57f | 2010-09-01 12:06:04 -0500 | [diff] [blame] | 984 | if (bundle->getOutputAPKFile() != NULL) { |
Kenny Root | 7c71023 | 2010-11-22 22:28:37 -0800 | [diff] [blame] | 985 | err = preProcessImages(bundle, assets, drawables, "drawable"); |
Anthony Newnam | 578a57f | 2010-09-01 12:06:04 -0500 | [diff] [blame] | 986 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | if (err == NO_ERROR) { |
| 988 | err = makeFileResources(bundle, assets, &table, drawables, "drawable"); |
| 989 | if (err != NO_ERROR) { |
| 990 | hasErrors = true; |
| 991 | } |
| 992 | } else { |
| 993 | hasErrors = true; |
| 994 | } |
| 995 | } |
| 996 | |
Kenny Root | 7c71023 | 2010-11-22 22:28:37 -0800 | [diff] [blame] | 997 | if (mipmaps != NULL) { |
| 998 | if (bundle->getOutputAPKFile() != NULL) { |
| 999 | err = preProcessImages(bundle, assets, mipmaps, "mipmap"); |
| 1000 | } |
| 1001 | if (err == NO_ERROR) { |
| 1002 | err = makeFileResources(bundle, assets, &table, mipmaps, "mipmap"); |
| 1003 | if (err != NO_ERROR) { |
| 1004 | hasErrors = true; |
| 1005 | } |
| 1006 | } else { |
| 1007 | hasErrors = true; |
| 1008 | } |
| 1009 | } |
| 1010 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | if (layouts != NULL) { |
| 1012 | err = makeFileResources(bundle, assets, &table, layouts, "layout"); |
| 1013 | if (err != NO_ERROR) { |
| 1014 | hasErrors = true; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | if (anims != NULL) { |
| 1019 | err = makeFileResources(bundle, assets, &table, anims, "anim"); |
| 1020 | if (err != NO_ERROR) { |
| 1021 | hasErrors = true; |
| 1022 | } |
| 1023 | } |
| 1024 | |
Dianne Hackborn | f31161a | 2011-01-04 21:02:48 -0800 | [diff] [blame] | 1025 | if (animators != NULL) { |
| 1026 | err = makeFileResources(bundle, assets, &table, animators, "animator"); |
| 1027 | if (err != NO_ERROR) { |
| 1028 | hasErrors = true; |
| 1029 | } |
| 1030 | } |
| 1031 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 1032 | if (transitions != NULL) { |
| 1033 | err = makeFileResources(bundle, assets, &table, transitions, "transition"); |
| 1034 | if (err != NO_ERROR) { |
| 1035 | hasErrors = true; |
| 1036 | } |
| 1037 | } |
| 1038 | |
Dianne Hackborn | f31161a | 2011-01-04 21:02:48 -0800 | [diff] [blame] | 1039 | if (interpolators != NULL) { |
| 1040 | err = makeFileResources(bundle, assets, &table, interpolators, "interpolator"); |
| 1041 | if (err != NO_ERROR) { |
| 1042 | hasErrors = true; |
| 1043 | } |
| 1044 | } |
| 1045 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | if (xmls != NULL) { |
| 1047 | err = makeFileResources(bundle, assets, &table, xmls, "xml"); |
| 1048 | if (err != NO_ERROR) { |
| 1049 | hasErrors = true; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | if (raws != NULL) { |
| 1054 | err = makeFileResources(bundle, assets, &table, raws, "raw"); |
| 1055 | if (err != NO_ERROR) { |
| 1056 | hasErrors = true; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | // compile resources |
| 1061 | current = assets; |
| 1062 | while(current.get()) { |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1063 | KeyedVector<String8, sp<ResourceTypeSet> > *resources = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | current->getResources(); |
| 1065 | |
| 1066 | ssize_t index = resources->indexOfKey(String8("values")); |
| 1067 | if (index >= 0) { |
| 1068 | ResourceDirIterator it(resources->valueAt(index), String8("values")); |
| 1069 | ssize_t res; |
| 1070 | while ((res=it.next()) == NO_ERROR) { |
| 1071 | sp<AaptFile> file = it.getFile(); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1072 | res = compileResourceFile(bundle, assets, file, it.getParams(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | (current!=assets), &table); |
| 1074 | if (res != NO_ERROR) { |
| 1075 | hasErrors = true; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | current = current->getOverlay(); |
| 1080 | } |
| 1081 | |
| 1082 | if (colors != NULL) { |
| 1083 | err = makeFileResources(bundle, assets, &table, colors, "color"); |
| 1084 | if (err != NO_ERROR) { |
| 1085 | hasErrors = true; |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | if (menus != NULL) { |
| 1090 | err = makeFileResources(bundle, assets, &table, menus, "menu"); |
| 1091 | if (err != NO_ERROR) { |
| 1092 | hasErrors = true; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | // -------------------------------------------------------------------- |
| 1097 | // Assignment of resource IDs and initial generation of resource table. |
| 1098 | // -------------------------------------------------------------------- |
| 1099 | |
| 1100 | if (table.hasResources()) { |
| 1101 | sp<AaptFile> resFile(getResourceFile(assets)); |
| 1102 | if (resFile == NULL) { |
| 1103 | fprintf(stderr, "Error: unable to generate entry for resource data\n"); |
| 1104 | return UNKNOWN_ERROR; |
| 1105 | } |
| 1106 | |
| 1107 | err = table.assignResourceIds(); |
| 1108 | if (err < NO_ERROR) { |
| 1109 | return err; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | // -------------------------------------------------------------- |
| 1114 | // Finally, we can now we can compile XML files, which may reference |
| 1115 | // resources. |
| 1116 | // -------------------------------------------------------------- |
| 1117 | |
| 1118 | if (layouts != NULL) { |
| 1119 | ResourceDirIterator it(layouts, String8("layout")); |
| 1120 | while ((err=it.next()) == NO_ERROR) { |
| 1121 | String8 src = it.getFile()->getPrintableSource(); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 1122 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1123 | if (err == NO_ERROR) { |
| 1124 | ResXMLTree block; |
| 1125 | block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); |
| 1126 | checkForIds(src, block); |
| 1127 | } else { |
| 1128 | hasErrors = true; |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | if (err < NO_ERROR) { |
| 1133 | hasErrors = true; |
| 1134 | } |
| 1135 | err = NO_ERROR; |
| 1136 | } |
| 1137 | |
| 1138 | if (anims != NULL) { |
| 1139 | ResourceDirIterator it(anims, String8("anim")); |
| 1140 | while ((err=it.next()) == NO_ERROR) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 1141 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1142 | if (err != NO_ERROR) { |
| 1143 | hasErrors = true; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | if (err < NO_ERROR) { |
| 1148 | hasErrors = true; |
| 1149 | } |
| 1150 | err = NO_ERROR; |
| 1151 | } |
| 1152 | |
Dianne Hackborn | f31161a | 2011-01-04 21:02:48 -0800 | [diff] [blame] | 1153 | if (animators != NULL) { |
| 1154 | ResourceDirIterator it(animators, String8("animator")); |
| 1155 | while ((err=it.next()) == NO_ERROR) { |
| 1156 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
| 1157 | if (err != NO_ERROR) { |
| 1158 | hasErrors = true; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | if (err < NO_ERROR) { |
| 1163 | hasErrors = true; |
| 1164 | } |
| 1165 | err = NO_ERROR; |
| 1166 | } |
| 1167 | |
| 1168 | if (interpolators != NULL) { |
| 1169 | ResourceDirIterator it(interpolators, String8("interpolator")); |
| 1170 | while ((err=it.next()) == NO_ERROR) { |
| 1171 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
| 1172 | if (err != NO_ERROR) { |
| 1173 | hasErrors = true; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | if (err < NO_ERROR) { |
| 1178 | hasErrors = true; |
| 1179 | } |
| 1180 | err = NO_ERROR; |
| 1181 | } |
| 1182 | |
Chet Haase | faebd8f | 2012-05-18 14:17:57 -0700 | [diff] [blame] | 1183 | if (transitions != NULL) { |
| 1184 | ResourceDirIterator it(transitions, String8("transition")); |
| 1185 | while ((err=it.next()) == NO_ERROR) { |
| 1186 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
| 1187 | if (err != NO_ERROR) { |
| 1188 | hasErrors = true; |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | if (err < NO_ERROR) { |
| 1193 | hasErrors = true; |
| 1194 | } |
| 1195 | err = NO_ERROR; |
| 1196 | } |
| 1197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | if (xmls != NULL) { |
| 1199 | ResourceDirIterator it(xmls, String8("xml")); |
| 1200 | while ((err=it.next()) == NO_ERROR) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 1201 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1202 | if (err != NO_ERROR) { |
| 1203 | hasErrors = true; |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | if (err < NO_ERROR) { |
| 1208 | hasErrors = true; |
| 1209 | } |
| 1210 | err = NO_ERROR; |
| 1211 | } |
| 1212 | |
| 1213 | if (drawables != NULL) { |
| 1214 | err = postProcessImages(assets, &table, drawables); |
| 1215 | if (err != NO_ERROR) { |
| 1216 | hasErrors = true; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | if (colors != NULL) { |
| 1221 | ResourceDirIterator it(colors, String8("color")); |
| 1222 | while ((err=it.next()) == NO_ERROR) { |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 1223 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | if (err != NO_ERROR) { |
| 1225 | hasErrors = true; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | if (err < NO_ERROR) { |
| 1230 | hasErrors = true; |
| 1231 | } |
| 1232 | err = NO_ERROR; |
| 1233 | } |
| 1234 | |
| 1235 | if (menus != NULL) { |
| 1236 | ResourceDirIterator it(menus, String8("menu")); |
| 1237 | while ((err=it.next()) == NO_ERROR) { |
| 1238 | String8 src = it.getFile()->getPrintableSource(); |
Kenny Root | 1913846 | 2009-12-04 09:38:48 -0800 | [diff] [blame] | 1239 | err = compileXmlFile(assets, it.getFile(), &table, xmlFlags); |
Adam Lesinski | f2d2c87 | 2014-04-08 12:01:38 -0700 | [diff] [blame] | 1240 | if (err == NO_ERROR) { |
| 1241 | ResXMLTree block; |
| 1242 | block.setTo(it.getFile()->getData(), it.getFile()->getSize(), true); |
| 1243 | checkForIds(src, block); |
| 1244 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | hasErrors = true; |
| 1246 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | if (err < NO_ERROR) { |
| 1250 | hasErrors = true; |
| 1251 | } |
| 1252 | err = NO_ERROR; |
| 1253 | } |
| 1254 | |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1255 | if (table.validateLocalizations()) { |
| 1256 | hasErrors = true; |
| 1257 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1258 | |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1259 | if (hasErrors) { |
| 1260 | return UNKNOWN_ERROR; |
| 1261 | } |
| 1262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | const sp<AaptFile> manifestFile(androidManifestFile->getFiles().valueAt(0)); |
| 1264 | String8 manifestPath(manifestFile->getPrintableSource()); |
| 1265 | |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1266 | // Generate final compiled manifest file. |
| 1267 | manifestFile->clearData(); |
| 1268 | sp<XMLNode> manifestTree = XMLNode::parse(manifestFile); |
| 1269 | if (manifestTree == NULL) { |
| 1270 | return UNKNOWN_ERROR; |
| 1271 | } |
| 1272 | err = massageManifest(bundle, manifestTree); |
| 1273 | if (err < NO_ERROR) { |
| 1274 | return err; |
| 1275 | } |
| 1276 | err = compileXmlFile(assets, manifestTree, manifestFile, &table); |
| 1277 | if (err < NO_ERROR) { |
| 1278 | return err; |
| 1279 | } |
| 1280 | |
| 1281 | //block.restart(); |
| 1282 | //printXMLBlock(&block); |
| 1283 | |
| 1284 | // -------------------------------------------------------------- |
| 1285 | // Generate the final resource table. |
| 1286 | // Re-flatten because we may have added new resource IDs |
| 1287 | // -------------------------------------------------------------- |
| 1288 | |
| 1289 | ResTable finalResTable; |
| 1290 | sp<AaptFile> resFile; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1291 | |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1292 | if (table.hasResources()) { |
| 1293 | sp<AaptSymbols> symbols = assets->getSymbolsFor(String8("R")); |
| 1294 | err = table.addSymbols(symbols); |
| 1295 | if (err < NO_ERROR) { |
| 1296 | return err; |
| 1297 | } |
| 1298 | |
| 1299 | resFile = getResourceFile(assets); |
| 1300 | if (resFile == NULL) { |
| 1301 | fprintf(stderr, "Error: unable to generate entry for resource data\n"); |
| 1302 | return UNKNOWN_ERROR; |
| 1303 | } |
| 1304 | |
| 1305 | err = table.flatten(bundle, resFile); |
| 1306 | if (err < NO_ERROR) { |
| 1307 | return err; |
| 1308 | } |
| 1309 | |
| 1310 | if (bundle->getPublicOutputFile()) { |
| 1311 | FILE* fp = fopen(bundle->getPublicOutputFile(), "w+"); |
| 1312 | if (fp == NULL) { |
| 1313 | fprintf(stderr, "ERROR: Unable to open public definitions output file %s: %s\n", |
| 1314 | (const char*)bundle->getPublicOutputFile(), strerror(errno)); |
| 1315 | return UNKNOWN_ERROR; |
| 1316 | } |
| 1317 | if (bundle->getVerbose()) { |
| 1318 | printf(" Writing public definitions to %s.\n", bundle->getPublicOutputFile()); |
| 1319 | } |
| 1320 | table.writePublicDefinitions(String16(assets->getPackage()), fp); |
| 1321 | fclose(fp); |
| 1322 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1323 | |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1324 | // Read resources back in, |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 1325 | finalResTable.add(resFile->getData(), resFile->getSize()); |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1326 | #if 0 |
| 1327 | NOISY( |
| 1328 | printf("Generated resources:\n"); |
| 1329 | finalResTable.print(); |
| 1330 | ) |
| 1331 | #endif |
| 1332 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1333 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | // Perform a basic validation of the manifest file. This time we |
| 1335 | // parse it with the comments intact, so that we can use them to |
| 1336 | // generate java docs... so we are not going to write this one |
| 1337 | // back out to the final manifest data. |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1338 | sp<AaptFile> outManifestFile = new AaptFile(manifestFile->getSourceFile(), |
| 1339 | manifestFile->getGroupEntry(), |
| 1340 | manifestFile->getResourceType()); |
| 1341 | err = compileXmlFile(assets, manifestFile, |
| 1342 | outManifestFile, &table, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | XML_COMPILE_ASSIGN_ATTRIBUTE_IDS |
| 1344 | | XML_COMPILE_STRIP_WHITESPACE | XML_COMPILE_STRIP_RAW_VALUES); |
| 1345 | if (err < NO_ERROR) { |
| 1346 | return err; |
| 1347 | } |
| 1348 | ResXMLTree block; |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1349 | block.setTo(outManifestFile->getData(), outManifestFile->getSize(), true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | String16 manifest16("manifest"); |
| 1351 | String16 permission16("permission"); |
| 1352 | String16 permission_group16("permission-group"); |
| 1353 | String16 uses_permission16("uses-permission"); |
| 1354 | String16 instrumentation16("instrumentation"); |
| 1355 | String16 application16("application"); |
| 1356 | String16 provider16("provider"); |
| 1357 | String16 service16("service"); |
| 1358 | String16 receiver16("receiver"); |
| 1359 | String16 activity16("activity"); |
| 1360 | String16 action16("action"); |
| 1361 | String16 category16("category"); |
| 1362 | String16 data16("scheme"); |
| 1363 | const char* packageIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 1364 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789"; |
| 1365 | const char* packageIdentCharsWithTheStupid = "abcdefghijklmnopqrstuvwxyz" |
| 1366 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; |
| 1367 | const char* classIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 1368 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789$"; |
| 1369 | const char* processIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 1370 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:"; |
| 1371 | const char* authoritiesIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 1372 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-:;"; |
| 1373 | const char* typeIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 1374 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789:-/*+"; |
| 1375 | const char* schemeIdentChars = "abcdefghijklmnopqrstuvwxyz" |
| 1376 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ._0123456789-"; |
| 1377 | ResXMLTree::event_code_t code; |
| 1378 | sp<AaptSymbols> permissionSymbols; |
| 1379 | sp<AaptSymbols> permissionGroupSymbols; |
| 1380 | while ((code=block.next()) != ResXMLTree::END_DOCUMENT |
| 1381 | && code > ResXMLTree::BAD_DOCUMENT) { |
| 1382 | if (code == ResXMLTree::START_TAG) { |
| 1383 | size_t len; |
| 1384 | if (block.getElementNamespace(&len) != NULL) { |
| 1385 | continue; |
| 1386 | } |
| 1387 | if (strcmp16(block.getElementName(&len), manifest16.string()) == 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1388 | if (validateAttr(manifestPath, finalResTable, block, NULL, "package", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | packageIdentChars, true) != ATTR_OKAY) { |
| 1390 | hasErrors = true; |
| 1391 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1392 | if (validateAttr(manifestPath, finalResTable, block, RESOURCES_ANDROID_NAMESPACE, |
| 1393 | "sharedUserId", packageIdentChars, false) != ATTR_OKAY) { |
| 1394 | hasErrors = true; |
| 1395 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | } else if (strcmp16(block.getElementName(&len), permission16.string()) == 0 |
| 1397 | || strcmp16(block.getElementName(&len), permission_group16.string()) == 0) { |
| 1398 | const bool isGroup = strcmp16(block.getElementName(&len), |
| 1399 | permission_group16.string()) == 0; |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1400 | if (validateAttr(manifestPath, finalResTable, block, RESOURCES_ANDROID_NAMESPACE, |
| 1401 | "name", isGroup ? packageIdentCharsWithTheStupid |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | : packageIdentChars, true) != ATTR_OKAY) { |
| 1403 | hasErrors = true; |
| 1404 | } |
| 1405 | SourcePos srcPos(manifestPath, block.getLineNumber()); |
| 1406 | sp<AaptSymbols> syms; |
| 1407 | if (!isGroup) { |
| 1408 | syms = permissionSymbols; |
| 1409 | if (syms == NULL) { |
| 1410 | sp<AaptSymbols> symbols = |
| 1411 | assets->getSymbolsFor(String8("Manifest")); |
| 1412 | syms = permissionSymbols = symbols->addNestedSymbol( |
| 1413 | String8("permission"), srcPos); |
| 1414 | } |
| 1415 | } else { |
| 1416 | syms = permissionGroupSymbols; |
| 1417 | if (syms == NULL) { |
| 1418 | sp<AaptSymbols> symbols = |
| 1419 | assets->getSymbolsFor(String8("Manifest")); |
| 1420 | syms = permissionGroupSymbols = symbols->addNestedSymbol( |
| 1421 | String8("permission_group"), srcPos); |
| 1422 | } |
| 1423 | } |
| 1424 | size_t len; |
| 1425 | ssize_t index = block.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE, "name"); |
| 1426 | const uint16_t* id = block.getAttributeStringValue(index, &len); |
| 1427 | if (id == NULL) { |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1428 | fprintf(stderr, "%s:%d: missing name attribute in element <%s>.\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | manifestPath.string(), block.getLineNumber(), |
| 1430 | String8(block.getElementName(&len)).string()); |
| 1431 | hasErrors = true; |
| 1432 | break; |
| 1433 | } |
| 1434 | String8 idStr(id); |
| 1435 | char* p = idStr.lockBuffer(idStr.size()); |
| 1436 | char* e = p + idStr.size(); |
| 1437 | bool begins_with_digit = true; // init to true so an empty string fails |
| 1438 | while (e > p) { |
| 1439 | e--; |
| 1440 | if (*e >= '0' && *e <= '9') { |
| 1441 | begins_with_digit = true; |
| 1442 | continue; |
| 1443 | } |
| 1444 | if ((*e >= 'a' && *e <= 'z') || |
| 1445 | (*e >= 'A' && *e <= 'Z') || |
| 1446 | (*e == '_')) { |
| 1447 | begins_with_digit = false; |
| 1448 | continue; |
| 1449 | } |
| 1450 | if (isGroup && (*e == '-')) { |
| 1451 | *e = '_'; |
| 1452 | begins_with_digit = false; |
| 1453 | continue; |
| 1454 | } |
| 1455 | e++; |
| 1456 | break; |
| 1457 | } |
| 1458 | idStr.unlockBuffer(); |
| 1459 | // verify that we stopped because we hit a period or |
| 1460 | // the beginning of the string, and that the |
| 1461 | // identifier didn't begin with a digit. |
| 1462 | if (begins_with_digit || (e != p && *(e-1) != '.')) { |
| 1463 | fprintf(stderr, |
| 1464 | "%s:%d: Permission name <%s> is not a valid Java symbol\n", |
| 1465 | manifestPath.string(), block.getLineNumber(), idStr.string()); |
| 1466 | hasErrors = true; |
| 1467 | } |
| 1468 | syms->addStringSymbol(String8(e), idStr, srcPos); |
| 1469 | const uint16_t* cmt = block.getComment(&len); |
| 1470 | if (cmt != NULL && *cmt != 0) { |
| 1471 | //printf("Comment of %s: %s\n", String8(e).string(), |
| 1472 | // String8(cmt).string()); |
| 1473 | syms->appendComment(String8(e), String16(cmt), srcPos); |
| 1474 | } else { |
| 1475 | //printf("No comment for %s\n", String8(e).string()); |
| 1476 | } |
| 1477 | syms->makeSymbolPublic(String8(e), srcPos); |
| 1478 | } else if (strcmp16(block.getElementName(&len), uses_permission16.string()) == 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1479 | if (validateAttr(manifestPath, finalResTable, block, RESOURCES_ANDROID_NAMESPACE, |
| 1480 | "name", packageIdentChars, true) != ATTR_OKAY) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1481 | hasErrors = true; |
| 1482 | } |
| 1483 | } else if (strcmp16(block.getElementName(&len), instrumentation16.string()) == 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1484 | if (validateAttr(manifestPath, finalResTable, block, RESOURCES_ANDROID_NAMESPACE, |
| 1485 | "name", classIdentChars, true) != ATTR_OKAY) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1486 | hasErrors = true; |
| 1487 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1488 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1489 | RESOURCES_ANDROID_NAMESPACE, "targetPackage", |
| 1490 | packageIdentChars, true) != ATTR_OKAY) { |
| 1491 | hasErrors = true; |
| 1492 | } |
| 1493 | } else if (strcmp16(block.getElementName(&len), application16.string()) == 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1494 | if (validateAttr(manifestPath, finalResTable, block, RESOURCES_ANDROID_NAMESPACE, |
| 1495 | "name", classIdentChars, false) != ATTR_OKAY) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | hasErrors = true; |
| 1497 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1498 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | RESOURCES_ANDROID_NAMESPACE, "permission", |
| 1500 | packageIdentChars, false) != ATTR_OKAY) { |
| 1501 | hasErrors = true; |
| 1502 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1503 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | RESOURCES_ANDROID_NAMESPACE, "process", |
| 1505 | processIdentChars, false) != ATTR_OKAY) { |
| 1506 | hasErrors = true; |
| 1507 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1508 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | RESOURCES_ANDROID_NAMESPACE, "taskAffinity", |
| 1510 | processIdentChars, false) != ATTR_OKAY) { |
| 1511 | hasErrors = true; |
| 1512 | } |
| 1513 | } else if (strcmp16(block.getElementName(&len), provider16.string()) == 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1514 | if (validateAttr(manifestPath, finalResTable, block, RESOURCES_ANDROID_NAMESPACE, |
| 1515 | "name", classIdentChars, true) != ATTR_OKAY) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | hasErrors = true; |
| 1517 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1518 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | RESOURCES_ANDROID_NAMESPACE, "authorities", |
| 1520 | authoritiesIdentChars, true) != ATTR_OKAY) { |
| 1521 | hasErrors = true; |
| 1522 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1523 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | RESOURCES_ANDROID_NAMESPACE, "permission", |
| 1525 | packageIdentChars, false) != ATTR_OKAY) { |
| 1526 | hasErrors = true; |
| 1527 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1528 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1529 | RESOURCES_ANDROID_NAMESPACE, "process", |
| 1530 | processIdentChars, false) != ATTR_OKAY) { |
| 1531 | hasErrors = true; |
| 1532 | } |
| 1533 | } else if (strcmp16(block.getElementName(&len), service16.string()) == 0 |
| 1534 | || strcmp16(block.getElementName(&len), receiver16.string()) == 0 |
| 1535 | || strcmp16(block.getElementName(&len), activity16.string()) == 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1536 | if (validateAttr(manifestPath, finalResTable, block, RESOURCES_ANDROID_NAMESPACE, |
| 1537 | "name", classIdentChars, true) != ATTR_OKAY) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | hasErrors = true; |
| 1539 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1540 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | RESOURCES_ANDROID_NAMESPACE, "permission", |
| 1542 | packageIdentChars, false) != ATTR_OKAY) { |
| 1543 | hasErrors = true; |
| 1544 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1545 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1546 | RESOURCES_ANDROID_NAMESPACE, "process", |
| 1547 | processIdentChars, false) != ATTR_OKAY) { |
| 1548 | hasErrors = true; |
| 1549 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1550 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | RESOURCES_ANDROID_NAMESPACE, "taskAffinity", |
| 1552 | processIdentChars, false) != ATTR_OKAY) { |
| 1553 | hasErrors = true; |
| 1554 | } |
| 1555 | } else if (strcmp16(block.getElementName(&len), action16.string()) == 0 |
| 1556 | || strcmp16(block.getElementName(&len), category16.string()) == 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1557 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1558 | RESOURCES_ANDROID_NAMESPACE, "name", |
| 1559 | packageIdentChars, true) != ATTR_OKAY) { |
| 1560 | hasErrors = true; |
| 1561 | } |
| 1562 | } else if (strcmp16(block.getElementName(&len), data16.string()) == 0) { |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1563 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1564 | RESOURCES_ANDROID_NAMESPACE, "mimeType", |
| 1565 | typeIdentChars, true) != ATTR_OKAY) { |
| 1566 | hasErrors = true; |
| 1567 | } |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1568 | if (validateAttr(manifestPath, finalResTable, block, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1569 | RESOURCES_ANDROID_NAMESPACE, "scheme", |
| 1570 | schemeIdentChars, true) != ATTR_OKAY) { |
| 1571 | hasErrors = true; |
| 1572 | } |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
Dianne Hackborn | cf244ad | 2010-03-09 15:00:30 -0800 | [diff] [blame] | 1577 | if (resFile != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1578 | // These resources are now considered to be a part of the included |
| 1579 | // resources, for others to reference. |
| 1580 | err = assets->addIncludedResources(resFile); |
| 1581 | if (err < NO_ERROR) { |
| 1582 | fprintf(stderr, "ERROR: Unable to parse generated resources, aborting.\n"); |
| 1583 | return err; |
| 1584 | } |
| 1585 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1587 | return err; |
| 1588 | } |
| 1589 | |
| 1590 | static const char* getIndentSpace(int indent) |
| 1591 | { |
| 1592 | static const char whitespace[] = |
| 1593 | " "; |
| 1594 | |
| 1595 | return whitespace + sizeof(whitespace) - 1 - indent*4; |
| 1596 | } |
| 1597 | |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1598 | static String8 flattenSymbol(const String8& symbol) { |
| 1599 | String8 result(symbol); |
| 1600 | ssize_t first; |
| 1601 | if ((first = symbol.find(":", 0)) >= 0 |
| 1602 | || (first = symbol.find(".", 0)) >= 0) { |
| 1603 | size_t size = symbol.size(); |
| 1604 | char* buf = result.lockBuffer(size); |
| 1605 | for (size_t i = first; i < size; i++) { |
| 1606 | if (buf[i] == ':' || buf[i] == '.') { |
| 1607 | buf[i] = '_'; |
| 1608 | } |
| 1609 | } |
| 1610 | result.unlockBuffer(size); |
| 1611 | } |
| 1612 | return result; |
| 1613 | } |
| 1614 | |
| 1615 | static String8 getSymbolPackage(const String8& symbol, const sp<AaptAssets>& assets, bool pub) { |
| 1616 | ssize_t colon = symbol.find(":", 0); |
| 1617 | if (colon >= 0) { |
| 1618 | return String8(symbol.string(), colon); |
| 1619 | } |
| 1620 | return pub ? assets->getPackage() : assets->getSymbolsPrivatePackage(); |
| 1621 | } |
| 1622 | |
| 1623 | static String8 getSymbolName(const String8& symbol) { |
| 1624 | ssize_t colon = symbol.find(":", 0); |
| 1625 | if (colon >= 0) { |
| 1626 | return String8(symbol.string() + colon + 1); |
| 1627 | } |
| 1628 | return symbol; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | static String16 getAttributeComment(const sp<AaptAssets>& assets, |
| 1632 | const String8& name, |
| 1633 | String16* outTypeComment = NULL) |
| 1634 | { |
| 1635 | sp<AaptSymbols> asym = assets->getSymbolsFor(String8("R")); |
| 1636 | if (asym != NULL) { |
| 1637 | //printf("Got R symbols!\n"); |
| 1638 | asym = asym->getNestedSymbols().valueFor(String8("attr")); |
| 1639 | if (asym != NULL) { |
| 1640 | //printf("Got attrs symbols! comment %s=%s\n", |
| 1641 | // name.string(), String8(asym->getComment(name)).string()); |
| 1642 | if (outTypeComment != NULL) { |
| 1643 | *outTypeComment = asym->getTypeComment(name); |
| 1644 | } |
| 1645 | return asym->getComment(name); |
| 1646 | } |
| 1647 | } |
| 1648 | return String16(); |
| 1649 | } |
| 1650 | |
| 1651 | static status_t writeLayoutClasses( |
| 1652 | FILE* fp, const sp<AaptAssets>& assets, |
Xavier Ducrohet | a068eed | 2013-04-13 09:48:01 -0700 | [diff] [blame] | 1653 | const sp<AaptSymbols>& symbols, int indent, bool includePrivate) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | { |
| 1655 | const char* indentStr = getIndentSpace(indent); |
| 1656 | if (!includePrivate) { |
| 1657 | fprintf(fp, "%s/** @doconly */\n", indentStr); |
| 1658 | } |
| 1659 | fprintf(fp, "%spublic static final class styleable {\n", indentStr); |
| 1660 | indent++; |
| 1661 | |
| 1662 | String16 attr16("attr"); |
| 1663 | String16 package16(assets->getPackage()); |
| 1664 | |
| 1665 | indentStr = getIndentSpace(indent); |
| 1666 | bool hasErrors = false; |
| 1667 | |
| 1668 | size_t i; |
| 1669 | size_t N = symbols->getNestedSymbols().size(); |
| 1670 | for (i=0; i<N; i++) { |
| 1671 | sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1672 | String8 realClassName(symbols->getNestedSymbols().keyAt(i)); |
| 1673 | String8 nclassName(flattenSymbol(realClassName)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1674 | |
| 1675 | SortedVector<uint32_t> idents; |
| 1676 | Vector<uint32_t> origOrder; |
| 1677 | Vector<bool> publicFlags; |
| 1678 | |
| 1679 | size_t a; |
| 1680 | size_t NA = nsymbols->getSymbols().size(); |
| 1681 | for (a=0; a<NA; a++) { |
| 1682 | const AaptSymbolEntry& sym(nsymbols->getSymbols().valueAt(a)); |
| 1683 | int32_t code = sym.typeCode == AaptSymbolEntry::TYPE_INT32 |
| 1684 | ? sym.int32Val : 0; |
| 1685 | bool isPublic = true; |
| 1686 | if (code == 0) { |
| 1687 | String16 name16(sym.name); |
| 1688 | uint32_t typeSpecFlags; |
| 1689 | code = assets->getIncludedResources().identifierForName( |
| 1690 | name16.string(), name16.size(), |
| 1691 | attr16.string(), attr16.size(), |
| 1692 | package16.string(), package16.size(), &typeSpecFlags); |
| 1693 | if (code == 0) { |
| 1694 | fprintf(stderr, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n", |
| 1695 | nclassName.string(), sym.name.string()); |
| 1696 | hasErrors = true; |
| 1697 | } |
| 1698 | isPublic = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; |
| 1699 | } |
| 1700 | idents.add(code); |
| 1701 | origOrder.add(code); |
| 1702 | publicFlags.add(isPublic); |
| 1703 | } |
| 1704 | |
| 1705 | NA = idents.size(); |
| 1706 | |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1707 | bool deprecated = false; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1708 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1709 | String16 comment = symbols->getComment(realClassName); |
| 1710 | fprintf(fp, "%s/** ", indentStr); |
| 1711 | if (comment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1712 | String8 cmt(comment); |
| 1713 | fprintf(fp, "%s\n", cmt.string()); |
| 1714 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1715 | deprecated = true; |
| 1716 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1717 | } else { |
| 1718 | fprintf(fp, "Attributes that can be used with a %s.\n", nclassName.string()); |
| 1719 | } |
| 1720 | bool hasTable = false; |
| 1721 | for (a=0; a<NA; a++) { |
| 1722 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); |
| 1723 | if (pos >= 0) { |
| 1724 | if (!hasTable) { |
| 1725 | hasTable = true; |
| 1726 | fprintf(fp, |
| 1727 | "%s <p>Includes the following attributes:</p>\n" |
Dirk Dougherty | 59ad275 | 2009-11-03 15:33:37 -0800 | [diff] [blame] | 1728 | "%s <table>\n" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1729 | "%s <colgroup align=\"left\" />\n" |
| 1730 | "%s <colgroup align=\"left\" />\n" |
Dirk Dougherty | 59ad275 | 2009-11-03 15:33:37 -0800 | [diff] [blame] | 1731 | "%s <tr><th>Attribute</th><th>Description</th></tr>\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1732 | indentStr, |
| 1733 | indentStr, |
| 1734 | indentStr, |
| 1735 | indentStr, |
| 1736 | indentStr); |
| 1737 | } |
| 1738 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); |
| 1739 | if (!publicFlags.itemAt(a) && !includePrivate) { |
| 1740 | continue; |
| 1741 | } |
| 1742 | String8 name8(sym.name); |
| 1743 | String16 comment(sym.comment); |
| 1744 | if (comment.size() <= 0) { |
| 1745 | comment = getAttributeComment(assets, name8); |
| 1746 | } |
| 1747 | if (comment.size() > 0) { |
| 1748 | const char16_t* p = comment.string(); |
| 1749 | while (*p != 0 && *p != '.') { |
| 1750 | if (*p == '{') { |
| 1751 | while (*p != 0 && *p != '}') { |
| 1752 | p++; |
| 1753 | } |
| 1754 | } else { |
| 1755 | p++; |
| 1756 | } |
| 1757 | } |
| 1758 | if (*p == '.') { |
| 1759 | p++; |
| 1760 | } |
| 1761 | comment = String16(comment.string(), p-comment.string()); |
| 1762 | } |
Dirk Dougherty | 59ad275 | 2009-11-03 15:33:37 -0800 | [diff] [blame] | 1763 | fprintf(fp, "%s <tr><td><code>{@link #%s_%s %s:%s}</code></td><td>%s</td></tr>\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1764 | indentStr, nclassName.string(), |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1765 | flattenSymbol(name8).string(), |
| 1766 | getSymbolPackage(name8, assets, true).string(), |
| 1767 | getSymbolName(name8).string(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1768 | String8(comment).string()); |
| 1769 | } |
| 1770 | } |
| 1771 | if (hasTable) { |
| 1772 | fprintf(fp, "%s </table>\n", indentStr); |
| 1773 | } |
| 1774 | for (a=0; a<NA; a++) { |
| 1775 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); |
| 1776 | if (pos >= 0) { |
| 1777 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); |
| 1778 | if (!publicFlags.itemAt(a) && !includePrivate) { |
| 1779 | continue; |
| 1780 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | fprintf(fp, "%s @see #%s_%s\n", |
| 1782 | indentStr, nclassName.string(), |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1783 | flattenSymbol(sym.name).string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | fprintf(fp, "%s */\n", getIndentSpace(indent)); |
| 1787 | |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1788 | if (deprecated) { |
| 1789 | fprintf(fp, "%s@Deprecated\n", indentStr); |
| 1790 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1791 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | fprintf(fp, |
Xavier Ducrohet | a068eed | 2013-04-13 09:48:01 -0700 | [diff] [blame] | 1793 | "%spublic static final int[] %s = {\n" |
| 1794 | "%s", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | indentStr, nclassName.string(), |
| 1796 | getIndentSpace(indent+1)); |
| 1797 | |
| 1798 | for (a=0; a<NA; a++) { |
| 1799 | if (a != 0) { |
| 1800 | if ((a&3) == 0) { |
| 1801 | fprintf(fp, ",\n%s", getIndentSpace(indent+1)); |
| 1802 | } else { |
| 1803 | fprintf(fp, ", "); |
| 1804 | } |
| 1805 | } |
| 1806 | fprintf(fp, "0x%08x", idents[a]); |
| 1807 | } |
| 1808 | |
| 1809 | fprintf(fp, "\n%s};\n", indentStr); |
| 1810 | |
| 1811 | for (a=0; a<NA; a++) { |
| 1812 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); |
| 1813 | if (pos >= 0) { |
| 1814 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); |
| 1815 | if (!publicFlags.itemAt(a) && !includePrivate) { |
| 1816 | continue; |
| 1817 | } |
| 1818 | String8 name8(sym.name); |
| 1819 | String16 comment(sym.comment); |
| 1820 | String16 typeComment; |
| 1821 | if (comment.size() <= 0) { |
| 1822 | comment = getAttributeComment(assets, name8, &typeComment); |
| 1823 | } else { |
| 1824 | getAttributeComment(assets, name8, &typeComment); |
| 1825 | } |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1826 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | uint32_t typeSpecFlags = 0; |
| 1828 | String16 name16(sym.name); |
| 1829 | assets->getIncludedResources().identifierForName( |
| 1830 | name16.string(), name16.size(), |
| 1831 | attr16.string(), attr16.size(), |
| 1832 | package16.string(), package16.size(), &typeSpecFlags); |
| 1833 | //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(), |
| 1834 | // String8(attr16).string(), String8(name16).string(), typeSpecFlags); |
| 1835 | const bool pub = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1836 | |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1837 | bool deprecated = false; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | fprintf(fp, "%s/**\n", indentStr); |
| 1840 | if (comment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1841 | String8 cmt(comment); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1842 | fprintf(fp, "%s <p>\n%s @attr description\n", indentStr, indentStr); |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1843 | fprintf(fp, "%s %s\n", indentStr, cmt.string()); |
| 1844 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1845 | deprecated = true; |
| 1846 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1847 | } else { |
| 1848 | fprintf(fp, |
| 1849 | "%s <p>This symbol is the offset where the {@link %s.R.attr#%s}\n" |
| 1850 | "%s attribute's value can be found in the {@link #%s} array.\n", |
| 1851 | indentStr, |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1852 | getSymbolPackage(name8, assets, pub).string(), |
| 1853 | getSymbolName(name8).string(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1854 | indentStr, nclassName.string()); |
| 1855 | } |
| 1856 | if (typeComment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1857 | String8 cmt(typeComment); |
| 1858 | fprintf(fp, "\n\n%s %s\n", indentStr, cmt.string()); |
| 1859 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 1860 | deprecated = true; |
| 1861 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1862 | } |
| 1863 | if (comment.size() > 0) { |
| 1864 | if (pub) { |
| 1865 | fprintf(fp, |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1866 | "%s <p>This corresponds to the global attribute\n" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1867 | "%s resource symbol {@link %s.R.attr#%s}.\n", |
| 1868 | indentStr, indentStr, |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1869 | getSymbolPackage(name8, assets, true).string(), |
| 1870 | getSymbolName(name8).string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1871 | } else { |
| 1872 | fprintf(fp, |
| 1873 | "%s <p>This is a private symbol.\n", indentStr); |
| 1874 | } |
| 1875 | } |
| 1876 | fprintf(fp, "%s @attr name %s:%s\n", indentStr, |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1877 | getSymbolPackage(name8, assets, pub).string(), |
| 1878 | getSymbolName(name8).string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1879 | fprintf(fp, "%s*/\n", indentStr); |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 1880 | if (deprecated) { |
| 1881 | fprintf(fp, "%s@Deprecated\n", indentStr); |
| 1882 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | fprintf(fp, |
Xavier Ducrohet | a068eed | 2013-04-13 09:48:01 -0700 | [diff] [blame] | 1884 | "%spublic static final int %s_%s = %d;\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1885 | indentStr, nclassName.string(), |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1886 | flattenSymbol(name8).string(), (int)pos); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1887 | } |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | indent--; |
| 1892 | fprintf(fp, "%s};\n", getIndentSpace(indent)); |
| 1893 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; |
| 1894 | } |
| 1895 | |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 1896 | static status_t writeTextLayoutClasses( |
| 1897 | FILE* fp, const sp<AaptAssets>& assets, |
| 1898 | const sp<AaptSymbols>& symbols, bool includePrivate) |
| 1899 | { |
| 1900 | String16 attr16("attr"); |
| 1901 | String16 package16(assets->getPackage()); |
| 1902 | |
| 1903 | bool hasErrors = false; |
| 1904 | |
| 1905 | size_t i; |
| 1906 | size_t N = symbols->getNestedSymbols().size(); |
| 1907 | for (i=0; i<N; i++) { |
| 1908 | sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1909 | String8 realClassName(symbols->getNestedSymbols().keyAt(i)); |
| 1910 | String8 nclassName(flattenSymbol(realClassName)); |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 1911 | |
| 1912 | SortedVector<uint32_t> idents; |
| 1913 | Vector<uint32_t> origOrder; |
| 1914 | Vector<bool> publicFlags; |
| 1915 | |
| 1916 | size_t a; |
| 1917 | size_t NA = nsymbols->getSymbols().size(); |
| 1918 | for (a=0; a<NA; a++) { |
| 1919 | const AaptSymbolEntry& sym(nsymbols->getSymbols().valueAt(a)); |
| 1920 | int32_t code = sym.typeCode == AaptSymbolEntry::TYPE_INT32 |
| 1921 | ? sym.int32Val : 0; |
| 1922 | bool isPublic = true; |
| 1923 | if (code == 0) { |
| 1924 | String16 name16(sym.name); |
| 1925 | uint32_t typeSpecFlags; |
| 1926 | code = assets->getIncludedResources().identifierForName( |
| 1927 | name16.string(), name16.size(), |
| 1928 | attr16.string(), attr16.size(), |
| 1929 | package16.string(), package16.size(), &typeSpecFlags); |
| 1930 | if (code == 0) { |
| 1931 | fprintf(stderr, "ERROR: In <declare-styleable> %s, unable to find attribute %s\n", |
| 1932 | nclassName.string(), sym.name.string()); |
| 1933 | hasErrors = true; |
| 1934 | } |
| 1935 | isPublic = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; |
| 1936 | } |
| 1937 | idents.add(code); |
| 1938 | origOrder.add(code); |
| 1939 | publicFlags.add(isPublic); |
| 1940 | } |
| 1941 | |
| 1942 | NA = idents.size(); |
| 1943 | |
| 1944 | fprintf(fp, "int[] styleable %s {", nclassName.string()); |
| 1945 | |
| 1946 | for (a=0; a<NA; a++) { |
| 1947 | if (a != 0) { |
| 1948 | fprintf(fp, ","); |
| 1949 | } |
| 1950 | fprintf(fp, " 0x%08x", idents[a]); |
| 1951 | } |
| 1952 | |
| 1953 | fprintf(fp, " }\n"); |
| 1954 | |
| 1955 | for (a=0; a<NA; a++) { |
| 1956 | ssize_t pos = idents.indexOf(origOrder.itemAt(a)); |
| 1957 | if (pos >= 0) { |
| 1958 | const AaptSymbolEntry& sym = nsymbols->getSymbols().valueAt(a); |
| 1959 | if (!publicFlags.itemAt(a) && !includePrivate) { |
| 1960 | continue; |
| 1961 | } |
| 1962 | String8 name8(sym.name); |
| 1963 | String16 comment(sym.comment); |
| 1964 | String16 typeComment; |
| 1965 | if (comment.size() <= 0) { |
| 1966 | comment = getAttributeComment(assets, name8, &typeComment); |
| 1967 | } else { |
| 1968 | getAttributeComment(assets, name8, &typeComment); |
| 1969 | } |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 1970 | |
| 1971 | uint32_t typeSpecFlags = 0; |
| 1972 | String16 name16(sym.name); |
| 1973 | assets->getIncludedResources().identifierForName( |
| 1974 | name16.string(), name16.size(), |
| 1975 | attr16.string(), attr16.size(), |
| 1976 | package16.string(), package16.size(), &typeSpecFlags); |
| 1977 | //printf("%s:%s/%s: 0x%08x\n", String8(package16).string(), |
| 1978 | // String8(attr16).string(), String8(name16).string(), typeSpecFlags); |
| 1979 | const bool pub = (typeSpecFlags&ResTable_typeSpec::SPEC_PUBLIC) != 0; |
| 1980 | |
| 1981 | fprintf(fp, |
Xavier Ducrohet | d160474 | 2012-09-26 10:11:54 -0700 | [diff] [blame] | 1982 | "int styleable %s_%s %d\n", |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 1983 | nclassName.string(), |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 1984 | flattenSymbol(name8).string(), (int)pos); |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | return hasErrors ? UNKNOWN_ERROR : NO_ERROR; |
| 1990 | } |
| 1991 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1992 | static status_t writeSymbolClass( |
| 1993 | FILE* fp, const sp<AaptAssets>& assets, bool includePrivate, |
Xavier Ducrohet | d06c1af | 2011-02-14 16:58:00 -0800 | [diff] [blame] | 1994 | const sp<AaptSymbols>& symbols, const String8& className, int indent, |
| 1995 | bool nonConstantId) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | { |
| 1997 | fprintf(fp, "%spublic %sfinal class %s {\n", |
| 1998 | getIndentSpace(indent), |
| 1999 | indent != 0 ? "static " : "", className.string()); |
| 2000 | indent++; |
| 2001 | |
| 2002 | size_t i; |
| 2003 | status_t err = NO_ERROR; |
| 2004 | |
Xavier Ducrohet | d06c1af | 2011-02-14 16:58:00 -0800 | [diff] [blame] | 2005 | const char * id_format = nonConstantId ? |
| 2006 | "%spublic static int %s=0x%08x;\n" : |
| 2007 | "%spublic static final int %s=0x%08x;\n"; |
| 2008 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2009 | size_t N = symbols->getSymbols().size(); |
| 2010 | for (i=0; i<N; i++) { |
| 2011 | const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); |
| 2012 | if (sym.typeCode != AaptSymbolEntry::TYPE_INT32) { |
| 2013 | continue; |
| 2014 | } |
Dianne Hackborn | 1644c6d7 | 2012-02-06 15:33:21 -0800 | [diff] [blame] | 2015 | if (!assets->isJavaSymbol(sym, includePrivate)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2016 | continue; |
| 2017 | } |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 2018 | String8 name8(sym.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2019 | String16 comment(sym.comment); |
| 2020 | bool haveComment = false; |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2021 | bool deprecated = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2022 | if (comment.size() > 0) { |
| 2023 | haveComment = true; |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2024 | String8 cmt(comment); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2025 | fprintf(fp, |
| 2026 | "%s/** %s\n", |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2027 | getIndentSpace(indent), cmt.string()); |
| 2028 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 2029 | deprecated = true; |
| 2030 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2031 | } else if (sym.isPublic && !includePrivate) { |
| 2032 | sym.sourcePos.warning("No comment for public symbol %s:%s/%s", |
| 2033 | assets->getPackage().string(), className.string(), |
| 2034 | String8(sym.name).string()); |
| 2035 | } |
| 2036 | String16 typeComment(sym.typeComment); |
| 2037 | if (typeComment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2038 | String8 cmt(typeComment); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2039 | if (!haveComment) { |
| 2040 | haveComment = true; |
| 2041 | fprintf(fp, |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2042 | "%s/** %s\n", getIndentSpace(indent), cmt.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | } else { |
| 2044 | fprintf(fp, |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2045 | "%s %s\n", getIndentSpace(indent), cmt.string()); |
| 2046 | } |
| 2047 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 2048 | deprecated = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2049 | } |
| 2050 | } |
| 2051 | if (haveComment) { |
| 2052 | fprintf(fp,"%s */\n", getIndentSpace(indent)); |
| 2053 | } |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2054 | if (deprecated) { |
| 2055 | fprintf(fp, "%s@Deprecated\n", getIndentSpace(indent)); |
| 2056 | } |
Xavier Ducrohet | d06c1af | 2011-02-14 16:58:00 -0800 | [diff] [blame] | 2057 | fprintf(fp, id_format, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2058 | getIndentSpace(indent), |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 2059 | flattenSymbol(name8).string(), (int)sym.int32Val); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | } |
| 2061 | |
| 2062 | for (i=0; i<N; i++) { |
| 2063 | const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); |
| 2064 | if (sym.typeCode != AaptSymbolEntry::TYPE_STRING) { |
| 2065 | continue; |
| 2066 | } |
Dianne Hackborn | 1644c6d7 | 2012-02-06 15:33:21 -0800 | [diff] [blame] | 2067 | if (!assets->isJavaSymbol(sym, includePrivate)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2068 | continue; |
| 2069 | } |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 2070 | String8 name8(sym.name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2071 | String16 comment(sym.comment); |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2072 | bool deprecated = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2073 | if (comment.size() > 0) { |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2074 | String8 cmt(comment); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | fprintf(fp, |
| 2076 | "%s/** %s\n" |
| 2077 | "%s */\n", |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2078 | getIndentSpace(indent), cmt.string(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | getIndentSpace(indent)); |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2080 | if (strstr(cmt.string(), "@deprecated") != NULL) { |
| 2081 | deprecated = true; |
| 2082 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2083 | } else if (sym.isPublic && !includePrivate) { |
| 2084 | sym.sourcePos.warning("No comment for public symbol %s:%s/%s", |
| 2085 | assets->getPackage().string(), className.string(), |
| 2086 | String8(sym.name).string()); |
| 2087 | } |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 2088 | if (deprecated) { |
| 2089 | fprintf(fp, "%s@Deprecated\n", getIndentSpace(indent)); |
| 2090 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2091 | fprintf(fp, "%spublic static final String %s=\"%s\";\n", |
| 2092 | getIndentSpace(indent), |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 2093 | flattenSymbol(name8).string(), sym.stringVal.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | sp<AaptSymbols> styleableSymbols; |
| 2097 | |
| 2098 | N = symbols->getNestedSymbols().size(); |
| 2099 | for (i=0; i<N; i++) { |
| 2100 | sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); |
| 2101 | String8 nclassName(symbols->getNestedSymbols().keyAt(i)); |
| 2102 | if (nclassName == "styleable") { |
| 2103 | styleableSymbols = nsymbols; |
| 2104 | } else { |
Xavier Ducrohet | d06c1af | 2011-02-14 16:58:00 -0800 | [diff] [blame] | 2105 | err = writeSymbolClass(fp, assets, includePrivate, nsymbols, nclassName, indent, nonConstantId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2106 | } |
| 2107 | if (err != NO_ERROR) { |
| 2108 | return err; |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | if (styleableSymbols != NULL) { |
Xavier Ducrohet | a068eed | 2013-04-13 09:48:01 -0700 | [diff] [blame] | 2113 | err = writeLayoutClasses(fp, assets, styleableSymbols, indent, includePrivate); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2114 | if (err != NO_ERROR) { |
| 2115 | return err; |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | indent--; |
| 2120 | fprintf(fp, "%s}\n", getIndentSpace(indent)); |
| 2121 | return NO_ERROR; |
| 2122 | } |
| 2123 | |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2124 | static status_t writeTextSymbolClass( |
| 2125 | FILE* fp, const sp<AaptAssets>& assets, bool includePrivate, |
| 2126 | const sp<AaptSymbols>& symbols, const String8& className) |
| 2127 | { |
| 2128 | size_t i; |
| 2129 | status_t err = NO_ERROR; |
| 2130 | |
| 2131 | size_t N = symbols->getSymbols().size(); |
| 2132 | for (i=0; i<N; i++) { |
| 2133 | const AaptSymbolEntry& sym = symbols->getSymbols().valueAt(i); |
| 2134 | if (sym.typeCode != AaptSymbolEntry::TYPE_INT32) { |
| 2135 | continue; |
| 2136 | } |
| 2137 | |
| 2138 | if (!assets->isJavaSymbol(sym, includePrivate)) { |
| 2139 | continue; |
| 2140 | } |
| 2141 | |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 2142 | String8 name8(sym.name); |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2143 | fprintf(fp, "int %s %s 0x%08x\n", |
| 2144 | className.string(), |
Jeff Brown | caf7b0a | 2013-04-25 21:24:44 -0700 | [diff] [blame] | 2145 | flattenSymbol(name8).string(), (int)sym.int32Val); |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
| 2148 | N = symbols->getNestedSymbols().size(); |
| 2149 | for (i=0; i<N; i++) { |
| 2150 | sp<AaptSymbols> nsymbols = symbols->getNestedSymbols().valueAt(i); |
| 2151 | String8 nclassName(symbols->getNestedSymbols().keyAt(i)); |
| 2152 | if (nclassName == "styleable") { |
| 2153 | err = writeTextLayoutClasses(fp, assets, nsymbols, includePrivate); |
| 2154 | } else { |
| 2155 | err = writeTextSymbolClass(fp, assets, includePrivate, nsymbols, nclassName); |
| 2156 | } |
| 2157 | if (err != NO_ERROR) { |
| 2158 | return err; |
| 2159 | } |
| 2160 | } |
| 2161 | |
| 2162 | return NO_ERROR; |
| 2163 | } |
| 2164 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2165 | status_t writeResourceSymbols(Bundle* bundle, const sp<AaptAssets>& assets, |
| 2166 | const String8& package, bool includePrivate) |
| 2167 | { |
| 2168 | if (!bundle->getRClassDir()) { |
| 2169 | return NO_ERROR; |
| 2170 | } |
| 2171 | |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2172 | const char* textSymbolsDest = bundle->getOutputTextSymbols(); |
| 2173 | |
| 2174 | String8 R("R"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2175 | const size_t N = assets->getSymbols().size(); |
| 2176 | for (size_t i=0; i<N; i++) { |
| 2177 | sp<AaptSymbols> symbols = assets->getSymbols().valueAt(i); |
| 2178 | String8 className(assets->getSymbols().keyAt(i)); |
| 2179 | String8 dest(bundle->getRClassDir()); |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2181 | if (bundle->getMakePackageDirs()) { |
| 2182 | String8 pkg(package); |
| 2183 | const char* last = pkg.string(); |
| 2184 | const char* s = last-1; |
| 2185 | do { |
| 2186 | s++; |
| 2187 | if (s > last && (*s == '.' || *s == 0)) { |
| 2188 | String8 part(last, s-last); |
| 2189 | dest.appendPath(part); |
| 2190 | #ifdef HAVE_MS_C_RUNTIME |
| 2191 | _mkdir(dest.string()); |
| 2192 | #else |
| 2193 | mkdir(dest.string(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
| 2194 | #endif |
| 2195 | last = s+1; |
| 2196 | } |
| 2197 | } while (*s); |
| 2198 | } |
| 2199 | dest.appendPath(className); |
| 2200 | dest.append(".java"); |
| 2201 | FILE* fp = fopen(dest.string(), "w+"); |
| 2202 | if (fp == NULL) { |
| 2203 | fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", |
| 2204 | dest.string(), strerror(errno)); |
| 2205 | return UNKNOWN_ERROR; |
| 2206 | } |
| 2207 | if (bundle->getVerbose()) { |
| 2208 | printf(" Writing symbols for class %s.\n", className.string()); |
| 2209 | } |
| 2210 | |
| 2211 | fprintf(fp, |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2212 | "/* AUTO-GENERATED FILE. DO NOT MODIFY.\n" |
| 2213 | " *\n" |
| 2214 | " * This class was automatically generated by the\n" |
| 2215 | " * aapt tool from the resource data it found. It\n" |
| 2216 | " * should not be modified by hand.\n" |
| 2217 | " */\n" |
| 2218 | "\n" |
| 2219 | "package %s;\n\n", package.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2220 | |
Dianne Hackborn | 1644c6d7 | 2012-02-06 15:33:21 -0800 | [diff] [blame] | 2221 | status_t err = writeSymbolClass(fp, assets, includePrivate, symbols, |
| 2222 | className, 0, bundle->getNonConstantId()); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 2223 | fclose(fp); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2224 | if (err != NO_ERROR) { |
| 2225 | return err; |
| 2226 | } |
Josiah Gaskin | 9bf34ca | 2011-06-14 13:57:09 -0700 | [diff] [blame] | 2227 | |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2228 | if (textSymbolsDest != NULL && R == className) { |
| 2229 | String8 textDest(textSymbolsDest); |
| 2230 | textDest.appendPath(className); |
| 2231 | textDest.append(".txt"); |
| 2232 | |
| 2233 | FILE* fp = fopen(textDest.string(), "w+"); |
| 2234 | if (fp == NULL) { |
| 2235 | fprintf(stderr, "ERROR: Unable to open text symbol file %s: %s\n", |
| 2236 | textDest.string(), strerror(errno)); |
| 2237 | return UNKNOWN_ERROR; |
| 2238 | } |
| 2239 | if (bundle->getVerbose()) { |
| 2240 | printf(" Writing text symbols for class %s.\n", className.string()); |
| 2241 | } |
| 2242 | |
| 2243 | status_t err = writeTextSymbolClass(fp, assets, includePrivate, symbols, |
| 2244 | className); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 2245 | fclose(fp); |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2246 | if (err != NO_ERROR) { |
| 2247 | return err; |
| 2248 | } |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
Josiah Gaskin | b711f3f | 2011-08-15 18:33:44 -0700 | [diff] [blame] | 2251 | // If we were asked to generate a dependency file, we'll go ahead and add this R.java |
| 2252 | // as a target in the dependency file right next to it. |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2253 | if (bundle->getGenDependencies() && R == className) { |
Josiah Gaskin | 9bf34ca | 2011-06-14 13:57:09 -0700 | [diff] [blame] | 2254 | // Add this R.java to the dependency file |
| 2255 | String8 dependencyFile(bundle->getRClassDir()); |
Josiah Gaskin | b711f3f | 2011-08-15 18:33:44 -0700 | [diff] [blame] | 2256 | dependencyFile.appendPath("R.java.d"); |
Josiah Gaskin | 9bf34ca | 2011-06-14 13:57:09 -0700 | [diff] [blame] | 2257 | |
Xavier Ducrohet | f5de650 | 2012-09-11 14:45:22 -0700 | [diff] [blame] | 2258 | FILE *fp = fopen(dependencyFile.string(), "a"); |
Josiah Gaskin | 9bf34ca | 2011-06-14 13:57:09 -0700 | [diff] [blame] | 2259 | fprintf(fp,"%s \\\n", dest.string()); |
| 2260 | fclose(fp); |
| 2261 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | return NO_ERROR; |
| 2265 | } |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2266 | |
| 2267 | |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2268 | class ProguardKeepSet |
| 2269 | { |
| 2270 | public: |
| 2271 | // { rule --> { file locations } } |
| 2272 | KeyedVector<String8, SortedVector<String8> > rules; |
| 2273 | |
| 2274 | void add(const String8& rule, const String8& where); |
| 2275 | }; |
| 2276 | |
| 2277 | void ProguardKeepSet::add(const String8& rule, const String8& where) |
| 2278 | { |
| 2279 | ssize_t index = rules.indexOfKey(rule); |
| 2280 | if (index < 0) { |
| 2281 | index = rules.add(rule, SortedVector<String8>()); |
| 2282 | } |
| 2283 | rules.editValueAt(index).add(where); |
| 2284 | } |
| 2285 | |
Dianne Hackborn | b0381ef | 2010-03-03 13:36:35 -0800 | [diff] [blame] | 2286 | void |
| 2287 | addProguardKeepRule(ProguardKeepSet* keep, const String8& inClassName, |
| 2288 | const char* pkg, const String8& srcName, int line) |
| 2289 | { |
| 2290 | String8 className(inClassName); |
| 2291 | if (pkg != NULL) { |
| 2292 | // asdf --> package.asdf |
| 2293 | // .asdf .a.b --> package.asdf package.a.b |
| 2294 | // asdf.adsf --> asdf.asdf |
| 2295 | const char* p = className.string(); |
| 2296 | const char* q = strchr(p, '.'); |
| 2297 | if (p == q) { |
| 2298 | className = pkg; |
| 2299 | className.append(inClassName); |
| 2300 | } else if (q == NULL) { |
| 2301 | className = pkg; |
| 2302 | className.append("."); |
| 2303 | className.append(inClassName); |
| 2304 | } |
| 2305 | } |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2306 | |
Dianne Hackborn | b0381ef | 2010-03-03 13:36:35 -0800 | [diff] [blame] | 2307 | String8 rule("-keep class "); |
| 2308 | rule += className; |
| 2309 | rule += " { <init>(...); }"; |
| 2310 | |
| 2311 | String8 location("view "); |
| 2312 | location += srcName; |
| 2313 | char lineno[20]; |
| 2314 | sprintf(lineno, ":%d", line); |
| 2315 | location += lineno; |
| 2316 | |
| 2317 | keep->add(rule, location); |
| 2318 | } |
| 2319 | |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 2320 | void |
| 2321 | addProguardKeepMethodRule(ProguardKeepSet* keep, const String8& memberName, |
| 2322 | const char* pkg, const String8& srcName, int line) |
| 2323 | { |
| 2324 | String8 rule("-keepclassmembers class * { *** "); |
| 2325 | rule += memberName; |
| 2326 | rule += "(...); }"; |
| 2327 | |
| 2328 | String8 location("onClick "); |
| 2329 | location += srcName; |
| 2330 | char lineno[20]; |
| 2331 | sprintf(lineno, ":%d", line); |
| 2332 | location += lineno; |
| 2333 | |
| 2334 | keep->add(rule, location); |
| 2335 | } |
| 2336 | |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2337 | status_t |
| 2338 | writeProguardForAndroidManifest(ProguardKeepSet* keep, const sp<AaptAssets>& assets) |
| 2339 | { |
| 2340 | status_t err; |
| 2341 | ResXMLTree tree; |
| 2342 | size_t len; |
| 2343 | ResXMLTree::event_code_t code; |
| 2344 | int depth = 0; |
| 2345 | bool inApplication = false; |
| 2346 | String8 error; |
| 2347 | sp<AaptGroup> assGroup; |
| 2348 | sp<AaptFile> assFile; |
| 2349 | String8 pkg; |
| 2350 | |
| 2351 | // First, look for a package file to parse. This is required to |
| 2352 | // be able to generate the resource information. |
| 2353 | assGroup = assets->getFiles().valueFor(String8("AndroidManifest.xml")); |
| 2354 | if (assGroup == NULL) { |
| 2355 | fprintf(stderr, "ERROR: No AndroidManifest.xml file found.\n"); |
| 2356 | return -1; |
| 2357 | } |
| 2358 | |
| 2359 | if (assGroup->getFiles().size() != 1) { |
| 2360 | fprintf(stderr, "warning: Multiple AndroidManifest.xml files found, using %s\n", |
| 2361 | assGroup->getFiles().valueAt(0)->getPrintableSource().string()); |
| 2362 | } |
| 2363 | |
| 2364 | assFile = assGroup->getFiles().valueAt(0); |
| 2365 | |
| 2366 | err = parseXMLResource(assFile, &tree); |
| 2367 | if (err != NO_ERROR) { |
| 2368 | return err; |
| 2369 | } |
| 2370 | |
| 2371 | tree.restart(); |
| 2372 | |
| 2373 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 2374 | if (code == ResXMLTree::END_TAG) { |
| 2375 | if (/* name == "Application" && */ depth == 2) { |
| 2376 | inApplication = false; |
| 2377 | } |
| 2378 | depth--; |
| 2379 | continue; |
| 2380 | } |
| 2381 | if (code != ResXMLTree::START_TAG) { |
| 2382 | continue; |
| 2383 | } |
| 2384 | depth++; |
| 2385 | String8 tag(tree.getElementName(&len)); |
| 2386 | // printf("Depth %d tag %s\n", depth, tag.string()); |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 2387 | bool keepTag = false; |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2388 | if (depth == 1) { |
| 2389 | if (tag != "manifest") { |
| 2390 | fprintf(stderr, "ERROR: manifest does not start with <manifest> tag\n"); |
| 2391 | return -1; |
| 2392 | } |
| 2393 | pkg = getAttribute(tree, NULL, "package", NULL); |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 2394 | } else if (depth == 2) { |
| 2395 | if (tag == "application") { |
| 2396 | inApplication = true; |
| 2397 | keepTag = true; |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2398 | |
Dianne Hackborn | b0381ef | 2010-03-03 13:36:35 -0800 | [diff] [blame] | 2399 | String8 agent = getAttribute(tree, "http://schemas.android.com/apk/res/android", |
| 2400 | "backupAgent", &error); |
| 2401 | if (agent.length() > 0) { |
| 2402 | addProguardKeepRule(keep, agent, pkg.string(), |
| 2403 | assFile->getPrintableSource(), tree.getLineNumber()); |
| 2404 | } |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 2405 | } else if (tag == "instrumentation") { |
| 2406 | keepTag = true; |
| 2407 | } |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2408 | } |
Ying Wang | 46f4b98 | 2010-01-13 14:18:11 -0800 | [diff] [blame] | 2409 | if (!keepTag && inApplication && depth == 3) { |
| 2410 | if (tag == "activity" || tag == "service" || tag == "receiver" || tag == "provider") { |
| 2411 | keepTag = true; |
| 2412 | } |
| 2413 | } |
| 2414 | if (keepTag) { |
| 2415 | String8 name = getAttribute(tree, "http://schemas.android.com/apk/res/android", |
| 2416 | "name", &error); |
| 2417 | if (error != "") { |
| 2418 | fprintf(stderr, "ERROR: %s\n", error.string()); |
| 2419 | return -1; |
| 2420 | } |
| 2421 | if (name.length() > 0) { |
Dianne Hackborn | b0381ef | 2010-03-03 13:36:35 -0800 | [diff] [blame] | 2422 | addProguardKeepRule(keep, name, pkg.string(), |
| 2423 | assFile->getPrintableSource(), tree.getLineNumber()); |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2424 | } |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | return NO_ERROR; |
| 2429 | } |
| 2430 | |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2431 | struct NamespaceAttributePair { |
| 2432 | const char* ns; |
| 2433 | const char* attr; |
| 2434 | |
| 2435 | NamespaceAttributePair(const char* n, const char* a) : ns(n), attr(a) {} |
| 2436 | NamespaceAttributePair() : ns(NULL), attr(NULL) {} |
| 2437 | }; |
| 2438 | |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2439 | status_t |
Dianne Hackborn | abd0365 | 2010-03-02 14:56:51 -0800 | [diff] [blame] | 2440 | writeProguardForXml(ProguardKeepSet* keep, const sp<AaptFile>& layoutFile, |
Xavier Ducrohet | 095cd2e | 2012-07-18 18:06:09 -0700 | [diff] [blame] | 2441 | const char* startTag, const KeyedVector<String8, Vector<NamespaceAttributePair> >* tagAttrPairs) |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2442 | { |
| 2443 | status_t err; |
| 2444 | ResXMLTree tree; |
| 2445 | size_t len; |
| 2446 | ResXMLTree::event_code_t code; |
| 2447 | |
| 2448 | err = parseXMLResource(layoutFile, &tree); |
| 2449 | if (err != NO_ERROR) { |
| 2450 | return err; |
| 2451 | } |
| 2452 | |
| 2453 | tree.restart(); |
| 2454 | |
Dianne Hackborn | abd0365 | 2010-03-02 14:56:51 -0800 | [diff] [blame] | 2455 | if (startTag != NULL) { |
| 2456 | bool haveStart = false; |
| 2457 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 2458 | if (code != ResXMLTree::START_TAG) { |
| 2459 | continue; |
| 2460 | } |
| 2461 | String8 tag(tree.getElementName(&len)); |
| 2462 | if (tag == startTag) { |
| 2463 | haveStart = true; |
| 2464 | } |
| 2465 | break; |
| 2466 | } |
| 2467 | if (!haveStart) { |
| 2468 | return NO_ERROR; |
| 2469 | } |
| 2470 | } |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2471 | |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2472 | while ((code=tree.next()) != ResXMLTree::END_DOCUMENT && code != ResXMLTree::BAD_DOCUMENT) { |
| 2473 | if (code != ResXMLTree::START_TAG) { |
| 2474 | continue; |
| 2475 | } |
| 2476 | String8 tag(tree.getElementName(&len)); |
| 2477 | |
| 2478 | // If there is no '.', we'll assume that it's one of the built in names. |
| 2479 | if (strchr(tag.string(), '.')) { |
Dianne Hackborn | b0381ef | 2010-03-03 13:36:35 -0800 | [diff] [blame] | 2480 | addProguardKeepRule(keep, tag, NULL, |
Dianne Hackborn | abd0365 | 2010-03-02 14:56:51 -0800 | [diff] [blame] | 2481 | layoutFile->getPrintableSource(), tree.getLineNumber()); |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2482 | } else if (tagAttrPairs != NULL) { |
| 2483 | ssize_t tagIndex = tagAttrPairs->indexOfKey(tag); |
| 2484 | if (tagIndex >= 0) { |
Xavier Ducrohet | 095cd2e | 2012-07-18 18:06:09 -0700 | [diff] [blame] | 2485 | const Vector<NamespaceAttributePair>& nsAttrVector = tagAttrPairs->valueAt(tagIndex); |
| 2486 | for (size_t i = 0; i < nsAttrVector.size(); i++) { |
| 2487 | const NamespaceAttributePair& nsAttr = nsAttrVector[i]; |
| 2488 | |
| 2489 | ssize_t attrIndex = tree.indexOfAttribute(nsAttr.ns, nsAttr.attr); |
| 2490 | if (attrIndex < 0) { |
| 2491 | // fprintf(stderr, "%s:%d: <%s> does not have attribute %s:%s.\n", |
| 2492 | // layoutFile->getPrintableSource().string(), tree.getLineNumber(), |
| 2493 | // tag.string(), nsAttr.ns, nsAttr.attr); |
| 2494 | } else { |
| 2495 | size_t len; |
| 2496 | addProguardKeepRule(keep, |
| 2497 | String8(tree.getAttributeStringValue(attrIndex, &len)), NULL, |
| 2498 | layoutFile->getPrintableSource(), tree.getLineNumber()); |
| 2499 | } |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2500 | } |
Dianne Hackborn | abd0365 | 2010-03-02 14:56:51 -0800 | [diff] [blame] | 2501 | } |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2502 | } |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 2503 | ssize_t attrIndex = tree.indexOfAttribute(RESOURCES_ANDROID_NAMESPACE, "onClick"); |
| 2504 | if (attrIndex >= 0) { |
| 2505 | size_t len; |
| 2506 | addProguardKeepMethodRule(keep, |
| 2507 | String8(tree.getAttributeStringValue(attrIndex, &len)), NULL, |
| 2508 | layoutFile->getPrintableSource(), tree.getLineNumber()); |
| 2509 | } |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | return NO_ERROR; |
| 2513 | } |
| 2514 | |
Xavier Ducrohet | 095cd2e | 2012-07-18 18:06:09 -0700 | [diff] [blame] | 2515 | static void addTagAttrPair(KeyedVector<String8, Vector<NamespaceAttributePair> >* dest, |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2516 | const char* tag, const char* ns, const char* attr) { |
Xavier Ducrohet | 095cd2e | 2012-07-18 18:06:09 -0700 | [diff] [blame] | 2517 | String8 tagStr(tag); |
| 2518 | ssize_t index = dest->indexOfKey(tagStr); |
| 2519 | |
| 2520 | if (index < 0) { |
| 2521 | Vector<NamespaceAttributePair> vector; |
| 2522 | vector.add(NamespaceAttributePair(ns, attr)); |
| 2523 | dest->add(tagStr, vector); |
| 2524 | } else { |
| 2525 | dest->editValueAt(index).add(NamespaceAttributePair(ns, attr)); |
| 2526 | } |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2527 | } |
| 2528 | |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2529 | status_t |
| 2530 | writeProguardForLayouts(ProguardKeepSet* keep, const sp<AaptAssets>& assets) |
| 2531 | { |
| 2532 | status_t err; |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2533 | |
| 2534 | // tag:attribute pairs that should be checked in layout files. |
Xavier Ducrohet | 095cd2e | 2012-07-18 18:06:09 -0700 | [diff] [blame] | 2535 | KeyedVector<String8, Vector<NamespaceAttributePair> > kLayoutTagAttrPairs; |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2536 | addTagAttrPair(&kLayoutTagAttrPairs, "view", NULL, "class"); |
Dianne Hackborn | 8a44bb2 | 2010-08-19 12:56:10 -0700 | [diff] [blame] | 2537 | addTagAttrPair(&kLayoutTagAttrPairs, "fragment", NULL, "class"); |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2538 | addTagAttrPair(&kLayoutTagAttrPairs, "fragment", RESOURCES_ANDROID_NAMESPACE, "name"); |
| 2539 | |
| 2540 | // tag:attribute pairs that should be checked in xml files. |
Xavier Ducrohet | 095cd2e | 2012-07-18 18:06:09 -0700 | [diff] [blame] | 2541 | KeyedVector<String8, Vector<NamespaceAttributePair> > kXmlTagAttrPairs; |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2542 | addTagAttrPair(&kXmlTagAttrPairs, "PreferenceScreen", RESOURCES_ANDROID_NAMESPACE, "fragment"); |
Dianne Hackborn | 8a44bb2 | 2010-08-19 12:56:10 -0700 | [diff] [blame] | 2543 | addTagAttrPair(&kXmlTagAttrPairs, "header", RESOURCES_ANDROID_NAMESPACE, "fragment"); |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2544 | |
Ying Wang | c111296 | 2010-01-20 22:12:46 -0800 | [diff] [blame] | 2545 | const Vector<sp<AaptDir> >& dirs = assets->resDirs(); |
| 2546 | const size_t K = dirs.size(); |
| 2547 | for (size_t k=0; k<K; k++) { |
| 2548 | const sp<AaptDir>& d = dirs.itemAt(k); |
| 2549 | const String8& dirName = d->getLeaf(); |
Dianne Hackborn | abd0365 | 2010-03-02 14:56:51 -0800 | [diff] [blame] | 2550 | const char* startTag = NULL; |
Xavier Ducrohet | 095cd2e | 2012-07-18 18:06:09 -0700 | [diff] [blame] | 2551 | const KeyedVector<String8, Vector<NamespaceAttributePair> >* tagAttrPairs = NULL; |
Dianne Hackborn | abd0365 | 2010-03-02 14:56:51 -0800 | [diff] [blame] | 2552 | if ((dirName == String8("layout")) || (strncmp(dirName.string(), "layout-", 7) == 0)) { |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2553 | tagAttrPairs = &kLayoutTagAttrPairs; |
Dianne Hackborn | abd0365 | 2010-03-02 14:56:51 -0800 | [diff] [blame] | 2554 | } else if ((dirName == String8("xml")) || (strncmp(dirName.string(), "xml-", 4) == 0)) { |
| 2555 | startTag = "PreferenceScreen"; |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2556 | tagAttrPairs = &kXmlTagAttrPairs; |
Dianne Hackborn | 9275197 | 2012-05-18 19:22:14 -0700 | [diff] [blame] | 2557 | } else if ((dirName == String8("menu")) || (strncmp(dirName.string(), "menu-", 5) == 0)) { |
| 2558 | startTag = "menu"; |
| 2559 | tagAttrPairs = NULL; |
Dianne Hackborn | abd0365 | 2010-03-02 14:56:51 -0800 | [diff] [blame] | 2560 | } else { |
Ying Wang | c111296 | 2010-01-20 22:12:46 -0800 | [diff] [blame] | 2561 | continue; |
| 2562 | } |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2563 | |
Ying Wang | c111296 | 2010-01-20 22:12:46 -0800 | [diff] [blame] | 2564 | const KeyedVector<String8,sp<AaptGroup> > groups = d->getFiles(); |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2565 | const size_t N = groups.size(); |
| 2566 | for (size_t i=0; i<N; i++) { |
| 2567 | const sp<AaptGroup>& group = groups.valueAt(i); |
| 2568 | const DefaultKeyedVector<AaptGroupEntry, sp<AaptFile> >& files = group->getFiles(); |
| 2569 | const size_t M = files.size(); |
| 2570 | for (size_t j=0; j<M; j++) { |
Ying Wang | 561a918 | 2010-08-13 13:56:07 -0700 | [diff] [blame] | 2571 | err = writeProguardForXml(keep, files.valueAt(j), startTag, tagAttrPairs); |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2572 | if (err < 0) { |
| 2573 | return err; |
| 2574 | } |
| 2575 | } |
| 2576 | } |
| 2577 | } |
Ying Wang | 45ccfa5 | 2011-06-20 15:41:08 -0700 | [diff] [blame] | 2578 | // Handle the overlays |
| 2579 | sp<AaptAssets> overlay = assets->getOverlay(); |
| 2580 | if (overlay.get()) { |
| 2581 | return writeProguardForLayouts(keep, overlay); |
| 2582 | } |
Xavier Ducrohet | 095cd2e | 2012-07-18 18:06:09 -0700 | [diff] [blame] | 2583 | |
Joe Onorato | 1553c82 | 2009-08-30 13:36:22 -0700 | [diff] [blame] | 2584 | return NO_ERROR; |
| 2585 | } |
| 2586 | |
| 2587 | status_t |
| 2588 | writeProguardFile(Bundle* bundle, const sp<AaptAssets>& assets) |
| 2589 | { |
| 2590 | status_t err = -1; |
| 2591 | |
| 2592 | if (!bundle->getProguardFile()) { |
| 2593 | return NO_ERROR; |
| 2594 | } |
| 2595 | |
| 2596 | ProguardKeepSet keep; |
| 2597 | |
| 2598 | err = writeProguardForAndroidManifest(&keep, assets); |
| 2599 | if (err < 0) { |
| 2600 | return err; |
| 2601 | } |
| 2602 | |
| 2603 | err = writeProguardForLayouts(&keep, assets); |
| 2604 | if (err < 0) { |
| 2605 | return err; |
| 2606 | } |
| 2607 | |
| 2608 | FILE* fp = fopen(bundle->getProguardFile(), "w+"); |
| 2609 | if (fp == NULL) { |
| 2610 | fprintf(stderr, "ERROR: Unable to open class file %s: %s\n", |
| 2611 | bundle->getProguardFile(), strerror(errno)); |
| 2612 | return UNKNOWN_ERROR; |
| 2613 | } |
| 2614 | |
| 2615 | const KeyedVector<String8, SortedVector<String8> >& rules = keep.rules; |
| 2616 | const size_t N = rules.size(); |
| 2617 | for (size_t i=0; i<N; i++) { |
| 2618 | const SortedVector<String8>& locations = rules.valueAt(i); |
| 2619 | const size_t M = locations.size(); |
| 2620 | for (size_t j=0; j<M; j++) { |
| 2621 | fprintf(fp, "# %s\n", locations.itemAt(j).string()); |
| 2622 | } |
| 2623 | fprintf(fp, "%s\n\n", rules.keyAt(i).string()); |
| 2624 | } |
| 2625 | fclose(fp); |
| 2626 | |
| 2627 | return err; |
| 2628 | } |
Josiah Gaskin | 9bf34ca | 2011-06-14 13:57:09 -0700 | [diff] [blame] | 2629 | |
Josiah Gaskin | 03589cc | 2011-06-27 16:26:02 -0700 | [diff] [blame] | 2630 | // Loops through the string paths and writes them to the file pointer |
| 2631 | // Each file path is written on its own line with a terminating backslash. |
| 2632 | status_t writePathsToFile(const sp<FilePathStore>& files, FILE* fp) |
Josiah Gaskin | 9bf34ca | 2011-06-14 13:57:09 -0700 | [diff] [blame] | 2633 | { |
| 2634 | status_t deps = -1; |
Josiah Gaskin | 9bf34ca | 2011-06-14 13:57:09 -0700 | [diff] [blame] | 2635 | for (size_t file_i = 0; file_i < files->size(); ++file_i) { |
| 2636 | // Add the full file path to the dependency file |
| 2637 | fprintf(fp, "%s \\\n", files->itemAt(file_i).string()); |
| 2638 | deps++; |
| 2639 | } |
| 2640 | return deps; |
Xavier Ducrohet | 9139868 | 2011-07-19 10:18:28 -0700 | [diff] [blame] | 2641 | } |
Josiah Gaskin | 03589cc | 2011-06-27 16:26:02 -0700 | [diff] [blame] | 2642 | |
| 2643 | status_t |
| 2644 | writeDependencyPreReqs(Bundle* bundle, const sp<AaptAssets>& assets, FILE* fp, bool includeRaw) |
| 2645 | { |
| 2646 | status_t deps = -1; |
| 2647 | deps += writePathsToFile(assets->getFullResPaths(), fp); |
| 2648 | if (includeRaw) { |
| 2649 | deps += writePathsToFile(assets->getFullAssetPaths(), fp); |
| 2650 | } |
| 2651 | return deps; |
| 2652 | } |