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