• 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    } else {
80      _target = invoker.package_info_from_target
81      deps += [ "${_target}$build_config_target_suffix" ]
82      _build_config = get_label_info(_target, "target_gen_dir") + "/" +
83                      get_label_info(_target, "name") + ".build_config.json"
84      inputs += [ _build_config ]
85      _rebased_build_config = rebase_path(_build_config, root_build_dir)
86      _package_name = "@FileArg($_rebased_build_config:deps_info:package_name)"
87      _version_code = "@FileArg($_rebased_build_config:deps_info:version_code)"
88      _version_name = "@FileArg($_rebased_build_config:deps_info:version_name)"
89    }
90    args += [
91      "--rename-manifest-package=$_package_name",
92      "--arsc-package-name=$_package_name",
93      "--version-code=$_version_code",
94      "--version-name=$_version_name",
95      "--include-resources",
96      rebase_path(android_sdk_jar, root_build_dir),
97    ]
98  }
99
100  package_apk(target_name) {
101    forward_variables_from(invoker,
102                           [
103                             "keystore_name",
104                             "keystore_path",
105                             "keystore_password",
106                           ])
107    min_sdk_version = default_min_sdk_version
108    deps = [ ":$_resource_apk_target_name" ]
109
110    packaged_resources_path = _resource_apk_path
111    output_apk_path = invoker.stub_output
112  }
113}
114
115# Generates artifacts for system APKs.
116#
117# Variables:
118#   apk_or_bundle_target: Target that creates input bundle or apk.
119#   input_apk_or_bundle: Path to input .apk or .aab.
120#   static_library_name: For static library apks, name for the <static-library>.
121#   static_library_version: For static library apks, version for the
122#       <static-library> tag (for TrichromeLibrary, we set this to be the same
123#       as the package's version_code)
124#   output: Path to the output system .apk or .zip.
125#   fuse_apk: Fuse all apk splits into a single .apk (default: false).
126#   stub_output: Path to output stub apk (default: do not create a stub).
127#
128template("system_image_apks") {
129  if (defined(invoker.stub_output)) {
130    _stub_apk_target_name = "${target_name}__stub"
131    system_image_stub_apk(_stub_apk_target_name) {
132      forward_variables_from(invoker,
133                             [
134                               "static_library_name",
135                               "static_library_version",
136                             ])
137      package_info_from_target = invoker.apk_or_bundle_target
138      stub_output = invoker.stub_output
139    }
140  }
141
142  action_with_pydeps(target_name) {
143    script = "//build/android/gyp/system_image_apks.py"
144    deps = [ invoker.apk_or_bundle_target ]
145    inputs = [ invoker.input_apk_or_bundle ]
146    if (defined(invoker.stub_output)) {
147      public_deps = [ ":$_stub_apk_target_name" ]
148    }
149    outputs = [ invoker.output ]
150    args = [
151      "--input",
152      rebase_path(invoker.input_apk_or_bundle, root_out_dir),
153      "--output",
154      rebase_path(invoker.output, root_out_dir),
155    ]
156
157    _is_bundle =
158        filter_exclude([ invoker.input_apk_or_bundle ], [ "*.aab" ]) == []
159
160    if (_is_bundle) {
161      _wrapper_path = "$root_out_dir/bin/" +
162                      get_label_info(invoker.apk_or_bundle_target, "name")
163      args += [
164        "--bundle-wrapper",
165        rebase_path(_wrapper_path, root_out_dir),
166      ]
167      inputs += [ _wrapper_path ]
168      deps += [ "//build/android:apk_operations_py" ]
169      if (defined(invoker.fuse_apk) && invoker.fuse_apk) {
170        args += [ "--fuse-apk" ]
171      }
172    }
173  }
174}
175