1 /*
2  * Copyright (C) 2017 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 
17 #include "MultiApkGenerator.h"
18 
19 #include <algorithm>
20 #include <regex>
21 #include <string>
22 
23 #include "androidfw/ConfigDescription.h"
24 #include "androidfw/StringPiece.h"
25 
26 #include "LoadedApk.h"
27 #include "ResourceUtils.h"
28 #include "ValueVisitor.h"
29 #include "configuration/ConfigurationParser.h"
30 #include "cmd/Util.h"
31 #include "filter/AbiFilter.h"
32 #include "filter/Filter.h"
33 #include "format/Archive.h"
34 #include "format/binary/XmlFlattener.h"
35 #include "optimize/VersionCollapser.h"
36 #include "process/IResourceTableConsumer.h"
37 #include "split/TableSplitter.h"
38 #include "util/Files.h"
39 #include "xml/XmlDom.h"
40 #include "xml/XmlUtil.h"
41 
42 namespace aapt {
43 
44 using ::aapt::configuration::AndroidSdk;
45 using ::aapt::configuration::OutputArtifact;
46 using ::aapt::xml::kSchemaAndroid;
47 using ::aapt::xml::XmlResource;
48 using ::android::ConfigDescription;
49 using ::android::StringPiece;
50 
51 /**
52  * Context wrapper that allows the min Android SDK value to be overridden.
53  */
54 class ContextWrapper : public IAaptContext {
55  public:
ContextWrapper(IAaptContext * context)56   explicit ContextWrapper(IAaptContext* context)
57       : context_(context), min_sdk_(context_->GetMinSdkVersion()) {
58   }
59 
GetPackageType()60   PackageType GetPackageType() override {
61     return context_->GetPackageType();
62   }
63 
GetExternalSymbols()64   SymbolTable* GetExternalSymbols() override {
65     return context_->GetExternalSymbols();
66   }
67 
GetDiagnostics()68   IDiagnostics* GetDiagnostics() override {
69     if (source_diag_) {
70       return source_diag_.get();
71     }
72     return context_->GetDiagnostics();
73   }
74 
GetCompilationPackage()75   const std::string& GetCompilationPackage() override {
76     return context_->GetCompilationPackage();
77   }
78 
GetPackageId()79   uint8_t GetPackageId() override {
80     return context_->GetPackageId();
81   }
82 
GetNameMangler()83   NameMangler* GetNameMangler() override {
84     return context_->GetNameMangler();
85   }
86 
IsVerbose()87   bool IsVerbose() override {
88     return context_->IsVerbose();
89   }
90 
GetMinSdkVersion()91   int GetMinSdkVersion() override {
92     return min_sdk_;
93   }
94 
SetMinSdkVersion(int min_sdk)95   void SetMinSdkVersion(int min_sdk) {
96     min_sdk_ = min_sdk;
97   }
98 
SetSource(const std::string & source)99   void SetSource(const std::string& source) {
100     source_diag_ =
101         util::make_unique<SourcePathDiagnostics>(Source{source}, context_->GetDiagnostics());
102   }
103 
104  private:
105   IAaptContext* context_;
106   std::unique_ptr<SourcePathDiagnostics> source_diag_;
107 
108   int min_sdk_ = -1;
109 };
110 
111 class SignatureFilter : public IPathFilter {
Keep(const std::string & path)112   bool Keep(const std::string& path) override {
113     static std::regex signature_regex(R"regex(^META-INF/.*\.(RSA|DSA|EC|SF)$)regex");
114     if (std::regex_search(path, signature_regex)) {
115       return false;
116     }
117     return !(path == "META-INF/MANIFEST.MF");
118   }
119 };
120 
MultiApkGenerator(LoadedApk * apk,IAaptContext * context)121 MultiApkGenerator::MultiApkGenerator(LoadedApk* apk, IAaptContext* context)
122     : apk_(apk), context_(context) {
123 }
124 
FromBaseApk(const MultiApkGeneratorOptions & options)125 bool MultiApkGenerator::FromBaseApk(const MultiApkGeneratorOptions& options) {
126   std::unordered_set<std::string> artifacts_to_keep = options.kept_artifacts;
127   std::unordered_set<std::string> filtered_artifacts;
128   std::unordered_set<std::string> kept_artifacts;
129 
130   // For now, just write out the stripped APK since ABI splitting doesn't modify anything else.
131   for (const OutputArtifact& artifact : options.apk_artifacts) {
132     FilterChain filters;
133 
134     ContextWrapper wrapped_context{context_};
135     wrapped_context.SetSource(artifact.name);
136 
137     if (!options.kept_artifacts.empty()) {
138       const auto& it = artifacts_to_keep.find(artifact.name);
139       if (it == artifacts_to_keep.end()) {
140         filtered_artifacts.insert(artifact.name);
141         if (context_->IsVerbose()) {
142           context_->GetDiagnostics()->Note(DiagMessage(artifact.name) << "skipping artifact");
143         }
144         continue;
145       } else {
146         artifacts_to_keep.erase(it);
147         kept_artifacts.insert(artifact.name);
148       }
149     }
150 
151     std::unique_ptr<ResourceTable> table =
152         FilterTable(context_, artifact, *apk_->GetResourceTable(), &filters);
153     if (!table) {
154       return false;
155     }
156 
157     IDiagnostics* diag = wrapped_context.GetDiagnostics();
158 
159     std::unique_ptr<XmlResource> manifest;
160     if (!UpdateManifest(artifact, &manifest, diag)) {
161       diag->Error(DiagMessage() << "could not update AndroidManifest.xml for output artifact");
162       return false;
163     }
164 
165     std::string out = options.out_dir;
166     if (!file::mkdirs(out)) {
167       diag->Warn(DiagMessage() << "could not create out dir: " << out);
168     }
169     file::AppendPath(&out, artifact.name);
170 
171     if (context_->IsVerbose()) {
172       diag->Note(DiagMessage() << "Generating split: " << out);
173     }
174 
175     std::unique_ptr<IArchiveWriter> writer = CreateZipFileArchiveWriter(diag, out);
176 
177     if (context_->IsVerbose()) {
178       diag->Note(DiagMessage() << "Writing output: " << out);
179     }
180 
181     filters.AddFilter(util::make_unique<SignatureFilter>());
182     if (!apk_->WriteToArchive(&wrapped_context, table.get(), options.table_flattener_options,
183                               &filters, writer.get(), manifest.get())) {
184       return false;
185     }
186   }
187 
188   // Make sure all of the requested artifacts were valid. If there are any kept artifacts left,
189   // either the config or the command line was wrong.
190   if (!artifacts_to_keep.empty()) {
191     context_->GetDiagnostics()->Error(
192         DiagMessage() << "The configuration and command line to filter artifacts do not match");
193 
194     context_->GetDiagnostics()->Error(DiagMessage() << kept_artifacts.size() << " kept:");
195     for (const auto& artifact : kept_artifacts) {
196       context_->GetDiagnostics()->Error(DiagMessage() << "  " << artifact);
197     }
198 
199     context_->GetDiagnostics()->Error(DiagMessage() << filtered_artifacts.size() << " filtered:");
200     for (const auto& artifact : filtered_artifacts) {
201       context_->GetDiagnostics()->Error(DiagMessage() << "  " << artifact);
202     }
203 
204     context_->GetDiagnostics()->Error(DiagMessage() << artifacts_to_keep.size() << " missing:");
205     for (const auto& artifact : artifacts_to_keep) {
206       context_->GetDiagnostics()->Error(DiagMessage() << "  " << artifact);
207     }
208 
209     return false;
210   }
211 
212   return true;
213 }
214 
FilterTable(IAaptContext * context,const OutputArtifact & artifact,const ResourceTable & old_table,FilterChain * filters)215 std::unique_ptr<ResourceTable> MultiApkGenerator::FilterTable(IAaptContext* context,
216                                                               const OutputArtifact& artifact,
217                                                               const ResourceTable& old_table,
218                                                               FilterChain* filters) {
219   TableSplitterOptions splits;
220   AxisConfigFilter axis_filter;
221   ContextWrapper wrapped_context{context};
222   wrapped_context.SetSource(artifact.name);
223 
224   if (!artifact.abis.empty()) {
225     filters->AddFilter(AbiFilter::FromAbiList(artifact.abis));
226   }
227 
228   if (!artifact.screen_densities.empty()) {
229     for (const auto& density_config : artifact.screen_densities) {
230       splits.preferred_densities.push_back(density_config.density);
231     }
232   }
233 
234   if (!artifact.locales.empty()) {
235     for (const auto& locale : artifact.locales) {
236       axis_filter.AddConfig(locale);
237     }
238     splits.config_filter = &axis_filter;
239   }
240 
241   if (artifact.android_sdk) {
242     wrapped_context.SetMinSdkVersion(artifact.android_sdk.value().min_sdk_version);
243   }
244 
245   std::unique_ptr<ResourceTable> table = old_table.Clone();
246 
247   VersionCollapser collapser;
248   if (!collapser.Consume(&wrapped_context, table.get())) {
249     context->GetDiagnostics()->Error(DiagMessage() << "Failed to strip versioned resources");
250     return {};
251   }
252 
253   TableSplitter splitter{{}, splits};
254   splitter.SplitTable(table.get());
255   return table;
256 }
257 
UpdateManifest(const OutputArtifact & artifact,std::unique_ptr<XmlResource> * updated_manifest,IDiagnostics * diag)258 bool MultiApkGenerator::UpdateManifest(const OutputArtifact& artifact,
259                                        std::unique_ptr<XmlResource>* updated_manifest,
260                                        IDiagnostics* diag) {
261   const xml::XmlResource* apk_manifest = apk_->GetManifest();
262   if (apk_manifest == nullptr) {
263     return false;
264   }
265 
266   *updated_manifest = apk_manifest->Clone();
267   XmlResource* manifest = updated_manifest->get();
268 
269   // Make sure the first element is <manifest> with package attribute.
270   xml::Element* manifest_el = manifest->root.get();
271   if (!manifest_el) {
272     return false;
273   }
274 
275   if (!manifest_el->namespace_uri.empty() || manifest_el->name != "manifest") {
276     diag->Error(DiagMessage(manifest->file.source) << "root tag must be <manifest>");
277     return false;
278   }
279 
280   // Retrieve the versionCode attribute.
281   auto version_code = manifest_el->FindAttribute(kSchemaAndroid, "versionCode");
282   if (!version_code) {
283     diag->Error(DiagMessage(manifest->file.source) << "manifest must have a versionCode attribute");
284     return false;
285   }
286 
287   auto version_code_value = ValueCast<BinaryPrimitive>(version_code->compiled_value.get());
288   if (!version_code_value) {
289     diag->Error(DiagMessage(manifest->file.source) << "versionCode is invalid");
290     return false;
291   }
292 
293   // Retrieve the versionCodeMajor attribute.
294   auto version_code_major = manifest_el->FindAttribute(kSchemaAndroid, "versionCodeMajor");
295   BinaryPrimitive* version_code_major_value = nullptr;
296   if (version_code_major) {
297     version_code_major_value = ValueCast<BinaryPrimitive>(version_code_major->compiled_value.get());
298     if (!version_code_major_value) {
299       diag->Error(DiagMessage(manifest->file.source) << "versionCodeMajor is invalid");
300       return false;
301     }
302   }
303 
304   // Calculate and set the updated version code
305   uint64_t major = (version_code_major_value)
306                   ? ((uint64_t) version_code_major_value->value.data) << 32 : 0;
307   uint64_t new_version = (major | version_code_value->value.data) + artifact.version;
308   SetLongVersionCode(manifest_el, new_version);
309 
310   // Check to see if the minSdkVersion needs to be updated.
311   if (artifact.android_sdk) {
312     // TODO(safarmer): Handle the rest of the Android SDK.
313     const AndroidSdk& android_sdk = artifact.android_sdk.value();
314 
315     if (xml::Element* uses_sdk_el = manifest_el->FindChild({}, "uses-sdk")) {
316       if (xml::Attribute* min_sdk_attr =
317               uses_sdk_el->FindAttribute(xml::kSchemaAndroid, "minSdkVersion")) {
318         // Populate with a pre-compiles attribute to we don't need to relink etc.
319         const std::string& min_sdk_str = std::to_string(android_sdk.min_sdk_version);
320         min_sdk_attr->compiled_value = ResourceUtils::TryParseInt(min_sdk_str);
321       } else {
322         // There was no minSdkVersion. This is strange since at this point we should have been
323         // through the manifest fixer which sets the default minSdkVersion.
324         diag->Error(DiagMessage(manifest->file.source) << "missing minSdkVersion from <uses-sdk>");
325         return false;
326       }
327     } else {
328       // No uses-sdk present. This is strange since at this point we should have been
329       // through the manifest fixer which should have added it.
330       diag->Error(DiagMessage(manifest->file.source) << "missing <uses-sdk> from <manifest>");
331       return false;
332     }
333   }
334 
335   if (!artifact.screen_densities.empty()) {
336     xml::Element* screens_el = manifest_el->FindChild({}, "compatible-screens");
337     if (!screens_el) {
338       // create a new element.
339       std::unique_ptr<xml::Element> new_screens_el = util::make_unique<xml::Element>();
340       new_screens_el->name = "compatible-screens";
341       screens_el = new_screens_el.get();
342       manifest_el->AppendChild(std::move(new_screens_el));
343     } else {
344       // clear out the old element.
345       screens_el->GetChildElements().clear();
346     }
347 
348     for (const auto& density : artifact.screen_densities) {
349       AddScreens(density, screens_el);
350     }
351   }
352 
353   return true;
354 }
355 
356 /**
357  * Adds a screen element with both screenSize and screenDensity set. Since we only know the density
358  * we add it for all screen sizes.
359  *
360  * This requires the resource IDs for the attributes from the framework library. Since these IDs are
361  * a part of the public API (and in public.xml) we hard code the values.
362  *
363  * The excert from the framework is as follows:
364  *    <public type="attr" name="screenSize" id="0x010102ca" />
365  *    <public type="attr" name="screenDensity" id="0x010102cb" />
366  */
AddScreens(const ConfigDescription & config,xml::Element * parent)367 void MultiApkGenerator::AddScreens(const ConfigDescription& config, xml::Element* parent) {
368   // Hard coded integer representation of the supported screen sizes:
369   //  small   = 200
370   //  normal  = 300
371   //  large   = 400
372   //  xlarge  = 500
373   constexpr const uint32_t kScreenSizes[4] = {200, 300, 400, 500,};
374   constexpr const uint32_t kScreenSizeResourceId = 0x010102ca;
375   constexpr const uint32_t kScreenDensityResourceId = 0x010102cb;
376 
377   for (uint32_t screen_size : kScreenSizes) {
378     std::unique_ptr<xml::Element> screen = util::make_unique<xml::Element>();
379     screen->name = "screen";
380 
381     xml::Attribute* size = screen->FindOrCreateAttribute(kSchemaAndroid, "screenSize");
382     size->compiled_attribute = xml::AaptAttribute(Attribute(), {kScreenSizeResourceId});
383     size->compiled_value = ResourceUtils::MakeInt(screen_size);
384 
385     xml::Attribute* density = screen->FindOrCreateAttribute(kSchemaAndroid, "screenDensity");
386     density->compiled_attribute = xml::AaptAttribute(Attribute(), {kScreenDensityResourceId});
387     density->compiled_value = ResourceUtils::MakeInt(config.density);
388 
389 
390     parent->AppendChild(std::move(screen));
391   }
392 }
393 
394 }  // namespace aapt
395