Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "AppInfo.h" |
| 18 | #include "BigBuffer.h" |
| 19 | #include "BinaryResourceParser.h" |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 20 | #include "BinaryXmlPullParser.h" |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 21 | #include "BindingXmlPullParser.h" |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 22 | #include "Debug.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | #include "Files.h" |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 24 | #include "Flag.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 25 | #include "JavaClassGenerator.h" |
| 26 | #include "Linker.h" |
| 27 | #include "ManifestParser.h" |
| 28 | #include "ManifestValidator.h" |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 29 | #include "NameMangler.h" |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 30 | #include "Png.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 31 | #include "ResourceParser.h" |
| 32 | #include "ResourceTable.h" |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 33 | #include "ResourceTableResolver.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | #include "ResourceValues.h" |
| 35 | #include "SdkConstants.h" |
| 36 | #include "SourceXmlPullParser.h" |
| 37 | #include "StringPiece.h" |
| 38 | #include "TableFlattener.h" |
| 39 | #include "Util.h" |
| 40 | #include "XmlFlattener.h" |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 41 | #include "ZipFile.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | |
| 43 | #include <algorithm> |
| 44 | #include <androidfw/AssetManager.h> |
| 45 | #include <cstdlib> |
| 46 | #include <dirent.h> |
| 47 | #include <errno.h> |
| 48 | #include <fstream> |
| 49 | #include <iostream> |
| 50 | #include <sstream> |
| 51 | #include <sys/stat.h> |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 52 | #include <unordered_set> |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 53 | #include <utils/Errors.h> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | 5886a92 | 2015-04-15 20:29:22 -0700 | [diff] [blame] | 55 | constexpr const char* kAaptVersionStr = "2.0-alpha"; |
| 56 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | using namespace aapt; |
| 58 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | /** |
| 60 | * Collect files from 'root', filtering out any files that do not |
| 61 | * match the FileFilter 'filter'. |
| 62 | */ |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 63 | bool walkTree(const Source& root, const FileFilter& filter, |
| 64 | std::vector<Source>* outEntries) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 65 | bool error = false; |
| 66 | |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 67 | for (const std::string& dirName : listFiles(root.path)) { |
| 68 | std::string dir = root.path; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 69 | appendPath(&dir, dirName); |
| 70 | |
| 71 | FileType ft = getFileType(dir); |
| 72 | if (!filter(dirName, ft)) { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | if (ft != FileType::kDirectory) { |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | for (const std::string& fileName : listFiles(dir)) { |
| 81 | std::string file(dir); |
| 82 | appendPath(&file, fileName); |
| 83 | |
| 84 | FileType ft = getFileType(file); |
| 85 | if (!filter(fileName, ft)) { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if (ft != FileType::kRegular) { |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 90 | Logger::error(Source{ file }) << "not a regular file." << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 91 | error = true; |
| 92 | continue; |
| 93 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 94 | outEntries->push_back(Source{ file }); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | return !error; |
| 98 | } |
| 99 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 100 | void versionStylesForCompat(const std::shared_ptr<ResourceTable>& table) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | for (auto& type : *table) { |
| 102 | if (type->type != ResourceType::kStyle) { |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | for (auto& entry : type->entries) { |
| 107 | // Add the versioned styles we want to create |
| 108 | // here. They are added to the table after |
| 109 | // iterating over the original set of styles. |
| 110 | // |
| 111 | // A stack is used since auto-generated styles |
| 112 | // from later versions should override |
| 113 | // auto-generated styles from earlier versions. |
| 114 | // Iterating over the styles is done in order, |
| 115 | // so we will always visit sdkVersions from smallest |
| 116 | // to largest. |
| 117 | std::stack<ResourceConfigValue> addStack; |
| 118 | |
| 119 | for (ResourceConfigValue& configValue : entry->values) { |
| 120 | visitFunc<Style>(*configValue.value, [&](Style& style) { |
| 121 | // Collect which entries we've stripped and the smallest |
| 122 | // SDK level which was stripped. |
| 123 | size_t minSdkStripped = std::numeric_limits<size_t>::max(); |
| 124 | std::vector<Style::Entry> stripped; |
| 125 | |
| 126 | // Iterate over the style's entries and erase/record the |
| 127 | // attributes whose SDK level exceeds the config's sdkVersion. |
| 128 | auto iter = style.entries.begin(); |
| 129 | while (iter != style.entries.end()) { |
| 130 | if (iter->key.name.package == u"android") { |
| 131 | size_t sdkLevel = findAttributeSdkLevel(iter->key.name.entry); |
| 132 | if (sdkLevel > 1 && sdkLevel > configValue.config.sdkVersion) { |
| 133 | // Record that we are about to strip this. |
| 134 | stripped.emplace_back(std::move(*iter)); |
| 135 | minSdkStripped = std::min(minSdkStripped, sdkLevel); |
| 136 | |
| 137 | // Erase this from this style. |
| 138 | iter = style.entries.erase(iter); |
| 139 | continue; |
| 140 | } |
| 141 | } |
| 142 | ++iter; |
| 143 | } |
| 144 | |
| 145 | if (!stripped.empty()) { |
| 146 | // We have stripped attributes, so let's create a new style to hold them. |
| 147 | ConfigDescription versionConfig(configValue.config); |
| 148 | versionConfig.sdkVersion = minSdkStripped; |
| 149 | |
| 150 | ResourceConfigValue value = { |
| 151 | versionConfig, |
| 152 | configValue.source, |
| 153 | {}, |
| 154 | |
| 155 | // Create a copy of the original style. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 156 | std::unique_ptr<Value>(configValue.value->clone( |
| 157 | &table->getValueStringPool())) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | Style& newStyle = static_cast<Style&>(*value.value); |
| 161 | |
| 162 | // Move the recorded stripped attributes into this new style. |
| 163 | std::move(stripped.begin(), stripped.end(), |
| 164 | std::back_inserter(newStyle.entries)); |
| 165 | |
| 166 | // We will add this style to the table later. If we do it now, we will |
| 167 | // mess up iteration. |
| 168 | addStack.push(std::move(value)); |
| 169 | } |
| 170 | }); |
| 171 | } |
| 172 | |
| 173 | auto comparator = |
| 174 | [](const ResourceConfigValue& lhs, const ConfigDescription& rhs) -> bool { |
| 175 | return lhs.config < rhs; |
| 176 | }; |
| 177 | |
| 178 | while (!addStack.empty()) { |
| 179 | ResourceConfigValue& value = addStack.top(); |
| 180 | auto iter = std::lower_bound(entry->values.begin(), entry->values.end(), |
| 181 | value.config, comparator); |
| 182 | if (iter == entry->values.end() || iter->config != value.config) { |
| 183 | entry->values.insert(iter, std::move(value)); |
| 184 | } |
| 185 | addStack.pop(); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 191 | struct CompileItem { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 192 | Source source; |
| 193 | ResourceName name; |
| 194 | ConfigDescription config; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 195 | std::string extension; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 196 | }; |
| 197 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 198 | struct LinkItem { |
| 199 | Source source; |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 200 | ResourceName name; |
| 201 | ConfigDescription config; |
| 202 | std::string originalPath; |
| 203 | ZipFile* apk; |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 204 | std::u16string originalPackage; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 207 | template <typename TChar> |
| 208 | static BasicStringPiece<TChar> getExtension(const BasicStringPiece<TChar>& str) { |
| 209 | auto iter = std::find(str.begin(), str.end(), static_cast<TChar>('.')); |
| 210 | if (iter == str.end()) { |
| 211 | return BasicStringPiece<TChar>(); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 212 | } |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 213 | size_t offset = (iter - str.begin()) + 1; |
| 214 | return str.substr(offset, str.size() - offset); |
| 215 | } |
| 216 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 217 | std::string buildFileReference(const ResourceNameRef& name, const ConfigDescription& config, |
| 218 | const StringPiece& extension) { |
| 219 | std::stringstream path; |
| 220 | path << "res/" << name.type; |
| 221 | if (config != ConfigDescription{}) { |
| 222 | path << "-" << config; |
| 223 | } |
| 224 | path << "/" << util::utf16ToUtf8(name.entry); |
| 225 | if (!extension.empty()) { |
| 226 | path << "." << extension; |
| 227 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 228 | return path.str(); |
| 229 | } |
| 230 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 231 | std::string buildFileReference(const CompileItem& item) { |
| 232 | return buildFileReference(item.name, item.config, item.extension); |
| 233 | } |
| 234 | |
| 235 | std::string buildFileReference(const LinkItem& item) { |
| 236 | return buildFileReference(item.name, item.config, getExtension<char>(item.originalPath)); |
| 237 | } |
| 238 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 239 | bool addFileReference(const std::shared_ptr<ResourceTable>& table, const CompileItem& item) { |
| 240 | StringPool& pool = table->getValueStringPool(); |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 241 | StringPool::Ref ref = pool.makeRef(util::utf8ToUtf16(buildFileReference(item)), |
| 242 | StringPool::Context{ 0, item.config }); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 243 | return table->addResource(item.name, item.config, item.source.line(0), |
| 244 | util::make_unique<FileReference>(ref)); |
| 245 | } |
| 246 | |
| 247 | struct AaptOptions { |
| 248 | enum class Phase { |
| 249 | Link, |
| 250 | Compile, |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 251 | Dump, |
| 252 | DumpStyleGraph, |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 253 | }; |
| 254 | |
Adam Lesinski | 6d8e4c4 | 2015-05-01 14:47:28 -0700 | [diff] [blame] | 255 | enum class PackageType { |
| 256 | StandardApp, |
| 257 | StaticLibrary, |
| 258 | }; |
| 259 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 260 | // The phase to process. |
| 261 | Phase phase; |
| 262 | |
Adam Lesinski | 6d8e4c4 | 2015-05-01 14:47:28 -0700 | [diff] [blame] | 263 | // The type of package to produce. |
| 264 | PackageType packageType = PackageType::StandardApp; |
| 265 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 266 | // Details about the app. |
| 267 | AppInfo appInfo; |
| 268 | |
| 269 | // The location of the manifest file. |
| 270 | Source manifest; |
| 271 | |
| 272 | // The APK files to link. |
| 273 | std::vector<Source> input; |
| 274 | |
| 275 | // The libraries these files may reference. |
| 276 | std::vector<Source> libraries; |
| 277 | |
| 278 | // Output path. This can be a directory or file |
| 279 | // depending on the phase. |
| 280 | Source output; |
| 281 | |
| 282 | // Directory in which to write binding xml files. |
| 283 | Source bindingOutput; |
| 284 | |
| 285 | // Directory to in which to generate R.java. |
| 286 | Maybe<Source> generateJavaClass; |
| 287 | |
| 288 | // Whether to output verbose details about |
| 289 | // compilation. |
| 290 | bool verbose = false; |
Adam Lesinski | 5886a92 | 2015-04-15 20:29:22 -0700 | [diff] [blame] | 291 | |
| 292 | // Whether or not to auto-version styles or layouts |
| 293 | // referencing attributes defined in a newer SDK |
| 294 | // level than the style or layout is defined for. |
| 295 | bool versionStylesAndLayouts = true; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | bool compileXml(const AaptOptions& options, const std::shared_ptr<ResourceTable>& table, |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 299 | const CompileItem& item, ZipFile* outApk) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 300 | std::ifstream in(item.source.path, std::ifstream::binary); |
| 301 | if (!in) { |
| 302 | Logger::error(item.source) << strerror(errno) << std::endl; |
| 303 | return false; |
| 304 | } |
| 305 | |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 306 | BigBuffer outBuffer(1024); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 307 | |
| 308 | // No resolver, since we are not compiling attributes here. |
| 309 | XmlFlattener flattener(table, {}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 310 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 311 | XmlFlattener::Options xmlOptions; |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 312 | xmlOptions.defaultPackage = table->getPackage(); |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 313 | xmlOptions.keepRawValues = true; |
| 314 | |
| 315 | std::shared_ptr<XmlPullParser> parser = std::make_shared<SourceXmlPullParser>(in); |
| 316 | |
| 317 | Maybe<size_t> minStrippedSdk = flattener.flatten(item.source, parser, &outBuffer, |
| 318 | xmlOptions); |
| 319 | if (!minStrippedSdk) { |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | // Write the resulting compiled XML file to the output APK. |
| 324 | if (outApk->add(outBuffer, buildFileReference(item).data(), ZipEntry::kCompressStored, |
| 325 | nullptr) != android::NO_ERROR) { |
| 326 | Logger::error(options.output) << "failed to write compiled '" << item.source |
| 327 | << "' to apk." << std::endl; |
| 328 | return false; |
| 329 | } |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | bool linkXml(const AaptOptions& options, const std::shared_ptr<IResolver>& resolver, |
| 334 | const LinkItem& item, const void* data, size_t dataLen, ZipFile* outApk, |
| 335 | std::queue<LinkItem>* outQueue) { |
| 336 | std::shared_ptr<android::ResXMLTree> tree = std::make_shared<android::ResXMLTree>(); |
| 337 | if (tree->setTo(data, dataLen, false) != android::NO_ERROR) { |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | std::shared_ptr<XmlPullParser> parser = std::make_shared<BinaryXmlPullParser>(tree); |
| 342 | |
| 343 | BigBuffer outBuffer(1024); |
| 344 | XmlFlattener flattener({}, resolver); |
| 345 | |
| 346 | XmlFlattener::Options xmlOptions; |
| 347 | xmlOptions.defaultPackage = item.originalPackage; |
| 348 | |
| 349 | if (options.packageType == AaptOptions::PackageType::StaticLibrary) { |
| 350 | xmlOptions.keepRawValues = true; |
| 351 | } |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 352 | |
Adam Lesinski | 5886a92 | 2015-04-15 20:29:22 -0700 | [diff] [blame] | 353 | if (options.versionStylesAndLayouts) { |
| 354 | // We strip attributes that do not belong in this version of the resource. |
| 355 | // Non-version qualified resources have an implicit version 1 requirement. |
| 356 | xmlOptions.maxSdkAttribute = item.config.sdkVersion ? item.config.sdkVersion : 1; |
| 357 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 358 | |
| 359 | std::shared_ptr<BindingXmlPullParser> binding; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 360 | if (item.name.type == ResourceType::kLayout) { |
| 361 | // Layouts may have defined bindings, so we need to make sure they get processed. |
| 362 | binding = std::make_shared<BindingXmlPullParser>(parser); |
| 363 | parser = binding; |
| 364 | } |
| 365 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 366 | Maybe<size_t> minStrippedSdk = flattener.flatten(item.source, parser, &outBuffer, |
| 367 | xmlOptions); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 368 | if (!minStrippedSdk) { |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | if (minStrippedSdk.value() > 0) { |
| 373 | // Something was stripped, so let's generate a new file |
| 374 | // with the version of the smallest SDK version stripped. |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 375 | LinkItem newWork = item; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 376 | newWork.config.sdkVersion = minStrippedSdk.value(); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 377 | outQueue->push(newWork); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 380 | if (outApk->add(outBuffer, buildFileReference(item).data(), ZipEntry::kCompressDeflated, |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 381 | nullptr) != android::NO_ERROR) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 382 | Logger::error(options.output) << "failed to write linked file '" |
| 383 | << buildFileReference(item) << "' to apk." << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 387 | if (binding && !options.bindingOutput.path.empty()) { |
| 388 | // We generated a binding xml file, write it out. |
| 389 | Source bindingOutput = options.bindingOutput; |
| 390 | appendPath(&bindingOutput.path, buildFileReference(item)); |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 391 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 392 | if (!mkdirs(bindingOutput.path)) { |
| 393 | Logger::error(bindingOutput) << strerror(errno) << std::endl; |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | appendPath(&bindingOutput.path, "bind.xml"); |
| 398 | |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 399 | std::ofstream bout(bindingOutput.path); |
| 400 | if (!bout) { |
| 401 | Logger::error(bindingOutput) << strerror(errno) << std::endl; |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | if (!binding->writeToFile(bout)) { |
| 406 | Logger::error(bindingOutput) << strerror(errno) << std::endl; |
| 407 | return false; |
| 408 | } |
| 409 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 410 | return true; |
| 411 | } |
| 412 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 413 | bool compilePng(const AaptOptions& options, const CompileItem& item, ZipFile* outApk) { |
| 414 | std::ifstream in(item.source.path, std::ifstream::binary); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 415 | if (!in) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 416 | Logger::error(item.source) << strerror(errno) << std::endl; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 417 | return false; |
| 418 | } |
| 419 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 420 | BigBuffer outBuffer(4096); |
| 421 | std::string err; |
| 422 | Png png; |
| 423 | if (!png.process(item.source, in, &outBuffer, {}, &err)) { |
| 424 | Logger::error(item.source) << err << std::endl; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 428 | if (outApk->add(outBuffer, buildFileReference(item).data(), ZipEntry::kCompressStored, |
| 429 | nullptr) != android::NO_ERROR) { |
| 430 | Logger::error(options.output) << "failed to write compiled '" << item.source |
| 431 | << "' to apk." << std::endl; |
| 432 | return false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 433 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 434 | return true; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 435 | } |
| 436 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 437 | bool copyFile(const AaptOptions& options, const CompileItem& item, ZipFile* outApk) { |
| 438 | if (outApk->add(item.source.path.data(), buildFileReference(item).data(), |
| 439 | ZipEntry::kCompressStored, nullptr) != android::NO_ERROR) { |
| 440 | Logger::error(options.output) << "failed to copy file '" << item.source << "' to apk." |
| 441 | << std::endl; |
| 442 | return false; |
| 443 | } |
| 444 | return true; |
| 445 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 446 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 447 | bool compileManifest(const AaptOptions& options, const std::shared_ptr<IResolver>& resolver, |
| 448 | const android::ResTable& table, ZipFile* outApk) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 449 | if (options.verbose) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 450 | Logger::note(options.manifest) << "compiling AndroidManifest.xml." << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | std::ifstream in(options.manifest.path, std::ifstream::binary); |
| 454 | if (!in) { |
| 455 | Logger::error(options.manifest) << strerror(errno) << std::endl; |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | BigBuffer outBuffer(1024); |
| 460 | std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(in); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 461 | XmlFlattener flattener({}, resolver); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 462 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 463 | XmlFlattener::Options xmlOptions; |
| 464 | xmlOptions.defaultPackage = options.appInfo.package; |
| 465 | if (!flattener.flatten(options.manifest, xmlParser, &outBuffer, xmlOptions)) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 469 | std::unique_ptr<uint8_t[]> data = util::copy(outBuffer); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 470 | |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 471 | android::ResXMLTree tree; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 472 | if (tree.setTo(data.get(), outBuffer.size(), false) != android::NO_ERROR) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 473 | return false; |
| 474 | } |
| 475 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 476 | ManifestValidator validator(table); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 477 | if (!validator.validate(options.manifest, &tree)) { |
| 478 | return false; |
| 479 | } |
| 480 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 481 | if (outApk->add(data.get(), outBuffer.size(), "AndroidManifest.xml", |
| 482 | ZipEntry::kCompressStored, nullptr) != android::NO_ERROR) { |
| 483 | Logger::error(options.output) << "failed to write 'AndroidManifest.xml' to apk." |
| 484 | << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 485 | return false; |
| 486 | } |
| 487 | return true; |
| 488 | } |
| 489 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 490 | static bool compileValues(const std::shared_ptr<ResourceTable>& table, const Source& source, |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 491 | const ConfigDescription& config) { |
| 492 | std::ifstream in(source.path, std::ifstream::binary); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 493 | if (!in) { |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 494 | Logger::error(source) << strerror(errno) << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 495 | return false; |
| 496 | } |
| 497 | |
| 498 | std::shared_ptr<XmlPullParser> xmlParser = std::make_shared<SourceXmlPullParser>(in); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 499 | ResourceParser parser(table, source, config, xmlParser); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 500 | return parser.parse(); |
| 501 | } |
| 502 | |
| 503 | struct ResourcePathData { |
| 504 | std::u16string resourceDir; |
| 505 | std::u16string name; |
| 506 | std::string extension; |
| 507 | ConfigDescription config; |
| 508 | }; |
| 509 | |
| 510 | /** |
| 511 | * Resource file paths are expected to look like: |
| 512 | * [--/res/]type[-config]/name |
| 513 | */ |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 514 | static Maybe<ResourcePathData> extractResourcePathData(const Source& source) { |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 515 | // TODO(adamlesinski): Use Windows path separator on windows. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 516 | std::vector<std::string> parts = util::splitAndLowercase(source.path, '/'); |
| 517 | if (parts.size() < 2) { |
| 518 | Logger::error(source) << "bad resource path." << std::endl; |
| 519 | return {}; |
| 520 | } |
| 521 | |
| 522 | std::string& dir = parts[parts.size() - 2]; |
| 523 | StringPiece dirStr = dir; |
| 524 | |
| 525 | ConfigDescription config; |
| 526 | size_t dashPos = dir.find('-'); |
| 527 | if (dashPos != std::string::npos) { |
| 528 | StringPiece configStr = dirStr.substr(dashPos + 1, dir.size() - (dashPos + 1)); |
| 529 | if (!ConfigDescription::parse(configStr, &config)) { |
| 530 | Logger::error(source) |
| 531 | << "invalid configuration '" |
| 532 | << configStr |
| 533 | << "'." |
| 534 | << std::endl; |
| 535 | return {}; |
| 536 | } |
| 537 | dirStr = dirStr.substr(0, dashPos); |
| 538 | } |
| 539 | |
| 540 | std::string& filename = parts[parts.size() - 1]; |
| 541 | StringPiece name = filename; |
| 542 | StringPiece extension; |
| 543 | size_t dotPos = filename.find('.'); |
| 544 | if (dotPos != std::string::npos) { |
| 545 | extension = name.substr(dotPos + 1, filename.size() - (dotPos + 1)); |
| 546 | name = name.substr(0, dotPos); |
| 547 | } |
| 548 | |
| 549 | return ResourcePathData{ |
| 550 | util::utf8ToUtf16(dirStr), |
| 551 | util::utf8ToUtf16(name), |
| 552 | extension.toString(), |
| 553 | config |
| 554 | }; |
| 555 | } |
| 556 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 557 | bool writeResourceTable(const AaptOptions& options, const std::shared_ptr<ResourceTable>& table, |
| 558 | const TableFlattener::Options& flattenerOptions, ZipFile* outApk) { |
| 559 | if (table->begin() != table->end()) { |
| 560 | BigBuffer buffer(1024); |
| 561 | TableFlattener flattener(flattenerOptions); |
| 562 | if (!flattener.flatten(&buffer, *table)) { |
| 563 | Logger::error() << "failed to flatten resource table." << std::endl; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 564 | return false; |
| 565 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 566 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 567 | if (options.verbose) { |
| 568 | Logger::note() << "Final resource table size=" << util::formatSize(buffer.size()) |
| 569 | << std::endl; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 572 | if (outApk->add(buffer, "resources.arsc", ZipEntry::kCompressStored, nullptr) != |
| 573 | android::NO_ERROR) { |
| 574 | Logger::note(options.output) << "failed to store resource table." << std::endl; |
| 575 | return false; |
| 576 | } |
| 577 | } |
| 578 | return true; |
| 579 | } |
| 580 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 581 | /** |
| 582 | * For each FileReference in the table, adds a LinkItem to the link queue for processing. |
| 583 | */ |
| 584 | static void addApkFilesToLinkQueue(const std::u16string& package, const Source& source, |
| 585 | const std::shared_ptr<ResourceTable>& table, |
| 586 | const std::unique_ptr<ZipFile>& apk, |
| 587 | std::queue<LinkItem>* outLinkQueue) { |
| 588 | bool mangle = package != table->getPackage(); |
| 589 | for (auto& type : *table) { |
| 590 | for (auto& entry : type->entries) { |
| 591 | ResourceName name = { package, type->type, entry->name }; |
| 592 | if (mangle) { |
| 593 | NameMangler::mangle(table->getPackage(), &name.entry); |
| 594 | } |
| 595 | |
| 596 | for (auto& value : entry->values) { |
| 597 | visitFunc<FileReference>(*value.value, [&](FileReference& ref) { |
| 598 | std::string pathUtf8 = util::utf16ToUtf8(*ref.path); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 599 | Source newSource = source; |
| 600 | newSource.path += "/"; |
| 601 | newSource.path += pathUtf8; |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 602 | outLinkQueue->push(LinkItem{ |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 603 | newSource, name, value.config, pathUtf8, apk.get(), |
| 604 | table->getPackage() }); |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 605 | // Now rewrite the file path. |
| 606 | if (mangle) { |
| 607 | ref.path = table->getValueStringPool().makeRef(util::utf8ToUtf16( |
| 608 | buildFileReference(name, value.config, |
| 609 | getExtension<char>(pathUtf8)))); |
| 610 | } |
| 611 | }); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 617 | static constexpr int kOpenFlags = ZipFile::kOpenCreate | ZipFile::kOpenTruncate | |
| 618 | ZipFile::kOpenReadWrite; |
| 619 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 620 | struct DeleteMalloc { |
| 621 | void operator()(void* ptr) { |
| 622 | free(ptr); |
| 623 | } |
| 624 | }; |
| 625 | |
| 626 | struct StaticLibraryData { |
| 627 | Source source; |
| 628 | std::unique_ptr<ZipFile> apk; |
| 629 | }; |
| 630 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 631 | bool link(const AaptOptions& options, const std::shared_ptr<ResourceTable>& outTable, |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 632 | const std::shared_ptr<IResolver>& resolver) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 633 | std::map<std::shared_ptr<ResourceTable>, StaticLibraryData> apkFiles; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 634 | std::unordered_set<std::u16string> linkedPackages; |
| 635 | |
| 636 | // Populate the linkedPackages with our own. |
| 637 | linkedPackages.insert(options.appInfo.package); |
| 638 | |
| 639 | // Load all APK files. |
| 640 | for (const Source& source : options.input) { |
| 641 | std::unique_ptr<ZipFile> zipFile = util::make_unique<ZipFile>(); |
| 642 | if (zipFile->open(source.path.data(), ZipFile::kOpenReadOnly) != android::NO_ERROR) { |
| 643 | Logger::error(source) << "failed to open: " << strerror(errno) << std::endl; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 644 | return false; |
| 645 | } |
| 646 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 647 | std::shared_ptr<ResourceTable> table = std::make_shared<ResourceTable>(); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 648 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 649 | ZipEntry* entry = zipFile->getEntryByName("resources.arsc"); |
| 650 | if (!entry) { |
| 651 | Logger::error(source) << "missing 'resources.arsc'." << std::endl; |
| 652 | return false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 655 | std::unique_ptr<void, DeleteMalloc> uncompressedData = std::unique_ptr<void, DeleteMalloc>( |
| 656 | zipFile->uncompress(entry)); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 657 | assert(uncompressedData); |
| 658 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 659 | BinaryResourceParser parser(table, resolver, source, uncompressedData.get(), |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 660 | entry->getUncompressedLen()); |
| 661 | if (!parser.parse()) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 662 | return false; |
| 663 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 664 | |
| 665 | // Keep track of where this table came from. |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 666 | apkFiles[table] = StaticLibraryData{ source, std::move(zipFile) }; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 667 | |
| 668 | // Add the package to the set of linked packages. |
| 669 | linkedPackages.insert(table->getPackage()); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 670 | } |
| 671 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 672 | std::queue<LinkItem> linkQueue; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 673 | for (auto& p : apkFiles) { |
| 674 | const std::shared_ptr<ResourceTable>& inTable = p.first; |
| 675 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 676 | // Collect all FileReferences and add them to the queue for processing. |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 677 | addApkFilesToLinkQueue(options.appInfo.package, p.second.source, inTable, p.second.apk, |
| 678 | &linkQueue); |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 679 | |
| 680 | // Merge the tables. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 681 | if (!outTable->merge(std::move(*inTable))) { |
| 682 | return false; |
| 683 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 686 | // Version all styles referencing attributes outside of their specified SDK version. |
| 687 | if (options.versionStylesAndLayouts) { |
| 688 | versionStylesForCompat(outTable); |
| 689 | } |
| 690 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 691 | { |
| 692 | // Now that everything is merged, let's link it. |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 693 | Linker::Options linkerOptions; |
| 694 | if (options.packageType == AaptOptions::PackageType::StaticLibrary) { |
| 695 | linkerOptions.linkResourceIds = false; |
| 696 | } |
| 697 | Linker linker(outTable, resolver, linkerOptions); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 698 | if (!linker.linkAndValidate()) { |
| 699 | return false; |
| 700 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 701 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 702 | // Verify that all symbols exist. |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 703 | const auto& unresolvedRefs = linker.getUnresolvedReferences(); |
| 704 | if (!unresolvedRefs.empty()) { |
| 705 | for (const auto& entry : unresolvedRefs) { |
| 706 | for (const auto& source : entry.second) { |
| 707 | Logger::error(source) << "unresolved symbol '" << entry.first << "'." |
| 708 | << std::endl; |
| 709 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 710 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 711 | return false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 712 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 713 | } |
| 714 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 715 | // Open the output APK file for writing. |
| 716 | ZipFile outApk; |
| 717 | if (outApk.open(options.output.path.data(), kOpenFlags) != android::NO_ERROR) { |
| 718 | Logger::error(options.output) << "failed to open: " << strerror(errno) << std::endl; |
| 719 | return false; |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 720 | } |
| 721 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 722 | android::ResTable binTable; |
| 723 | if (!compileManifest(options, resolver, binTable, &outApk)) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 724 | return false; |
| 725 | } |
| 726 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 727 | for (; !linkQueue.empty(); linkQueue.pop()) { |
| 728 | const LinkItem& item = linkQueue.front(); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 729 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 730 | assert(!item.originalPackage.empty()); |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 731 | ZipEntry* entry = item.apk->getEntryByName(item.originalPath.data()); |
| 732 | if (!entry) { |
| 733 | Logger::error(item.source) << "failed to find '" << item.originalPath << "'." |
| 734 | << std::endl; |
| 735 | return false; |
| 736 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 737 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 738 | if (util::stringEndsWith<char>(item.originalPath, ".xml")) { |
| 739 | void* uncompressedData = item.apk->uncompress(entry); |
| 740 | assert(uncompressedData); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 741 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 742 | if (!linkXml(options, resolver, item, uncompressedData, entry->getUncompressedLen(), |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 743 | &outApk, &linkQueue)) { |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 744 | Logger::error(options.output) << "failed to link '" << item.originalPath << "'." |
| 745 | << std::endl; |
| 746 | return false; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 747 | } |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 748 | } else { |
| 749 | if (outApk.add(item.apk, entry, buildFileReference(item).data(), 0, nullptr) != |
| 750 | android::NO_ERROR) { |
| 751 | Logger::error(options.output) << "failed to copy '" << item.originalPath << "'." |
| 752 | << std::endl; |
| 753 | return false; |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 754 | } |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | |
| 758 | // Generate the Java class file. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 759 | if (options.generateJavaClass) { |
Adam Lesinski | 6d8e4c4 | 2015-05-01 14:47:28 -0700 | [diff] [blame] | 760 | JavaClassGenerator::Options javaOptions; |
| 761 | if (options.packageType == AaptOptions::PackageType::StaticLibrary) { |
| 762 | javaOptions.useFinal = false; |
| 763 | } |
| 764 | JavaClassGenerator generator(outTable, javaOptions); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 765 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 766 | for (const std::u16string& package : linkedPackages) { |
| 767 | Source outPath = options.generateJavaClass.value(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 768 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 769 | // Build the output directory from the package name. |
| 770 | // Eg. com.android.app -> com/android/app |
| 771 | const std::string packageUtf8 = util::utf16ToUtf8(package); |
| 772 | for (StringPiece part : util::tokenize<char>(packageUtf8, '.')) { |
| 773 | appendPath(&outPath.path, part); |
| 774 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 775 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 776 | if (!mkdirs(outPath.path)) { |
| 777 | Logger::error(outPath) << strerror(errno) << std::endl; |
| 778 | return false; |
| 779 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 780 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 781 | appendPath(&outPath.path, "R.java"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 782 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 783 | if (options.verbose) { |
| 784 | Logger::note(outPath) << "writing Java symbols." << std::endl; |
| 785 | } |
| 786 | |
| 787 | std::ofstream fout(outPath.path); |
| 788 | if (!fout) { |
| 789 | Logger::error(outPath) << strerror(errno) << std::endl; |
| 790 | return false; |
| 791 | } |
| 792 | |
| 793 | if (!generator.generate(package, fout)) { |
| 794 | Logger::error(outPath) << generator.getError() << "." << std::endl; |
| 795 | return false; |
| 796 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 797 | } |
| 798 | } |
| 799 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 800 | outTable->getValueStringPool().prune(); |
| 801 | outTable->getValueStringPool().sort( |
| 802 | [](const StringPool::Entry& a, const StringPool::Entry& b) -> bool { |
| 803 | if (a.context.priority < b.context.priority) { |
| 804 | return true; |
| 805 | } |
| 806 | |
| 807 | if (a.context.priority > b.context.priority) { |
| 808 | return false; |
| 809 | } |
| 810 | return a.value < b.value; |
| 811 | }); |
| 812 | |
| 813 | |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 814 | // Flatten the resource table. |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 815 | TableFlattener::Options flattenerOptions; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 816 | if (options.packageType != AaptOptions::PackageType::StaticLibrary) { |
| 817 | flattenerOptions.useExtendedChunks = false; |
Adam Lesinski | 6d8e4c4 | 2015-05-01 14:47:28 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 820 | if (!writeResourceTable(options, outTable, flattenerOptions, &outApk)) { |
| 821 | return false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 822 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 823 | |
| 824 | outApk.flush(); |
| 825 | return true; |
| 826 | } |
| 827 | |
| 828 | bool compile(const AaptOptions& options, const std::shared_ptr<ResourceTable>& table, |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 829 | const std::shared_ptr<IResolver>& resolver) { |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 830 | std::queue<CompileItem> compileQueue; |
| 831 | bool error = false; |
| 832 | |
| 833 | // Compile all the resource files passed in on the command line. |
| 834 | for (const Source& source : options.input) { |
| 835 | // Need to parse the resource type/config/filename. |
| 836 | Maybe<ResourcePathData> maybePathData = extractResourcePathData(source); |
| 837 | if (!maybePathData) { |
| 838 | return false; |
| 839 | } |
| 840 | |
| 841 | const ResourcePathData& pathData = maybePathData.value(); |
| 842 | if (pathData.resourceDir == u"values") { |
| 843 | // The file is in the values directory, which means its contents will |
| 844 | // go into the resource table. |
| 845 | if (options.verbose) { |
| 846 | Logger::note(source) << "compiling values." << std::endl; |
| 847 | } |
| 848 | |
| 849 | error |= !compileValues(table, source, pathData.config); |
| 850 | } else { |
| 851 | // The file is in a directory like 'layout' or 'drawable'. Find out |
| 852 | // the type. |
| 853 | const ResourceType* type = parseResourceType(pathData.resourceDir); |
| 854 | if (!type) { |
| 855 | Logger::error(source) << "invalid resource type '" << pathData.resourceDir << "'." |
| 856 | << std::endl; |
| 857 | return false; |
| 858 | } |
| 859 | |
| 860 | compileQueue.push(CompileItem{ |
| 861 | source, |
| 862 | ResourceName{ table->getPackage(), *type, pathData.name }, |
| 863 | pathData.config, |
| 864 | pathData.extension |
| 865 | }); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | if (error) { |
| 870 | return false; |
| 871 | } |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 872 | // Open the output APK file for writing. |
| 873 | ZipFile outApk; |
| 874 | if (outApk.open(options.output.path.data(), kOpenFlags) != android::NO_ERROR) { |
| 875 | Logger::error(options.output) << "failed to open: " << strerror(errno) << std::endl; |
| 876 | return false; |
| 877 | } |
| 878 | |
| 879 | // Compile each file. |
| 880 | for (; !compileQueue.empty(); compileQueue.pop()) { |
| 881 | const CompileItem& item = compileQueue.front(); |
| 882 | |
| 883 | // Add the file name to the resource table. |
| 884 | error |= !addFileReference(table, item); |
| 885 | |
| 886 | if (item.extension == "xml") { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 887 | error |= !compileXml(options, table, item, &outApk); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 888 | } else if (item.extension == "png" || item.extension == "9.png") { |
| 889 | error |= !compilePng(options, item, &outApk); |
| 890 | } else { |
| 891 | error |= !copyFile(options, item, &outApk); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | if (error) { |
| 896 | return false; |
| 897 | } |
| 898 | |
| 899 | // Link and assign resource IDs. |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 900 | Linker linker(table, resolver, {}); |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 901 | if (!linker.linkAndValidate()) { |
| 902 | return false; |
| 903 | } |
| 904 | |
| 905 | // Flatten the resource table. |
| 906 | if (!writeResourceTable(options, table, {}, &outApk)) { |
| 907 | return false; |
| 908 | } |
| 909 | |
| 910 | outApk.flush(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 911 | return true; |
| 912 | } |
| 913 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 914 | bool loadAppInfo(const Source& source, AppInfo* outInfo) { |
| 915 | std::ifstream ifs(source.path, std::ifstream::in | std::ifstream::binary); |
| 916 | if (!ifs) { |
| 917 | Logger::error(source) << strerror(errno) << std::endl; |
| 918 | return false; |
| 919 | } |
| 920 | |
| 921 | ManifestParser parser; |
| 922 | std::shared_ptr<XmlPullParser> pullParser = std::make_shared<SourceXmlPullParser>(ifs); |
| 923 | return parser.parse(source, pullParser, outInfo); |
| 924 | } |
| 925 | |
| 926 | static void printCommandsAndDie() { |
| 927 | std::cerr << "The following commands are supported:" << std::endl << std::endl; |
| 928 | std::cerr << "compile compiles a subset of resources" << std::endl; |
| 929 | std::cerr << "link links together compiled resources and libraries" << std::endl; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 930 | std::cerr << "dump dumps resource contents to to standard out" << std::endl; |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 931 | std::cerr << std::endl; |
| 932 | std::cerr << "run aapt2 with one of the commands and the -h flag for extra details." |
| 933 | << std::endl; |
| 934 | exit(1); |
| 935 | } |
| 936 | |
| 937 | static AaptOptions prepareArgs(int argc, char** argv) { |
| 938 | if (argc < 2) { |
| 939 | std::cerr << "no command specified." << std::endl << std::endl; |
| 940 | printCommandsAndDie(); |
| 941 | } |
| 942 | |
| 943 | const StringPiece command(argv[1]); |
| 944 | argc -= 2; |
| 945 | argv += 2; |
| 946 | |
| 947 | AaptOptions options; |
| 948 | |
| 949 | if (command == "--version" || command == "version") { |
| 950 | std::cout << kAaptVersionStr << std::endl; |
| 951 | exit(0); |
| 952 | } else if (command == "link") { |
| 953 | options.phase = AaptOptions::Phase::Link; |
| 954 | } else if (command == "compile") { |
| 955 | options.phase = AaptOptions::Phase::Compile; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 956 | } else if (command == "dump") { |
| 957 | options.phase = AaptOptions::Phase::Dump; |
| 958 | } else if (command == "dump-style-graph") { |
| 959 | options.phase = AaptOptions::Phase::DumpStyleGraph; |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 960 | } else { |
| 961 | std::cerr << "invalid command '" << command << "'." << std::endl << std::endl; |
| 962 | printCommandsAndDie(); |
| 963 | } |
| 964 | |
Adam Lesinski | 6d8e4c4 | 2015-05-01 14:47:28 -0700 | [diff] [blame] | 965 | bool isStaticLib = false; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 966 | if (options.phase == AaptOptions::Phase::Compile || |
| 967 | options.phase == AaptOptions::Phase::Link) { |
| 968 | if (options.phase == AaptOptions::Phase::Compile) { |
| 969 | flag::requiredFlag("--package", "Android package name", |
| 970 | [&options](const StringPiece& arg) { |
| 971 | options.appInfo.package = util::utf8ToUtf16(arg); |
| 972 | }); |
| 973 | } else if (options.phase == AaptOptions::Phase::Link) { |
| 974 | flag::requiredFlag("--manifest", "AndroidManifest.xml of your app", |
| 975 | [&options](const StringPiece& arg) { |
| 976 | options.manifest = Source{ arg.toString() }; |
| 977 | }); |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 978 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 979 | flag::optionalFlag("-I", "add an Android APK to link against", |
| 980 | [&options](const StringPiece& arg) { |
| 981 | options.libraries.push_back(Source{ arg.toString() }); |
| 982 | }); |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 983 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 984 | flag::optionalFlag("--java", "directory in which to generate R.java", |
| 985 | [&options](const StringPiece& arg) { |
| 986 | options.generateJavaClass = Source{ arg.toString() }; |
| 987 | }); |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 988 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 989 | flag::optionalSwitch("--static-lib", "generate a static Android library", true, |
| 990 | &isStaticLib); |
| 991 | |
| 992 | flag::optionalFlag("--binding", "Output directory for binding XML files", |
| 993 | [&options](const StringPiece& arg) { |
| 994 | options.bindingOutput = Source{ arg.toString() }; |
| 995 | }); |
| 996 | flag::optionalSwitch("--no-version", "Disables automatic style and layout versioning", |
| 997 | false, &options.versionStylesAndLayouts); |
| 998 | } |
| 999 | |
| 1000 | // Common flags for all steps. |
| 1001 | flag::requiredFlag("-o", "Output path", [&options](const StringPiece& arg) { |
| 1002 | options.output = Source{ arg.toString() }; |
| 1003 | }); |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 1004 | } |
| 1005 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 1006 | bool help = false; |
| 1007 | flag::optionalSwitch("-v", "enables verbose logging", true, &options.verbose); |
| 1008 | flag::optionalSwitch("-h", "displays this help menu", true, &help); |
| 1009 | |
| 1010 | // Build the command string for output (eg. "aapt2 compile"). |
| 1011 | std::string fullCommand = "aapt2"; |
| 1012 | fullCommand += " "; |
| 1013 | fullCommand += command.toString(); |
| 1014 | |
| 1015 | // Actually read the command line flags. |
| 1016 | flag::parse(argc, argv, fullCommand); |
| 1017 | |
| 1018 | if (help) { |
| 1019 | flag::usageAndDie(fullCommand); |
| 1020 | } |
| 1021 | |
Adam Lesinski | 6d8e4c4 | 2015-05-01 14:47:28 -0700 | [diff] [blame] | 1022 | if (isStaticLib) { |
| 1023 | options.packageType = AaptOptions::PackageType::StaticLibrary; |
| 1024 | } |
| 1025 | |
Adam Lesinski | d5c4f87 | 2015-04-21 13:56:10 -0700 | [diff] [blame] | 1026 | // Copy all the remaining arguments. |
| 1027 | for (const std::string& arg : flag::getArgs()) { |
| 1028 | options.input.push_back(Source{ arg }); |
| 1029 | } |
| 1030 | return options; |
| 1031 | } |
| 1032 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1033 | static bool doDump(const AaptOptions& options) { |
| 1034 | for (const Source& source : options.input) { |
| 1035 | std::unique_ptr<ZipFile> zipFile = util::make_unique<ZipFile>(); |
| 1036 | if (zipFile->open(source.path.data(), ZipFile::kOpenReadOnly) != android::NO_ERROR) { |
| 1037 | Logger::error(source) << "failed to open: " << strerror(errno) << std::endl; |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
| 1041 | std::shared_ptr<ResourceTable> table = std::make_shared<ResourceTable>(); |
| 1042 | std::shared_ptr<ResourceTableResolver> resolver = |
| 1043 | std::make_shared<ResourceTableResolver>( |
| 1044 | table, std::vector<std::shared_ptr<const android::AssetManager>>()); |
| 1045 | |
| 1046 | ZipEntry* entry = zipFile->getEntryByName("resources.arsc"); |
| 1047 | if (!entry) { |
| 1048 | Logger::error(source) << "missing 'resources.arsc'." << std::endl; |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
| 1052 | std::unique_ptr<void, DeleteMalloc> uncompressedData = std::unique_ptr<void, DeleteMalloc>( |
| 1053 | zipFile->uncompress(entry)); |
| 1054 | assert(uncompressedData); |
| 1055 | |
| 1056 | BinaryResourceParser parser(table, resolver, source, uncompressedData.get(), |
| 1057 | entry->getUncompressedLen()); |
| 1058 | if (!parser.parse()) { |
| 1059 | return false; |
| 1060 | } |
| 1061 | |
| 1062 | if (options.phase == AaptOptions::Phase::Dump) { |
| 1063 | Debug::printTable(table); |
| 1064 | } else if (options.phase == AaptOptions::Phase::DumpStyleGraph) { |
| 1065 | Debug::printStyleGraph(table); |
| 1066 | } |
| 1067 | } |
| 1068 | return true; |
| 1069 | } |
| 1070 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1071 | int main(int argc, char** argv) { |
| 1072 | Logger::setLog(std::make_shared<Log>(std::cerr, std::cerr)); |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1073 | AaptOptions options = prepareArgs(argc, argv); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1074 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1075 | if (options.phase == AaptOptions::Phase::Dump || |
| 1076 | options.phase == AaptOptions::Phase::DumpStyleGraph) { |
| 1077 | if (!doDump(options)) { |
| 1078 | return 1; |
| 1079 | } |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1083 | // If we specified a manifest, go ahead and load the package name from the manifest. |
| 1084 | if (!options.manifest.path.empty()) { |
| 1085 | if (!loadAppInfo(options.manifest, &options.appInfo)) { |
| 1086 | return false; |
| 1087 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1088 | } |
| 1089 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1090 | // Verify we have some common options set. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1091 | if (options.appInfo.package.empty()) { |
| 1092 | Logger::error() << "no package name specified." << std::endl; |
| 1093 | return false; |
| 1094 | } |
| 1095 | |
Adam Lesinski | 98aa3ad | 2015-04-06 11:46:52 -0700 | [diff] [blame] | 1096 | // Every phase needs a resource table. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1097 | std::shared_ptr<ResourceTable> table = std::make_shared<ResourceTable>(); |
| 1098 | table->setPackage(options.appInfo.package); |
| 1099 | if (options.appInfo.package == u"android") { |
| 1100 | table->setPackageId(0x01); |
| 1101 | } else { |
| 1102 | table->setPackageId(0x7f); |
| 1103 | } |
| 1104 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1105 | // Load the included libraries. |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1106 | std::vector<std::shared_ptr<const android::AssetManager>> sources; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1107 | for (const Source& source : options.libraries) { |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1108 | std::shared_ptr<android::AssetManager> assetManager = |
| 1109 | std::make_shared<android::AssetManager>(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1110 | int32_t cookie; |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1111 | if (!assetManager->addAssetPath(android::String8(source.path.data()), &cookie)) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1112 | Logger::error(source) << "failed to load library." << std::endl; |
| 1113 | return false; |
| 1114 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1115 | |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1116 | if (cookie == 0) { |
| 1117 | Logger::error(source) << "failed to load library." << std::endl; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1118 | return false; |
| 1119 | } |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1120 | sources.push_back(assetManager); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | // Make the resolver that will cache IDs for us. |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 1124 | std::shared_ptr<ResourceTableResolver> resolver = std::make_shared<ResourceTableResolver>( |
Adam Lesinski | 330edcd | 2015-05-04 17:40:56 -0700 | [diff] [blame] | 1125 | table, sources); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1126 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 1127 | if (options.phase == AaptOptions::Phase::Compile) { |
| 1128 | if (!compile(options, table, resolver)) { |
| 1129 | Logger::error() << "aapt exiting with failures." << std::endl; |
| 1130 | return 1; |
| 1131 | } |
| 1132 | } else if (options.phase == AaptOptions::Phase::Link) { |
| 1133 | if (!link(options, table, resolver)) { |
| 1134 | Logger::error() << "aapt exiting with failures." << std::endl; |
| 1135 | return 1; |
| 1136 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1137 | } |
| 1138 | return 0; |
| 1139 | } |