Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 AAPT2_LINK_H |
| 18 | #define AAPT2_LINK_H |
| 19 | |
| 20 | #include "Command.h" |
| 21 | #include "Diagnostics.h" |
| 22 | #include "Resource.h" |
| 23 | #include "split/TableSplitter.h" |
| 24 | #include "format/binary/TableFlattener.h" |
| 25 | #include "link/ManifestFixer.h" |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
| 29 | enum class OutputFormat { |
| 30 | kApk, |
| 31 | kProto, |
| 32 | }; |
| 33 | |
| 34 | struct LinkOptions { |
| 35 | std::string output_path; |
| 36 | std::string manifest_path; |
| 37 | std::vector<std::string> include_paths; |
| 38 | std::vector<std::string> overlay_files; |
| 39 | std::vector<std::string> assets_dirs; |
| 40 | bool output_to_directory = false; |
| 41 | bool auto_add_overlay = false; |
| 42 | OutputFormat output_format = OutputFormat::kApk; |
| 43 | |
| 44 | // Java/Proguard options. |
| 45 | Maybe<std::string> generate_java_class_path; |
| 46 | Maybe<std::string> custom_java_package; |
| 47 | std::set<std::string> extra_java_packages; |
| 48 | Maybe<std::string> generate_text_symbols_path; |
| 49 | Maybe<std::string> generate_proguard_rules_path; |
| 50 | Maybe<std::string> generate_main_dex_proguard_rules_path; |
| 51 | bool generate_conditional_proguard_rules = false; |
| 52 | bool generate_non_final_ids = false; |
| 53 | std::vector<std::string> javadoc_annotations; |
| 54 | Maybe<std::string> private_symbols; |
| 55 | |
| 56 | // Optimizations/features. |
| 57 | bool no_auto_version = false; |
| 58 | bool no_version_vectors = false; |
| 59 | bool no_version_transitions = false; |
| 60 | bool no_resource_deduping = false; |
| 61 | bool no_xml_namespaces = false; |
| 62 | bool do_not_compress_anything = false; |
| 63 | std::unordered_set<std::string> extensions_to_not_compress; |
| 64 | |
| 65 | // Static lib options. |
| 66 | bool no_static_lib_packages = false; |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 67 | |
| 68 | // AndroidManifest.xml massaging options. |
| 69 | ManifestFixerOptions manifest_fixer_options; |
| 70 | |
| 71 | // Products to use/filter on. |
| 72 | std::unordered_set<std::string> products; |
| 73 | |
| 74 | // Flattening options. |
| 75 | TableFlattenerOptions table_flattener_options; |
| 76 | |
| 77 | // Split APK options. |
| 78 | TableSplitterOptions table_splitter_options; |
| 79 | std::vector<SplitConstraints> split_constraints; |
| 80 | std::vector<std::string> split_paths; |
| 81 | |
| 82 | // Stable ID options. |
| 83 | std::unordered_map<ResourceName, ResourceId> stable_id_map; |
| 84 | Maybe<std::string> resource_id_map_path; |
| 85 | |
| 86 | // When 'true', allow reserved package IDs to be used for applications. Pre-O, the platform |
| 87 | // treats negative resource IDs [those with a package ID of 0x80 or higher] as invalid. |
| 88 | // In order to work around this limitation, we allow the use of traditionally reserved |
| 89 | // resource IDs [those between 0x02 and 0x7E]. |
| 90 | bool allow_reserved_package_id = false; |
| 91 | |
| 92 | // Whether we should fail on definitions of a resource with conflicting visibility. |
| 93 | bool strict_visibility = false; |
| 94 | }; |
| 95 | |
| 96 | class LinkCommand : public Command { |
| 97 | public: |
| 98 | explicit LinkCommand(IDiagnostics* diag) : Command("link", "l"), |
| 99 | diag_(diag) { |
| 100 | SetDescription("Links resources into an apk."); |
| 101 | AddRequiredFlag("-o", "Output path.", &options_.output_path); |
| 102 | AddRequiredFlag("--manifest", "Path to the Android manifest to build.", |
| 103 | &options_.manifest_path); |
| 104 | AddOptionalFlagList("-I", "Adds an Android APK to link against.", &options_.include_paths); |
| 105 | AddOptionalFlagList("-A", "An assets directory to include in the APK. These are unprocessed.", |
| 106 | &options_.assets_dirs); |
| 107 | AddOptionalFlagList("-R", "Compilation unit to link, using `overlay` semantics.\n" |
| 108 | "The last conflicting resource given takes precedence.", &overlay_arg_list_); |
| 109 | AddOptionalFlag("--package-id", |
| 110 | "Specify the package ID to use for this app. Must be greater or equal to\n" |
| 111 | "0x7f and can't be used with --static-lib or --shared-lib.", &package_id_); |
| 112 | AddOptionalFlag("--java", "Directory in which to generate R.java.", |
| 113 | &options_.generate_java_class_path); |
| 114 | AddOptionalFlag("--proguard", "Output file for generated Proguard rules.", |
| 115 | &options_.generate_proguard_rules_path); |
| 116 | AddOptionalFlag("--proguard-main-dex", |
| 117 | "Output file for generated Proguard rules for the main dex.", |
| 118 | &options_.generate_main_dex_proguard_rules_path); |
| 119 | AddOptionalSwitch("--proguard-conditional-keep-rules", |
| 120 | "Generate conditional Proguard keep rules.", |
| 121 | &options_.generate_conditional_proguard_rules); |
| 122 | AddOptionalSwitch("--no-auto-version", "Disables automatic style and layout SDK versioning.", |
| 123 | &options_.no_auto_version); |
| 124 | AddOptionalSwitch("--no-version-vectors", |
| 125 | "Disables automatic versioning of vector drawables. Use this only\n" |
| 126 | "when building with vector drawable support library.", |
| 127 | &options_.no_version_vectors); |
| 128 | AddOptionalSwitch("--no-version-transitions", |
| 129 | "Disables automatic versioning of transition resources. Use this only\n" |
| 130 | "when building with transition support library.", |
| 131 | &options_.no_version_transitions); |
| 132 | AddOptionalSwitch("--no-resource-deduping", "Disables automatic deduping of resources with\n" |
| 133 | "identical values across compatible configurations.", |
| 134 | &options_.no_resource_deduping); |
| 135 | AddOptionalSwitch("--enable-sparse-encoding", |
| 136 | "This decreases APK size at the cost of resource retrieval performance.", |
| 137 | &options_.table_flattener_options.use_sparse_entries); |
| 138 | AddOptionalSwitch("-x", "Legacy flag that specifies to use the package identifier 0x01.", |
| 139 | &legacy_x_flag_); |
| 140 | AddOptionalSwitch("-z", "Require localization of strings marked 'suggested'.", |
| 141 | &require_localization_); |
| 142 | AddOptionalFlagList("-c", |
| 143 | "Comma separated list of configurations to include. The default\n" |
| 144 | "is all configurations.", &configs_); |
| 145 | AddOptionalFlag("--preferred-density", |
| 146 | "Selects the closest matching density and strips out all others.", |
| 147 | &preferred_density_); |
| 148 | AddOptionalFlag("--product", "Comma separated list of product names to keep", &product_list_); |
| 149 | AddOptionalSwitch("--output-to-dir", "Outputs the APK contents to a directory specified by -o.", |
| 150 | &options_.output_to_directory); |
| 151 | AddOptionalSwitch("--no-xml-namespaces", "Removes XML namespace prefix and URI information\n" |
| 152 | "from AndroidManifest.xml and XML binaries in res/*.", |
| 153 | &options_.no_xml_namespaces); |
| 154 | AddOptionalFlag("--min-sdk-version", |
| 155 | "Default minimum SDK version to use for AndroidManifest.xml.", |
| 156 | &options_.manifest_fixer_options.min_sdk_version_default); |
| 157 | AddOptionalFlag("--target-sdk-version", |
| 158 | "Default target SDK version to use for AndroidManifest.xml.", |
| 159 | &options_.manifest_fixer_options.target_sdk_version_default); |
| 160 | AddOptionalFlag("--version-code", |
| 161 | "Version code (integer) to inject into the AndroidManifest.xml if none is\n" |
Ryan Mitchell | 704090e | 2018-07-31 14:59:25 -0700 | [diff] [blame^] | 162 | "present.", &options_.manifest_fixer_options.version_code_default); |
| 163 | AddOptionalFlag("--version-code-major", |
| 164 | "Version code major (integer) to inject into the AndroidManifest.xml if none is\n" |
| 165 | "present.", &options_.manifest_fixer_options.version_code_major_default); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 166 | AddOptionalFlag("--version-name", |
| 167 | "Version name to inject into the AndroidManifest.xml if none is present.", |
| 168 | &options_.manifest_fixer_options.version_name_default); |
| 169 | AddOptionalSwitch("--replace-version", |
| 170 | "If --version-code and/or --version-name are specified, these\n" |
| 171 | "values will replace any value already in the manifest. By\n" |
| 172 | "default, nothing is changed if the manifest already defines\n" |
| 173 | "these attributes.", |
| 174 | &options_.manifest_fixer_options.replace_version); |
| 175 | AddOptionalFlag("--compile-sdk-version-code", |
| 176 | "Version code (integer) to inject into the AndroidManifest.xml if none is\n" |
| 177 | "present.", |
| 178 | &options_.manifest_fixer_options.compile_sdk_version); |
| 179 | AddOptionalFlag("--compile-sdk-version-name", |
| 180 | "Version name to inject into the AndroidManifest.xml if none is present.", |
| 181 | &options_.manifest_fixer_options.compile_sdk_version_codename); |
| 182 | AddOptionalSwitch("--shared-lib", "Generates a shared Android runtime library.", |
| 183 | &shared_lib_); |
| 184 | AddOptionalSwitch("--static-lib", "Generate a static Android library.", &static_lib_); |
| 185 | AddOptionalSwitch("--proto-format", |
| 186 | "Generates compiled resources in Protobuf format.\n" |
| 187 | "Suitable as input to the bundle tool for generating an App Bundle.", |
| 188 | &proto_format_); |
| 189 | AddOptionalSwitch("--no-static-lib-packages", |
| 190 | "Merge all library resources under the app's package.", |
| 191 | &options_.no_static_lib_packages); |
Ryan Mitchell | 833a1a6 | 2018-07-10 13:51:36 -0700 | [diff] [blame] | 192 | AddOptionalSwitch("--non-final-ids", |
| 193 | "Generates R.java without the final modifier. This is implied when\n" |
| 194 | "--static-lib is specified.", |
| 195 | &options_.generate_non_final_ids); |
| 196 | AddOptionalFlag("--stable-ids", "File containing a list of name to ID mapping.", |
| 197 | &stable_id_file_path_); |
| 198 | AddOptionalFlag("--emit-ids", |
| 199 | "Emit a file at the given path with a list of name to ID mappings,\n" |
| 200 | "suitable for use with --stable-ids.", |
| 201 | &options_.resource_id_map_path); |
| 202 | AddOptionalFlag("--private-symbols", |
| 203 | "Package name to use when generating R.java for private symbols.\n" |
| 204 | "If not specified, public and private symbols will use the application's\n" |
| 205 | "package name.", |
| 206 | &options_.private_symbols); |
| 207 | AddOptionalFlag("--custom-package", "Custom Java package under which to generate R.java.", |
| 208 | &options_.custom_java_package); |
| 209 | AddOptionalFlagList("--extra-packages", |
| 210 | "Generate the same R.java but with different package names.", |
| 211 | &extra_java_packages_); |
| 212 | AddOptionalFlagList("--add-javadoc-annotation", |
| 213 | "Adds a JavaDoc annotation to all generated Java classes.", |
| 214 | &options_.javadoc_annotations); |
| 215 | AddOptionalFlag("--output-text-symbols", |
| 216 | "Generates a text file containing the resource symbols of the R class in\n" |
| 217 | "the specified folder.", |
| 218 | &options_.generate_text_symbols_path); |
| 219 | AddOptionalSwitch("--allow-reserved-package-id", |
| 220 | "Allows the use of a reserved package ID. This should on be used for\n" |
| 221 | "packages with a pre-O min-sdk\n", |
| 222 | &options_.allow_reserved_package_id); |
| 223 | AddOptionalSwitch("--auto-add-overlay", |
| 224 | "Allows the addition of new resources in overlays without\n" |
| 225 | "<add-resource> tags.", |
| 226 | &options_.auto_add_overlay); |
| 227 | AddOptionalFlag("--rename-manifest-package", "Renames the package in AndroidManifest.xml.", |
| 228 | &options_.manifest_fixer_options.rename_manifest_package); |
| 229 | AddOptionalFlag("--rename-instrumentation-target-package", |
| 230 | "Changes the name of the target package for instrumentation. Most useful\n" |
| 231 | "when used in conjunction with --rename-manifest-package.", |
| 232 | &options_.manifest_fixer_options.rename_instrumentation_target_package); |
| 233 | AddOptionalFlagList("-0", "File extensions not to compress.", |
| 234 | &options_.extensions_to_not_compress); |
| 235 | AddOptionalSwitch("--no-compress", "Do not compress any resources.", |
| 236 | &options_.do_not_compress_anything); |
| 237 | AddOptionalSwitch("--warn-manifest-validation", |
| 238 | "Treat manifest validation errors as warnings.", |
| 239 | &options_.manifest_fixer_options.warn_validation); |
| 240 | AddOptionalFlagList("--split", |
| 241 | "Split resources matching a set of configs out to a Split APK.\n" |
| 242 | "Syntax: path/to/output.apk:<config>[,<config>[...]].\n" |
| 243 | "On Windows, use a semicolon ';' separator instead.", |
| 244 | &split_args_); |
| 245 | AddOptionalSwitch("-v", "Enables verbose logging.", &verbose_); |
| 246 | AddOptionalSwitch("--debug-mode", |
| 247 | "Inserts android:debuggable=\"true\" in to the application node of the\n" |
| 248 | "manifest, making the application debuggable even on production devices.", |
| 249 | &options_.manifest_fixer_options.debug_mode); |
| 250 | AddOptionalSwitch("--strict-visibility", |
| 251 | "Do not allow overlays with different visibility levels.", |
| 252 | &options_.strict_visibility); |
| 253 | } |
| 254 | |
| 255 | int Action(const std::vector<std::string>& args) override; |
| 256 | |
| 257 | private: |
| 258 | IDiagnostics* diag_; |
| 259 | LinkOptions options_; |
| 260 | |
| 261 | std::vector<std::string> overlay_arg_list_; |
| 262 | std::vector<std::string> extra_java_packages_; |
| 263 | Maybe<std::string> package_id_; |
| 264 | std::vector<std::string> configs_; |
| 265 | Maybe<std::string> preferred_density_; |
| 266 | Maybe<std::string> product_list_; |
| 267 | bool legacy_x_flag_ = false; |
| 268 | bool require_localization_ = false; |
| 269 | bool verbose_ = false; |
| 270 | bool shared_lib_ = false; |
| 271 | bool static_lib_ = false; |
| 272 | bool proto_format_ = false; |
| 273 | Maybe<std::string> stable_id_file_path_; |
| 274 | std::vector<std::string> split_args_; |
| 275 | }; |
| 276 | |
| 277 | }// namespace aapt |
| 278 | |
| 279 | #endif //AAPT2_LINK_H |