blob: f8e44b72247cea89c47d62dd33adf6dac3db05c1 [file] [log] [blame]
Adam Lesinskid0f492d2017-04-03 18:12:45 -07001/*
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#ifndef AAPT_SPLIT_UTIL_H
18#define AAPT_SPLIT_UTIL_H
19
Mark Punzalan5579cad2023-10-30 13:47:51 -070020#include <functional>
21#include <map>
22#include <memory>
23#include <optional>
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010024#include <regex>
Iurii Makhno054e4332022-10-12 16:03:03 +000025#include <set>
Mark Punzalan5579cad2023-10-30 13:47:51 -070026#include <string>
Iurii Makhno054e4332022-10-12 16:03:03 +000027#include <unordered_set>
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010028
Adam Lesinskid0f492d2017-04-03 18:12:45 -070029#include "AppInfo.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070030#include "SdkConstants.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000031#include "androidfw/IDiagnostics.h"
32#include "androidfw/StringPiece.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070033#include "filter/ConfigFilter.h"
Iurii Makhno054e4332022-10-12 16:03:03 +000034#include "process/IResourceTableConsumer.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070035#include "split/TableSplitter.h"
Adam Lesinskid0f492d2017-04-03 18:12:45 -070036#include "xml/XmlDom.h"
37
38namespace aapt {
39
Jeremy Meyerb5c0ef02024-06-12 10:42:55 -070040struct FeatureFlagProperties {
41 bool read_only;
42 std::optional<bool> enabled;
43
44 FeatureFlagProperties(bool ro, std::optional<bool> e) : read_only(ro), enabled(e) {
45 }
46
47 bool operator==(const FeatureFlagProperties&) const = default;
48};
49
50using FeatureFlagValues = std::map<std::string, FeatureFlagProperties, std::less<>>;
Mark Punzalan5579cad2023-10-30 13:47:51 -070051
Jeremy Meyer3d8d4a12024-08-23 17:29:03 -070052std::optional<FlagStatus> GetFlagStatus(const std::optional<FeatureFlagAttribute>& flag,
53 const FeatureFlagValues& feature_flag_values,
54 std::string* out_err);
55
Adam Lesinskid0f492d2017-04-03 18:12:45 -070056// Parses a configuration density (ex. hdpi, xxhdpi, 234dpi, anydpi, etc).
57// Returns Nothing and logs a human friendly error message if the string was not legal.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070058std::optional<uint16_t> ParseTargetDensityParameter(android::StringPiece arg,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000059 android::IDiagnostics* diag);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070060
61// Parses a string of the form 'path/to/output.apk:<config>[,<config>...]' and fills in
62// `out_path` with the path and `out_split` with the set of ConfigDescriptions.
63// Returns false and logs a human friendly error message if the string was not legal.
Yurii Zubrytskyia5775142022-11-02 17:49:49 -070064bool ParseSplitParameter(android::StringPiece arg, android::IDiagnostics* diag,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000065 std::string* out_path, SplitConstraints* out_split);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070066
67// Parses a set of config filter strings of the form 'en,fr-rFR' and returns an IConfigFilter.
68// Returns nullptr and logs a human friendly error message if the string was not legal.
69std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000070 android::IDiagnostics* diag);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070071
Mark Punzalan5579cad2023-10-30 13:47:51 -070072// Parses a feature flags parameter, which can contain one or more pairs of flag names and optional
73// values, and fills in `out_feature_flag_values` with the parsed values. The pairs in the argument
74// are separated by ',' and the name is separated from the value by '=' if there is a value given.
75// Example arg: "flag1=true,flag2=false,flag3=,flag4" where flag3 and flag4 have no given value.
76bool ParseFeatureFlagsParameter(android::StringPiece arg, android::IDiagnostics* diag,
77 FeatureFlagValues* out_feature_flag_values);
78
Adam Lesinskid0f492d2017-04-03 18:12:45 -070079// Adjust the SplitConstraints so that their SDK version is stripped if it
80// is less than or equal to the min_sdk. Otherwise the resources that have had
81// their SDK version stripped due to min_sdk won't ever match.
82std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk(
83 int min_sdk, const std::vector<SplitConstraints>& split_constraints);
84
85// Generates a split AndroidManifest.xml given the split constraints and app info. The resulting
86// XmlResource does not need to be linked via XmlReferenceLinker.
87// This will never fail/return nullptr.
88std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info,
89 const SplitConstraints& constraints);
90
91// Extracts relevant info from the AndroidManifest.xml.
Ryan Mitchell4382e442021-07-14 12:53:01 -070092std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000093 android::IDiagnostics* diag);
Adam Lesinskid0f492d2017-04-03 18:12:45 -070094
Izabela Orlowska10560192018-04-13 11:56:35 +010095// Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by
96// replacing nonconforming characters with underscores.
97//
98// See frameworks/base/core/java/android/content/pm/PackageParser.java which
99// checks this at runtime.
100std::string MakePackageSafeName(const std::string &name);
101
Ryan Mitchell5fa2bb12018-07-12 11:24:51 -0700102// Sets the versionCode and versionCodeMajor attributes to the version code. Attempts to encode the
103// version code using the versionCode attribute only, and encodes using both versionCode and
104// versionCodeMajor if the version code requires more than 32 bits.
105void SetLongVersionCode(xml::Element* manifest, uint64_t version_code);
106
Izabela Orlowska0faba5f2018-06-01 12:06:31 +0100107// Returns a case insensitive regular expression based on the input.
108std::regex GetRegularExpression(const std::string &input);
109
Iurii Makhno054e4332022-10-12 16:03:03 +0000110bool ParseResourceConfig(const std::string& content, IAaptContext* context,
111 std::unordered_set<ResourceName>& out_resource_exclude_list,
branliuf1ed5232022-12-16 19:02:29 +0800112 std::set<ResourceName>& out_name_collapse_exemptions,
113 std::set<ResourceName>& out_path_shorten_exemptions);
Iurii Makhno054e4332022-10-12 16:03:03 +0000114
Adam Lesinskid0f492d2017-04-03 18:12:45 -0700115} // namespace aapt
116
117#endif /* AAPT_SPLIT_UTIL_H */