Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [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 "Debug.h" |
| 19 | #include "Flags.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "NameMangler.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "compile/IdAssigner.h" |
| 22 | #include "flatten/Archive.h" |
| 23 | #include "flatten/TableFlattener.h" |
| 24 | #include "flatten/XmlFlattener.h" |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 25 | #include "io/FileSystem.h" |
| 26 | #include "io/ZipArchive.h" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 27 | #include "java/JavaClassGenerator.h" |
| 28 | #include "java/ManifestClassGenerator.h" |
| 29 | #include "java/ProguardRules.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "link/Linkers.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 31 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 32 | #include "link/ManifestFixer.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | #include "link/TableMerger.h" |
| 34 | #include "process/IResourceTableConsumer.h" |
| 35 | #include "process/SymbolTable.h" |
| 36 | #include "unflatten/BinaryResourceParser.h" |
| 37 | #include "unflatten/FileExportHeaderReader.h" |
| 38 | #include "util/Files.h" |
| 39 | #include "util/StringPiece.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 40 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | |
| 42 | #include <fstream> |
| 43 | #include <sys/stat.h> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 44 | #include <vector> |
| 45 | |
| 46 | namespace aapt { |
| 47 | |
| 48 | struct LinkOptions { |
| 49 | std::string outputPath; |
| 50 | std::string manifestPath; |
| 51 | std::vector<std::string> includePaths; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 52 | std::vector<std::string> overlayFiles; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 53 | Maybe<std::string> generateJavaClassPath; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 54 | Maybe<std::u16string> customJavaPackage; |
| 55 | std::set<std::u16string> extraJavaPackages; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 56 | Maybe<std::string> generateProguardRulesPath; |
| 57 | bool noAutoVersion = false; |
| 58 | bool staticLib = false; |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame^] | 59 | bool generateNonFinalIds = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 60 | bool verbose = false; |
| 61 | bool outputToDirectory = false; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 62 | bool autoAddOverlay = false; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 63 | bool doNotCompressAnything = false; |
| 64 | std::vector<std::string> extensionsToNotCompress; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 65 | Maybe<std::u16string> privateSymbols; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 66 | ManifestFixerOptions manifestFixerOptions; |
| 67 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | struct LinkContext : public IAaptContext { |
| 71 | StdErrDiagnostics mDiagnostics; |
| 72 | std::unique_ptr<NameMangler> mNameMangler; |
| 73 | std::u16string mCompilationPackage; |
| 74 | uint8_t mPackageId; |
| 75 | std::unique_ptr<ISymbolTable> mSymbols; |
| 76 | |
| 77 | IDiagnostics* getDiagnostics() override { |
| 78 | return &mDiagnostics; |
| 79 | } |
| 80 | |
| 81 | NameMangler* getNameMangler() override { |
| 82 | return mNameMangler.get(); |
| 83 | } |
| 84 | |
| 85 | StringPiece16 getCompilationPackage() override { |
| 86 | return mCompilationPackage; |
| 87 | } |
| 88 | |
| 89 | uint8_t getPackageId() override { |
| 90 | return mPackageId; |
| 91 | } |
| 92 | |
| 93 | ISymbolTable* getExternalSymbols() override { |
| 94 | return mSymbols.get(); |
| 95 | } |
| 96 | }; |
| 97 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 98 | class LinkCommand { |
| 99 | public: |
| 100 | LinkCommand(const LinkOptions& options) : |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 101 | mOptions(options), mContext(), mFinalTable(), mFileCollection(nullptr) { |
| 102 | std::unique_ptr<io::FileCollection> fileCollection = |
| 103 | util::make_unique<io::FileCollection>(); |
| 104 | |
| 105 | // Get a pointer to the FileCollection for convenience, but it will be owned by the vector. |
| 106 | mFileCollection = fileCollection.get(); |
| 107 | |
| 108 | // Move it to the collection. |
| 109 | mCollections.push_back(std::move(fileCollection)); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 110 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 111 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Creates a SymbolTable that loads symbols from the various APKs and caches the |
| 114 | * results for faster lookup. |
| 115 | */ |
| 116 | std::unique_ptr<ISymbolTable> createSymbolTableFromIncludePaths() { |
| 117 | AssetManagerSymbolTableBuilder builder; |
| 118 | for (const std::string& path : mOptions.includePaths) { |
| 119 | if (mOptions.verbose) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 120 | mContext.getDiagnostics()->note(DiagMessage(path) << "loading include path"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | std::unique_ptr<android::AssetManager> assetManager = |
| 124 | util::make_unique<android::AssetManager>(); |
| 125 | int32_t cookie = 0; |
| 126 | if (!assetManager->addAssetPath(android::String8(path.data(), path.size()), &cookie)) { |
| 127 | mContext.getDiagnostics()->error( |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 128 | DiagMessage(path) << "failed to load include path"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 129 | return {}; |
| 130 | } |
| 131 | builder.add(std::move(assetManager)); |
| 132 | } |
| 133 | return builder.build(); |
| 134 | } |
| 135 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 136 | std::unique_ptr<ResourceTable> loadTable(const Source& source, const void* data, size_t len) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | std::unique_ptr<ResourceTable> table = util::make_unique<ResourceTable>(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 138 | BinaryResourceParser parser(&mContext, table.get(), source, data, len); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 139 | if (!parser.parse()) { |
| 140 | return {}; |
| 141 | } |
| 142 | return table; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Inflates an XML file from the source path. |
| 147 | */ |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 148 | static std::unique_ptr<xml::XmlResource> loadXml(const std::string& path, IDiagnostics* diag) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 149 | std::ifstream fin(path, std::ifstream::binary); |
| 150 | if (!fin) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 151 | diag->error(DiagMessage(path) << strerror(errno)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 152 | return {}; |
| 153 | } |
| 154 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 155 | return xml::inflate(&fin, diag, Source(path)); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 158 | static std::unique_ptr<xml::XmlResource> loadBinaryXmlSkipFileExport( |
| 159 | const Source& source, |
| 160 | const void* data, size_t len, |
| 161 | IDiagnostics* diag) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 162 | std::string errorStr; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 163 | ssize_t offset = getWrappedDataOffset(data, len, &errorStr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 164 | if (offset < 0) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 165 | diag->error(DiagMessage(source) << errorStr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 166 | return {}; |
| 167 | } |
| 168 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 169 | std::unique_ptr<xml::XmlResource> xmlRes = xml::inflate( |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 170 | reinterpret_cast<const uint8_t*>(data) + static_cast<size_t>(offset), |
| 171 | len - static_cast<size_t>(offset), |
| 172 | diag, |
| 173 | source); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 174 | if (!xmlRes) { |
| 175 | return {}; |
| 176 | } |
| 177 | return xmlRes; |
| 178 | } |
| 179 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 180 | static std::unique_ptr<ResourceFile> loadFileExportHeader(const Source& source, |
| 181 | const void* data, size_t len, |
| 182 | IDiagnostics* diag) { |
| 183 | std::unique_ptr<ResourceFile> resFile = util::make_unique<ResourceFile>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 184 | std::string errorStr; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 185 | ssize_t offset = unwrapFileExportHeader(data, len, resFile.get(), &errorStr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | if (offset < 0) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 187 | diag->error(DiagMessage(source) << errorStr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 188 | return {}; |
| 189 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 190 | return resFile; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 193 | uint32_t getCompressionFlags(const StringPiece& str) { |
| 194 | if (mOptions.doNotCompressAnything) { |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | for (const std::string& extension : mOptions.extensionsToNotCompress) { |
| 199 | if (util::stringEndsWith<char>(str, extension)) { |
| 200 | return 0; |
| 201 | } |
| 202 | } |
| 203 | return ArchiveEntry::kCompress; |
| 204 | } |
| 205 | |
| 206 | bool copyFileToArchive(io::IFile* file, const std::string& outPath, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 207 | IArchiveWriter* writer) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 208 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 209 | if (!data) { |
| 210 | mContext.getDiagnostics()->error(DiagMessage(file->getSource()) |
| 211 | << "failed to open file"); |
| 212 | return false; |
| 213 | } |
| 214 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 215 | std::string errorStr; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 216 | ssize_t offset = getWrappedDataOffset(data->data(), data->size(), &errorStr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 217 | if (offset < 0) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 218 | mContext.getDiagnostics()->error(DiagMessage(file->getSource()) << errorStr); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 222 | if (writer->startEntry(outPath, getCompressionFlags(outPath))) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 223 | if (writer->writeEntry(reinterpret_cast<const uint8_t*>(data->data()) + offset, |
| 224 | data->size() - static_cast<size_t>(offset))) { |
| 225 | if (writer->finishEntry()) { |
| 226 | return true; |
| 227 | } |
| 228 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 229 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 230 | |
| 231 | mContext.getDiagnostics()->error( |
| 232 | DiagMessage(mOptions.outputPath) << "failed to write file " << outPath); |
| 233 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 236 | Maybe<AppInfo> extractAppInfoFromManifest(xml::XmlResource* xmlRes) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 237 | // Make sure the first element is <manifest> with package attribute. |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 238 | if (xml::Element* manifestEl = xml::findRootElement(xmlRes->root.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 239 | if (manifestEl->namespaceUri.empty() && manifestEl->name == u"manifest") { |
| 240 | if (xml::Attribute* packageAttr = manifestEl->findAttribute({}, u"package")) { |
| 241 | return AppInfo{ packageAttr->value }; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | return {}; |
| 246 | } |
| 247 | |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 248 | /** |
| 249 | * Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked. |
| 250 | * Postcondition: ResourceTable has only one package left. All others are stripped, or there |
| 251 | * is an error and false is returned. |
| 252 | */ |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 253 | bool verifyNoExternalPackages() { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 254 | auto isExtPackageFunc = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool { |
| 255 | return mContext.getCompilationPackage() != pkg->name || |
| 256 | !pkg->id || |
| 257 | pkg->id.value() != mContext.getPackageId(); |
| 258 | }; |
| 259 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 260 | bool error = false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 261 | for (const auto& package : mFinalTable.packages) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 262 | if (isExtPackageFunc(package)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 263 | // We have a package that is not related to the one we're building! |
| 264 | for (const auto& type : package->types) { |
| 265 | for (const auto& entry : type->entries) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 266 | ResourceNameRef resName(package->name, type->type, entry->name); |
| 267 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 268 | for (const auto& configValue : entry->values) { |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 269 | // Special case the occurrence of an ID that is being generated for the |
| 270 | // 'android' package. This is due to legacy reasons. |
| 271 | if (valueCast<Id>(configValue.value.get()) && |
| 272 | package->name == u"android") { |
| 273 | mContext.getDiagnostics()->warn( |
| 274 | DiagMessage(configValue.value->getSource()) |
| 275 | << "generated id '" << resName |
| 276 | << "' for external package '" << package->name |
| 277 | << "'"); |
| 278 | } else { |
| 279 | mContext.getDiagnostics()->error( |
| 280 | DiagMessage(configValue.value->getSource()) |
| 281 | << "defined resource '" << resName |
| 282 | << "' for external package '" << package->name |
| 283 | << "'"); |
| 284 | error = true; |
| 285 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 291 | |
| 292 | auto newEndIter = std::remove_if(mFinalTable.packages.begin(), mFinalTable.packages.end(), |
| 293 | isExtPackageFunc); |
| 294 | mFinalTable.packages.erase(newEndIter, mFinalTable.packages.end()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 295 | return !error; |
| 296 | } |
| 297 | |
| 298 | std::unique_ptr<IArchiveWriter> makeArchiveWriter() { |
| 299 | if (mOptions.outputToDirectory) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 300 | return createDirectoryArchiveWriter(mContext.getDiagnostics(), mOptions.outputPath); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 301 | } else { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 302 | return createZipFileArchiveWriter(mContext.getDiagnostics(), mOptions.outputPath); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | bool flattenTable(ResourceTable* table, IArchiveWriter* writer) { |
| 307 | BigBuffer buffer(1024); |
| 308 | TableFlattenerOptions options = {}; |
| 309 | options.useExtendedChunks = mOptions.staticLib; |
| 310 | TableFlattener flattener(&buffer, options); |
| 311 | if (!flattener.consume(&mContext, table)) { |
| 312 | return false; |
| 313 | } |
| 314 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 315 | if (writer->startEntry("resources.arsc", ArchiveEntry::kAlign)) { |
| 316 | if (writer->writeEntry(buffer)) { |
| 317 | if (writer->finishEntry()) { |
| 318 | return true; |
| 319 | } |
| 320 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 321 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 322 | |
| 323 | mContext.getDiagnostics()->error( |
| 324 | DiagMessage() << "failed to write resources.arsc to archive"); |
| 325 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 328 | bool flattenXml(xml::XmlResource* xmlRes, const StringPiece& path, Maybe<size_t> maxSdkLevel, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 329 | IArchiveWriter* writer) { |
| 330 | BigBuffer buffer(1024); |
| 331 | XmlFlattenerOptions options = {}; |
| 332 | options.keepRawValues = mOptions.staticLib; |
| 333 | options.maxSdkLevel = maxSdkLevel; |
| 334 | XmlFlattener flattener(&buffer, options); |
| 335 | if (!flattener.consume(&mContext, xmlRes)) { |
| 336 | return false; |
| 337 | } |
| 338 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 339 | if (writer->startEntry(path, ArchiveEntry::kCompress)) { |
| 340 | if (writer->writeEntry(buffer)) { |
| 341 | if (writer->finishEntry()) { |
| 342 | return true; |
| 343 | } |
| 344 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 345 | } |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 346 | mContext.getDiagnostics()->error( |
| 347 | DiagMessage() << "failed to write " << path << " to archive"); |
| 348 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 351 | bool writeJavaFile(ResourceTable* table, const StringPiece16& packageNameToGenerate, |
| 352 | const StringPiece16& outPackage, JavaClassGeneratorOptions javaOptions) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 353 | if (!mOptions.generateJavaClassPath) { |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | std::string outPath = mOptions.generateJavaClassPath.value(); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 358 | file::appendPath(&outPath, file::packageToPath(util::utf16ToUtf8(outPackage))); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 359 | file::mkdirs(outPath); |
| 360 | file::appendPath(&outPath, "R.java"); |
| 361 | |
| 362 | std::ofstream fout(outPath, std::ofstream::binary); |
| 363 | if (!fout) { |
| 364 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 365 | return false; |
| 366 | } |
| 367 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 368 | JavaClassGenerator generator(table, javaOptions); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 369 | if (!generator.generate(packageNameToGenerate, outPackage, &fout)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 370 | mContext.getDiagnostics()->error(DiagMessage(outPath) << generator.getError()); |
| 371 | return false; |
| 372 | } |
| 373 | return true; |
| 374 | } |
| 375 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 376 | bool writeManifestJavaFile(xml::XmlResource* manifestXml) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 377 | if (!mOptions.generateJavaClassPath) { |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | std::string outPath = mOptions.generateJavaClassPath.value(); |
| 382 | file::appendPath(&outPath, |
| 383 | file::packageToPath(util::utf16ToUtf8(mContext.getCompilationPackage()))); |
| 384 | file::mkdirs(outPath); |
| 385 | file::appendPath(&outPath, "Manifest.java"); |
| 386 | |
| 387 | std::ofstream fout(outPath, std::ofstream::binary); |
| 388 | if (!fout) { |
| 389 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | ManifestClassGenerator generator; |
| 394 | if (!generator.generate(mContext.getDiagnostics(), mContext.getCompilationPackage(), |
| 395 | manifestXml, &fout)) { |
| 396 | return false; |
| 397 | } |
| 398 | |
| 399 | if (!fout) { |
| 400 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 401 | return false; |
| 402 | } |
| 403 | return true; |
| 404 | } |
| 405 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 406 | bool writeProguardFile(const proguard::KeepSet& keepSet) { |
| 407 | if (!mOptions.generateProguardRulesPath) { |
| 408 | return true; |
| 409 | } |
| 410 | |
| 411 | std::ofstream fout(mOptions.generateProguardRulesPath.value(), std::ofstream::binary); |
| 412 | if (!fout) { |
| 413 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | proguard::writeKeepSet(&fout, keepSet); |
| 418 | if (!fout) { |
| 419 | mContext.getDiagnostics()->error(DiagMessage() << strerror(errno)); |
| 420 | return false; |
| 421 | } |
| 422 | return true; |
| 423 | } |
| 424 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 425 | bool mergeStaticLibrary(const std::string& input) { |
| 426 | // TODO(adamlesinski): Load resources from a static library APK and merge the table into |
| 427 | // TableMerger. |
| 428 | mContext.getDiagnostics()->warn(DiagMessage() |
| 429 | << "linking static libraries not supported yet: " |
| 430 | << input); |
| 431 | return true; |
| 432 | } |
| 433 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 434 | bool mergeResourceTable(io::IFile* file, bool override) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 435 | if (mOptions.verbose) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 436 | mContext.getDiagnostics()->note(DiagMessage() << "linking " << file->getSource()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 439 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 440 | if (!data) { |
| 441 | mContext.getDiagnostics()->error(DiagMessage(file->getSource()) |
| 442 | << "failed to open file"); |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | std::unique_ptr<ResourceTable> table = loadTable(file->getSource(), data->data(), |
| 447 | data->size()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 448 | if (!table) { |
| 449 | return false; |
| 450 | } |
| 451 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 452 | bool result = false; |
| 453 | if (override) { |
| 454 | result = mTableMerger->mergeOverlay(file->getSource(), table.get()); |
| 455 | } else { |
| 456 | result = mTableMerger->merge(file->getSource(), table.get()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 457 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 458 | return result; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 461 | bool mergeCompiledFile(io::IFile* file, std::unique_ptr<ResourceFile> fileDesc, bool overlay) { |
| 462 | if (mOptions.verbose) { |
| 463 | mContext.getDiagnostics()->note(DiagMessage() << "adding " << file->getSource()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 466 | bool result = false; |
| 467 | if (overlay) { |
| 468 | result = mTableMerger->mergeFileOverlay(*fileDesc, file); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 469 | } else { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 470 | result = mTableMerger->mergeFile(*fileDesc, file); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 471 | } |
| 472 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 473 | if (!result) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 474 | return false; |
| 475 | } |
| 476 | |
| 477 | // Add the exports of this file to the table. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 478 | for (SourcedResourceName& exportedSymbol : fileDesc->exportedSymbols) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 479 | if (exportedSymbol.name.package.empty()) { |
| 480 | exportedSymbol.name.package = mContext.getCompilationPackage().toString(); |
| 481 | } |
| 482 | |
| 483 | ResourceNameRef resName = exportedSymbol.name; |
| 484 | |
| 485 | Maybe<ResourceName> mangledName = mContext.getNameMangler()->mangleName( |
| 486 | exportedSymbol.name); |
| 487 | if (mangledName) { |
| 488 | resName = mangledName.value(); |
| 489 | } |
| 490 | |
| 491 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 492 | id->setSource(fileDesc->source.withLine(exportedSymbol.line)); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 493 | bool result = mFinalTable.addResourceAllowMangled(resName, {}, std::move(id), |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 494 | mContext.getDiagnostics()); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 495 | if (!result) { |
| 496 | return false; |
| 497 | } |
| 498 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 499 | return true; |
| 500 | } |
| 501 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 502 | /** |
| 503 | * Creates an io::IFileCollection from the ZIP archive and processes the files within. |
| 504 | */ |
| 505 | bool mergeArchive(const std::string& input, bool override) { |
| 506 | std::string errorStr; |
| 507 | std::unique_ptr<io::ZipFileCollection> collection = io::ZipFileCollection::create( |
| 508 | input, &errorStr); |
| 509 | if (!collection) { |
| 510 | mContext.getDiagnostics()->error(DiagMessage(input) << errorStr); |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | bool error = false; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 515 | for (auto iter = collection->iterator(); iter->hasNext(); ) { |
| 516 | if (!processFile(iter->next(), override)) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 517 | error = true; |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | // Make sure to move the collection into the set of IFileCollections. |
| 522 | mCollections.push_back(std::move(collection)); |
| 523 | return !error; |
| 524 | } |
| 525 | |
| 526 | bool processFile(const std::string& path, bool override) { |
Adam Lesinski | 656a577 | 2016-01-14 15:17:41 -0800 | [diff] [blame] | 527 | if (util::stringEndsWith<char>(path, ".flata") || |
| 528 | util::stringEndsWith<char>(path, ".jar") || |
| 529 | util::stringEndsWith<char>(path, ".jack") || |
| 530 | util::stringEndsWith<char>(path, ".zip")) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 531 | return mergeArchive(path, override); |
| 532 | } |
| 533 | |
| 534 | io::IFile* file = mFileCollection->insertFile(path); |
| 535 | return processFile(file, override); |
| 536 | } |
| 537 | |
| 538 | bool processFile(io::IFile* file, bool override) { |
| 539 | const Source& src = file->getSource(); |
| 540 | if (util::stringEndsWith<char>(src.path, ".arsc.flat")) { |
| 541 | return mergeResourceTable(file, override); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 542 | } else if (util::stringEndsWith<char>(src.path, ".flat")){ |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 543 | // Try opening the file and looking for an Export header. |
| 544 | std::unique_ptr<io::IData> data = file->openAsData(); |
| 545 | if (!data) { |
| 546 | mContext.getDiagnostics()->error(DiagMessage(src) << "failed to open"); |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | std::unique_ptr<ResourceFile> resourceFile = loadFileExportHeader( |
| 551 | src, data->data(), data->size(), mContext.getDiagnostics()); |
| 552 | if (resourceFile) { |
| 553 | return mergeCompiledFile(file, std::move(resourceFile), override); |
| 554 | } |
Adam Lesinski | c446a73 | 2016-01-21 11:04:46 -0800 | [diff] [blame] | 555 | |
| 556 | return false; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 557 | } |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 558 | |
Adam Lesinski | c446a73 | 2016-01-21 11:04:46 -0800 | [diff] [blame] | 559 | // Ignore non .flat files. This could be classes.dex or something else that happens |
| 560 | // to be in an archive. |
| 561 | return true; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 562 | } |
| 563 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 564 | int run(const std::vector<std::string>& inputFiles) { |
| 565 | // Load the AndroidManifest.xml |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 566 | std::unique_ptr<xml::XmlResource> manifestXml = loadXml(mOptions.manifestPath, |
| 567 | mContext.getDiagnostics()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 568 | if (!manifestXml) { |
| 569 | return 1; |
| 570 | } |
| 571 | |
| 572 | if (Maybe<AppInfo> maybeAppInfo = extractAppInfoFromManifest(manifestXml.get())) { |
| 573 | mContext.mCompilationPackage = maybeAppInfo.value().package; |
| 574 | } else { |
| 575 | mContext.getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
| 576 | << "no package specified in <manifest> tag"); |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | if (!util::isJavaPackageName(mContext.mCompilationPackage)) { |
| 581 | mContext.getDiagnostics()->error(DiagMessage(mOptions.manifestPath) |
| 582 | << "invalid package name '" |
| 583 | << mContext.mCompilationPackage |
| 584 | << "'"); |
| 585 | return 1; |
| 586 | } |
| 587 | |
| 588 | mContext.mNameMangler = util::make_unique<NameMangler>( |
| 589 | NameManglerPolicy{ mContext.mCompilationPackage }); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 590 | |
| 591 | if (mContext.mCompilationPackage == u"android") { |
| 592 | mContext.mPackageId = 0x01; |
| 593 | } else { |
| 594 | mContext.mPackageId = 0x7f; |
| 595 | } |
| 596 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 597 | mContext.mSymbols = createSymbolTableFromIncludePaths(); |
| 598 | if (!mContext.mSymbols) { |
| 599 | return 1; |
| 600 | } |
| 601 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 602 | TableMergerOptions tableMergerOptions; |
| 603 | tableMergerOptions.autoAddOverlay = mOptions.autoAddOverlay; |
| 604 | mTableMerger = util::make_unique<TableMerger>(&mContext, &mFinalTable, tableMergerOptions); |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 605 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 606 | if (mOptions.verbose) { |
| 607 | mContext.getDiagnostics()->note( |
| 608 | DiagMessage() << "linking package '" << mContext.mCompilationPackage << "' " |
| 609 | << "with package ID " << std::hex << (int) mContext.mPackageId); |
| 610 | } |
| 611 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 612 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 613 | for (const std::string& input : inputFiles) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 614 | if (!processFile(input, false)) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 615 | mContext.getDiagnostics()->error(DiagMessage() << "failed parsing input"); |
| 616 | return 1; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 617 | } |
| 618 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 619 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 620 | for (const std::string& input : mOptions.overlayFiles) { |
| 621 | if (!processFile(input, true)) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 622 | mContext.getDiagnostics()->error(DiagMessage() << "failed parsing overlays"); |
| 623 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 627 | if (!verifyNoExternalPackages()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 628 | return 1; |
| 629 | } |
| 630 | |
| 631 | if (!mOptions.staticLib) { |
| 632 | PrivateAttributeMover mover; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 633 | if (!mover.consume(&mContext, &mFinalTable)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 634 | mContext.getDiagnostics()->error( |
| 635 | DiagMessage() << "failed moving private attributes"); |
| 636 | return 1; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | { |
| 641 | IdAssigner idAssigner; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 642 | if (!idAssigner.consume(&mContext, &mFinalTable)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 643 | mContext.getDiagnostics()->error(DiagMessage() << "failed assigning IDs"); |
| 644 | return 1; |
| 645 | } |
| 646 | } |
| 647 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 648 | mContext.mNameMangler = util::make_unique<NameMangler>(NameManglerPolicy{ |
| 649 | mContext.mCompilationPackage, mTableMerger->getMergedPackages() }); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 650 | mContext.mSymbols = JoinedSymbolTableBuilder() |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 651 | .addSymbolTable(util::make_unique<SymbolTableWrapper>(&mFinalTable)) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 652 | .addSymbolTable(std::move(mContext.mSymbols)) |
| 653 | .build(); |
| 654 | |
| 655 | { |
| 656 | ReferenceLinker linker; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 657 | if (!linker.consume(&mContext, &mFinalTable)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 658 | mContext.getDiagnostics()->error(DiagMessage() << "failed linking references"); |
| 659 | return 1; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | proguard::KeepSet proguardKeepSet; |
| 664 | |
| 665 | std::unique_ptr<IArchiveWriter> archiveWriter = makeArchiveWriter(); |
| 666 | if (!archiveWriter) { |
| 667 | mContext.getDiagnostics()->error(DiagMessage() << "failed to create archive"); |
| 668 | return 1; |
| 669 | } |
| 670 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 671 | bool error = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 672 | { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 673 | ManifestFixer manifestFixer(mOptions.manifestFixerOptions); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 674 | if (!manifestFixer.consume(&mContext, manifestXml.get())) { |
| 675 | error = true; |
| 676 | } |
| 677 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 678 | // AndroidManifest.xml has no resource name, but the CallSite is built from the name |
| 679 | // (aka, which package the AndroidManifest.xml is coming from). |
| 680 | // So we give it a package name so it can see local resources. |
| 681 | manifestXml->file.name.package = mContext.getCompilationPackage().toString(); |
| 682 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 683 | XmlReferenceLinker manifestLinker; |
| 684 | if (manifestLinker.consume(&mContext, manifestXml.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 685 | if (!proguard::collectProguardRulesForManifest(Source(mOptions.manifestPath), |
| 686 | manifestXml.get(), |
| 687 | &proguardKeepSet)) { |
| 688 | error = true; |
| 689 | } |
| 690 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 691 | if (mOptions.generateJavaClassPath) { |
| 692 | if (!writeManifestJavaFile(manifestXml.get())) { |
| 693 | error = true; |
| 694 | } |
| 695 | } |
| 696 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 697 | if (!flattenXml(manifestXml.get(), "AndroidManifest.xml", {}, |
| 698 | archiveWriter.get())) { |
| 699 | error = true; |
| 700 | } |
| 701 | } else { |
| 702 | error = true; |
| 703 | } |
| 704 | } |
| 705 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 706 | if (error) { |
| 707 | mContext.getDiagnostics()->error(DiagMessage() << "failed processing manifest"); |
| 708 | return 1; |
| 709 | } |
| 710 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 711 | for (auto& mergeEntry : mTableMerger->getFilesToMerge()) { |
| 712 | const ResourceKeyRef& key = mergeEntry.first; |
| 713 | const FileToMerge& fileToMerge = mergeEntry.second; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 714 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 715 | const StringPiece path = fileToMerge.file->getSource().path; |
| 716 | |
| 717 | if (key.name.type != ResourceType::kRaw && |
| 718 | (util::stringEndsWith<char>(path, ".xml.flat") || |
| 719 | util::stringEndsWith<char>(path, ".xml"))) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 720 | if (mOptions.verbose) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 721 | mContext.getDiagnostics()->note(DiagMessage() << "linking " << path); |
| 722 | } |
| 723 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 724 | io::IFile* file = fileToMerge.file; |
| 725 | std::unique_ptr<io::IData> data = file->openAsData(); |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 726 | if (!data) { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 727 | mContext.getDiagnostics()->error(DiagMessage(file->getSource()) |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 728 | << "failed to open file"); |
| 729 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 732 | std::unique_ptr<xml::XmlResource> xmlRes; |
| 733 | if (util::stringEndsWith<char>(path, ".flat")) { |
| 734 | xmlRes = loadBinaryXmlSkipFileExport(file->getSource(), |
| 735 | data->data(), data->size(), |
| 736 | mContext.getDiagnostics()); |
| 737 | } else { |
| 738 | xmlRes = xml::inflate(data->data(), data->size(), mContext.getDiagnostics(), |
| 739 | file->getSource()); |
| 740 | } |
| 741 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 742 | if (!xmlRes) { |
| 743 | return 1; |
| 744 | } |
| 745 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 746 | // Create the file description header. |
| 747 | xmlRes->file = ResourceFile{ |
| 748 | key.name.toResourceName(), |
| 749 | key.config, |
| 750 | fileToMerge.originalSource, |
| 751 | }; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 752 | |
| 753 | XmlReferenceLinker xmlLinker; |
| 754 | if (xmlLinker.consume(&mContext, xmlRes.get())) { |
| 755 | if (!proguard::collectProguardRules(xmlRes->file.source, xmlRes.get(), |
| 756 | &proguardKeepSet)) { |
| 757 | error = true; |
| 758 | } |
| 759 | |
| 760 | Maybe<size_t> maxSdkLevel; |
| 761 | if (!mOptions.noAutoVersion) { |
| 762 | maxSdkLevel = std::max<size_t>(xmlRes->file.config.sdkVersion, 1u); |
| 763 | } |
| 764 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 765 | if (!flattenXml(xmlRes.get(), fileToMerge.dstPath, maxSdkLevel, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 766 | archiveWriter.get())) { |
| 767 | error = true; |
| 768 | } |
| 769 | |
| 770 | if (!mOptions.noAutoVersion) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 771 | Maybe<ResourceTable::SearchResult> result = mFinalTable.findResource( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 772 | xmlRes->file.name); |
| 773 | for (int sdkLevel : xmlLinker.getSdkLevels()) { |
| 774 | if (sdkLevel > xmlRes->file.config.sdkVersion && |
| 775 | shouldGenerateVersionedResource(result.value().entry, |
| 776 | xmlRes->file.config, |
| 777 | sdkLevel)) { |
| 778 | xmlRes->file.config.sdkVersion = sdkLevel; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 779 | |
| 780 | std::string genResourcePath = ResourceUtils::buildResourceFileName( |
| 781 | xmlRes->file, mContext.getNameMangler()); |
| 782 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 783 | bool added = mFinalTable.addFileReference( |
| 784 | xmlRes->file.name, |
| 785 | xmlRes->file.config, |
| 786 | xmlRes->file.source, |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 787 | util::utf8ToUtf16(genResourcePath), |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 788 | mContext.getDiagnostics()); |
| 789 | if (!added) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 790 | error = true; |
| 791 | continue; |
| 792 | } |
| 793 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 794 | if (!flattenXml(xmlRes.get(), genResourcePath, sdkLevel, |
| 795 | archiveWriter.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 796 | error = true; |
| 797 | } |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | } else { |
| 803 | error = true; |
| 804 | } |
| 805 | } else { |
| 806 | if (mOptions.verbose) { |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 807 | mContext.getDiagnostics()->note(DiagMessage() << "copying " << path); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 808 | } |
| 809 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 810 | if (!copyFileToArchive(fileToMerge.file, fileToMerge.dstPath, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 811 | archiveWriter.get())) { |
| 812 | error = true; |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | if (error) { |
| 818 | mContext.getDiagnostics()->error(DiagMessage() << "failed linking file resources"); |
| 819 | return 1; |
| 820 | } |
| 821 | |
| 822 | if (!mOptions.noAutoVersion) { |
| 823 | AutoVersioner versioner; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 824 | if (!versioner.consume(&mContext, &mFinalTable)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 825 | mContext.getDiagnostics()->error(DiagMessage() << "failed versioning styles"); |
| 826 | return 1; |
| 827 | } |
| 828 | } |
| 829 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 830 | if (!flattenTable(&mFinalTable, archiveWriter.get())) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 831 | mContext.getDiagnostics()->error(DiagMessage() << "failed to write resources.arsc"); |
| 832 | return 1; |
| 833 | } |
| 834 | |
| 835 | if (mOptions.generateJavaClassPath) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 836 | JavaClassGeneratorOptions options; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 837 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 838 | |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame^] | 839 | if (mOptions.staticLib || mOptions.generateNonFinalIds) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 840 | options.useFinal = false; |
| 841 | } |
| 842 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 843 | const StringPiece16 actualPackage = mContext.getCompilationPackage(); |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 844 | StringPiece16 outputPackage = mContext.getCompilationPackage(); |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 845 | if (mOptions.customJavaPackage) { |
| 846 | // Override the output java package to the custom one. |
| 847 | outputPackage = mOptions.customJavaPackage.value(); |
| 848 | } |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 849 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 850 | if (mOptions.privateSymbols) { |
| 851 | // If we defined a private symbols package, we only emit Public symbols |
| 852 | // to the original package, and private and public symbols to the private package. |
| 853 | |
| 854 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 855 | if (!writeJavaFile(&mFinalTable, mContext.getCompilationPackage(), |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 856 | outputPackage, options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 857 | return 1; |
| 858 | } |
| 859 | |
| 860 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 861 | outputPackage = mOptions.privateSymbols.value(); |
| 862 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 863 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 864 | if (!writeJavaFile(&mFinalTable, actualPackage, outputPackage, options)) { |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 865 | return 1; |
| 866 | } |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 867 | |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 868 | for (const std::u16string& extraPackage : mOptions.extraJavaPackages) { |
| 869 | if (!writeJavaFile(&mFinalTable, actualPackage, extraPackage, options)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 870 | return 1; |
| 871 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 872 | } |
| 873 | } |
| 874 | |
| 875 | if (mOptions.generateProguardRulesPath) { |
| 876 | if (!writeProguardFile(proguardKeepSet)) { |
| 877 | return 1; |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | if (mOptions.verbose) { |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 882 | Debug::printTable(&mFinalTable); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 883 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 884 | return 0; |
| 885 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 886 | |
| 887 | private: |
| 888 | LinkOptions mOptions; |
| 889 | LinkContext mContext; |
| 890 | ResourceTable mFinalTable; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 891 | |
| 892 | ResourceTable mLocalFileTable; |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 893 | std::unique_ptr<TableMerger> mTableMerger; |
| 894 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 895 | // A pointer to the FileCollection representing the filesystem (not archives). |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 896 | io::FileCollection* mFileCollection; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 897 | |
| 898 | // A vector of IFileCollections. This is mainly here to keep ownership of the collections. |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 899 | std::vector<std::unique_ptr<io::IFileCollection>> mCollections; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 900 | }; |
| 901 | |
| 902 | int link(const std::vector<StringPiece>& args) { |
| 903 | LinkOptions options; |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 904 | Maybe<std::string> privateSymbolsPackage; |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 905 | Maybe<std::string> minSdkVersion, targetSdkVersion; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 906 | Maybe<std::string> renameManifestPackage, renameInstrumentationTargetPackage; |
| 907 | Maybe<std::string> versionCode, versionName; |
| 908 | Maybe<std::string> customJavaPackage; |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 909 | std::vector<std::string> extraJavaPackages; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 910 | Flags flags = Flags() |
| 911 | .requiredFlag("-o", "Output path", &options.outputPath) |
| 912 | .requiredFlag("--manifest", "Path to the Android manifest to build", |
| 913 | &options.manifestPath) |
| 914 | .optionalFlagList("-I", "Adds an Android APK to link against", &options.includePaths) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 915 | .optionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n" |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 916 | "The last conflicting resource given takes precedence.", |
| 917 | &options.overlayFiles) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 918 | .optionalFlag("--java", "Directory in which to generate R.java", |
| 919 | &options.generateJavaClassPath) |
| 920 | .optionalFlag("--proguard", "Output file for generated Proguard rules", |
| 921 | &options.generateProguardRulesPath) |
| 922 | .optionalSwitch("--no-auto-version", |
| 923 | "Disables automatic style and layout SDK versioning", |
| 924 | &options.noAutoVersion) |
| 925 | .optionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified " |
| 926 | "by -o", |
| 927 | &options.outputToDirectory) |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 928 | .optionalFlag("--min-sdk-version", "Default minimum SDK version to use for " |
| 929 | "AndroidManifest.xml", &minSdkVersion) |
| 930 | .optionalFlag("--target-sdk-version", "Default target SDK version to use for " |
| 931 | "AndroidManifest.xml", &targetSdkVersion) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 932 | .optionalFlag("--version-code", "Version code (integer) to inject into the " |
| 933 | "AndroidManifest.xml if none is present", &versionCode) |
| 934 | .optionalFlag("--version-name", "Version name to inject into the AndroidManifest.xml " |
| 935 | "if none is present", &versionName) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 936 | .optionalSwitch("--static-lib", "Generate a static Android library", &options.staticLib) |
Adam Lesinski | ef9c501 | 2016-01-22 14:09:53 -0800 | [diff] [blame^] | 937 | .optionalSwitch("--non-final-ids", "Generates R.java without the final modifier.\n" |
| 938 | "This is implied when --static-lib is specified.", |
| 939 | &options.generateNonFinalIds) |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 940 | .optionalFlag("--private-symbols", "Package name to use when generating R.java for " |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 941 | "private symbols.\n" |
| 942 | "If not specified, public and private symbols will use the application's " |
| 943 | "package name", &privateSymbolsPackage) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 944 | .optionalFlag("--custom-package", "Custom Java package under which to generate R.java", |
| 945 | &customJavaPackage) |
Adam Lesinski | 83f2255 | 2015-11-07 11:51:23 -0800 | [diff] [blame] | 946 | .optionalFlagList("--extra-packages", "Generate the same R.java but with different " |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 947 | "package names", &extraJavaPackages) |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 948 | .optionalSwitch("--auto-add-overlay", "Allows the addition of new resources in " |
| 949 | "overlays without <add-resource> tags", &options.autoAddOverlay) |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 950 | .optionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml", |
| 951 | &renameManifestPackage) |
| 952 | .optionalFlag("--rename-instrumentation-target-package", |
| 953 | "Changes the name of the target package for instrumentation. Most useful " |
| 954 | "when used\nin conjunction with --rename-manifest-package", |
| 955 | &renameInstrumentationTargetPackage) |
| 956 | .optionalFlagList("-0", "File extensions not to compress", |
| 957 | &options.extensionsToNotCompress) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 958 | .optionalSwitch("-v", "Enables verbose logging", &options.verbose); |
| 959 | |
| 960 | if (!flags.parse("aapt2 link", args, &std::cerr)) { |
| 961 | return 1; |
| 962 | } |
| 963 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 964 | if (privateSymbolsPackage) { |
| 965 | options.privateSymbols = util::utf8ToUtf16(privateSymbolsPackage.value()); |
| 966 | } |
| 967 | |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 968 | if (minSdkVersion) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 969 | options.manifestFixerOptions.minSdkVersionDefault = |
| 970 | util::utf8ToUtf16(minSdkVersion.value()); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | if (targetSdkVersion) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 974 | options.manifestFixerOptions.targetSdkVersionDefault = |
| 975 | util::utf8ToUtf16(targetSdkVersion.value()); |
| 976 | } |
| 977 | |
| 978 | if (renameManifestPackage) { |
| 979 | options.manifestFixerOptions.renameManifestPackage = |
| 980 | util::utf8ToUtf16(renameManifestPackage.value()); |
| 981 | } |
| 982 | |
| 983 | if (renameInstrumentationTargetPackage) { |
| 984 | options.manifestFixerOptions.renameInstrumentationTargetPackage = |
| 985 | util::utf8ToUtf16(renameInstrumentationTargetPackage.value()); |
| 986 | } |
| 987 | |
| 988 | if (versionCode) { |
| 989 | options.manifestFixerOptions.versionCodeDefault = util::utf8ToUtf16(versionCode.value()); |
| 990 | } |
| 991 | |
| 992 | if (versionName) { |
| 993 | options.manifestFixerOptions.versionNameDefault = util::utf8ToUtf16(versionName.value()); |
| 994 | } |
| 995 | |
| 996 | if (customJavaPackage) { |
| 997 | options.customJavaPackage = util::utf8ToUtf16(customJavaPackage.value()); |
Adam Lesinski | 2ae4a87 | 2015-11-02 16:10:55 -0800 | [diff] [blame] | 998 | } |
| 999 | |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1000 | // Populate the set of extra packages for which to generate R.java. |
| 1001 | for (std::string& extraPackage : extraJavaPackages) { |
| 1002 | // A given package can actually be a colon separated list of packages. |
| 1003 | for (StringPiece package : util::split(extraPackage, ':')) { |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1004 | options.extraJavaPackages.insert(util::utf8ToUtf16(package)); |
Adam Lesinski | fc9570e6 | 2015-11-16 15:07:54 -0800 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1008 | LinkCommand cmd(options); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1009 | return cmd.run(flags.getArgs()); |
| 1010 | } |
| 1011 | |
| 1012 | } // namespace aapt |