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 
17 #ifndef AAPT_LINK_MANIFESTFIXER_H
18 #define AAPT_LINK_MANIFESTFIXER_H
19 
20 #include <string>
21 #include <vector>
22 
23 #include "android-base/macros.h"
24 #include "process/IResourceTableConsumer.h"
25 #include "xml/XmlActionExecutor.h"
26 #include "xml/XmlDom.h"
27 
28 namespace aapt {
29 
30 struct ManifestFixerOptions {
31   // The minimum SDK version to set if no 'android:minSdkVersion' is defined in a <uses-sdk> tag.
32   std::optional<std::string> min_sdk_version_default;
33 
34   // The target SDK version to set if no 'android:targetSdkVersion' is defined in a <uses-sdk> tag.
35   std::optional<std::string> target_sdk_version_default;
36 
37   // The Android package to use instead of the one defined in 'package' in <manifest>.
38   // This also renames all relative package/class names in the manifest to fully qualified
39   // Java names.
40   std::optional<std::string> rename_manifest_package;
41 
42   // The Android package to use instead of the one defined in 'android:targetPackage' in
43   // <instrumentation>.
44   std::optional<std::string> rename_instrumentation_target_package;
45 
46   // The Android package to use instead of the one defined in 'android:targetPackage' in
47   // <overlay>.
48   std::optional<std::string> rename_overlay_target_package;
49 
50   // The category to use instead of the one defined in 'android:category' in <overlay>.
51   std::optional<std::string> rename_overlay_category;
52 
53   // The version name to set if 'android:versionName' is not defined in <manifest> or if
54   // replace_version is set.
55   std::optional<std::string> version_name_default;
56 
57   // The version code to set if 'android:versionCode' is not defined in <manifest> or if
58   // replace_version is set.
59   std::optional<std::string> version_code_default;
60 
61   // The version code to set if 'android:versionCodeMajor' is not defined in <manifest> or if
62   // replace_version is set.
63   std::optional<std::string> version_code_major_default;
64 
65   // The revision code to set if 'android:revisionCode' is not defined in <manifest> or if
66   // replace_version is set.
67   std::optional<std::string> revision_code_default;
68 
69   // The version of the framework being compiled against to set for 'android:compileSdkVersion' in
70   // the <manifest> tag. Not used if no_compile_sdk_metadata is set.
71   std::optional<std::string> compile_sdk_version;
72 
73   // The version codename of the framework being compiled against to set for
74   // 'android:compileSdkVersionCodename' in the <manifest> tag. Not used if no_compile_sdk_metadata
75   // is set.
76   std::optional<std::string> compile_sdk_version_codename;
77 
78   // The fingerprint prefixes to be added to the <install-constraints> tag.
79   std::vector<std::string> fingerprint_prefixes;
80 
81   // Whether validation errors should be treated only as warnings. If this is 'true', then an
82   // incorrect node will not result in an error, but only as a warning, and the parsing will
83   // continue.
84   bool warn_validation = false;
85 
86   // Whether to inject the android:debuggable="true" flag into the manifest
87   bool debug_mode = false;
88 
89   // Whether to replace the manifest version with the the command line version
90   bool replace_version = false;
91 
92   // Whether to suppress `android:compileSdkVersion*` and `platformBuildVersion*` attributes.
93   bool no_compile_sdk_metadata = false;
94 
95   // Whether to mark the app as a non-updatable system app. This adds `updatableSystem="false"` to
96   // the <manifest> tag. Not used if a version code is set either explicitly in the manifest or
97   // through version_code_default.
98   bool non_updatable_system = false;
99 };
100 
101 // Verifies that the manifest is correctly formed and inserts defaults where specified with
102 // ManifestFixerOptions.
103 class ManifestFixer : public IXmlResourceConsumer {
104  public:
ManifestFixer(const ManifestFixerOptions & options)105   explicit ManifestFixer(const ManifestFixerOptions& options) : options_(options) {
106   }
107 
108   bool Consume(IAaptContext* context, xml::XmlResource* doc) override;
109 
110  private:
111   DISALLOW_COPY_AND_ASSIGN(ManifestFixer);
112 
113   bool BuildRules(xml::XmlActionExecutor* executor, android::IDiagnostics* diag);
114 
115   ManifestFixerOptions options_;
116 };
117 
118 }  // namespace aapt
119 
120 #endif /* AAPT_LINK_MANIFESTFIXER_H */
121