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