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 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 17 | #include "Link.h" |
| 18 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 19 | #include <sys/stat.h> |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 20 | #include <cinttypes> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | |
Mohamed Heikal | d3c5fb6 | 2018-01-12 11:37:26 -0500 | [diff] [blame] | 22 | #include <algorithm> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include <queue> |
| 24 | #include <unordered_map> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include "android-base/errors.h" |
| 28 | #include "android-base/file.h" |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 29 | #include "android-base/stringprintf.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 30 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 31 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | #include "AppInfo.h" |
| 33 | #include "Debug.h" |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 34 | #include "LoadedApk.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 35 | #include "Locale.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 36 | #include "NameMangler.h" |
Adam Lesinski | 59e04c6 | 2016-02-04 15:59:23 -0800 | [diff] [blame] | 37 | #include "ResourceUtils.h" |
Adam Lesinski | d3ffa844 | 2017-09-28 13:34:35 -0700 | [diff] [blame] | 38 | #include "ResourceValues.h" |
| 39 | #include "ValueVisitor.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 40 | #include "cmd/Util.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 41 | #include "compile/IdAssigner.h" |
Adam Lesinski | 2427dce | 2017-11-30 15:10:28 -0800 | [diff] [blame] | 42 | #include "compile/XmlIdCollector.h" |
Adam Lesinski | 6a00817 | 2016-02-02 17:02:58 -0800 | [diff] [blame] | 43 | #include "filter/ConfigFilter.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 44 | #include "format/Archive.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 45 | #include "format/Container.h" |
Adam Lesinski | 4670805 | 2017-09-29 14:49:15 -0700 | [diff] [blame] | 46 | #include "format/binary/TableFlattener.h" |
| 47 | #include "format/binary/XmlFlattener.h" |
| 48 | #include "format/proto/ProtoDeserialize.h" |
| 49 | #include "format/proto/ProtoSerialize.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 50 | #include "io/BigBufferStream.h" |
| 51 | #include "io/FileStream.h" |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 52 | #include "io/FileSystem.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 53 | #include "io/Util.h" |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 54 | #include "io/ZipArchive.h" |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 55 | #include "java/JavaClassGenerator.h" |
| 56 | #include "java/ManifestClassGenerator.h" |
| 57 | #include "java/ProguardRules.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 58 | #include "link/Linkers.h" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | #include "link/ManifestFixer.h" |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 60 | #include "link/NoDefaultResourceRemover.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 61 | #include "link/ReferenceLinker.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 62 | #include "link/TableMerger.h" |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 63 | #include "link/XmlCompatVersioner.h" |
Adam Lesinski | d48944a | 2017-02-21 14:22:30 -0800 | [diff] [blame] | 64 | #include "optimize/ResourceDeduper.h" |
| 65 | #include "optimize/VersionCollapser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 66 | #include "process/IResourceTableConsumer.h" |
| 67 | #include "process/SymbolTable.h" |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 68 | #include "split/TableSplitter.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 69 | #include "util/Files.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 70 | #include "xml/XmlDom.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 72 | using ::aapt::io::FileInputStream; |
| 73 | using ::android::StringPiece; |
| 74 | using ::android::base::StringPrintf; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 75 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 76 | namespace aapt { |
| 77 | |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 78 | class LinkContext : public IAaptContext { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | public: |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 80 | LinkContext(IDiagnostics* diagnostics) |
| 81 | : diagnostics_(diagnostics), name_mangler_({}), symbols_(&name_mangler_) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 82 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 84 | PackageType GetPackageType() override { |
| 85 | return package_type_; |
| 86 | } |
| 87 | |
| 88 | void SetPackageType(PackageType type) { |
| 89 | package_type_ = type; |
| 90 | } |
| 91 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 92 | IDiagnostics* GetDiagnostics() override { |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 93 | return diagnostics_; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 94 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 95 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 96 | NameMangler* GetNameMangler() override { |
| 97 | return &name_mangler_; |
| 98 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 99 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | void SetNameManglerPolicy(const NameManglerPolicy& policy) { |
| 101 | name_mangler_ = NameMangler(policy); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | const std::string& GetCompilationPackage() override { |
| 105 | return compilation_package_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 106 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 107 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | void SetCompilationPackage(const StringPiece& package_name) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 109 | compilation_package_ = package_name.to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 111 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 112 | uint8_t GetPackageId() override { |
| 113 | return package_id_; |
| 114 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 115 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 116 | void SetPackageId(uint8_t id) { |
| 117 | package_id_ = id; |
| 118 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 119 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 120 | SymbolTable* GetExternalSymbols() override { |
| 121 | return &symbols_; |
| 122 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 123 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 124 | bool IsVerbose() override { |
| 125 | return verbose_; |
| 126 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 127 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 128 | void SetVerbose(bool val) { |
| 129 | verbose_ = val; |
| 130 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 131 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 132 | int GetMinSdkVersion() override { |
| 133 | return min_sdk_version_; |
| 134 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 135 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 136 | void SetMinSdkVersion(int minSdk) { |
| 137 | min_sdk_version_ = minSdk; |
| 138 | } |
Adam Lesinski | fb6312f | 2016-06-28 14:40:32 -0700 | [diff] [blame] | 139 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 140 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 141 | DISALLOW_COPY_AND_ASSIGN(LinkContext); |
| 142 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 143 | PackageType package_type_ = PackageType::kApp; |
Chris Warrington | 820d72a | 2017-04-27 15:27:01 +0100 | [diff] [blame] | 144 | IDiagnostics* diagnostics_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 145 | NameMangler name_mangler_; |
| 146 | std::string compilation_package_; |
| 147 | uint8_t package_id_ = 0x0; |
| 148 | SymbolTable symbols_; |
| 149 | bool verbose_ = false; |
| 150 | int min_sdk_version_ = 0; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 153 | // A custom delegate that generates compatible pre-O IDs for use with feature splits. |
| 154 | // Feature splits use package IDs > 7f, which in Java (since Java doesn't have unsigned ints) |
| 155 | // is interpreted as a negative number. Some verification was wrongly assuming negative values |
| 156 | // were invalid. |
| 157 | // |
| 158 | // This delegate will attempt to masquerade any '@id/' references with ID 0xPPTTEEEE, |
| 159 | // where PP > 7f, as 0x7fPPEEEE. Any potential overlapping is verified and an error occurs if such |
| 160 | // an overlap exists. |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 161 | // |
| 162 | // See b/37498913. |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 163 | class FeatureSplitSymbolTableDelegate : public DefaultSymbolTableDelegate { |
| 164 | public: |
| 165 | FeatureSplitSymbolTableDelegate(IAaptContext* context) : context_(context) { |
| 166 | } |
| 167 | |
| 168 | virtual ~FeatureSplitSymbolTableDelegate() = default; |
| 169 | |
| 170 | virtual std::unique_ptr<SymbolTable::Symbol> FindByName( |
| 171 | const ResourceName& name, |
| 172 | const std::vector<std::unique_ptr<ISymbolSource>>& sources) override { |
| 173 | std::unique_ptr<SymbolTable::Symbol> symbol = |
| 174 | DefaultSymbolTableDelegate::FindByName(name, sources); |
| 175 | if (symbol == nullptr) { |
| 176 | return {}; |
| 177 | } |
| 178 | |
| 179 | // Check to see if this is an 'id' with the target package. |
| 180 | if (name.type == ResourceType::kId && symbol->id) { |
| 181 | ResourceId* id = &symbol->id.value(); |
| 182 | if (id->package_id() > kAppPackageId) { |
| 183 | // Rewrite the resource ID to be compatible pre-O. |
| 184 | ResourceId rewritten_id(kAppPackageId, id->package_id(), id->entry_id()); |
| 185 | |
| 186 | // Check that this doesn't overlap another resource. |
| 187 | if (DefaultSymbolTableDelegate::FindById(rewritten_id, sources) != nullptr) { |
| 188 | // The ID overlaps, so log a message (since this is a weird failure) and fail. |
| 189 | context_->GetDiagnostics()->Error(DiagMessage() << "Failed to rewrite " << name |
| 190 | << " for pre-O feature split support"); |
| 191 | return {}; |
| 192 | } |
| 193 | |
| 194 | if (context_->IsVerbose()) { |
| 195 | context_->GetDiagnostics()->Note(DiagMessage() << "rewriting " << name << " (" << *id |
| 196 | << ") -> (" << rewritten_id << ")"); |
| 197 | } |
| 198 | |
| 199 | *id = rewritten_id; |
| 200 | } |
| 201 | } |
| 202 | return symbol; |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | DISALLOW_COPY_AND_ASSIGN(FeatureSplitSymbolTableDelegate); |
| 207 | |
| 208 | IAaptContext* context_; |
| 209 | }; |
| 210 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 211 | static bool FlattenXml(IAaptContext* context, const xml::XmlResource& xml_res, |
| 212 | const StringPiece& path, bool keep_raw_values, bool utf16, |
| 213 | OutputFormat format, IArchiveWriter* writer) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 214 | if (context->IsVerbose()) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 215 | context->GetDiagnostics()->Note(DiagMessage(path) << "writing to archive (keep_raw_values=" |
| 216 | << (keep_raw_values ? "true" : "false") |
| 217 | << ")"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 220 | switch (format) { |
| 221 | case OutputFormat::kApk: { |
| 222 | BigBuffer buffer(1024); |
| 223 | XmlFlattenerOptions options = {}; |
| 224 | options.keep_raw_values = keep_raw_values; |
| 225 | options.use_utf16 = utf16; |
| 226 | XmlFlattener flattener(&buffer, options); |
| 227 | if (!flattener.Consume(context, &xml_res)) { |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | io::BigBufferInputStream input_stream(&buffer); |
| 232 | return io::CopyInputStreamToArchive(context, &input_stream, path.to_string(), |
| 233 | ArchiveEntry::kCompress, writer); |
| 234 | } break; |
| 235 | |
| 236 | case OutputFormat::kProto: { |
| 237 | pb::XmlNode pb_node; |
| 238 | SerializeXmlResourceToPb(xml_res, &pb_node); |
| 239 | return io::CopyProtoToArchive(context, &pb_node, path.to_string(), ArchiveEntry::kCompress, |
| 240 | writer); |
| 241 | } break; |
| 242 | } |
| 243 | return false; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 244 | } |
| 245 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 246 | // Inflates an XML file from the source path. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 247 | static std::unique_ptr<xml::XmlResource> LoadXml(const std::string& path, IDiagnostics* diag) { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 248 | FileInputStream fin(path); |
| 249 | if (fin.HadError()) { |
| 250 | diag->Error(DiagMessage(path) << "failed to load XML file: " << fin.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 251 | return {}; |
| 252 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 253 | return xml::Inflate(&fin, diag, Source(path)); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 256 | struct ResourceFileFlattenerOptions { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 257 | bool no_auto_version = false; |
| 258 | bool no_version_vectors = false; |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 259 | bool no_version_transitions = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 260 | bool no_xml_namespaces = false; |
| 261 | bool keep_raw_values = false; |
| 262 | bool do_not_compress_anything = false; |
| 263 | bool update_proguard_spec = false; |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 264 | OutputFormat output_format = OutputFormat::kApk; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 265 | std::unordered_set<std::string> extensions_to_not_compress; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 266 | }; |
| 267 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 268 | // A sampling of public framework resource IDs. |
| 269 | struct R { |
| 270 | struct attr { |
| 271 | enum : uint32_t { |
| 272 | paddingLeft = 0x010100d6u, |
| 273 | paddingRight = 0x010100d8u, |
| 274 | paddingHorizontal = 0x0101053du, |
| 275 | |
| 276 | paddingTop = 0x010100d7u, |
| 277 | paddingBottom = 0x010100d9u, |
| 278 | paddingVertical = 0x0101053eu, |
| 279 | |
| 280 | layout_marginLeft = 0x010100f7u, |
| 281 | layout_marginRight = 0x010100f9u, |
| 282 | layout_marginHorizontal = 0x0101053bu, |
| 283 | |
| 284 | layout_marginTop = 0x010100f8u, |
| 285 | layout_marginBottom = 0x010100fau, |
| 286 | layout_marginVertical = 0x0101053cu, |
| 287 | }; |
| 288 | }; |
| 289 | }; |
| 290 | |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 291 | class ResourceFileFlattener { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 292 | public: |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 293 | ResourceFileFlattener(const ResourceFileFlattenerOptions& options, IAaptContext* context, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 294 | proguard::KeepSet* keep_set); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 295 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 296 | bool Flatten(ResourceTable* table, IArchiveWriter* archive_writer); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 297 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 298 | private: |
| 299 | struct FileOperation { |
| 300 | ConfigDescription config; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 301 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 302 | // The entry this file came from. |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 303 | ResourceEntry* entry; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 304 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 305 | // The file to copy as-is. |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 306 | io::IFile* file_to_copy; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 307 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 308 | // The XML to process and flatten. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 309 | std::unique_ptr<xml::XmlResource> xml_to_flatten; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 310 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 311 | // The destination to write this file to. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 312 | std::string dst_path; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 313 | }; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 314 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 315 | uint32_t GetCompressionFlags(const StringPiece& str); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 316 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 317 | std::vector<std::unique_ptr<xml::XmlResource>> LinkAndVersionXmlFile(ResourceTable* table, |
| 318 | FileOperation* file_op); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 319 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 320 | ResourceFileFlattenerOptions options_; |
| 321 | IAaptContext* context_; |
| 322 | proguard::KeepSet* keep_set_; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 323 | XmlCompatVersioner::Rules rules_; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 324 | }; |
| 325 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 326 | ResourceFileFlattener::ResourceFileFlattener(const ResourceFileFlattenerOptions& options, |
| 327 | IAaptContext* context, proguard::KeepSet* keep_set) |
| 328 | : options_(options), context_(context), keep_set_(keep_set) { |
| 329 | SymbolTable* symm = context_->GetExternalSymbols(); |
| 330 | |
| 331 | // Build up the rules for degrading newer attributes to older ones. |
| 332 | // NOTE(adamlesinski): These rules are hardcoded right now, but they should be |
| 333 | // generated from the attribute definitions themselves (b/62028956). |
| 334 | if (const SymbolTable::Symbol* s = symm->FindById(R::attr::paddingHorizontal)) { |
| 335 | std::vector<ReplacementAttr> replacements{ |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 336 | {"paddingLeft", R::attr::paddingLeft, Attribute(android::ResTable_map::TYPE_DIMENSION)}, |
| 337 | {"paddingRight", R::attr::paddingRight, Attribute(android::ResTable_map::TYPE_DIMENSION)}, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 338 | }; |
| 339 | rules_[R::attr::paddingHorizontal] = |
| 340 | util::make_unique<DegradeToManyRule>(std::move(replacements)); |
| 341 | } |
| 342 | |
| 343 | if (const SymbolTable::Symbol* s = symm->FindById(R::attr::paddingVertical)) { |
| 344 | std::vector<ReplacementAttr> replacements{ |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 345 | {"paddingTop", R::attr::paddingTop, Attribute(android::ResTable_map::TYPE_DIMENSION)}, |
| 346 | {"paddingBottom", R::attr::paddingBottom, Attribute(android::ResTable_map::TYPE_DIMENSION)}, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 347 | }; |
| 348 | rules_[R::attr::paddingVertical] = |
| 349 | util::make_unique<DegradeToManyRule>(std::move(replacements)); |
| 350 | } |
| 351 | |
| 352 | if (const SymbolTable::Symbol* s = symm->FindById(R::attr::layout_marginHorizontal)) { |
| 353 | std::vector<ReplacementAttr> replacements{ |
| 354 | {"layout_marginLeft", R::attr::layout_marginLeft, |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 355 | Attribute(android::ResTable_map::TYPE_DIMENSION)}, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 356 | {"layout_marginRight", R::attr::layout_marginRight, |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 357 | Attribute(android::ResTable_map::TYPE_DIMENSION)}, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 358 | }; |
| 359 | rules_[R::attr::layout_marginHorizontal] = |
| 360 | util::make_unique<DegradeToManyRule>(std::move(replacements)); |
| 361 | } |
| 362 | |
| 363 | if (const SymbolTable::Symbol* s = symm->FindById(R::attr::layout_marginVertical)) { |
| 364 | std::vector<ReplacementAttr> replacements{ |
| 365 | {"layout_marginTop", R::attr::layout_marginTop, |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 366 | Attribute(android::ResTable_map::TYPE_DIMENSION)}, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 367 | {"layout_marginBottom", R::attr::layout_marginBottom, |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 368 | Attribute(android::ResTable_map::TYPE_DIMENSION)}, |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 369 | }; |
| 370 | rules_[R::attr::layout_marginVertical] = |
| 371 | util::make_unique<DegradeToManyRule>(std::move(replacements)); |
| 372 | } |
| 373 | } |
| 374 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 375 | uint32_t ResourceFileFlattener::GetCompressionFlags(const StringPiece& str) { |
| 376 | if (options_.do_not_compress_anything) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 377 | return 0; |
| 378 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 379 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 380 | for (const std::string& extension : options_.extensions_to_not_compress) { |
| 381 | if (util::EndsWith(str, extension)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 382 | return 0; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 383 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 384 | } |
| 385 | return ArchiveEntry::kCompress; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 388 | static bool IsTransitionElement(const std::string& name) { |
| 389 | return name == "fade" || name == "changeBounds" || name == "slide" || name == "explode" || |
| 390 | name == "changeImageTransform" || name == "changeTransform" || |
| 391 | name == "changeClipBounds" || name == "autoTransition" || name == "recolor" || |
| 392 | name == "changeScroll" || name == "transitionSet" || name == "transition" || |
| 393 | name == "transitionManager"; |
| 394 | } |
| 395 | |
| 396 | static bool IsVectorElement(const std::string& name) { |
| 397 | return name == "vector" || name == "animated-vector" || name == "pathInterpolator" || |
Nick Butcher | e78a816 | 2018-01-09 15:24:21 +0000 | [diff] [blame] | 398 | name == "objectAnimator" || name == "gradient" || name == "animated-selector"; |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 401 | template <typename T> |
| 402 | std::vector<T> make_singleton_vec(T&& val) { |
| 403 | std::vector<T> vec; |
| 404 | vec.emplace_back(std::forward<T>(val)); |
| 405 | return vec; |
| 406 | } |
| 407 | |
| 408 | std::vector<std::unique_ptr<xml::XmlResource>> ResourceFileFlattener::LinkAndVersionXmlFile( |
| 409 | ResourceTable* table, FileOperation* file_op) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 410 | xml::XmlResource* doc = file_op->xml_to_flatten.get(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 411 | const Source& src = doc->file.source; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 412 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 413 | if (context_->IsVerbose()) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 414 | context_->GetDiagnostics()->Note(DiagMessage() |
| 415 | << "linking " << src.path << " (" << doc->file.name << ")"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 418 | // First, strip out any tools namespace attributes. AAPT stripped them out early, which means |
| 419 | // that existing projects have out-of-date references which pass compilation. |
| 420 | xml::StripAndroidStudioAttributes(doc->root.get()); |
| 421 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 422 | XmlReferenceLinker xml_linker; |
| 423 | if (!xml_linker.Consume(context_, doc)) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 424 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 425 | } |
| 426 | |
Ryan Mitchell | 9a2f6e6 | 2018-05-23 14:23:18 -0700 | [diff] [blame] | 427 | if (options_.update_proguard_spec && !proguard::CollectProguardRules(context_, doc, keep_set_)) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 428 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 431 | if (options_.no_xml_namespaces) { |
| 432 | XmlNamespaceRemover namespace_remover; |
| 433 | if (!namespace_remover.Consume(context_, doc)) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 434 | return {}; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 435 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 436 | } |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 437 | |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 438 | if (options_.no_auto_version) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 439 | return make_singleton_vec(std::move(file_op->xml_to_flatten)); |
| 440 | } |
Alexandria Cornwall | a7cc3f1 | 2016-08-16 13:33:32 -0700 | [diff] [blame] | 441 | |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 442 | if (options_.no_version_vectors || options_.no_version_transitions) { |
| 443 | // Skip this if it is a vector or animated-vector. |
Adam Lesinski | 6b37299 | 2017-08-09 10:54:23 -0700 | [diff] [blame] | 444 | xml::Element* el = doc->root.get(); |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 445 | if (el && el->namespace_uri.empty()) { |
| 446 | if ((options_.no_version_vectors && IsVectorElement(el->name)) || |
| 447 | (options_.no_version_transitions && IsTransitionElement(el->name))) { |
| 448 | return make_singleton_vec(std::move(file_op->xml_to_flatten)); |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 453 | const ConfigDescription& config = file_op->config; |
| 454 | ResourceEntry* entry = file_op->entry; |
| 455 | |
| 456 | XmlCompatVersioner xml_compat_versioner(&rules_); |
| 457 | const util::Range<ApiVersion> api_range{config.sdkVersion, |
| 458 | FindNextApiVersionForConfig(entry, config)}; |
| 459 | return xml_compat_versioner.Process(context_, doc, api_range); |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Adam Lesinski | a65bbdf | 2018-02-15 12:39:44 -0800 | [diff] [blame] | 462 | ResourceFile::Type XmlFileTypeForOutputFormat(OutputFormat format) { |
| 463 | switch (format) { |
| 464 | case OutputFormat::kApk: |
| 465 | return ResourceFile::Type::kBinaryXml; |
| 466 | case OutputFormat::kProto: |
| 467 | return ResourceFile::Type::kProtoXml; |
| 468 | } |
| 469 | LOG_ALWAYS_FATAL("unreachable"); |
| 470 | return ResourceFile::Type::kUnknown; |
| 471 | } |
| 472 | |
Ryan Mitchell | 70f7972 | 2018-08-13 07:37:24 -0700 | [diff] [blame] | 473 | static auto kDrawableVersions = std::map<std::string, ApiVersion>{ |
| 474 | { "adaptive-icon" , SDK_O }, |
| 475 | }; |
| 476 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 477 | bool ResourceFileFlattener::Flatten(ResourceTable* table, IArchiveWriter* archive_writer) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 478 | bool error = false; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 479 | std::map<std::pair<ConfigDescription, StringPiece>, FileOperation> config_sorted_files; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 480 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 481 | proguard::CollectResourceReferences(context_, table, keep_set_); |
| 482 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 483 | for (auto& pkg : table->packages) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 484 | CHECK(!pkg->name.empty()) << "Packages must have names when being linked"; |
| 485 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 486 | for (auto& type : pkg->types) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 487 | // Sort by config and name, so that we get better locality in the zip file. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 488 | config_sorted_files.clear(); |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 489 | std::queue<FileOperation> file_operations; |
Adam Lesinski | 355f285 | 2016-02-13 20:26:45 -0800 | [diff] [blame] | 490 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 491 | // Populate the queue with all files in the ResourceTable. |
| 492 | for (auto& entry : type->entries) { |
Adam Lesinski | bb94f32 | 2017-07-12 07:41:55 -0700 | [diff] [blame] | 493 | for (auto& config_value : entry->values) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 494 | // WARNING! Do not insert or remove any resources while executing in this scope. It will |
| 495 | // corrupt the iteration order. |
| 496 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 497 | FileReference* file_ref = ValueCast<FileReference>(config_value->value.get()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 498 | if (!file_ref) { |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 499 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 500 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 501 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 502 | io::IFile* file = file_ref->file; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 503 | if (!file) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 504 | context_->GetDiagnostics()->Error(DiagMessage(file_ref->GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 505 | << "file not found"); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 506 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 509 | FileOperation file_op; |
| 510 | file_op.entry = entry.get(); |
| 511 | file_op.dst_path = *file_ref->path; |
| 512 | file_op.config = config_value->config; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 513 | file_op.file_to_copy = file; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 514 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 515 | if (type->type != ResourceType::kRaw && |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 516 | (file_ref->type == ResourceFile::Type::kBinaryXml || |
| 517 | file_ref->type == ResourceFile::Type::kProtoXml)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 518 | std::unique_ptr<io::IData> data = file->OpenAsData(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 519 | if (!data) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 520 | context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 521 | << "failed to open file"); |
| 522 | return false; |
| 523 | } |
| 524 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 525 | if (file_ref->type == ResourceFile::Type::kProtoXml) { |
| 526 | pb::XmlNode pb_xml_node; |
| 527 | if (!pb_xml_node.ParseFromArray(data->data(), static_cast<int>(data->size()))) { |
| 528 | context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 529 | << "failed to parse proto XML"); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 530 | return false; |
| 531 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 532 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 533 | std::string error; |
| 534 | file_op.xml_to_flatten = DeserializeXmlResourceFromPb(pb_xml_node, &error); |
| 535 | if (file_op.xml_to_flatten == nullptr) { |
| 536 | context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 537 | << "failed to deserialize proto XML: " << error); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 538 | return false; |
| 539 | } |
| 540 | } else { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 541 | std::string error_str; |
| 542 | file_op.xml_to_flatten = xml::Inflate(data->data(), data->size(), &error_str); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 543 | if (file_op.xml_to_flatten == nullptr) { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 544 | context_->GetDiagnostics()->Error(DiagMessage(file->GetSource()) |
| 545 | << "failed to parse binary XML: " << error_str); |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 546 | return false; |
| 547 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 548 | } |
| 549 | |
Adam Lesinski | a65bbdf | 2018-02-15 12:39:44 -0800 | [diff] [blame] | 550 | // Update the type that this file will be written as. |
| 551 | file_ref->type = XmlFileTypeForOutputFormat(options_.output_format); |
| 552 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 553 | file_op.xml_to_flatten->file.config = config_value->config; |
| 554 | file_op.xml_to_flatten->file.source = file_ref->GetSource(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 555 | file_op.xml_to_flatten->file.name = ResourceName(pkg->name, type->type, entry->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 556 | } |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 557 | |
| 558 | // NOTE(adamlesinski): Explicitly construct a StringPiece here, or |
| 559 | // else we end up copying the string in the std::make_pair() method, |
| 560 | // then creating a StringPiece from the copy, which would cause us |
| 561 | // to end up referencing garbage in the map. |
| 562 | const StringPiece entry_name(entry->name); |
| 563 | config_sorted_files[std::make_pair(config_value->config, entry_name)] = |
| 564 | std::move(file_op); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 568 | // Now flatten the sorted values. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 569 | for (auto& map_entry : config_sorted_files) { |
| 570 | const ConfigDescription& config = map_entry.first.first; |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 571 | FileOperation& file_op = map_entry.second; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 572 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 573 | if (file_op.xml_to_flatten) { |
Ryan Mitchell | 70f7972 | 2018-08-13 07:37:24 -0700 | [diff] [blame] | 574 | // Check minimum sdk versions supported for drawables |
| 575 | auto drawable_entry = kDrawableVersions.find(file_op.xml_to_flatten->root->name); |
| 576 | if (drawable_entry != kDrawableVersions.end()) { |
| 577 | if (drawable_entry->second > context_->GetMinSdkVersion() |
| 578 | && drawable_entry->second > config.sdkVersion) { |
| 579 | context_->GetDiagnostics()->Error(DiagMessage(file_op.xml_to_flatten->file.source) |
| 580 | << "<" << drawable_entry->first << "> elements " |
| 581 | << "require a sdk version of at least " |
| 582 | << (int16_t) drawable_entry->second); |
| 583 | error = true; |
| 584 | continue; |
| 585 | } |
| 586 | } |
| 587 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 588 | std::vector<std::unique_ptr<xml::XmlResource>> versioned_docs = |
| 589 | LinkAndVersionXmlFile(table, &file_op); |
Adam Lesinski | c0a5e1e | 2017-08-07 11:56:32 -0700 | [diff] [blame] | 590 | if (versioned_docs.empty()) { |
| 591 | error = true; |
| 592 | continue; |
| 593 | } |
| 594 | |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 595 | for (std::unique_ptr<xml::XmlResource>& doc : versioned_docs) { |
| 596 | std::string dst_path = file_op.dst_path; |
| 597 | if (doc->file.config != file_op.config) { |
| 598 | // Only add the new versioned configurations. |
| 599 | if (context_->IsVerbose()) { |
| 600 | context_->GetDiagnostics()->Note(DiagMessage(doc->file.source) |
| 601 | << "auto-versioning resource from config '" |
| 602 | << config << "' -> '" << doc->file.config << "'"); |
| 603 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 604 | |
Adam Lesinski | a65bbdf | 2018-02-15 12:39:44 -0800 | [diff] [blame] | 605 | const ResourceFile& file = doc->file; |
| 606 | dst_path = ResourceUtils::BuildResourceFileName(file, context_->GetNameMangler()); |
| 607 | |
| 608 | std::unique_ptr<FileReference> file_ref = |
| 609 | util::make_unique<FileReference>(table->string_pool.MakeRef(dst_path)); |
| 610 | file_ref->SetSource(doc->file.source); |
| 611 | // Update the output format of this XML file. |
| 612 | file_ref->type = XmlFileTypeForOutputFormat(options_.output_format); |
| 613 | if (!table->AddResourceMangled(file.name, file.config, {}, std::move(file_ref), |
| 614 | context_->GetDiagnostics())) { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 615 | return false; |
| 616 | } |
| 617 | } |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 618 | |
| 619 | error |= !FlattenXml(context_, *doc, dst_path, options_.keep_raw_values, |
| 620 | false /*utf16*/, options_.output_format, archive_writer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 621 | } |
| 622 | } else { |
Adam Lesinski | c744ae8 | 2017-05-17 19:28:38 -0700 | [diff] [blame] | 623 | error |= !io::CopyFileToArchive(context_, file_op.file_to_copy, file_op.dst_path, |
| 624 | GetCompressionFlags(file_op.dst_path), archive_writer); |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 625 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 626 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 627 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 628 | } |
| 629 | return !error; |
| 630 | } |
| 631 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 632 | static bool WriteStableIdMapToPath(IDiagnostics* diag, |
| 633 | const std::unordered_map<ResourceName, ResourceId>& id_map, |
| 634 | const std::string& id_map_path) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 635 | io::FileOutputStream fout(id_map_path); |
| 636 | if (fout.HadError()) { |
| 637 | diag->Error(DiagMessage(id_map_path) << "failed to open: " << fout.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 638 | return false; |
| 639 | } |
| 640 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 641 | text::Printer printer(&fout); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 642 | for (const auto& entry : id_map) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 643 | const ResourceName& name = entry.first; |
| 644 | const ResourceId& id = entry.second; |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 645 | printer.Print(name.to_string()); |
| 646 | printer.Print(" = "); |
| 647 | printer.Println(id.to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 648 | } |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 649 | fout.Flush(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 650 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 651 | if (fout.HadError()) { |
| 652 | diag->Error(DiagMessage(id_map_path) << "failed writing to file: " << fout.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 653 | return false; |
| 654 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 655 | return true; |
| 656 | } |
| 657 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 658 | static bool LoadStableIdMap(IDiagnostics* diag, const std::string& path, |
| 659 | std::unordered_map<ResourceName, ResourceId>* out_id_map) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 660 | std::string content; |
Adam Lesinski | 2354b56 | 2017-05-26 16:31:38 -0700 | [diff] [blame] | 661 | if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 662 | diag->Error(DiagMessage(path) << "failed reading stable ID file"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 663 | return false; |
| 664 | } |
| 665 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 666 | out_id_map->clear(); |
| 667 | size_t line_no = 0; |
| 668 | for (StringPiece line : util::Tokenize(content, '\n')) { |
| 669 | line_no++; |
| 670 | line = util::TrimWhitespace(line); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 671 | if (line.empty()) { |
| 672 | continue; |
| 673 | } |
| 674 | |
| 675 | auto iter = std::find(line.begin(), line.end(), '='); |
| 676 | if (iter == line.end()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 677 | diag->Error(DiagMessage(Source(path, line_no)) << "missing '='"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 678 | return false; |
| 679 | } |
| 680 | |
| 681 | ResourceNameRef name; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 682 | StringPiece res_name_str = |
| 683 | util::TrimWhitespace(line.substr(0, std::distance(line.begin(), iter))); |
| 684 | if (!ResourceUtils::ParseResourceName(res_name_str, &name)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 685 | diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource name '" << res_name_str |
| 686 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 687 | return false; |
| 688 | } |
| 689 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 690 | const size_t res_id_start_idx = std::distance(line.begin(), iter) + 1; |
| 691 | const size_t res_id_str_len = line.size() - res_id_start_idx; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 692 | StringPiece res_id_str = util::TrimWhitespace(line.substr(res_id_start_idx, res_id_str_len)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 693 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 694 | Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(res_id_str); |
| 695 | if (!maybe_id) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 696 | diag->Error(DiagMessage(Source(path, line_no)) << "invalid resource ID '" << res_id_str |
| 697 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 698 | return false; |
| 699 | } |
| 700 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 701 | (*out_id_map)[name.ToResourceName()] = maybe_id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 702 | } |
| 703 | return true; |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 706 | static int32_t FindFrameworkAssetManagerCookie(const android::AssetManager& assets) { |
| 707 | using namespace android; |
| 708 | |
| 709 | // Find the system package (0x01). AAPT always generates attributes with the type 0x01, so |
| 710 | // we're looking for the first attribute resource in the system package. |
| 711 | const ResTable& table = assets.getResources(true); |
| 712 | Res_value val; |
| 713 | ssize_t idx = table.getResource(0x01010000, &val, true); |
| 714 | if (idx != NO_ERROR) { |
| 715 | // Try as a bag. |
| 716 | const ResTable::bag_entry* entry; |
| 717 | ssize_t cnt = table.lockBag(0x01010000, &entry); |
| 718 | if (cnt >= 0) { |
| 719 | idx = entry->stringBlock; |
| 720 | } |
| 721 | table.unlockBag(entry); |
| 722 | } |
| 723 | |
| 724 | if (idx < 0) { |
| 725 | return 0; |
| 726 | } |
| 727 | return table.getTableCookie(idx); |
| 728 | } |
| 729 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 730 | class Linker { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 731 | public: |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 732 | Linker(LinkContext* context, const LinkOptions& options) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 733 | : options_(options), |
| 734 | context_(context), |
| 735 | final_table_(), |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 736 | file_collection_(util::make_unique<io::FileCollection>()) { |
| 737 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 738 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 739 | void ExtractCompileSdkVersions(android::AssetManager* assets) { |
| 740 | using namespace android; |
| 741 | |
| 742 | int32_t cookie = FindFrameworkAssetManagerCookie(*assets); |
| 743 | if (cookie == 0) { |
| 744 | // No Framework assets loaded. Not a failure. |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | std::unique_ptr<Asset> manifest( |
| 749 | assets->openNonAsset(cookie, kAndroidManifestPath, Asset::AccessMode::ACCESS_BUFFER)); |
| 750 | if (manifest == nullptr) { |
| 751 | // No errors. |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | std::string error; |
| 756 | std::unique_ptr<xml::XmlResource> manifest_xml = |
| 757 | xml::Inflate(manifest->getBuffer(true /*wordAligned*/), manifest->getLength(), &error); |
| 758 | if (manifest_xml == nullptr) { |
| 759 | // No errors. |
| 760 | return; |
| 761 | } |
| 762 | |
Todd Kennedy | 9f6dec1 | 2018-04-20 12:29:29 -0700 | [diff] [blame] | 763 | if (!options_.manifest_fixer_options.compile_sdk_version) { |
| 764 | xml::Attribute* attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionCode"); |
| 765 | if (attr != nullptr) { |
| 766 | Maybe<std::string>& compile_sdk_version = options_.manifest_fixer_options.compile_sdk_version; |
| 767 | if (BinaryPrimitive* prim = ValueCast<BinaryPrimitive>(attr->compiled_value.get())) { |
| 768 | switch (prim->value.dataType) { |
| 769 | case Res_value::TYPE_INT_DEC: |
| 770 | compile_sdk_version = StringPrintf("%" PRId32, static_cast<int32_t>(prim->value.data)); |
| 771 | break; |
| 772 | case Res_value::TYPE_INT_HEX: |
| 773 | compile_sdk_version = StringPrintf("%" PRIx32, prim->value.data); |
| 774 | break; |
| 775 | default: |
| 776 | break; |
| 777 | } |
| 778 | } else if (String* str = ValueCast<String>(attr->compiled_value.get())) { |
| 779 | compile_sdk_version = *str->value; |
| 780 | } else { |
| 781 | compile_sdk_version = attr->value; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 782 | } |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Todd Kennedy | 9f6dec1 | 2018-04-20 12:29:29 -0700 | [diff] [blame] | 786 | if (!options_.manifest_fixer_options.compile_sdk_version_codename) { |
| 787 | xml::Attribute* attr = manifest_xml->root->FindAttribute(xml::kSchemaAndroid, "versionName"); |
| 788 | if (attr != nullptr) { |
| 789 | Maybe<std::string>& compile_sdk_version_codename = |
| 790 | options_.manifest_fixer_options.compile_sdk_version_codename; |
| 791 | if (String* str = ValueCast<String>(attr->compiled_value.get())) { |
| 792 | compile_sdk_version_codename = *str->value; |
| 793 | } else { |
| 794 | compile_sdk_version_codename = attr->value; |
| 795 | } |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 800 | // Creates a SymbolTable that loads symbols from the various APKs. |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 801 | // Pre-condition: context_->GetCompilationPackage() needs to be set. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 802 | bool LoadSymbolsFromIncludePaths() { |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 803 | auto asset_source = util::make_unique<AssetManagerSymbolSource>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 804 | for (const std::string& path : options_.include_paths) { |
| 805 | if (context_->IsVerbose()) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 806 | context_->GetDiagnostics()->Note(DiagMessage() << "including " << path); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 807 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 808 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 809 | std::string error; |
| 810 | auto zip_collection = io::ZipFileCollection::Create(path, &error); |
| 811 | if (zip_collection == nullptr) { |
| 812 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to open APK: " << error); |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | if (zip_collection->FindFile(kProtoResourceTablePath) != nullptr) { |
| 817 | // Load this as a static library include. |
| 818 | std::unique_ptr<LoadedApk> static_apk = LoadedApk::LoadProtoApkFromFileCollection( |
| 819 | Source(path), std::move(zip_collection), context_->GetDiagnostics()); |
| 820 | if (static_apk == nullptr) { |
| 821 | return false; |
| 822 | } |
| 823 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 824 | if (context_->GetPackageType() != PackageType::kStaticLib) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 825 | // Can't include static libraries when not building a static library (they have no IDs |
| 826 | // assigned). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 827 | context_->GetDiagnostics()->Error( |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 828 | DiagMessage(path) << "can't include static library when not building a static lib"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 829 | return false; |
| 830 | } |
| 831 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 832 | ResourceTable* table = static_apk->GetResourceTable(); |
| 833 | |
| 834 | // If we are using --no-static-lib-packages, we need to rename the package of this table to |
| 835 | // our compilation package. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 836 | if (options_.no_static_lib_packages) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 837 | // Since package names can differ, and multiple packages can exist in a ResourceTable, |
| 838 | // we place the requirement that all static libraries are built with the package |
| 839 | // ID 0x7f. So if one is not found, this is an error. |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 840 | if (ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 841 | pkg->name = context_->GetCompilationPackage(); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 842 | } else { |
| 843 | context_->GetDiagnostics()->Error(DiagMessage(path) |
| 844 | << "no package with ID 0x7f found in static library"); |
| 845 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 846 | } |
| 847 | } |
| 848 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 849 | context_->GetExternalSymbols()->AppendSource( |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 850 | util::make_unique<ResourceTableSymbolSource>(table)); |
| 851 | static_library_includes_.push_back(std::move(static_apk)); |
| 852 | } else { |
| 853 | if (!asset_source->AddAssetPath(path)) { |
| 854 | context_->GetDiagnostics()->Error(DiagMessage() |
| 855 | << "failed to load include path " << path); |
| 856 | return false; |
| 857 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
| 860 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 861 | // Capture the shared libraries so that the final resource table can be properly flattened |
| 862 | // with support for shared libraries. |
| 863 | for (auto& entry : asset_source->GetAssignedPackageIds()) { |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 864 | if (entry.first == kAppPackageId) { |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 865 | // Capture the included base feature package. |
| 866 | included_feature_base_ = entry.second; |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 867 | } else if (entry.first == kFrameworkPackageId) { |
| 868 | // Try to embed which version of the framework we're compiling against. |
| 869 | // First check if we should use compileSdkVersion at all. Otherwise compilation may fail |
| 870 | // when linking our synthesized 'android:compileSdkVersion' attribute. |
| 871 | std::unique_ptr<SymbolTable::Symbol> symbol = asset_source->FindByName( |
| 872 | ResourceName("android", ResourceType::kAttr, "compileSdkVersion")); |
| 873 | if (symbol != nullptr && symbol->is_public) { |
| 874 | // The symbol is present and public, extract the android:versionName and |
| 875 | // android:versionCode from the framework AndroidManifest.xml. |
| 876 | ExtractCompileSdkVersions(asset_source->GetAssetManager()); |
| 877 | } |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 878 | } else if (asset_source->IsPackageDynamic(entry.first)) { |
| 879 | final_table_.included_packages_[entry.first] = entry.second; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 883 | context_->GetExternalSymbols()->AppendSource(std::move(asset_source)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 884 | return true; |
| 885 | } |
| 886 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 887 | Maybe<AppInfo> ExtractAppInfoFromManifest(xml::XmlResource* xml_res, IDiagnostics* diag) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 888 | // Make sure the first element is <manifest> with package attribute. |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 889 | xml::Element* manifest_el = xml::FindRootElement(xml_res->root.get()); |
| 890 | if (manifest_el == nullptr) { |
| 891 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 892 | } |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 893 | |
| 894 | AppInfo app_info; |
| 895 | |
| 896 | if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") { |
| 897 | diag->Error(DiagMessage(xml_res->file.source) << "root tag must be <manifest>"); |
| 898 | return {}; |
| 899 | } |
| 900 | |
| 901 | xml::Attribute* package_attr = manifest_el->FindAttribute({}, "package"); |
| 902 | if (!package_attr) { |
| 903 | diag->Error(DiagMessage(xml_res->file.source) |
| 904 | << "<manifest> must have a 'package' attribute"); |
| 905 | return {}; |
| 906 | } |
| 907 | app_info.package = package_attr->value; |
| 908 | |
| 909 | if (xml::Attribute* version_code_attr = |
| 910 | manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCode")) { |
| 911 | Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_attr->value); |
| 912 | if (!maybe_code) { |
| 913 | diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number)) |
| 914 | << "invalid android:versionCode '" << version_code_attr->value << "'"); |
| 915 | return {}; |
| 916 | } |
| 917 | app_info.version_code = maybe_code.value(); |
| 918 | } |
| 919 | |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame] | 920 | if (xml::Attribute* version_code_major_attr = |
| 921 | manifest_el->FindAttribute(xml::kSchemaAndroid, "versionCodeMajor")) { |
| 922 | Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(version_code_major_attr->value); |
| 923 | if (!maybe_code) { |
| 924 | diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number)) |
| 925 | << "invalid android:versionCodeMajor '" |
| 926 | << version_code_major_attr->value << "'"); |
| 927 | return {}; |
| 928 | } |
| 929 | app_info.version_code_major = maybe_code.value(); |
| 930 | } |
| 931 | |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 932 | if (xml::Attribute* revision_code_attr = |
| 933 | manifest_el->FindAttribute(xml::kSchemaAndroid, "revisionCode")) { |
| 934 | Maybe<uint32_t> maybe_code = ResourceUtils::ParseInt(revision_code_attr->value); |
| 935 | if (!maybe_code) { |
| 936 | diag->Error(DiagMessage(xml_res->file.source.WithLine(manifest_el->line_number)) |
| 937 | << "invalid android:revisionCode '" << revision_code_attr->value << "'"); |
| 938 | return {}; |
| 939 | } |
| 940 | app_info.revision_code = maybe_code.value(); |
| 941 | } |
| 942 | |
| 943 | if (xml::Attribute* split_name_attr = manifest_el->FindAttribute({}, "split")) { |
| 944 | if (!split_name_attr->value.empty()) { |
| 945 | app_info.split_name = split_name_attr->value; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) { |
| 950 | if (xml::Attribute* min_sdk = |
| 951 | uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 952 | app_info.min_sdk_version = ResourceUtils::ParseSdkVersion(min_sdk->value); |
Adam Lesinski | b0c47ef | 2017-03-06 20:05:57 -0800 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | return app_info; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 958 | // Precondition: ResourceTable doesn't have any IDs assigned yet, nor is it linked. |
| 959 | // Postcondition: ResourceTable has only one package left. All others are |
| 960 | // stripped, or there is an error and false is returned. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 961 | bool VerifyNoExternalPackages() { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 962 | auto is_ext_package_func = [&](const std::unique_ptr<ResourceTablePackage>& pkg) -> bool { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 963 | return context_->GetCompilationPackage() != pkg->name || !pkg->id || |
| 964 | pkg->id.value() != context_->GetPackageId(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 965 | }; |
| 966 | |
| 967 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 968 | for (const auto& package : final_table_.packages) { |
| 969 | if (is_ext_package_func(package)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 970 | // We have a package that is not related to the one we're building! |
| 971 | for (const auto& type : package->types) { |
| 972 | for (const auto& entry : type->entries) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 973 | ResourceNameRef res_name(package->name, type->type, entry->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 974 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 975 | for (const auto& config_value : entry->values) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 976 | // Special case the occurrence of an ID that is being generated |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 977 | // for the 'android' package. This is due to legacy reasons. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 978 | if (ValueCast<Id>(config_value->value.get()) && package->name == "android") { |
| 979 | context_->GetDiagnostics()->Warn(DiagMessage(config_value->value->GetSource()) |
| 980 | << "generated id '" << res_name |
| 981 | << "' for external package '" << package->name |
| 982 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 983 | } else { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 984 | context_->GetDiagnostics()->Error(DiagMessage(config_value->value->GetSource()) |
| 985 | << "defined resource '" << res_name |
| 986 | << "' for external package '" << package->name |
| 987 | << "'"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 988 | error = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 989 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 990 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 996 | auto new_end_iter = std::remove_if(final_table_.packages.begin(), final_table_.packages.end(), |
| 997 | is_ext_package_func); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 998 | final_table_.packages.erase(new_end_iter, final_table_.packages.end()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 999 | return !error; |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Returns true if no IDs have been set, false otherwise. |
| 1004 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1005 | bool VerifyNoIdsSet() { |
| 1006 | for (const auto& package : final_table_.packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1007 | for (const auto& type : package->types) { |
| 1008 | if (type->id) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1009 | context_->GetDiagnostics()->Error(DiagMessage() << "type " << type->type << " has ID " |
| 1010 | << StringPrintf("%02x", type->id.value()) |
| 1011 | << " assigned"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1012 | return false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1015 | for (const auto& entry : type->entries) { |
| 1016 | if (entry->id) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1017 | ResourceNameRef res_name(package->name, type->type, entry->name); |
| 1018 | context_->GetDiagnostics()->Error( |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1019 | DiagMessage() << "entry " << res_name << " has ID " |
| 1020 | << StringPrintf("%02x", entry->id.value()) << " assigned"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1021 | return false; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | } |
| 1026 | return true; |
| 1027 | } |
| 1028 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1029 | std::unique_ptr<IArchiveWriter> MakeArchiveWriter(const StringPiece& out) { |
| 1030 | if (options_.output_to_directory) { |
| 1031 | return CreateDirectoryArchiveWriter(context_->GetDiagnostics(), out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1032 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1033 | return CreateZipFileArchiveWriter(context_->GetDiagnostics(), out); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1034 | } |
| 1035 | } |
| 1036 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 1037 | bool FlattenTable(ResourceTable* table, OutputFormat format, IArchiveWriter* writer) { |
| 1038 | switch (format) { |
| 1039 | case OutputFormat::kApk: { |
| 1040 | BigBuffer buffer(1024); |
| 1041 | TableFlattener flattener(options_.table_flattener_options, &buffer); |
| 1042 | if (!flattener.Consume(context_, table)) { |
| 1043 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to flatten resource table"); |
| 1044 | return false; |
| 1045 | } |
| 1046 | |
| 1047 | io::BigBufferInputStream input_stream(&buffer); |
| 1048 | return io::CopyInputStreamToArchive(context_, &input_stream, kApkResourceTablePath, |
| 1049 | ArchiveEntry::kAlign, writer); |
| 1050 | } break; |
| 1051 | |
| 1052 | case OutputFormat::kProto: { |
| 1053 | pb::ResourceTable pb_table; |
Ryan Mitchell | a15c2a8 | 2018-03-26 11:05:31 -0700 | [diff] [blame] | 1054 | SerializeTableToPb(*table, &pb_table, context_->GetDiagnostics()); |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 1055 | return io::CopyProtoToArchive(context_, &pb_table, kProtoResourceTablePath, |
| 1056 | ArchiveEntry::kCompress, writer); |
| 1057 | } break; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1058 | } |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 1059 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1062 | bool WriteJavaFile(ResourceTable* table, const StringPiece& package_name_to_generate, |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 1063 | const StringPiece& out_package, const JavaClassGeneratorOptions& java_options, |
Chih-Hung Hsieh | 4dc5812 | 2017-08-03 16:28:10 -0700 | [diff] [blame] | 1064 | const Maybe<std::string>& out_text_symbols_path = {}) { |
Izabela Orlowska | 23a6e1e | 2017-12-05 14:52:07 +0000 | [diff] [blame] | 1065 | if (!options_.generate_java_class_path && !out_text_symbols_path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1066 | return true; |
| 1067 | } |
| 1068 | |
Izabela Orlowska | 23a6e1e | 2017-12-05 14:52:07 +0000 | [diff] [blame] | 1069 | std::string out_path; |
| 1070 | std::unique_ptr<io::FileOutputStream> fout; |
| 1071 | if (options_.generate_java_class_path) { |
| 1072 | out_path = options_.generate_java_class_path.value(); |
| 1073 | file::AppendPath(&out_path, file::PackageToPath(out_package)); |
| 1074 | if (!file::mkdirs(out_path)) { |
| 1075 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1076 | << "failed to create directory '" << out_path << "'"); |
| 1077 | return false; |
| 1078 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1079 | |
Izabela Orlowska | 23a6e1e | 2017-12-05 14:52:07 +0000 | [diff] [blame] | 1080 | file::AppendPath(&out_path, "R.java"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1081 | |
Izabela Orlowska | 23a6e1e | 2017-12-05 14:52:07 +0000 | [diff] [blame] | 1082 | fout = util::make_unique<io::FileOutputStream>(out_path); |
| 1083 | if (fout->HadError()) { |
| 1084 | context_->GetDiagnostics()->Error(DiagMessage() << "failed writing to '" << out_path |
| 1085 | << "': " << fout->GetError()); |
| 1086 | return false; |
| 1087 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 1090 | std::unique_ptr<io::FileOutputStream> fout_text; |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 1091 | if (out_text_symbols_path) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 1092 | fout_text = util::make_unique<io::FileOutputStream>(out_text_symbols_path.value()); |
| 1093 | if (fout_text->HadError()) { |
| 1094 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1095 | << "failed writing to '" << out_text_symbols_path.value() |
| 1096 | << "': " << fout_text->GetError()); |
Adam Lesinski | 418763f | 2017-04-11 17:36:53 -0700 | [diff] [blame] | 1097 | return false; |
| 1098 | } |
| 1099 | } |
| 1100 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1101 | JavaClassGenerator generator(context_, table, java_options); |
Izabela Orlowska | 23a6e1e | 2017-12-05 14:52:07 +0000 | [diff] [blame] | 1102 | if (!generator.Generate(package_name_to_generate, out_package, fout.get(), fout_text.get())) { |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 1103 | context_->GetDiagnostics()->Error(DiagMessage(out_path) << generator.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1104 | return false; |
| 1105 | } |
| 1106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1107 | return true; |
| 1108 | } |
| 1109 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1110 | bool GenerateJavaClasses() { |
| 1111 | // The set of packages whose R class to call in the main classes onResourcesLoaded callback. |
| 1112 | std::vector<std::string> packages_to_callback; |
| 1113 | |
| 1114 | JavaClassGeneratorOptions template_options; |
| 1115 | template_options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 1116 | template_options.javadoc_annotations = options_.javadoc_annotations; |
| 1117 | |
| 1118 | if (context_->GetPackageType() == PackageType::kStaticLib || options_.generate_non_final_ids) { |
| 1119 | template_options.use_final = false; |
| 1120 | } |
| 1121 | |
| 1122 | if (context_->GetPackageType() == PackageType::kSharedLib) { |
| 1123 | template_options.use_final = false; |
| 1124 | template_options.rewrite_callback_options = OnResourcesLoadedCallbackOptions{}; |
| 1125 | } |
| 1126 | |
| 1127 | const StringPiece actual_package = context_->GetCompilationPackage(); |
| 1128 | StringPiece output_package = context_->GetCompilationPackage(); |
| 1129 | if (options_.custom_java_package) { |
| 1130 | // Override the output java package to the custom one. |
| 1131 | output_package = options_.custom_java_package.value(); |
| 1132 | } |
| 1133 | |
| 1134 | // Generate the private symbols if required. |
| 1135 | if (options_.private_symbols) { |
| 1136 | packages_to_callback.push_back(options_.private_symbols.value()); |
| 1137 | |
| 1138 | // If we defined a private symbols package, we only emit Public symbols |
| 1139 | // to the original package, and private and public symbols to the private package. |
| 1140 | JavaClassGeneratorOptions options = template_options; |
| 1141 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublicPrivate; |
| 1142 | if (!WriteJavaFile(&final_table_, actual_package, options_.private_symbols.value(), |
| 1143 | options)) { |
| 1144 | return false; |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | // Generate copies of the original package R class but with different package names. |
| 1149 | // This is to support non-namespaced builds. |
| 1150 | for (const std::string& extra_package : options_.extra_java_packages) { |
| 1151 | packages_to_callback.push_back(extra_package); |
| 1152 | |
| 1153 | JavaClassGeneratorOptions options = template_options; |
| 1154 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 1155 | if (!WriteJavaFile(&final_table_, actual_package, extra_package, options)) { |
| 1156 | return false; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | // Generate R classes for each package that was merged (static library). |
| 1161 | // Use the actual package's resources only. |
| 1162 | for (const std::string& package : table_merger_->merged_packages()) { |
| 1163 | packages_to_callback.push_back(package); |
| 1164 | |
| 1165 | JavaClassGeneratorOptions options = template_options; |
| 1166 | options.types = JavaClassGeneratorOptions::SymbolTypes::kAll; |
| 1167 | if (!WriteJavaFile(&final_table_, package, package, options)) { |
| 1168 | return false; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | // Generate the main public R class. |
| 1173 | JavaClassGeneratorOptions options = template_options; |
| 1174 | |
| 1175 | // Only generate public symbols if we have a private package. |
| 1176 | if (options_.private_symbols) { |
| 1177 | options.types = JavaClassGeneratorOptions::SymbolTypes::kPublic; |
| 1178 | } |
| 1179 | |
| 1180 | if (options.rewrite_callback_options) { |
| 1181 | options.rewrite_callback_options.value().packages_to_callback = |
| 1182 | std::move(packages_to_callback); |
| 1183 | } |
| 1184 | |
| 1185 | if (!WriteJavaFile(&final_table_, actual_package, output_package, options, |
| 1186 | options_.generate_text_symbols_path)) { |
| 1187 | return false; |
| 1188 | } |
| 1189 | |
| 1190 | return true; |
| 1191 | } |
| 1192 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1193 | bool WriteManifestJavaFile(xml::XmlResource* manifest_xml) { |
| 1194 | if (!options_.generate_java_class_path) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1195 | return true; |
| 1196 | } |
| 1197 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1198 | std::unique_ptr<ClassDefinition> manifest_class = |
| 1199 | GenerateManifestClass(context_->GetDiagnostics(), manifest_xml); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1200 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1201 | if (!manifest_class) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1202 | // Something bad happened, but we already logged it, so exit. |
| 1203 | return false; |
| 1204 | } |
| 1205 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1206 | if (manifest_class->empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1207 | // Empty Manifest class, no need to generate it. |
| 1208 | return true; |
| 1209 | } |
| 1210 | |
| 1211 | // Add any JavaDoc annotations to the generated class. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1212 | for (const std::string& annotation : options_.javadoc_annotations) { |
| 1213 | std::string proper_annotation = "@"; |
| 1214 | proper_annotation += annotation; |
| 1215 | manifest_class->GetCommentBuilder()->AppendComment(proper_annotation); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1216 | } |
| 1217 | |
Adam Lesinski | 0d81f70 | 2017-06-27 15:51:09 -0700 | [diff] [blame] | 1218 | const std::string package_utf8 = |
| 1219 | options_.custom_java_package.value_or_default(context_->GetCompilationPackage()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1220 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1221 | std::string out_path = options_.generate_java_class_path.value(); |
| 1222 | file::AppendPath(&out_path, file::PackageToPath(package_utf8)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1223 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1224 | if (!file::mkdirs(out_path)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1225 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to create directory '" << out_path |
| 1226 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1227 | return false; |
| 1228 | } |
| 1229 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1230 | file::AppendPath(&out_path, "Manifest.java"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1231 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 1232 | io::FileOutputStream fout(out_path); |
| 1233 | if (fout.HadError()) { |
| 1234 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to open '" << out_path |
| 1235 | << "': " << fout.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1236 | return false; |
| 1237 | } |
| 1238 | |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 1239 | ClassDefinition::WriteJavaFile(manifest_class.get(), package_utf8, true, &fout); |
| 1240 | fout.Flush(); |
| 1241 | |
| 1242 | if (fout.HadError()) { |
| 1243 | context_->GetDiagnostics()->Error(DiagMessage() << "failed writing to '" << out_path |
| 1244 | << "': " << fout.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1245 | return false; |
| 1246 | } |
| 1247 | return true; |
| 1248 | } |
| 1249 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1250 | bool WriteProguardFile(const Maybe<std::string>& out, const proguard::KeepSet& keep_set) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1251 | if (!out) { |
| 1252 | return true; |
| 1253 | } |
| 1254 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1255 | const std::string& out_path = out.value(); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 1256 | io::FileOutputStream fout(out_path); |
| 1257 | if (fout.HadError()) { |
| 1258 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to open '" << out_path |
| 1259 | << "': " << fout.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1260 | return false; |
| 1261 | } |
| 1262 | |
Ryan Mitchell | 7e5236d | 2018-09-25 15:20:59 -0700 | [diff] [blame] | 1263 | proguard::WriteKeepSet(keep_set, &fout, options_.generate_minimal_proguard_rules); |
Adam Lesinski | a693c4a | 2017-11-09 11:29:39 -0800 | [diff] [blame] | 1264 | fout.Flush(); |
| 1265 | |
| 1266 | if (fout.HadError()) { |
| 1267 | context_->GetDiagnostics()->Error(DiagMessage() << "failed writing to '" << out_path |
| 1268 | << "': " << fout.GetError()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1269 | return false; |
| 1270 | } |
| 1271 | return true; |
| 1272 | } |
| 1273 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1274 | bool MergeStaticLibrary(const std::string& input, bool override) { |
| 1275 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1276 | context_->GetDiagnostics()->Note(DiagMessage() << "merging static library " << input); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1277 | } |
| 1278 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1279 | std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(input, context_->GetDiagnostics()); |
| 1280 | if (apk == nullptr) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1281 | context_->GetDiagnostics()->Error(DiagMessage(input) << "invalid static library"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1282 | return false; |
| 1283 | } |
| 1284 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1285 | ResourceTable* table = apk->GetResourceTable(); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1286 | ResourceTablePackage* pkg = table->FindPackageById(kAppPackageId); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1287 | if (!pkg) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1288 | context_->GetDiagnostics()->Error(DiagMessage(input) << "static library has no package"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1289 | return false; |
| 1290 | } |
| 1291 | |
| 1292 | bool result; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1293 | if (options_.no_static_lib_packages) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1294 | // Merge all resources as if they were in the compilation package. This is the old behavior |
| 1295 | // of aapt. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1296 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1297 | // Add the package to the set of --extra-packages so we emit an R.java for each library |
| 1298 | // package. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1299 | if (!pkg->name.empty()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1300 | options_.extra_java_packages.insert(pkg->name); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1301 | } |
| 1302 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1303 | // Clear the package name, so as to make the resources look like they are coming from the |
| 1304 | // local package. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1305 | pkg->name = ""; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1306 | result = table_merger_->Merge(Source(input), table, override); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1307 | |
| 1308 | } else { |
| 1309 | // This is the proper way to merge libraries, where the package name is |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1310 | // preserved and resource names are mangled. |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1311 | result = table_merger_->MergeAndMangle(Source(input), pkg->name, table); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | if (!result) { |
| 1315 | return false; |
| 1316 | } |
| 1317 | |
| 1318 | // Make sure to move the collection into the set of IFileCollections. |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1319 | merged_apks_.push_back(std::move(apk)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1320 | return true; |
| 1321 | } |
| 1322 | |
Adam Lesinski | 2427dce | 2017-11-30 15:10:28 -0800 | [diff] [blame] | 1323 | bool MergeExportedSymbols(const Source& source, |
| 1324 | const std::vector<SourcedResourceName>& exported_symbols) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1325 | // Add the exports of this file to the table. |
Adam Lesinski | 2427dce | 2017-11-30 15:10:28 -0800 | [diff] [blame] | 1326 | for (const SourcedResourceName& exported_symbol : exported_symbols) { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1327 | ResourceName res_name = exported_symbol.name; |
| 1328 | if (res_name.package.empty()) { |
| 1329 | res_name.package = context_->GetCompilationPackage(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1332 | Maybe<ResourceName> mangled_name = context_->GetNameMangler()->MangleName(res_name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1333 | if (mangled_name) { |
| 1334 | res_name = mangled_name.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
Adam Lesinski | 2427dce | 2017-11-30 15:10:28 -0800 | [diff] [blame] | 1338 | id->SetSource(source.WithLine(exported_symbol.line)); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1339 | bool result = |
| 1340 | final_table_.AddResourceMangled(res_name, ConfigDescription::DefaultConfig(), |
| 1341 | std::string(), std::move(id), context_->GetDiagnostics()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1342 | if (!result) { |
| 1343 | return false; |
| 1344 | } |
| 1345 | } |
| 1346 | return true; |
| 1347 | } |
| 1348 | |
Adam Lesinski | 2427dce | 2017-11-30 15:10:28 -0800 | [diff] [blame] | 1349 | bool MergeCompiledFile(const ResourceFile& compiled_file, io::IFile* file, bool override) { |
| 1350 | if (context_->IsVerbose()) { |
| 1351 | context_->GetDiagnostics()->Note(DiagMessage() |
| 1352 | << "merging '" << compiled_file.name |
| 1353 | << "' from compiled file " << compiled_file.source); |
| 1354 | } |
| 1355 | |
| 1356 | if (!table_merger_->MergeFile(compiled_file, override, file)) { |
| 1357 | return false; |
| 1358 | } |
| 1359 | return MergeExportedSymbols(compiled_file.source, compiled_file.exported_symbols); |
| 1360 | } |
| 1361 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1362 | // Takes a path to load as a ZIP file and merges the files within into the master ResourceTable. |
| 1363 | // If override is true, conflicting resources are allowed to override each other, in order of last |
| 1364 | // seen. |
| 1365 | // An io::IFileCollection is created from the ZIP file and added to the set of |
| 1366 | // io::IFileCollections that are open. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1367 | bool MergeArchive(const std::string& input, bool override) { |
| 1368 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1369 | context_->GetDiagnostics()->Note(DiagMessage() << "merging archive " << input); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1372 | std::string error_str; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1373 | std::unique_ptr<io::ZipFileCollection> collection = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1374 | io::ZipFileCollection::Create(input, &error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1375 | if (!collection) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1376 | context_->GetDiagnostics()->Error(DiagMessage(input) << error_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1377 | return false; |
| 1378 | } |
| 1379 | |
| 1380 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1381 | for (auto iter = collection->Iterator(); iter->HasNext();) { |
| 1382 | if (!MergeFile(iter->Next(), override)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1383 | error = true; |
| 1384 | } |
| 1385 | } |
| 1386 | |
| 1387 | // Make sure to move the collection into the set of IFileCollections. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1388 | collections_.push_back(std::move(collection)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1389 | return !error; |
| 1390 | } |
| 1391 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1392 | // Takes a path to load and merge into the master ResourceTable. If override is true, |
| 1393 | // conflicting resources are allowed to override each other, in order of last seen. |
| 1394 | // If the file path ends with .flata, .jar, .jack, or .zip the file is treated |
| 1395 | // as ZIP archive and the files within are merged individually. |
| 1396 | // Otherwise the file is processed on its own. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1397 | bool MergePath(const std::string& path, bool override) { |
| 1398 | if (util::EndsWith(path, ".flata") || util::EndsWith(path, ".jar") || |
| 1399 | util::EndsWith(path, ".jack") || util::EndsWith(path, ".zip")) { |
| 1400 | return MergeArchive(path, override); |
| 1401 | } else if (util::EndsWith(path, ".apk")) { |
| 1402 | return MergeStaticLibrary(path, override); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1405 | io::IFile* file = file_collection_->InsertFile(path); |
| 1406 | return MergeFile(file, override); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1409 | // Takes an AAPT Container file (.apc/.flat) to load and merge into the master ResourceTable. |
| 1410 | // If override is true, conflicting resources are allowed to override each other, in order of last |
| 1411 | // seen. |
| 1412 | // All other file types are ignored. This is because these files could be coming from a zip, |
| 1413 | // where we could have other files like classes.dex. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1414 | bool MergeFile(io::IFile* file, bool override) { |
| 1415 | const Source& src = file->GetSource(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1416 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1417 | if (util::EndsWith(src.path, ".xml") || util::EndsWith(src.path, ".png")) { |
Adam Lesinski | 6a396c1 | 2016-10-20 14:38:23 -0700 | [diff] [blame] | 1418 | // Since AAPT compiles these file types and appends .flat to them, seeing |
| 1419 | // their raw extensions is a sign that they weren't compiled. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1420 | const StringPiece file_type = util::EndsWith(src.path, ".xml") ? "XML" : "PNG"; |
| 1421 | context_->GetDiagnostics()->Error(DiagMessage(src) << "uncompiled " << file_type |
| 1422 | << " file passed as argument. Must be " |
| 1423 | "compiled first into .flat file."); |
Adam Lesinski | 6a396c1 | 2016-10-20 14:38:23 -0700 | [diff] [blame] | 1424 | return false; |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1425 | } else if (!util::EndsWith(src.path, ".apc") && !util::EndsWith(src.path, ".flat")) { |
| 1426 | if (context_->IsVerbose()) { |
| 1427 | context_->GetDiagnostics()->Warn(DiagMessage(src) << "ignoring unrecognized file"); |
| 1428 | return true; |
| 1429 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1432 | std::unique_ptr<io::InputStream> input_stream = file->OpenInputStream(); |
| 1433 | if (input_stream == nullptr) { |
| 1434 | context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to open file"); |
| 1435 | return false; |
| 1436 | } |
| 1437 | |
| 1438 | if (input_stream->HadError()) { |
| 1439 | context_->GetDiagnostics()->Error(DiagMessage(src) |
| 1440 | << "failed to open file: " << input_stream->GetError()); |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
| 1444 | ContainerReaderEntry* entry; |
| 1445 | ContainerReader reader(input_stream.get()); |
Mohamed Heikal | 1084432 | 2018-02-07 15:17:38 -0500 | [diff] [blame] | 1446 | |
| 1447 | if (reader.HadError()) { |
| 1448 | context_->GetDiagnostics()->Error(DiagMessage(src) |
| 1449 | << "failed to read file: " << reader.GetError()); |
| 1450 | return false; |
| 1451 | } |
| 1452 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1453 | while ((entry = reader.Next()) != nullptr) { |
| 1454 | if (entry->Type() == ContainerEntryType::kResTable) { |
| 1455 | pb::ResourceTable pb_table; |
| 1456 | if (!entry->GetResTable(&pb_table)) { |
| 1457 | context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to read resource table: " |
| 1458 | << entry->GetError()); |
| 1459 | return false; |
| 1460 | } |
| 1461 | |
| 1462 | ResourceTable table; |
| 1463 | std::string error; |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1464 | if (!DeserializeTableFromPb(pb_table, nullptr /*files*/, &table, &error)) { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 1465 | context_->GetDiagnostics()->Error(DiagMessage(src) |
| 1466 | << "failed to deserialize resource table: " << error); |
| 1467 | return false; |
| 1468 | } |
| 1469 | |
| 1470 | if (!table_merger_->Merge(src, &table, override)) { |
| 1471 | context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to merge resource table"); |
| 1472 | return false; |
| 1473 | } |
| 1474 | } else if (entry->Type() == ContainerEntryType::kResFile) { |
| 1475 | pb::internal::CompiledFile pb_compiled_file; |
| 1476 | off64_t offset; |
| 1477 | size_t len; |
| 1478 | if (!entry->GetResFileOffsets(&pb_compiled_file, &offset, &len)) { |
| 1479 | context_->GetDiagnostics()->Error(DiagMessage(src) << "failed to get resource file: " |
| 1480 | << entry->GetError()); |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | ResourceFile resource_file; |
| 1485 | std::string error; |
| 1486 | if (!DeserializeCompiledFileFromPb(pb_compiled_file, &resource_file, &error)) { |
| 1487 | context_->GetDiagnostics()->Error(DiagMessage(src) |
| 1488 | << "failed to read compiled header: " << error); |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
| 1492 | if (!MergeCompiledFile(resource_file, file->CreateFileSegment(offset, len), override)) { |
| 1493 | return false; |
| 1494 | } |
| 1495 | } |
| 1496 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1497 | return true; |
| 1498 | } |
| 1499 | |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1500 | bool CopyAssetsDirsToApk(IArchiveWriter* writer) { |
| 1501 | std::map<std::string, std::unique_ptr<io::RegularFile>> merged_assets; |
| 1502 | for (const std::string& assets_dir : options_.assets_dirs) { |
| 1503 | Maybe<std::vector<std::string>> files = |
| 1504 | file::FindFiles(assets_dir, context_->GetDiagnostics(), nullptr); |
| 1505 | if (!files) { |
| 1506 | return false; |
| 1507 | } |
| 1508 | |
| 1509 | for (const std::string& file : files.value()) { |
| 1510 | std::string full_key = "assets/" + file; |
| 1511 | std::string full_path = assets_dir; |
| 1512 | file::AppendPath(&full_path, file); |
| 1513 | |
| 1514 | auto iter = merged_assets.find(full_key); |
| 1515 | if (iter == merged_assets.end()) { |
| 1516 | merged_assets.emplace(std::move(full_key), |
| 1517 | util::make_unique<io::RegularFile>(Source(std::move(full_path)))); |
| 1518 | } else if (context_->IsVerbose()) { |
| 1519 | context_->GetDiagnostics()->Warn(DiagMessage(iter->second->GetSource()) |
| 1520 | << "asset file overrides '" << full_path << "'"); |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | |
| 1525 | for (auto& entry : merged_assets) { |
| 1526 | uint32_t compression_flags = ArchiveEntry::kCompress; |
| 1527 | std::string extension = file::GetExtension(entry.first).to_string(); |
| 1528 | if (options_.extensions_to_not_compress.count(extension) > 0) { |
| 1529 | compression_flags = 0u; |
| 1530 | } |
| 1531 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1532 | if (!io::CopyFileToArchive(context_, entry.second.get(), entry.first, compression_flags, |
| 1533 | writer)) { |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1534 | return false; |
| 1535 | } |
| 1536 | } |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 1540 | // Writes the AndroidManifest, ResourceTable, and all XML files referenced by the ResourceTable |
| 1541 | // to the IArchiveWriter. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1542 | bool WriteApk(IArchiveWriter* writer, proguard::KeepSet* keep_set, xml::XmlResource* manifest, |
| 1543 | ResourceTable* table) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1544 | const bool keep_raw_values = context_->GetPackageType() == PackageType::kStaticLib; |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 1545 | bool result = FlattenXml(context_, *manifest, "AndroidManifest.xml", keep_raw_values, |
| 1546 | true /*utf16*/, options_.output_format, writer); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1547 | if (!result) { |
| 1548 | return false; |
| 1549 | } |
| 1550 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1551 | ResourceFileFlattenerOptions file_flattener_options; |
| 1552 | file_flattener_options.keep_raw_values = keep_raw_values; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1553 | file_flattener_options.do_not_compress_anything = options_.do_not_compress_anything; |
| 1554 | file_flattener_options.extensions_to_not_compress = options_.extensions_to_not_compress; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1555 | file_flattener_options.no_auto_version = options_.no_auto_version; |
| 1556 | file_flattener_options.no_version_vectors = options_.no_version_vectors; |
Yuichi Araki | 4d35cca | 2017-01-18 20:42:17 +0900 | [diff] [blame] | 1557 | file_flattener_options.no_version_transitions = options_.no_version_transitions; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1558 | file_flattener_options.no_xml_namespaces = options_.no_xml_namespaces; |
| 1559 | file_flattener_options.update_proguard_spec = |
| 1560 | static_cast<bool>(options_.generate_proguard_rules_path); |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 1561 | file_flattener_options.output_format = options_.output_format; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1562 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1563 | ResourceFileFlattener file_flattener(file_flattener_options, context_, keep_set); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1564 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1565 | if (!file_flattener.Flatten(table, writer)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1566 | context_->GetDiagnostics()->Error(DiagMessage() << "failed linking file resources"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1567 | return false; |
| 1568 | } |
| 1569 | |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 1570 | // Hack to fix b/68820737. |
| 1571 | // We need to modify the ResourceTable's package name, but that should NOT affect |
| 1572 | // anything else being generated, which includes the Java classes. |
| 1573 | // If required, the package name is modifed before flattening, and then modified back |
| 1574 | // to its original name. |
| 1575 | ResourceTablePackage* package_to_rewrite = nullptr; |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 1576 | // Pre-O, the platform treats negative resource IDs [those with a package ID of 0x80 |
| 1577 | // or higher] as invalid. In order to work around this limitation, we allow the use |
| 1578 | // of traditionally reserved resource IDs [those between 0x02 and 0x7E]. Allow the |
| 1579 | // definition of what a valid "split" package ID is to account for this. |
| 1580 | const bool isSplitPackage = (options_.allow_reserved_package_id && |
| 1581 | context_->GetPackageId() != kAppPackageId && |
| 1582 | context_->GetPackageId() != kFrameworkPackageId) |
| 1583 | || (!options_.allow_reserved_package_id && context_->GetPackageId() > kAppPackageId); |
| 1584 | if (isSplitPackage && |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 1585 | included_feature_base_ == make_value(context_->GetCompilationPackage())) { |
| 1586 | // The base APK is included, and this is a feature split. If the base package is |
| 1587 | // the same as this package, then we are building an old style Android Instant Apps feature |
| 1588 | // split and must apply this workaround to avoid requiring namespaces support. |
| 1589 | package_to_rewrite = table->FindPackage(context_->GetCompilationPackage()); |
| 1590 | if (package_to_rewrite != nullptr) { |
| 1591 | CHECK_EQ(1u, table->packages.size()) << "can't change name of package when > 1 package"; |
| 1592 | |
| 1593 | std::string new_package_name = |
| 1594 | StringPrintf("%s.%s", package_to_rewrite->name.c_str(), |
| 1595 | app_info_.split_name.value_or_default("feature").c_str()); |
| 1596 | |
| 1597 | if (context_->IsVerbose()) { |
| 1598 | context_->GetDiagnostics()->Note( |
| 1599 | DiagMessage() << "rewriting resource package name for feature split to '" |
| 1600 | << new_package_name << "'"); |
| 1601 | } |
| 1602 | package_to_rewrite->name = new_package_name; |
| 1603 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1604 | } |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 1605 | |
| 1606 | bool success = FlattenTable(table, options_.output_format, writer); |
| 1607 | |
| 1608 | if (package_to_rewrite != nullptr) { |
| 1609 | // Change the name back. |
| 1610 | package_to_rewrite->name = context_->GetCompilationPackage(); |
| 1611 | if (package_to_rewrite->id) { |
| 1612 | table->included_packages_.erase(package_to_rewrite->id.value()); |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | if (!success) { |
| 1617 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to write resource table"); |
| 1618 | } |
| 1619 | return success; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1620 | } |
| 1621 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1622 | int Run(const std::vector<std::string>& input_files) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1623 | // Load the AndroidManifest.xml |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1624 | std::unique_ptr<xml::XmlResource> manifest_xml = |
| 1625 | LoadXml(options_.manifest_path, context_->GetDiagnostics()); |
| 1626 | if (!manifest_xml) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1627 | return 1; |
| 1628 | } |
| 1629 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1630 | // First extract the Package name without modifying it (via --rename-manifest-package). |
| 1631 | if (Maybe<AppInfo> maybe_app_info = |
| 1632 | ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1633 | const AppInfo& app_info = maybe_app_info.value(); |
| 1634 | context_->SetCompilationPackage(app_info.package); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1635 | } |
| 1636 | |
Adam Lesinski | c628437 | 2017-12-04 13:46:23 -0800 | [diff] [blame] | 1637 | // Now that the compilation package is set, load the dependencies. This will also extract |
| 1638 | // the Android framework's versionCode and versionName, if they exist. |
| 1639 | if (!LoadSymbolsFromIncludePaths()) { |
| 1640 | return 1; |
| 1641 | } |
| 1642 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1643 | ManifestFixer manifest_fixer(options_.manifest_fixer_options); |
| 1644 | if (!manifest_fixer.Consume(context_, manifest_xml.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1645 | return 1; |
| 1646 | } |
| 1647 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1648 | Maybe<AppInfo> maybe_app_info = |
| 1649 | ExtractAppInfoFromManifest(manifest_xml.get(), context_->GetDiagnostics()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1650 | if (!maybe_app_info) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1651 | return 1; |
| 1652 | } |
| 1653 | |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 1654 | app_info_ = maybe_app_info.value(); |
| 1655 | context_->SetMinSdkVersion(app_info_.min_sdk_version.value_or_default(0)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1656 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1657 | context_->SetNameManglerPolicy(NameManglerPolicy{context_->GetCompilationPackage()}); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1658 | |
| 1659 | // Override the package ID when it is "android". |
| 1660 | if (context_->GetCompilationPackage() == "android") { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1661 | context_->SetPackageId(0x01); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1662 | |
| 1663 | // Verify we're building a regular app. |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1664 | if (context_->GetPackageType() != PackageType::kApp) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1665 | context_->GetDiagnostics()->Error( |
| 1666 | DiagMessage() << "package 'android' can only be built as a regular app"); |
| 1667 | return 1; |
| 1668 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1669 | } |
| 1670 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1671 | TableMergerOptions table_merger_options; |
| 1672 | table_merger_options.auto_add_overlay = options_.auto_add_overlay; |
Izabela Orlowska | d51efe8 | 2018-04-24 18:18:29 +0100 | [diff] [blame] | 1673 | table_merger_options.strict_visibility = options_.strict_visibility; |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1674 | table_merger_ = util::make_unique<TableMerger>(context_, &final_table_, table_merger_options); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1675 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1676 | if (context_->IsVerbose()) { |
| 1677 | context_->GetDiagnostics()->Note(DiagMessage() |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 1678 | << StringPrintf("linking package '%s' using package ID %02x", |
| 1679 | context_->GetCompilationPackage().data(), |
| 1680 | context_->GetPackageId())); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
Adam Lesinski | 2427dce | 2017-11-30 15:10:28 -0800 | [diff] [blame] | 1683 | // Extract symbols from AndroidManifest.xml, since this isn't merged like the other XML files |
| 1684 | // in res/**/*. |
| 1685 | { |
| 1686 | XmlIdCollector collector; |
| 1687 | if (!collector.Consume(context_, manifest_xml.get())) { |
| 1688 | return false; |
| 1689 | } |
| 1690 | |
| 1691 | if (!MergeExportedSymbols(manifest_xml->file.source, manifest_xml->file.exported_symbols)) { |
| 1692 | return false; |
| 1693 | } |
| 1694 | } |
| 1695 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1696 | for (const std::string& input : input_files) { |
| 1697 | if (!MergePath(input, false)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1698 | context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing input"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1699 | return 1; |
| 1700 | } |
| 1701 | } |
| 1702 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1703 | for (const std::string& input : options_.overlay_files) { |
| 1704 | if (!MergePath(input, true)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1705 | context_->GetDiagnostics()->Error(DiagMessage() << "failed parsing overlays"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1706 | return 1; |
| 1707 | } |
| 1708 | } |
| 1709 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1710 | if (!VerifyNoExternalPackages()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1711 | return 1; |
| 1712 | } |
| 1713 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1714 | if (context_->GetPackageType() != PackageType::kStaticLib) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1715 | PrivateAttributeMover mover; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1716 | if (!mover.Consume(context_, &final_table_)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1717 | context_->GetDiagnostics()->Error(DiagMessage() << "failed moving private attributes"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1718 | return 1; |
| 1719 | } |
| 1720 | |
| 1721 | // Assign IDs if we are building a regular app. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1722 | IdAssigner id_assigner(&options_.stable_id_map); |
| 1723 | if (!id_assigner.Consume(context_, &final_table_)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1724 | context_->GetDiagnostics()->Error(DiagMessage() << "failed assigning IDs"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1725 | return 1; |
| 1726 | } |
| 1727 | |
| 1728 | // Now grab each ID and emit it as a file. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1729 | if (options_.resource_id_map_path) { |
| 1730 | for (auto& package : final_table_.packages) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1731 | for (auto& type : package->types) { |
| 1732 | for (auto& entry : type->entries) { |
| 1733 | ResourceName name(package->name, type->type, entry->name); |
| 1734 | // The IDs are guaranteed to exist. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1735 | options_.stable_id_map[std::move(name)] = |
| 1736 | ResourceId(package->id.value(), type->id.value(), entry->id.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1737 | } |
| 1738 | } |
| 1739 | } |
| 1740 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1741 | if (!WriteStableIdMapToPath(context_->GetDiagnostics(), options_.stable_id_map, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1742 | options_.resource_id_map_path.value())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1743 | return 1; |
| 1744 | } |
| 1745 | } |
| 1746 | } else { |
| 1747 | // Static libs are merged with other apps, and ID collisions are bad, so |
| 1748 | // verify that |
| 1749 | // no IDs have been set. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1750 | if (!VerifyNoIdsSet()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1751 | return 1; |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | // Add the names to mangle based on our source merge earlier. |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1756 | context_->SetNameManglerPolicy( |
| 1757 | NameManglerPolicy{context_->GetCompilationPackage(), table_merger_->merged_packages()}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1758 | |
| 1759 | // Add our table to the symbol table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1760 | context_->GetExternalSymbols()->PrependSource( |
| 1761 | util::make_unique<ResourceTableSymbolSource>(&final_table_)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1762 | |
Adam Lesinski | 1e4b0e5 | 2017-04-27 15:01:10 -0700 | [diff] [blame] | 1763 | // Workaround for pre-O runtime that would treat negative resource IDs |
| 1764 | // (any ID with a package ID > 7f) as invalid. Intercept any ID (PPTTEEEE) with PP > 0x7f |
| 1765 | // and type == 'id', and return the ID 0x7fPPEEEE. IDs don't need to be real resources, they |
| 1766 | // are just identifiers. |
| 1767 | if (context_->GetMinSdkVersion() < SDK_O && context_->GetPackageType() == PackageType::kApp) { |
| 1768 | if (context_->IsVerbose()) { |
| 1769 | context_->GetDiagnostics()->Note(DiagMessage() |
| 1770 | << "enabling pre-O feature split ID rewriting"); |
| 1771 | } |
| 1772 | context_->GetExternalSymbols()->SetDelegate( |
| 1773 | util::make_unique<FeatureSplitSymbolTableDelegate>(context_)); |
| 1774 | } |
| 1775 | |
Adam Lesinski | 34a1687 | 2018-02-23 16:18:10 -0800 | [diff] [blame] | 1776 | // Before we process anything, remove the resources whose default values don't exist. |
| 1777 | // We want to force any references to these to fail the build. |
| 1778 | if (!NoDefaultResourceRemover{}.Consume(context_, &final_table_)) { |
| 1779 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1780 | << "failed removing resources with no defaults"); |
| 1781 | return 1; |
| 1782 | } |
| 1783 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1784 | ReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1785 | if (!linker.Consume(context_, &final_table_)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1786 | context_->GetDiagnostics()->Error(DiagMessage() << "failed linking references"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1787 | return 1; |
| 1788 | } |
| 1789 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1790 | if (context_->GetPackageType() == PackageType::kStaticLib) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1791 | if (!options_.products.empty()) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1792 | context_->GetDiagnostics()->Warn(DiagMessage() |
| 1793 | << "can't select products when building static library"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1794 | } |
| 1795 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1796 | ProductFilter product_filter(options_.products); |
| 1797 | if (!product_filter.Consume(context_, &final_table_)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1798 | context_->GetDiagnostics()->Error(DiagMessage() << "failed stripping products"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1799 | return 1; |
| 1800 | } |
| 1801 | } |
| 1802 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1803 | if (!options_.no_auto_version) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1804 | AutoVersioner versioner; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1805 | if (!versioner.Consume(context_, &final_table_)) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1806 | context_->GetDiagnostics()->Error(DiagMessage() << "failed versioning styles"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1807 | return 1; |
| 1808 | } |
| 1809 | } |
| 1810 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1811 | if (context_->GetPackageType() != PackageType::kStaticLib && context_->GetMinSdkVersion() > 0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1812 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1813 | context_->GetDiagnostics()->Note(DiagMessage() |
| 1814 | << "collapsing resource versions for minimum SDK " |
| 1815 | << context_->GetMinSdkVersion()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | VersionCollapser collapser; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1819 | if (!collapser.Consume(context_, &final_table_)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1820 | return 1; |
| 1821 | } |
| 1822 | } |
| 1823 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1824 | if (!options_.no_resource_deduping) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1825 | ResourceDeduper deduper; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1826 | if (!deduper.Consume(context_, &final_table_)) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1827 | context_->GetDiagnostics()->Error(DiagMessage() << "failed deduping resources"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1828 | return 1; |
| 1829 | } |
| 1830 | } |
| 1831 | |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 1832 | proguard::KeepSet proguard_keep_set = |
| 1833 | proguard::KeepSet(options_.generate_conditional_proguard_rules); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1834 | proguard::KeepSet proguard_main_dex_keep_set; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1835 | |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 1836 | if (context_->GetPackageType() == PackageType::kStaticLib) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1837 | if (options_.table_splitter_options.config_filter != nullptr || |
Pierre Lecesne | 672384b | 2017-02-06 10:29:02 +0000 | [diff] [blame] | 1838 | !options_.table_splitter_options.preferred_densities.empty()) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1839 | context_->GetDiagnostics()->Warn(DiagMessage() |
| 1840 | << "can't strip resources when building static library"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1841 | } |
| 1842 | } else { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1843 | // Adjust the SplitConstraints so that their SDK version is stripped if it is less than or |
| 1844 | // equal to the minSdk. |
Todd Kennedy | 9fbdf89 | 2018-08-28 16:31:15 -0700 | [diff] [blame] | 1845 | const size_t origConstraintSize = options_.split_constraints.size(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1846 | options_.split_constraints = |
| 1847 | AdjustSplitConstraintsForMinSdk(context_->GetMinSdkVersion(), options_.split_constraints); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1848 | |
Todd Kennedy | 9fbdf89 | 2018-08-28 16:31:15 -0700 | [diff] [blame] | 1849 | if (origConstraintSize != options_.split_constraints.size()) { |
| 1850 | context_->GetDiagnostics()->Warn(DiagMessage() |
| 1851 | << "requested to split resources prior to min sdk of " |
| 1852 | << context_->GetMinSdkVersion()); |
| 1853 | } |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1854 | TableSplitter table_splitter(options_.split_constraints, options_.table_splitter_options); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1855 | if (!table_splitter.VerifySplitConstraints(context_)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1856 | return 1; |
| 1857 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1858 | table_splitter.SplitTable(&final_table_); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1859 | |
| 1860 | // Now we need to write out the Split APKs. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1861 | auto path_iter = options_.split_paths.begin(); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1862 | auto split_constraints_iter = options_.split_constraints.begin(); |
| 1863 | for (std::unique_ptr<ResourceTable>& split_table : table_splitter.splits()) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1864 | if (context_->IsVerbose()) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1865 | context_->GetDiagnostics()->Note(DiagMessage(*path_iter) |
| 1866 | << "generating split with configurations '" |
| 1867 | << util::Joiner(split_constraints_iter->configs, ", ") |
| 1868 | << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1869 | } |
| 1870 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1871 | std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(*path_iter); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1872 | if (!archive_writer) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1873 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1874 | return 1; |
| 1875 | } |
| 1876 | |
| 1877 | // Generate an AndroidManifest.xml for each split. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1878 | std::unique_ptr<xml::XmlResource> split_manifest = |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 1879 | GenerateSplitManifest(app_info_, *split_constraints_iter); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1880 | |
| 1881 | XmlReferenceLinker linker; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1882 | if (!linker.Consume(context_, split_manifest.get())) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1883 | context_->GetDiagnostics()->Error(DiagMessage() |
| 1884 | << "failed to create Split AndroidManifest.xml"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1885 | return 1; |
| 1886 | } |
| 1887 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 1888 | if (!WriteApk(archive_writer.get(), &proguard_keep_set, split_manifest.get(), |
| 1889 | split_table.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1890 | return 1; |
| 1891 | } |
| 1892 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1893 | ++path_iter; |
| 1894 | ++split_constraints_iter; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1895 | } |
| 1896 | } |
| 1897 | |
| 1898 | // Start writing the base APK. |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1899 | std::unique_ptr<IArchiveWriter> archive_writer = MakeArchiveWriter(options_.output_path); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1900 | if (!archive_writer) { |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1901 | context_->GetDiagnostics()->Error(DiagMessage() << "failed to create archive"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1902 | return 1; |
| 1903 | } |
| 1904 | |
| 1905 | bool error = false; |
| 1906 | { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1907 | // AndroidManifest.xml has no resource name, but the CallSite is built from the name |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1908 | // (aka, which package the AndroidManifest.xml is coming from). |
| 1909 | // So we give it a package name so it can see local resources. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1910 | manifest_xml->file.name.package = context_->GetCompilationPackage(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1911 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1912 | XmlReferenceLinker manifest_linker; |
| 1913 | if (manifest_linker.Consume(context_, manifest_xml.get())) { |
| 1914 | if (options_.generate_proguard_rules_path && |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 1915 | !proguard::CollectProguardRulesForManifest(manifest_xml.get(), &proguard_keep_set)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1916 | error = true; |
| 1917 | } |
| 1918 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1919 | if (options_.generate_main_dex_proguard_rules_path && |
Adam Koski | dc21dea | 2017-07-21 10:55:27 -0700 | [diff] [blame] | 1920 | !proguard::CollectProguardRulesForManifest(manifest_xml.get(), |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1921 | &proguard_main_dex_keep_set, true)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1922 | error = true; |
Alexandria Cornwall | 637b482 | 2016-08-11 09:53:16 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1925 | if (options_.generate_java_class_path) { |
| 1926 | if (!WriteManifestJavaFile(manifest_xml.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1927 | error = true; |
| 1928 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1929 | } |
| 1930 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1931 | if (options_.no_xml_namespaces) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1932 | // PackageParser will fail if URIs are removed from |
| 1933 | // AndroidManifest.xml. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1934 | XmlNamespaceRemover namespace_remover(true /* keepUris */); |
| 1935 | if (!namespace_remover.Consume(context_, manifest_xml.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1936 | error = true; |
| 1937 | } |
Rohit Agrawal | e49bb30 | 2016-04-22 12:27:55 -0700 | [diff] [blame] | 1938 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1939 | } else { |
| 1940 | error = true; |
| 1941 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1942 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1943 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1944 | if (error) { |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1945 | context_->GetDiagnostics()->Error(DiagMessage() << "failed processing manifest"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1946 | return 1; |
| 1947 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1948 | |
Adam Lesinski | b39ad7c | 2017-03-13 11:40:48 -0700 | [diff] [blame] | 1949 | if (!WriteApk(archive_writer.get(), &proguard_keep_set, manifest_xml.get(), &final_table_)) { |
| 1950 | return 1; |
| 1951 | } |
| 1952 | |
| 1953 | if (!CopyAssetsDirsToApk(archive_writer.get())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1954 | return 1; |
| 1955 | } |
Adam Lesinski | fb48d29 | 2015-11-07 15:52:13 -0800 | [diff] [blame] | 1956 | |
Izabela Orlowska | 23a6e1e | 2017-12-05 14:52:07 +0000 | [diff] [blame] | 1957 | if (options_.generate_java_class_path || options_.generate_text_symbols_path) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1958 | if (!GenerateJavaClasses()) { |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1959 | return 1; |
| 1960 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Adam Lesinski | b5dc4bd | 2017-02-22 19:29:29 -0800 | [diff] [blame] | 1963 | if (!WriteProguardFile(options_.generate_proguard_rules_path, proguard_keep_set)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1964 | return 1; |
| 1965 | } |
| 1966 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1967 | if (!WriteProguardFile(options_.generate_main_dex_proguard_rules_path, |
| 1968 | proguard_main_dex_keep_set)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1969 | return 1; |
| 1970 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1971 | return 0; |
| 1972 | } |
| 1973 | |
| 1974 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1975 | LinkOptions options_; |
| 1976 | LinkContext* context_; |
| 1977 | ResourceTable final_table_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1978 | |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 1979 | AppInfo app_info_; |
| 1980 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1981 | std::unique_ptr<TableMerger> table_merger_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1982 | |
| 1983 | // A pointer to the FileCollection representing the filesystem (not archives). |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1984 | std::unique_ptr<io::FileCollection> file_collection_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1985 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1986 | // A vector of IFileCollections. This is mainly here to retain ownership of the |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1987 | // collections. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1988 | std::vector<std::unique_ptr<io::IFileCollection>> collections_; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1989 | |
Adam Lesinski | 8780eb6 | 2017-10-31 17:44:39 -0700 | [diff] [blame] | 1990 | // The set of merged APKs. This is mainly here to retain ownership of the APKs. |
| 1991 | std::vector<std::unique_ptr<LoadedApk>> merged_apks_; |
| 1992 | |
| 1993 | // The set of included APKs (not merged). This is mainly here to retain ownership of the APKs. |
| 1994 | std::vector<std::unique_ptr<LoadedApk>> static_library_includes_; |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 1995 | |
| 1996 | // The set of shared libraries being used, mapping their assigned package ID to package name. |
| 1997 | std::map<size_t, std::string> shared_libs_; |
Adam Lesinski | 490595a | 2017-11-07 17:08:07 -0800 | [diff] [blame] | 1998 | |
| 1999 | // The package name of the base application, if it is included. |
| 2000 | Maybe<std::string> included_feature_base_; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 2001 | }; |
| 2002 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2003 | int LinkCommand::Action(const std::vector<std::string>& args) { |
| 2004 | LinkContext context(diag_); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2005 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2006 | // Expand all argument-files passed into the command line. These start with '@'. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2007 | std::vector<std::string> arg_list; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2008 | for (const std::string& arg : args) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2009 | if (util::StartsWith(arg, "@")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2010 | const std::string path = arg.substr(1, arg.size() - 1); |
| 2011 | std::string error; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2012 | if (!file::AppendArgsFromFile(path, &arg_list, &error)) { |
| 2013 | context.GetDiagnostics()->Error(DiagMessage(path) << error); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 2014 | return 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2015 | } |
| 2016 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2017 | arg_list.push_back(arg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2018 | } |
| 2019 | } |
| 2020 | |
| 2021 | // Expand all argument-files passed to -R. |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2022 | for (const std::string& arg : overlay_arg_list_) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2023 | if (util::StartsWith(arg, "@")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2024 | const std::string path = arg.substr(1, arg.size() - 1); |
| 2025 | std::string error; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2026 | if (!file::AppendArgsFromFile(path, &options_.overlay_files, &error)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2027 | context.GetDiagnostics()->Error(DiagMessage(path) << error); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2028 | return 1; |
| 2029 | } |
| 2030 | } else { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2031 | options_.overlay_files.push_back(arg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2032 | } |
| 2033 | } |
| 2034 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2035 | if (verbose_) { |
| 2036 | context.SetVerbose(verbose_); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2039 | if (int{shared_lib_} + int{static_lib_} + int{proto_format_} > 1) { |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 2040 | context.GetDiagnostics()->Error( |
| 2041 | DiagMessage() |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2042 | << "only one of --shared-lib, --static-lib, or --proto_format can be defined"); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2043 | return 1; |
| 2044 | } |
| 2045 | |
Adam Lesinski | e59f0d8 | 2017-10-13 09:36:53 -0700 | [diff] [blame] | 2046 | // The default build type. |
| 2047 | context.SetPackageType(PackageType::kApp); |
| 2048 | context.SetPackageId(kAppPackageId); |
| 2049 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2050 | if (shared_lib_) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2051 | context.SetPackageType(PackageType::kSharedLib); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2052 | context.SetPackageId(0x00); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2053 | } else if (static_lib_) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2054 | context.SetPackageType(PackageType::kStaticLib); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2055 | options_.output_format = OutputFormat::kProto; |
| 2056 | } else if (proto_format_) { |
| 2057 | options_.output_format = OutputFormat::kProto; |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2058 | } |
| 2059 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2060 | if (package_id_) { |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2061 | if (context.GetPackageType() != PackageType::kApp) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2062 | context.GetDiagnostics()->Error( |
| 2063 | DiagMessage() << "can't specify --package-id when not building a regular app"); |
| 2064 | return 1; |
| 2065 | } |
| 2066 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2067 | const Maybe<uint32_t> maybe_package_id_int = ResourceUtils::ParseInt(package_id_.value()); |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2068 | if (!maybe_package_id_int) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2069 | context.GetDiagnostics()->Error(DiagMessage() << "package ID '" << package_id_.value() |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2070 | << "' is not a valid integer"); |
| 2071 | return 1; |
| 2072 | } |
| 2073 | |
| 2074 | const uint32_t package_id_int = maybe_package_id_int.value(); |
Todd Kennedy | 3251299 | 2018-04-25 16:45:59 -0700 | [diff] [blame] | 2075 | if (package_id_int > std::numeric_limits<uint8_t>::max() |
| 2076 | || package_id_int == kFrameworkPackageId |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2077 | || (!options_.allow_reserved_package_id && package_id_int < kAppPackageId)) { |
Adam Lesinski | f34b6f4 | 2017-03-03 16:33:26 -0800 | [diff] [blame] | 2078 | context.GetDiagnostics()->Error( |
| 2079 | DiagMessage() << StringPrintf( |
| 2080 | "invalid package ID 0x%02x. Must be in the range 0x7f-0xff.", package_id_int)); |
| 2081 | return 1; |
| 2082 | } |
| 2083 | context.SetPackageId(static_cast<uint8_t>(package_id_int)); |
| 2084 | } |
| 2085 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2086 | // Populate the set of extra packages for which to generate R.java. |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2087 | for (std::string& extra_package : extra_java_packages_) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2088 | // A given package can actually be a colon separated list of packages. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2089 | for (StringPiece package : util::Split(extra_package, ':')) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2090 | options_.extra_java_packages.insert(package.to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2094 | if (product_list_) { |
| 2095 | for (StringPiece product : util::Tokenize(product_list_.value(), ',')) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2096 | if (product != "" && product != "default") { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2097 | options_.products.insert(product.to_string()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2098 | } |
| 2099 | } |
| 2100 | } |
| 2101 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2102 | std::unique_ptr<IConfigFilter> filter; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2103 | if (!configs_.empty()) { |
| 2104 | filter = ParseConfigFilterParameters(configs_, context.GetDiagnostics()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2105 | if (filter == nullptr) { |
| 2106 | return 1; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 2107 | } |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2108 | options_.table_splitter_options.config_filter = filter.get(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2109 | } |
| 2110 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2111 | if (preferred_density_) { |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2112 | Maybe<uint16_t> density = |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2113 | ParseTargetDensityParameter(preferred_density_.value(), context.GetDiagnostics()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2114 | if (!density) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2115 | return 1; |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 2116 | } |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2117 | options_.table_splitter_options.preferred_densities.push_back(density.value()); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2118 | } |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 2119 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 2120 | // Parse the split parameters. |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2121 | for (const std::string& split_arg : split_args_) { |
| 2122 | options_.split_paths.push_back({}); |
| 2123 | options_.split_constraints.push_back({}); |
| 2124 | if (!ParseSplitParameter(split_arg, context.GetDiagnostics(), &options_.split_paths.back(), |
| 2125 | &options_.split_constraints.back())) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2126 | return 1; |
Adam Lesinski | 1e21ff0 | 2016-06-24 14:57:58 -0700 | [diff] [blame] | 2127 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2128 | } |
Adam Lesinski | 1e21ff0 | 2016-06-24 14:57:58 -0700 | [diff] [blame] | 2129 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2130 | if (context.GetPackageType() != PackageType::kStaticLib && stable_id_file_path_) { |
| 2131 | if (!LoadStableIdMap(context.GetDiagnostics(), stable_id_file_path_.value(), |
| 2132 | &options_.stable_id_map)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2133 | return 1; |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 2134 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2135 | } |
Adam Lesinski | 64587af | 2016-02-18 18:33:06 -0800 | [diff] [blame] | 2136 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2137 | // Populate some default no-compress extensions that are already compressed. |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2138 | options_.extensions_to_not_compress.insert( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2139 | {".jpg", ".jpeg", ".png", ".gif", ".wav", ".mp2", ".mp3", ".ogg", |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2140 | ".aac", ".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet", ".rtttl", |
| 2141 | ".imy", ".xmf", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2", |
| 2142 | ".3gpp2", ".amr", ".awb", ".wma", ".wmv", ".webm", ".mkv"}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2143 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2144 | // Turn off auto versioning for static-libs. |
Adam Lesinski | b522f04 | 2017-04-21 16:57:59 -0700 | [diff] [blame] | 2145 | if (context.GetPackageType() == PackageType::kStaticLib) { |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2146 | options_.no_auto_version = true; |
| 2147 | options_.no_version_vectors = true; |
| 2148 | options_.no_version_transitions = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2149 | } |
Adam Lesinski | e4bb9eb | 2016-02-12 22:18:51 -0800 | [diff] [blame] | 2150 | |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 2151 | Linker cmd(&context, options_); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 2152 | return cmd.Run(arg_list); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 2155 | } // namespace aapt |