1# Copyright 2022 The Chromium Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import("//build/config/android/rules.gni") 6 7# Creates a stub .apk suitable for use with compressed system APKs. 8# 9# Variables: 10# package_name: Package name to use for the stub. 11# version_code: Version code for the stub. 12# version_name: Version name for the stub. 13# package_info_from_target: Use the package name and version_code from this 14# apk/bundle target. 15# static_library_name: For static library apks, name for the <static-library>. 16# static_library_version: For static library apks, version for the 17# <static-library> tag (for TrichromeLibrary, we set this to be the same 18# as the package's version_code) 19# stub_output: Path to output stub apk (default: do not create a stub). 20# 21# package_name and package_info_from_target are mutually exclusive. 22template("system_image_stub_apk") { 23 # Android requires stubs end with -Stub.apk. 24 assert(filter_exclude([ invoker.stub_output ], [ "*-Stub.apk" ]) == [], 25 "stub_output \"${invoker.stub_output}\" must end with \"-Stub.apk\"") 26 27 _resource_apk_path = "${target_out_dir}/$target_name.ap_" 28 _resource_apk_target_name = "${target_name}__compile_resources" 29 30 _manifest_target_name = "${target_name}__manifest" 31 _manifest_path = "$target_gen_dir/$_manifest_target_name.xml" 32 action("$_manifest_target_name") { 33 outputs = [ _manifest_path ] 34 script = "//build/android/gyp/create_stub_manifest.py" 35 args = [ 36 "--output", 37 rebase_path(_manifest_path, root_build_dir), 38 ] 39 if (defined(invoker.static_library_name)) { 40 args += [ 41 "--static-library-name", 42 invoker.static_library_name, 43 ] 44 45 # TODO(crbug.com/40253411): Make static_library_version mandatory. 46 if (defined(invoker.static_library_version)) { 47 args += [ 48 "--static-library-version", 49 invoker.static_library_version, 50 ] 51 } else { 52 args += [ "--static-library-version=1" ] 53 } 54 } 55 } 56 57 _target_sdk_version = default_target_sdk_version 58 if (defined(invoker.override_target_sdk)) { 59 _target_sdk_version = invoker.override_target_sdk 60 } 61 62 action_with_pydeps(_resource_apk_target_name) { 63 script = "//build/android/gyp/compile_resources.py" 64 inputs = [ 65 _manifest_path, 66 android_sdk_tools_bundle_aapt2, 67 android_sdk_jar, 68 69 # TODO(b/315080809#comment4): remove these files after fixing 70 # build/print_python_deps.py. 71 "//third_party/protobuf/python/google/__init__.py", 72 "//third_party/protobuf/python/google/protobuf/__init__.py", 73 "//third_party/protobuf/python/google/protobuf/compiler/__init__.py", 74 "//third_party/protobuf/python/google/protobuf/compiler/plugin_pb2.py", 75 "//third_party/protobuf/python/google/protobuf/descriptor.py", 76 "//third_party/protobuf/python/google/protobuf/descriptor_database.py", 77 "//third_party/protobuf/python/google/protobuf/descriptor_pb2.py", 78 "//third_party/protobuf/python/google/protobuf/descriptor_pool.py", 79 "//third_party/protobuf/python/google/protobuf/internal/__init__.py", 80 "//third_party/protobuf/python/google/protobuf/internal/_parameterized.py", 81 "//third_party/protobuf/python/google/protobuf/internal/api_implementation.py", 82 "//third_party/protobuf/python/google/protobuf/internal/builder.py", 83 "//third_party/protobuf/python/google/protobuf/internal/containers.py", 84 "//third_party/protobuf/python/google/protobuf/internal/decoder.py", 85 "//third_party/protobuf/python/google/protobuf/internal/descriptor_database_test.py", 86 "//third_party/protobuf/python/google/protobuf/internal/descriptor_pool_test.py", 87 "//third_party/protobuf/python/google/protobuf/internal/descriptor_test.py", 88 "//third_party/protobuf/python/google/protobuf/internal/encoder.py", 89 "//third_party/protobuf/python/google/protobuf/internal/enum_type_wrapper.py", 90 "//third_party/protobuf/python/google/protobuf/internal/extension_dict.py", 91 "//third_party/protobuf/python/google/protobuf/internal/generator_test.py", 92 "//third_party/protobuf/python/google/protobuf/internal/import_test.py", 93 "//third_party/protobuf/python/google/protobuf/internal/import_test_package/__init__.py", 94 "//third_party/protobuf/python/google/protobuf/internal/json_format_test.py", 95 "//third_party/protobuf/python/google/protobuf/internal/keywords_test.py", 96 "//third_party/protobuf/python/google/protobuf/internal/message_factory_test.py", 97 "//third_party/protobuf/python/google/protobuf/internal/message_listener.py", 98 "//third_party/protobuf/python/google/protobuf/internal/message_test.py", 99 "//third_party/protobuf/python/google/protobuf/internal/proto_builder_test.py", 100 "//third_party/protobuf/python/google/protobuf/internal/python_message.py", 101 "//third_party/protobuf/python/google/protobuf/internal/reflection_test.py", 102 "//third_party/protobuf/python/google/protobuf/internal/service_reflection_test.py", 103 "//third_party/protobuf/python/google/protobuf/internal/symbol_database_test.py", 104 "//third_party/protobuf/python/google/protobuf/internal/test_util.py", 105 "//third_party/protobuf/python/google/protobuf/internal/testing_refleaks.py", 106 "//third_party/protobuf/python/google/protobuf/internal/text_encoding_test.py", 107 "//third_party/protobuf/python/google/protobuf/internal/text_format_test.py", 108 "//third_party/protobuf/python/google/protobuf/internal/type_checkers.py", 109 "//third_party/protobuf/python/google/protobuf/internal/unknown_fields_test.py", 110 "//third_party/protobuf/python/google/protobuf/internal/well_known_types.py", 111 "//third_party/protobuf/python/google/protobuf/internal/well_known_types_test.py", 112 "//third_party/protobuf/python/google/protobuf/internal/wire_format.py", 113 "//third_party/protobuf/python/google/protobuf/internal/wire_format_test.py", 114 "//third_party/protobuf/python/google/protobuf/json_format.py", 115 "//third_party/protobuf/python/google/protobuf/message.py", 116 "//third_party/protobuf/python/google/protobuf/message_factory.py", 117 "//third_party/protobuf/python/google/protobuf/proto_builder.py", 118 "//third_party/protobuf/python/google/protobuf/pyext/__init__.py", 119 "//third_party/protobuf/python/google/protobuf/pyext/cpp_message.py", 120 "//third_party/protobuf/python/google/protobuf/reflection.py", 121 "//third_party/protobuf/python/google/protobuf/service.py", 122 "//third_party/protobuf/python/google/protobuf/service_reflection.py", 123 "//third_party/protobuf/python/google/protobuf/symbol_database.py", 124 "//third_party/protobuf/python/google/protobuf/text_encoding.py", 125 "//third_party/protobuf/python/google/protobuf/text_format.py", 126 "//third_party/protobuf/python/google/protobuf/unknown_fields.py", 127 "//third_party/protobuf/python/google/protobuf/util/__init__.py", 128 ] 129 outputs = [ _resource_apk_path ] 130 args = [ 131 "--aapt2-path", 132 rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir), 133 "--min-sdk-version=$default_min_sdk_version", 134 "--target-sdk-version=$_target_sdk_version", 135 "--android-manifest", 136 rebase_path(_manifest_path, root_build_dir), 137 "--arsc-path", 138 rebase_path(_resource_apk_path, root_build_dir), 139 ] 140 deps = [ ":$_manifest_target_name" ] 141 if (defined(invoker.package_name)) { 142 _package_name = invoker.package_name 143 _version_code = invoker.version_code 144 _version_name = invoker.version_name 145 146 # TODO(crbug.com/40253411): Make static_library_version mandatory. 147 if (defined(invoker.static_library_version)) { 148 assert(invoker.static_library_version == _version_code, 149 "$invoker.static_library_version must equal $_version_code.") 150 } 151 } else { 152 _target = invoker.package_info_from_target 153 deps += [ "${_target}$build_config_target_suffix" ] 154 _build_config = get_label_info(_target, "target_gen_dir") + "/" + 155 get_label_info(_target, "name") + ".build_config.json" 156 inputs += [ _build_config ] 157 _rebased_build_config = rebase_path(_build_config, root_build_dir) 158 _package_name = "@FileArg($_rebased_build_config:deps_info:package_name)" 159 _version_code = "@FileArg($_rebased_build_config:deps_info:version_code)" 160 _version_name = "@FileArg($_rebased_build_config:deps_info:version_name)" 161 162 # TODO(crbug.com/40253411): Make static_library_version mandatory. 163 # Pass this through to ensure that the version code in the build config is 164 # the same as the static library version. 165 if (defined(invoker.static_library_version)) { 166 args += [ 167 "--static-library-version", 168 invoker.static_library_version, 169 ] 170 } 171 } 172 173 args += [ 174 "--rename-manifest-package=$_package_name", 175 "--arsc-package-name=$_package_name", 176 "--version-code=$_version_code", 177 "--version-name=$_version_name", 178 "--include-resources", 179 rebase_path(android_sdk_jar, root_build_dir), 180 ] 181 } 182 183 package_apk(target_name) { 184 forward_variables_from(invoker, 185 [ 186 "keystore_name", 187 "keystore_path", 188 "keystore_password", 189 ]) 190 min_sdk_version = default_min_sdk_version 191 deps = [ ":$_resource_apk_target_name" ] 192 193 packaged_resources_path = _resource_apk_path 194 output_apk_path = invoker.stub_output 195 } 196} 197 198# Generates artifacts for system APKs. 199# 200# Variables: 201# apk_or_bundle_target: Target that creates input bundle or apk. 202# input_apk_or_bundle: Path to input .apk or .aab. 203# static_library_name: For static library apks, name for the <static-library>. 204# static_library_version: For static library apks, version for the 205# <static-library> tag (for TrichromeLibrary, we set this to be the same 206# as the package's version_code) 207# output: Path to the output system .apk or .zip. 208# fuse_apk: Fuse all apk splits into a single .apk (default: false). 209# stub_output: Path to output stub apk (default: do not create a stub). 210# 211template("system_image_apks") { 212 if (defined(invoker.stub_output)) { 213 _stub_apk_target_name = "${target_name}__stub" 214 system_image_stub_apk(_stub_apk_target_name) { 215 forward_variables_from(invoker, 216 [ 217 "static_library_name", 218 "static_library_version", 219 "override_target_sdk", 220 ]) 221 package_info_from_target = invoker.apk_or_bundle_target 222 stub_output = invoker.stub_output 223 } 224 } 225 226 action_with_pydeps(target_name) { 227 script = "//build/android/gyp/system_image_apks.py" 228 deps = [ invoker.apk_or_bundle_target ] 229 inputs = [ invoker.input_apk_or_bundle ] 230 if (defined(invoker.stub_output)) { 231 public_deps = [ ":$_stub_apk_target_name" ] 232 } 233 outputs = [ invoker.output ] 234 args = [ 235 "--input", 236 rebase_path(invoker.input_apk_or_bundle, root_out_dir), 237 "--output", 238 rebase_path(invoker.output, root_out_dir), 239 ] 240 241 _is_bundle = 242 filter_exclude([ invoker.input_apk_or_bundle ], [ "*.aab" ]) == [] 243 244 if (_is_bundle) { 245 _wrapper_path = "$root_out_dir/bin/" + 246 get_label_info(invoker.apk_or_bundle_target, "name") 247 args += [ 248 "--bundle-wrapper", 249 rebase_path(_wrapper_path, root_out_dir), 250 ] 251 inputs += [ _wrapper_path ] 252 deps += [ "//build/android:apk_operations_py" ] 253 if (defined(invoker.fuse_apk) && invoker.fuse_apk) { 254 args += [ "--fuse-apk" ] 255 } 256 } 257 } 258} 259