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