xref: /aosp_15_r20/external/cronet/build/config/android/system_image.gni (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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/1408164): 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_android_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_jar,
67    ]
68    outputs = [ _resource_apk_path ]
69    args = [
70      "--aapt2-path",
71      rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
72      "--min-sdk-version=$default_min_sdk_version",
73      "--target-sdk-version=$_target_sdk_version",
74      "--android-manifest",
75      rebase_path(_manifest_path, root_build_dir),
76      "--arsc-path",
77      rebase_path(_resource_apk_path, root_build_dir),
78    ]
79    deps = [ ":$_manifest_target_name" ]
80    if (defined(invoker.package_name)) {
81      _package_name = invoker.package_name
82      _version_code = invoker.version_code
83      _version_name = invoker.version_name
84
85      # TODO(crbug.com/1408164): Make static_library_version mandatory.
86      if (defined(invoker.static_library_version)) {
87        assert(invoker.static_library_version == _version_code,
88               "$invoker.static_library_version must equal $_version_code.")
89      }
90    } else {
91      _target = invoker.package_info_from_target
92      deps += [ "${_target}$build_config_target_suffix" ]
93      _build_config = get_label_info(_target, "target_gen_dir") + "/" +
94                      get_label_info(_target, "name") + ".build_config.json"
95      inputs += [ _build_config ]
96      _rebased_build_config = rebase_path(_build_config, root_build_dir)
97      _package_name = "@FileArg($_rebased_build_config:deps_info:package_name)"
98      _version_code = "@FileArg($_rebased_build_config:deps_info:version_code)"
99      _version_name = "@FileArg($_rebased_build_config:deps_info:version_name)"
100
101      # TODO(crbug.com/1408164): Make static_library_version mandatory.
102      # Pass this through to ensure that the version code in the build config is
103      # the same as the static library version.
104      if (defined(invoker.static_library_version)) {
105        args += [
106          "--static-library-version",
107          invoker.static_library_version,
108        ]
109      }
110    }
111
112    args += [
113      "--rename-manifest-package=$_package_name",
114      "--arsc-package-name=$_package_name",
115      "--version-code=$_version_code",
116      "--version-name=$_version_name",
117      "--include-resources",
118      rebase_path(android_sdk_jar, root_build_dir),
119    ]
120  }
121
122  package_apk(target_name) {
123    forward_variables_from(invoker,
124                           [
125                             "keystore_name",
126                             "keystore_path",
127                             "keystore_password",
128                           ])
129    min_sdk_version = default_min_sdk_version
130    deps = [ ":$_resource_apk_target_name" ]
131
132    packaged_resources_path = _resource_apk_path
133    output_apk_path = invoker.stub_output
134  }
135}
136
137# Generates artifacts for system APKs.
138#
139# Variables:
140#   apk_or_bundle_target: Target that creates input bundle or apk.
141#   input_apk_or_bundle: Path to input .apk or .aab.
142#   static_library_name: For static library apks, name for the <static-library>.
143#   static_library_version: For static library apks, version for the
144#       <static-library> tag (for TrichromeLibrary, we set this to be the same
145#       as the package's version_code)
146#   output: Path to the output system .apk or .zip.
147#   fuse_apk: Fuse all apk splits into a single .apk (default: false).
148#   stub_output: Path to output stub apk (default: do not create a stub).
149#
150template("system_image_apks") {
151  if (defined(invoker.stub_output)) {
152    _stub_apk_target_name = "${target_name}__stub"
153    system_image_stub_apk(_stub_apk_target_name) {
154      forward_variables_from(invoker,
155                             [
156                               "static_library_name",
157                               "static_library_version",
158                               "override_target_sdk"
159                             ])
160      package_info_from_target = invoker.apk_or_bundle_target
161      stub_output = invoker.stub_output
162    }
163  }
164
165  action_with_pydeps(target_name) {
166    script = "//build/android/gyp/system_image_apks.py"
167    deps = [ invoker.apk_or_bundle_target ]
168    inputs = [ invoker.input_apk_or_bundle ]
169    if (defined(invoker.stub_output)) {
170      public_deps = [ ":$_stub_apk_target_name" ]
171    }
172    outputs = [ invoker.output ]
173    args = [
174      "--input",
175      rebase_path(invoker.input_apk_or_bundle, root_out_dir),
176      "--output",
177      rebase_path(invoker.output, root_out_dir),
178    ]
179
180    _is_bundle =
181        filter_exclude([ invoker.input_apk_or_bundle ], [ "*.aab" ]) == []
182
183    if (_is_bundle) {
184      _wrapper_path = "$root_out_dir/bin/" +
185                      get_label_info(invoker.apk_or_bundle_target, "name")
186      args += [
187        "--bundle-wrapper",
188        rebase_path(_wrapper_path, root_out_dir),
189      ]
190      inputs += [ _wrapper_path ]
191      deps += [ "//build/android:apk_operations_py" ]
192      if (defined(invoker.fuse_apk) && invoker.fuse_apk) {
193        args += [ "--fuse-apk" ]
194      }
195    }
196  }
197}
198