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