Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 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 | #ifndef AAPT_SPLIT_UTIL_H |
| 18 | #define AAPT_SPLIT_UTIL_H |
| 19 | |
Mark Punzalan | 5579cad | 2023-10-30 13:47:51 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <map> |
| 22 | #include <memory> |
| 23 | #include <optional> |
Izabela Orlowska | 0faba5f | 2018-06-01 12:06:31 +0100 | [diff] [blame] | 24 | #include <regex> |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 25 | #include <set> |
Mark Punzalan | 5579cad | 2023-10-30 13:47:51 -0700 | [diff] [blame] | 26 | #include <string> |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 27 | #include <unordered_set> |
Izabela Orlowska | 0faba5f | 2018-06-01 12:06:31 +0100 | [diff] [blame] | 28 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 29 | #include "AppInfo.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 30 | #include "SdkConstants.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 31 | #include "androidfw/IDiagnostics.h" |
| 32 | #include "androidfw/StringPiece.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 33 | #include "filter/ConfigFilter.h" |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 34 | #include "process/IResourceTableConsumer.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 35 | #include "split/TableSplitter.h" |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 36 | #include "xml/XmlDom.h" |
| 37 | |
| 38 | namespace aapt { |
| 39 | |
Jeremy Meyer | fc7aba6 | 2024-07-16 20:25:38 +0000 | [diff] [blame] | 40 | struct 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 | |
| 50 | using FeatureFlagValues = std::map<std::string, FeatureFlagProperties, std::less<>>; |
Mark Punzalan | 5579cad | 2023-10-30 13:47:51 -0700 | [diff] [blame] | 51 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 52 | // Parses a configuration density (ex. hdpi, xxhdpi, 234dpi, anydpi, etc). |
| 53 | // Returns Nothing and logs a human friendly error message if the string was not legal. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 54 | std::optional<uint16_t> ParseTargetDensityParameter(android::StringPiece arg, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 55 | android::IDiagnostics* diag); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 56 | |
| 57 | // Parses a string of the form 'path/to/output.apk:<config>[,<config>...]' and fills in |
| 58 | // `out_path` with the path and `out_split` with the set of ConfigDescriptions. |
| 59 | // Returns false and logs a human friendly error message if the string was not legal. |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 60 | bool ParseSplitParameter(android::StringPiece arg, android::IDiagnostics* diag, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 61 | std::string* out_path, SplitConstraints* out_split); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 62 | |
| 63 | // Parses a set of config filter strings of the form 'en,fr-rFR' and returns an IConfigFilter. |
| 64 | // Returns nullptr and logs a human friendly error message if the string was not legal. |
| 65 | std::unique_ptr<IConfigFilter> ParseConfigFilterParameters(const std::vector<std::string>& args, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 66 | android::IDiagnostics* diag); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 67 | |
Mark Punzalan | 5579cad | 2023-10-30 13:47:51 -0700 | [diff] [blame] | 68 | // Parses a feature flags parameter, which can contain one or more pairs of flag names and optional |
| 69 | // values, and fills in `out_feature_flag_values` with the parsed values. The pairs in the argument |
| 70 | // are separated by ',' and the name is separated from the value by '=' if there is a value given. |
| 71 | // Example arg: "flag1=true,flag2=false,flag3=,flag4" where flag3 and flag4 have no given value. |
| 72 | bool ParseFeatureFlagsParameter(android::StringPiece arg, android::IDiagnostics* diag, |
| 73 | FeatureFlagValues* out_feature_flag_values); |
| 74 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 75 | // Adjust the SplitConstraints so that their SDK version is stripped if it |
| 76 | // is less than or equal to the min_sdk. Otherwise the resources that have had |
| 77 | // their SDK version stripped due to min_sdk won't ever match. |
| 78 | std::vector<SplitConstraints> AdjustSplitConstraintsForMinSdk( |
| 79 | int min_sdk, const std::vector<SplitConstraints>& split_constraints); |
| 80 | |
| 81 | // Generates a split AndroidManifest.xml given the split constraints and app info. The resulting |
| 82 | // XmlResource does not need to be linked via XmlReferenceLinker. |
| 83 | // This will never fail/return nullptr. |
| 84 | std::unique_ptr<xml::XmlResource> GenerateSplitManifest(const AppInfo& app_info, |
| 85 | const SplitConstraints& constraints); |
| 86 | |
| 87 | // Extracts relevant info from the AndroidManifest.xml. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 88 | std::optional<AppInfo> ExtractAppInfoFromBinaryManifest(const xml::XmlResource& xml_res, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 89 | android::IDiagnostics* diag); |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 90 | |
Izabela Orlowska | 1056019 | 2018-04-13 11:56:35 +0100 | [diff] [blame] | 91 | // Returns a copy of 'name' which conforms to the regex '[a-zA-Z]+[a-zA-Z0-9_]*' by |
| 92 | // replacing nonconforming characters with underscores. |
| 93 | // |
| 94 | // See frameworks/base/core/java/android/content/pm/PackageParser.java which |
| 95 | // checks this at runtime. |
| 96 | std::string MakePackageSafeName(const std::string &name); |
| 97 | |
Ryan Mitchell | 5fa2bb1 | 2018-07-12 11:24:51 -0700 | [diff] [blame] | 98 | // Sets the versionCode and versionCodeMajor attributes to the version code. Attempts to encode the |
| 99 | // version code using the versionCode attribute only, and encodes using both versionCode and |
| 100 | // versionCodeMajor if the version code requires more than 32 bits. |
| 101 | void SetLongVersionCode(xml::Element* manifest, uint64_t version_code); |
| 102 | |
Izabela Orlowska | 0faba5f | 2018-06-01 12:06:31 +0100 | [diff] [blame] | 103 | // Returns a case insensitive regular expression based on the input. |
| 104 | std::regex GetRegularExpression(const std::string &input); |
| 105 | |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 106 | bool ParseResourceConfig(const std::string& content, IAaptContext* context, |
| 107 | std::unordered_set<ResourceName>& out_resource_exclude_list, |
branliu | f1ed523 | 2022-12-16 19:02:29 +0800 | [diff] [blame] | 108 | std::set<ResourceName>& out_name_collapse_exemptions, |
| 109 | std::set<ResourceName>& out_path_shorten_exemptions); |
Iurii Makhno | 054e433 | 2022-10-12 16:03:03 +0000 | [diff] [blame] | 110 | |
Adam Lesinski | d0f492d | 2017-04-03 18:12:45 -0700 | [diff] [blame] | 111 | } // namespace aapt |
| 112 | |
| 113 | #endif /* AAPT_SPLIT_UTIL_H */ |