xref: /aosp_15_r20/frameworks/base/tools/aapt2/cmd/Util.h (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker /*
2*d57664e9SAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*d57664e9SAndroid Build Coastguard Worker  *
4*d57664e9SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*d57664e9SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*d57664e9SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*d57664e9SAndroid Build Coastguard Worker  *
8*d57664e9SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*d57664e9SAndroid Build Coastguard Worker  *
10*d57664e9SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*d57664e9SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*d57664e9SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d57664e9SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*d57664e9SAndroid Build Coastguard Worker  * limitations under the License.
15*d57664e9SAndroid Build Coastguard Worker  */
16*d57664e9SAndroid Build Coastguard Worker 
17*d57664e9SAndroid Build Coastguard Worker #ifndef AAPT_SPLIT_UTIL_H
18*d57664e9SAndroid Build Coastguard Worker #define AAPT_SPLIT_UTIL_H
19*d57664e9SAndroid Build Coastguard Worker 
20*d57664e9SAndroid Build Coastguard Worker #include <functional>
21*d57664e9SAndroid Build Coastguard Worker #include <map>
22*d57664e9SAndroid Build Coastguard Worker #include <memory>
23*d57664e9SAndroid Build Coastguard Worker #include <optional>
24*d57664e9SAndroid Build Coastguard Worker #include <regex>
25*d57664e9SAndroid Build Coastguard Worker #include <set>
26*d57664e9SAndroid Build Coastguard Worker #include <string>
27*d57664e9SAndroid Build Coastguard Worker #include <unordered_set>
28*d57664e9SAndroid Build Coastguard Worker 
29*d57664e9SAndroid Build Coastguard Worker #include "AppInfo.h"
30*d57664e9SAndroid Build Coastguard Worker #include "SdkConstants.h"
31*d57664e9SAndroid Build Coastguard Worker #include "androidfw/IDiagnostics.h"
32*d57664e9SAndroid Build Coastguard Worker #include "androidfw/StringPiece.h"
33*d57664e9SAndroid Build Coastguard Worker #include "filter/ConfigFilter.h"
34*d57664e9SAndroid Build Coastguard Worker #include "process/IResourceTableConsumer.h"
35*d57664e9SAndroid Build Coastguard Worker #include "split/TableSplitter.h"
36*d57664e9SAndroid Build Coastguard Worker #include "xml/XmlDom.h"
37*d57664e9SAndroid Build Coastguard Worker 
38*d57664e9SAndroid Build Coastguard Worker namespace aapt {
39*d57664e9SAndroid Build Coastguard Worker 
40*d57664e9SAndroid Build Coastguard Worker struct FeatureFlagProperties {
41*d57664e9SAndroid Build Coastguard Worker   bool read_only;
42*d57664e9SAndroid Build Coastguard Worker   std::optional<bool> enabled;
43*d57664e9SAndroid Build Coastguard Worker 
FeatureFlagPropertiesFeatureFlagProperties44*d57664e9SAndroid Build Coastguard Worker   FeatureFlagProperties(bool ro, std::optional<bool> e) : read_only(ro), enabled(e) {
45*d57664e9SAndroid Build Coastguard Worker   }
46*d57664e9SAndroid Build Coastguard Worker 
47*d57664e9SAndroid Build Coastguard Worker   bool operator==(const FeatureFlagProperties&) const = default;
48*d57664e9SAndroid Build Coastguard Worker };
49*d57664e9SAndroid Build Coastguard Worker 
50*d57664e9SAndroid Build Coastguard Worker using FeatureFlagValues = std::map<std::string, FeatureFlagProperties, std::less<>>;
51*d57664e9SAndroid Build Coastguard Worker 
52*d57664e9SAndroid Build Coastguard Worker std::optional<FeatureFlagAttribute> ParseFlag(std::optional<std::string_view> flag_text);
53*d57664e9SAndroid Build Coastguard Worker 
54*d57664e9SAndroid Build Coastguard Worker std::optional<FlagStatus> GetFlagStatus(const std::optional<FeatureFlagAttribute>& flag,
55*d57664e9SAndroid Build Coastguard Worker                                         const FeatureFlagValues& feature_flag_values,
56*d57664e9SAndroid Build Coastguard Worker                                         std::string* out_err);
57*d57664e9SAndroid Build Coastguard Worker 
58*d57664e9SAndroid Build Coastguard Worker // Parses a configuration density (ex. hdpi, xxhdpi, 234dpi, anydpi, etc).
59*d57664e9SAndroid Build Coastguard Worker // Returns Nothing and logs a human friendly error message if the string was not legal.
60*d57664e9SAndroid Build Coastguard Worker std::optional<uint16_t> ParseTargetDensityParameter(android::StringPiece arg,
61*d57664e9SAndroid Build Coastguard Worker                                                     android::IDiagnostics* diag);
62*d57664e9SAndroid Build Coastguard Worker 
63*d57664e9SAndroid Build Coastguard Worker // Parses a string of the form 'path/to/output.apk:<config>[,<config>...]' and fills in
64*d57664e9SAndroid Build Coastguard Worker // `out_path` with the path and `out_split` with the set of ConfigDescriptions.
65*d57664e9SAndroid Build Coastguard Worker // Returns false and logs a human friendly error message if the string was not legal.
66*d57664e9SAndroid Build Coastguard Worker bool ParseSplitParameter(android::StringPiece arg, android::IDiagnostics* diag,
67*d57664e9SAndroid Build Coastguard Worker                          std::string* out_path, SplitConstraints* out_split);
68*d57664e9SAndroid Build Coastguard Worker 
69*d57664e9SAndroid Build Coastguard Worker // Parses a set of config filter strings of the form 'en,fr-rFR' and returns an IConfigFilter.
70*d57664e9SAndroid Build Coastguard Worker // Returns nullptr and logs a human friendly error message if the string was not legal.
71*d57664e9SAndroid Build Coastguard Worker std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
72*d57664e9SAndroid Build Coastguard Worker                                                            android::IDiagnostics* diag);
73*d57664e9SAndroid Build Coastguard Worker 
74*d57664e9SAndroid Build Coastguard Worker // Parses a feature flags parameter, which can contain one or more pairs of flag names and optional
75*d57664e9SAndroid Build Coastguard Worker // values, and fills in `out_feature_flag_values` with the parsed values. The pairs in the argument
76*d57664e9SAndroid Build Coastguard Worker // are separated by ',' and the name is separated from the value by '=' if there is a value given.
77*d57664e9SAndroid Build Coastguard Worker // Example arg: "flag1=true,flag2=false,flag3=,flag4" where flag3 and flag4 have no given value.
78*d57664e9SAndroid Build Coastguard Worker bool ParseFeatureFlagsParameter(android::StringPiece arg, android::IDiagnostics* diag,
79*d57664e9SAndroid Build Coastguard Worker                                 FeatureFlagValues* out_feature_flag_values);
80*d57664e9SAndroid Build Coastguard Worker 
81*d57664e9SAndroid Build Coastguard Worker // Adjust the SplitConstraints so that their SDK version is stripped if it
82*d57664e9SAndroid Build Coastguard Worker // is less than or equal to the min_sdk. Otherwise the resources that have had
83*d57664e9SAndroid Build Coastguard Worker // their SDK version stripped due to min_sdk won't ever match.
84*d57664e9SAndroid Build Coastguard Worker std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
85*d57664e9SAndroid Build Coastguard Worker     int min_sdk, const std::vector<SplitConstraints>& split_constraints);
86*d57664e9SAndroid Build Coastguard Worker 
87*d57664e9SAndroid Build Coastguard Worker // Generates a split AndroidManifest.xml given the split constraints and app info. The resulting
88*d57664e9SAndroid Build Coastguard Worker // XmlResource does not need to be linked via XmlReferenceLinker.
89*d57664e9SAndroid Build Coastguard Worker // This will never fail/return nullptr.
90*d57664e9SAndroid Build Coastguard Worker std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
91*d57664e9SAndroid Build Coastguard Worker                                                         const SplitConstraints& constraints);
92*d57664e9SAndroid Build Coastguard Worker 
93*d57664e9SAndroid Build Coastguard Worker // Extracts relevant info from the AndroidManifest.xml.
94*d57664e9SAndroid Build Coastguard Worker std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
95*d57664e9SAndroid Build Coastguard Worker                                                         android::IDiagnostics* diag);
96*d57664e9SAndroid Build Coastguard Worker 
97*d57664e9SAndroid Build Coastguard Worker // Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
98*d57664e9SAndroid Build Coastguard Worker // replacing nonconforming characters with underscores.
99*d57664e9SAndroid Build Coastguard Worker //
100*d57664e9SAndroid Build Coastguard Worker // See frameworks/base/core/java/android/content/pm/PackageParser.java which
101*d57664e9SAndroid Build Coastguard Worker // checks this at runtime.
102*d57664e9SAndroid Build Coastguard Worker std::string MakePackageSafeName(const std::string &name);
103*d57664e9SAndroid Build Coastguard Worker 
104*d57664e9SAndroid Build Coastguard Worker // Sets the versionCode and versionCodeMajor attributes to the version code. Attempts to encode the
105*d57664e9SAndroid Build Coastguard Worker // version code using the versionCode attribute only, and encodes using both versionCode and
106*d57664e9SAndroid Build Coastguard Worker // versionCodeMajor if the version code requires more than 32 bits.
107*d57664e9SAndroid Build Coastguard Worker void SetLongVersionCode(xml::Element* manifest, uint64_t version_code);
108*d57664e9SAndroid Build Coastguard Worker 
109*d57664e9SAndroid Build Coastguard Worker // Returns a case insensitive regular expression based on the input.
110*d57664e9SAndroid Build Coastguard Worker std::regex GetRegularExpression(const std::string &input);
111*d57664e9SAndroid Build Coastguard Worker 
112*d57664e9SAndroid Build Coastguard Worker bool ParseResourceConfig(const std::string& content, IAaptContext* context,
113*d57664e9SAndroid Build Coastguard Worker                          std::unordered_set<ResourceName>& out_resource_exclude_list,
114*d57664e9SAndroid Build Coastguard Worker                          std::set<ResourceName>& out_name_collapse_exemptions,
115*d57664e9SAndroid Build Coastguard Worker                          std::set<ResourceName>& out_path_shorten_exemptions);
116*d57664e9SAndroid Build Coastguard Worker 
117*d57664e9SAndroid Build Coastguard Worker }  // namespace aapt
118*d57664e9SAndroid Build Coastguard Worker 
119*d57664e9SAndroid Build Coastguard Worker #endif /* AAPT_SPLIT_UTIL_H */
120