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