• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2014 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
5# Do not add any imports to non-//build directories here.
6# Some projects (e.g. V8) do not have non-build directories DEPS'ed in.
7import("//build/config/android/config.gni")
8import("//build/config/android/copy_ex.gni")
9import("//build/config/clang/clang.gni")
10import("//build/config/compiler/compiler.gni")
11import("//build/config/coverage/coverage.gni")
12import("//build/config/profiling/profiling.gni")
13import("//build/config/python.gni")
14import("//build/config/sanitizers/sanitizers.gni")
15import("//build/config/zip.gni")
16import("//build/toolchain/toolchain.gni")
17assert(
18    is_android || is_robolectric,
19    "current_toolchain=$current_toolchain default_toolchain=$default_toolchain")
20
21if (target_cpu == "arm") {
22  _sanitizer_arch = "arm"
23} else if (target_cpu == "arm64") {
24  _sanitizer_arch = "aarch64"
25} else if (target_cpu == "x86") {
26  _sanitizer_arch = "i686"
27} else if (target_cpu == "riscv64") {
28  _sanitizer_arch = "riscv64"
29}
30
31_sanitizer_runtimes = []
32if (use_cfi_diag || is_ubsan || is_ubsan_security || is_ubsan_vptr) {
33  _sanitizer_runtimes = [ "$clang_base_path/lib/clang/$clang_version/lib/linux/libclang_rt.ubsan_standalone-$_sanitizer_arch-android.so" ]
34}
35
36_BUNDLETOOL_JAR_PATH =
37    "//third_party/android_build_tools/bundletool/bundletool.jar"
38
39# Creates a dist directory for a native executable.
40#
41# Running a native executable on a device requires all the shared library
42# dependencies of that executable. To make it easier to install and run such an
43# executable, this will create a directory containing the native exe and all
44# it's library dependencies.
45#
46# Note: It's usually better to package things as an APK than as a native
47# executable.
48#
49# Variables
50#   dist_dir: Directory for the exe and libraries. Everything in this directory
51#     will be deleted before copying in the exe and libraries.
52#   binary: Path to (stripped) executable.
53#   extra_files: List of extra files to copy in (optional).
54#
55# Example
56#   create_native_executable_dist("foo_dist") {
57#     dist_dir = "$root_build_dir/foo_dist"
58#     binary = "$root_build_dir/foo"
59#     deps = [ ":the_thing_that_makes_foo" ]
60#   }
61template("create_native_executable_dist") {
62  forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
63
64  _libraries_list = "${target_gen_dir}/${target_name}_library_dependencies.list"
65
66  _sanitizer_runtimes_target_name = "${target_name}__sanitizer_runtimes"
67  group(_sanitizer_runtimes_target_name) {
68    metadata = {
69      shared_libraries = _sanitizer_runtimes
70    }
71  }
72
73  generated_file("${target_name}__library_list") {
74    forward_variables_from(invoker, [ "deps" ])
75    if (!defined(deps)) {
76      deps = []
77    }
78    deps += [ ":${_sanitizer_runtimes_target_name}" ]
79    output_conversion = "json"
80    outputs = [ _libraries_list ]
81    data_keys = [ "shared_libraries" ]
82    walk_keys = [ "shared_libraries_barrier" ]
83    rebase = root_build_dir
84  }
85
86  copy_ex(target_name) {
87    inputs = [
88      _libraries_list,
89      invoker.binary,
90    ]
91
92    dest = invoker.dist_dir
93    data = [ "${invoker.dist_dir}/" ]
94
95    _rebased_libraries_list = rebase_path(_libraries_list, root_build_dir)
96    _rebased_binaries_list = rebase_path([ invoker.binary ], root_build_dir)
97    args = [
98      "--clear",
99      "--files=@FileArg($_rebased_libraries_list)",
100      "--files=$_rebased_binaries_list",
101    ]
102    if (defined(invoker.extra_files)) {
103      _rebased_extra_files = rebase_path(invoker.extra_files, root_build_dir)
104      args += [ "--files=$_rebased_extra_files" ]
105    }
106
107    _depfile = "$target_gen_dir/$target_name.d"
108    _stamp_file = "$target_gen_dir/$target_name.stamp"
109    outputs = [ _stamp_file ]
110    args += [
111      "--depfile",
112      rebase_path(_depfile, root_build_dir),
113      "--stamp",
114      rebase_path(_stamp_file, root_build_dir),
115    ]
116
117    deps = [ ":${target_name}__library_list" ]
118    if (defined(invoker.deps)) {
119      deps += invoker.deps
120    }
121  }
122}
123
124# non-robolectric things
125if (enable_java_templates && is_android) {
126  import("//build/config/android/internal_rules.gni")
127
128  # Declare a target for c-preprocessor-generated java files
129  #
130  # NOTE: For generating Java conterparts to enums prefer using the java_cpp_enum
131  #       rule instead.
132  #
133  # This target generates java files using the host C pre-processor. Each file in
134  # sources will be compiled using the C pre-processor. If include_path is
135  # specified, it will be passed (with --I) to the pre-processor.
136  #
137  # This target will create a single .srcjar. Adding this target to an
138  # android_library target's srcjar_deps will make the generated java files be
139  # included in that library's final outputs.
140  #
141  # Variables
142  #   sources: list of files to be processed by the C pre-processor. For each
143  #     file in sources, there will be one .java file in the final .srcjar. For a
144  #     file named FooBar.template, a java file will be created with name
145  #     FooBar.java.
146  #   inputs: additional compile-time dependencies. Any files
147  #     `#include`-ed in the templates should be listed here.
148  #   defines: List of -D arguments for the preprocessor.
149  #
150  # Example
151  #   java_cpp_template("foo_generated_enum") {
152  #     sources = [
153  #       "android/java/templates/Foo.template",
154  #     ]
155  #     inputs = [
156  #       "android/java/templates/native_foo_header.h",
157  #     ]
158  #   }
159  template("java_cpp_template") {
160    action_with_pydeps(target_name) {
161      forward_variables_from(invoker,
162                             [
163                               "data_deps",
164                               "deps",
165                               "inputs",
166                               "public_deps",
167                               "sources",
168                               "testonly",
169                               "visibility",
170                             ])
171      script = "//build/android/gyp/gcc_preprocess.py"
172      outputs = [ "$target_gen_dir/$target_name.srcjar" ]
173
174      _include_dirs = [
175        "//",
176        root_gen_dir,
177      ]
178      _rebased_include_dirs = rebase_path(_include_dirs, root_build_dir)
179      args = [
180        "--include-dirs=$_rebased_include_dirs",
181        "--output",
182        rebase_path(outputs[0], root_build_dir),
183      ]
184      if (defined(invoker.defines)) {
185        foreach(_define, invoker.defines) {
186          args += [
187            "--define",
188            _define,
189          ]
190        }
191      }
192      args += rebase_path(sources, root_build_dir)
193    }
194  }
195
196  # Declare a target for generating Java classes from C++ enums.
197  #
198  # This target generates Java files from C++ enums using a script.
199  #
200  # This target will create a single .srcjar. Adding this target to an
201  # android_library target's srcjar_deps will make the generated java files be
202  # included in that library's final outputs.
203  #
204  # Variables
205  #   sources: list of files to be processed by the script. For each annotated
206  #     enum contained in the sources files the script will generate a .java
207  #     file with the same name as the name of the enum.
208  #
209  # Example
210  #   java_cpp_enum("foo_generated_enum") {
211  #     sources = [
212  #       "src/native_foo_header.h",
213  #     ]
214  #   }
215  template("java_cpp_enum") {
216    action_with_pydeps(target_name) {
217      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "sources" ])
218
219      # The sources aren't compiled so don't check their dependencies.
220      check_includes = false
221      script = "//build/android/gyp/java_cpp_enum.py"
222
223      _srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
224      _rebased_srcjar_path = rebase_path(_srcjar_path, root_build_dir)
225      _rebased_sources = rebase_path(invoker.sources, root_build_dir)
226
227      args = [ "--srcjar=$_rebased_srcjar_path" ] + _rebased_sources
228      outputs = [ _srcjar_path ]
229    }
230  }
231
232  # Declare a target for generating Java classes with string constants matching
233  # those found in C++ files using a python script.
234  #
235  # This target will create a single .srcjar. Adding this target to an
236  # android_library target's srcjar_deps will make the generated java files be
237  # included in that library's final outputs.
238  #
239  # Variables
240  #   sources: list of files to be processed by the script. For each string
241  #            constant in the source files, the script will add a corresponding
242  #            Java string to the specified template file.
243  # Example
244  #   java_cpp_strings("foo_switches") {
245  #     sources = [
246  #       "src/foo_switches.cc",
247  #     ]
248  #     template = "src/templates/FooSwitches.java.tmpl
249  #   }
250  #
251  # foo_switches.cc:
252  #
253  # // A switch.
254  # const char kASwitch = "a-switch";
255  #
256  # FooSwitches.java.tmpl
257  #
258  # // Copyright {YEAR} The Chromium Authors
259  # // Use of this source code is governed by a BSD-style license that can be
260  # // found in the LICENSE file.
261  #
262  # // This file is autogenerated by
263  # //     {SCRIPT_NAME}
264  # // From
265  # //     {SOURCE_PATH}, and
266  # //     {TEMPLATE_PATH}
267  #
268  # package my.java.package;
269  #
270  # public abstract class FooSwitches {{
271  #     // ...snip...
272  # {NATIVE_STRINGS}
273  #     // ...snip...
274  # }}
275  #
276  # result:
277  #   A FooSwitches.java file, defining a class named FooSwitches in the package
278  #   my.java.package.
279  template("java_cpp_strings") {
280    action_with_pydeps(target_name) {
281      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "sources" ])
282
283      # The sources aren't compiled so don't check their dependencies.
284      check_includes = false
285      script = "//build/android/gyp/java_cpp_strings.py"
286
287      _srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
288      _rebased_srcjar_path = rebase_path(_srcjar_path, root_build_dir)
289      _rebased_sources = rebase_path(invoker.sources, root_build_dir)
290      _rebased_template = rebase_path(invoker.template, root_build_dir)
291
292      args = [
293        "--srcjar=$_rebased_srcjar_path",
294        "--template=$_rebased_template",
295      ]
296      args += _rebased_sources
297      sources += [ invoker.template ]
298
299      outputs = [ _srcjar_path ]
300    }
301  }
302
303  # Declare a target for generating Java classes with string constants matching
304  # those found in C++ base::Feature declarations, using a python script.
305  #
306  # This target will create a single .srcjar. Adding this target to an
307  # android_library target's srcjar_deps will make the generated java files be
308  # included in that library's final outputs.
309  #
310  # Variables
311  #   sources: list of files to be processed by the script. For each
312  #            base::Feature in the source files, the script will add a
313  #            corresponding Java string for that feature's name to the
314  #            specified template file.
315  # Example
316  #   java_cpp_features("foo_features") {
317  #     sources = [
318  #       "src/foo_features.cc",
319  #     ]
320  #     template = "src/templates/FooFeatures.java.tmpl
321  #   }
322  #
323  # foo_features.cc:
324  #
325  # // A feature.
326  # BASE_FEATURE(kSomeFeature, "SomeFeature",
327  #              base::FEATURE_DISABLED_BY_DEFAULT);
328  #
329  # FooFeatures.java.tmpl
330  #
331  # // Copyright $YEAR The Chromium Authors
332  # // Use of this source code is governed by a BSD-style license that can be
333  # // found in the LICENSE file.
334  #
335  # package my.java.package;
336  #
337  # public final class FooFeatures {{
338  #     // ...snip...
339  # {NATIVE_STRINGS}
340  #     // ...snip...
341  #     // Do not instantiate this class.
342  #     private FooFeatures() {{}}
343  # }}
344  #
345  # result:
346  #   A FooFeatures.java file, defining a class named FooFeatures in the package
347  #   my.java.package.
348  template("java_cpp_features") {
349    action_with_pydeps(target_name) {
350      forward_variables_from(invoker,
351                             TESTONLY_AND_VISIBILITY + [
352                                   "deps",
353                                   "sources",
354                                 ])
355
356      # The sources aren't compiled so don't check their dependencies.
357      check_includes = false
358      script = "//build/android/gyp/java_cpp_features.py"
359
360      _srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
361      _rebased_srcjar_path = rebase_path(_srcjar_path, root_build_dir)
362      _rebased_sources = rebase_path(invoker.sources, root_build_dir)
363      _rebased_template = rebase_path(invoker.template, root_build_dir)
364
365      args = [
366        "--srcjar=$_rebased_srcjar_path",
367        "--template=$_rebased_template",
368      ]
369      args += _rebased_sources
370      sources += [ invoker.template ]
371
372      outputs = [ _srcjar_path ]
373    }
374  }
375
376  # Declare a target for processing a Jinja template.
377  #
378  # Variables
379  #   input: The template file to be processed.
380  #   includes: List of files {% include %}'ed by input.
381  #   output: Where to save the result.
382  #   variables: (Optional) A list of variables to make available to the template
383  #     processing environment, e.g. ["name=foo", "color=red"].
384  #
385  # Example
386  #   jinja_template("chrome_public_manifest") {
387  #     input = "java/AndroidManifest.xml"
388  #     output = "$target_gen_dir/AndroidManifest.xml"
389  #   }
390  template("jinja_template") {
391    action_with_pydeps(target_name) {
392      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
393      inputs = [ invoker.input ]
394      if (defined(invoker.includes)) {
395        inputs += invoker.includes
396      }
397      script = "//build/android/gyp/jinja_template.py"
398
399      outputs = [ invoker.output ]
400
401      args = [
402        "--loader-base-dir",
403        rebase_path("//", root_build_dir),
404        "--inputs",
405        rebase_path(invoker.input, root_build_dir),
406        "--output",
407        rebase_path(invoker.output, root_build_dir),
408        "--check-includes",
409      ]
410      if (defined(invoker.includes)) {
411        _rebased_includes = rebase_path(invoker.includes, root_build_dir)
412        args += [ "--includes=$_rebased_includes" ]
413      }
414      if (defined(invoker.variables)) {
415        args += [ "--variables=${invoker.variables}" ]
416      }
417    }
418  }
419
420  # Writes native libraries to a NativeLibaries.java file.
421  #
422  # This target will create a single .srcjar. Adding this target to an
423  # android_library target's srcjar_deps will make the generated java files be
424  # included in that library's final outputs.
425  #
426  # Variables:
427  #   native_libraries_list_file: (Optional) Path to file listing all native
428  #     libraries to write.
429  #   version_number: (Optional) String of expected version of 'main' native
430  #     library.
431  #   enable_chromium_linker: (Optional) Whether to use the Chromium linker.
432  #   use_final_fields: True to use final fields. When false, all other
433  #       variables must not be set.
434  template("write_native_libraries_java") {
435    _native_libraries_file = "$target_gen_dir/$target_name.srcjar"
436    if (current_cpu == "arm" || current_cpu == "arm64") {
437      _cpu_family = "CPU_FAMILY_ARM"
438    } else if (current_cpu == "x86" || current_cpu == "x64") {
439      _cpu_family = "CPU_FAMILY_X86"
440    } else if (current_cpu == "mipsel" || current_cpu == "mips64el") {
441      _cpu_family = "CPU_FAMILY_MIPS"
442    } else if (current_cpu == "riscv64") {
443      _cpu_family = "CPU_FAMILY_RISCV"
444    } else {
445      assert(false, "Unsupported CPU family")
446    }
447
448    action_with_pydeps(target_name) {
449      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
450      script = "//build/android/gyp/write_native_libraries_java.py"
451      outputs = [ _native_libraries_file ]
452      args = [
453        "--output",
454        rebase_path(_native_libraries_file, root_build_dir),
455        "--cpu-family",
456        _cpu_family,
457      ]
458      if (invoker.use_final_fields) {
459        # Write native_libraries_list_file via depfile rather than specifyin it
460        # as a dep in order allow R8 to run in parallel with native compilation.
461        args += [ "--final" ]
462        if (defined(invoker.native_libraries_list_file)) {
463          depfile = "$target_gen_dir/$target_name.d"
464          args += [
465            "--native-libraries-list",
466            rebase_path(invoker.native_libraries_list_file, root_build_dir),
467            "--depfile",
468            rebase_path(depfile, root_build_dir),
469          ]
470        }
471        if (defined(invoker.main_component_library)) {
472          args += [
473            "--main-component-library",
474            invoker.main_component_library,
475          ]
476        }
477        if (defined(invoker.enable_chromium_linker) &&
478            invoker.enable_chromium_linker) {
479          args += [ "--enable-chromium-linker" ]
480        }
481        if (defined(invoker.native_lib_32_bit) && invoker.native_lib_32_bit) {
482          args += [ "--native-lib-32-bit" ]
483        }
484        if (defined(invoker.native_lib_64_bit) && invoker.native_lib_64_bit) {
485          args += [ "--native-lib-64-bit" ]
486        }
487      }
488    }
489  }
490
491  # Declare a target for a set of Android resources generated at build
492  # time and stored in a single zip archive. The content of the archive
493  # should match the layout of a regular Android res/ folder (but the
494  # archive should not include a top-level res/ directory).
495  #
496  # Note that there is no associated .srcjar, R.txt or package name
497  # associated with this target.
498  #
499  # Variables:
500  #   generated_resources_zip: Generated zip archive path.
501  #   generating_target: Name of the target generating
502  #     generated_resources_zip. This rule will check that it is part
503  #     of its outputs.
504  #   deps: Specifies the dependencies of this target. Any Android resources
505  #     listed here will be also be included *after* this one when compiling
506  #     all resources for a final apk or junit binary. This is useful to
507  #     ensure that the resources of the current target override those of the
508  #     dependency as well (and would not work if you have these deps to the
509  #     generating target's dependencies).
510  #
511  # Example
512  #   _zip_archive = "$target_gen_dir/${target_name}.resources_zip"
513  #
514  #   action("my_resources__create_zip") {
515  #     _depfile = "$target_gen_dir/${target_name}.d"
516  #     script = "//build/path/to/create_my_resources_zip.py"
517  #     args = [
518  #       "--depfile", rebase_path(_depfile, root_build_dir),
519  #       "--output-zip", rebase_path(_zip_archive, root_build_dir),
520  #     ]
521  #     inputs = []
522  #     outputs = _zip_archive
523  #     depfile = _depfile
524  #   }
525  #
526  #   android_generated_resources("my_resources") {
527  #      generated_resources_zip = _zip_archive
528  #      generating_target = ":my_resources__create_zip"
529  #   }
530  #
531  template("android_generated_resources") {
532    forward_variables_from(invoker, [ "testonly" ])
533    _build_config = "$target_gen_dir/${target_name}.build_config.json"
534    _rtxt_out_path = "$target_gen_dir/${target_name}.R.txt"
535    write_build_config("$target_name$build_config_target_suffix") {
536      forward_variables_from(invoker, [ "resource_overlay" ])
537
538      build_config = _build_config
539      resources_zip = invoker.generated_resources_zip
540      type = "android_resources"
541      if (defined(invoker.deps)) {
542        possible_config_deps = invoker.deps
543      }
544      r_text = _rtxt_out_path
545    }
546    action_with_pydeps(target_name) {
547      forward_variables_from(invoker, [ "visibility" ])
548      public_deps = [
549        ":$target_name$build_config_target_suffix",
550        invoker.generating_target,
551      ]
552      inputs = [ invoker.generated_resources_zip ]
553      outputs = [ _rtxt_out_path ]
554      script = "//build/android/gyp/create_r_txt.py"
555      args = [
556        "--resources-zip-path",
557        rebase_path(invoker.generated_resources_zip, root_build_dir),
558        "--rtxt-path",
559        rebase_path(_rtxt_out_path, root_build_dir),
560      ]
561    }
562  }
563
564  # Declare a target for processing Android resources as Jinja templates.
565  #
566  # This takes an Android resource directory where each resource is a Jinja
567  # template, processes each template, then packages the results in a zip file
568  # which can be consumed by an android resources, library, or apk target.
569  #
570  # If this target is included in the deps of an android resources/library/apk,
571  # the resources will be included with that target.
572  #
573  # Variables
574  #   resources: The list of resources files to process.
575  #   res_dir: The resource directory containing the resources.
576  #   variables: (Optional) A list of variables to make available to the template
577  #     processing environment, e.g. ["name=foo", "color=red"].
578  #
579  # Example
580  #   jinja_template_resources("chrome_public_template_resources") {
581  #     res_dir = "res_template"
582  #     resources = ["res_template/xml/syncable.xml"]
583  #     variables = ["color=red"]
584  #   }
585  template("jinja_template_resources") {
586    _resources_zip = "$target_out_dir/${target_name}.resources.zip"
587    _generating_target_name = "${target_name}__template"
588
589    action_with_pydeps(_generating_target_name) {
590      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
591      inputs = invoker.resources
592      script = "//build/android/gyp/jinja_template.py"
593
594      outputs = [ _resources_zip ]
595
596      _rebased_resources = rebase_path(invoker.resources, root_build_dir)
597      args = [
598        "--inputs=${_rebased_resources}",
599        "--inputs-base-dir",
600        rebase_path(invoker.res_dir, root_build_dir),
601        "--outputs-zip",
602        rebase_path(_resources_zip, root_build_dir),
603        "--check-includes",
604      ]
605      if (defined(invoker.variables)) {
606        variables = invoker.variables
607        args += [ "--variables=${variables}" ]
608      }
609    }
610
611    android_generated_resources(target_name) {
612      forward_variables_from(invoker,
613                             TESTONLY_AND_VISIBILITY + [
614                                   "deps",
615                                   "resource_overlay",
616                                 ])
617      generating_target = ":$_generating_target_name"
618      generated_resources_zip = _resources_zip
619    }
620  }
621
622  # Declare a prebuilt android native library.
623  #
624  # This takes a base directory and library name and then looks for the library
625  # in <base dir>/$android_app_abi/<library name>.
626  #
627  # If you depend on this target, the library is stripped and output to the
628  # same locations non-prebuilt libraries are output.
629  #
630  # Variables
631  #   base_dir: Directory where all ABIs of the library live.
632  #   library_name: Name of the library .so file.
633  #
634  # Example
635  #   android_native_prebuilt("elements_native") {
636  #     base_dir = "//third_party/elements"
637  #     lib_name = "elements.so"
638  #   }
639  template("android_native_prebuilt") {
640    action_with_pydeps(target_name) {
641      forward_variables_from(invoker,
642                             [
643                               "deps",
644                               "testonly",
645                             ])
646      script = "//build/android/gyp/process_native_prebuilt.py"
647      _lib_path = "${invoker.base_dir}/$android_app_abi/${invoker.lib_name}"
648      _stripped_output_path = "$root_out_dir/${invoker.lib_name}"
649      _unstripped_output_path =
650          "$root_out_dir/lib.unstripped/${invoker.lib_name}"
651      inputs = [ _lib_path ]
652      outputs = [
653        _stripped_output_path,
654        _unstripped_output_path,
655      ]
656
657      # Add unstripped output to runtime deps for use by bots during stacktrace
658      # symbolization.
659      data = [ _unstripped_output_path ]
660
661      _rebased_lib_path = rebase_path(_lib_path, root_build_dir)
662      _rebased_stripped_ouput_path =
663          rebase_path(_stripped_output_path, root_build_dir)
664      _rebased_unstripped_ouput_path =
665          rebase_path(_unstripped_output_path, root_build_dir)
666      _strip_tool_path =
667          rebase_path("//buildtools/third_party/eu-strip/bin/eu-strip",
668                      root_build_dir)
669
670      args = [
671        "--strip-path=$_strip_tool_path",
672        "--input-path=$_rebased_lib_path",
673        "--stripped-output-path=$_rebased_stripped_ouput_path",
674        "--unstripped-output-path=$_rebased_unstripped_ouput_path",
675      ]
676    }
677  }
678
679  # Declare an Android resources target
680  #
681  # This creates a resources zip file that will be used when building an Android
682  # library or apk and included into a final apk.
683  #
684  # To include these resources in a library/apk, this target should be listed in
685  # the library's deps. A library/apk will also include any resources used by its
686  # own dependencies.
687  #
688  # Variables
689  #   sources: List of resource files for this target.
690  #   deps: Specifies the dependencies of this target. Any Android resources
691  #     listed in deps will be included by libraries/apks that depend on this
692  #     target.
693  #   alternative_android_sdk_dep: Optional. Alternative Android system
694  #     android java target to use.
695  #   android_manifest: AndroidManifest.xml for this target (optional). Will be
696  #     merged into apks that directly or indirectly depend on this target.
697  #   android_manifest_dep: Target that generates AndroidManifest (if applicable)
698  #   custom_package: java package for generated .java files.
699  #   allow_missing_resources: Do not fail if a resource exists in a directory
700  #      but is not listed in sources.
701  #   shared_resources: If true make a resource package that can be loaded by a
702  #     different application at runtime to access the package's resources.
703  #   resource_overlay: Whether the resources in 'sources' should override
704  #     resources with the same name. Does not affect the behaviour of any
705  #     android_resources() deps of this target. If a target with
706  #     resource_overlay=true depends on another target with
707  #     resource_overlay=true the target with the dependency overrides the
708  #     other.
709  #   r_text_file: (optional) path to pre-generated R.txt to be used when
710  #     generating R.java instead of resource-based aapt-generated one.
711  #   recursive_resource_deps: (optional) whether deps should be walked
712  #     recursively to find resource deps.
713  #
714  # Example:
715  #   android_resources("foo_resources") {
716  #     deps = [":foo_strings_grd"]
717  #     sources = [
718  #       "res/drawable/foo1.xml",
719  #       "res/drawable/foo2.xml",
720  #     ]
721  #     custom_package = "org.chromium.foo"
722  #   }
723  #
724  #   android_resources("foo_resources_overrides") {
725  #     deps = [":foo_resources"]
726  #     sources = [
727  #       "res_overrides/drawable/foo1.xml",
728  #       "res_overrides/drawable/foo2.xml",
729  #     ]
730  #   }
731  template("android_resources") {
732    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
733
734    _base_path = "$target_gen_dir/$target_name"
735    if (defined(invoker.v14_skip)) {
736      not_needed(invoker, [ "v14_skip" ])
737    }
738
739    _res_sources_path = "$target_gen_dir/${invoker.target_name}.res.sources"
740
741    _resources_zip = "$target_out_dir/$target_name.resources.zip"
742    _r_text_out_path = _base_path + "_R.txt"
743    _build_config = _base_path + ".build_config.json"
744    _build_config_target_name = "$target_name$build_config_target_suffix"
745
746    _deps = []
747    if (defined(invoker.deps)) {
748      _deps += invoker.deps
749    }
750
751    if (defined(invoker.alternative_android_sdk_dep)) {
752      _android_sdk_dep = invoker.alternative_android_sdk_dep
753    } else {
754      _android_sdk_dep = default_android_sdk_dep
755    }
756
757    _resource_files = []
758    if (defined(invoker.sources)) {
759      _resource_files += invoker.sources
760    }
761
762    _rebased_resource_files = rebase_path(_resource_files, root_build_dir)
763    write_file(_res_sources_path, _rebased_resource_files)
764
765    # This is necessary so we only lint chromium resources.
766    if (defined(invoker.chromium_code)) {
767      _chromium_code = invoker.chromium_code
768    } else {
769      # Default based on whether target is in third_party.
770      _chromium_code =
771          filter_exclude([ get_label_info(":$target_name", "dir") ],
772                         [ "*\bthird_party\b*" ]) != []
773    }
774
775    write_build_config(_build_config_target_name) {
776      type = "android_resources"
777      build_config = _build_config
778      resources_zip = _resources_zip
779      res_sources_path = _res_sources_path
780      chromium_code = _chromium_code
781
782      forward_variables_from(invoker,
783                             [
784                               "android_manifest",
785                               "android_manifest_dep",
786                               "custom_package",
787                               "mergeable_android_manifests",
788                               "resource_overlay",
789                               "recursive_resource_deps",
790                             ])
791
792      r_text = _r_text_out_path
793      possible_config_deps = _deps + [ _android_sdk_dep ]
794
795      # Always merge manifests from resources.
796      # * Might want to change this at some point for consistency and clarity,
797      #   but keeping for backwards-compatibility.
798      if (!defined(mergeable_android_manifests) && defined(android_manifest)) {
799        mergeable_android_manifests = [ android_manifest ]
800      }
801    }
802
803    prepare_resources(target_name) {
804      forward_variables_from(invoker,
805                             [
806                               "allow_missing_resources",
807                               "public_deps",
808                               "strip_drawables",
809                               "visibility",
810                             ])
811      _lib_deps = filter_exclude(filter_include(_deps, java_library_patterns),
812                                 java_resource_patterns)
813      if (defined(public_deps)) {
814        # Since java library targets depend directly on sub-targets rather than
815        # top-level targets, public_deps are not properly propagated, at least
816        # in terms of the "did you depend on the target that generates your
817        # inputs" GN check.
818        assert(filter_include(public_deps, java_target_patterns) == [],
819               "Java targets should use deps, not public_deps. " +
820                   "target=${target_name}, public_deps=${public_deps}")
821      }
822
823      # Depend on non-library deps and on __assetres subtargets of library deps.
824      deps = filter_exclude(_deps, _lib_deps) + [ _android_sdk_dep ]
825      foreach(_lib_dep, _lib_deps) {
826        # Expand //foo/java -> //foo/java:java
827        _lib_dep = get_label_info(_lib_dep, "label_no_toolchain")
828        deps += [ "${_lib_dep}__assetres" ]
829      }
830
831      res_sources_path = _res_sources_path
832      sources = _resource_files
833
834      resources_zip = _resources_zip
835      r_text_out_path = _r_text_out_path
836
837      if (defined(invoker.r_text_file)) {
838        r_text_in_path = invoker.r_text_file
839      }
840    }
841  }
842
843  # Declare an Android assets target.
844  #
845  # Defines a set of files to include as assets in a dependent apk.
846  #
847  # To include these assets in an apk, this target should be listed in
848  # the apk's deps, or in the deps of a library target used by an apk.
849  #
850  # Variables
851  #   deps: Specifies the dependencies of this target. Any Android assets
852  #     listed in deps will be included by libraries/apks that depend on this
853  #     target.
854  #   sources: List of files to include as assets.
855  #   renaming_sources: List of files to include as assets and be renamed.
856  #   renaming_destinations: List of asset paths for files in renaming_sources.
857  #   disable_compression: Whether to disable compression for files that are
858  #     known to be compressable (default: false).
859  #   treat_as_locale_paks: Causes base's BuildConfig.java to consider these
860  #     assets to be locale paks.
861  #
862  # Example:
863  # android_assets("content_shell_assets") {
864  #   deps = [
865  #     ":generates_foo",
866  #     ":other_assets",
867  #     ]
868  #   sources = [
869  #     "//path/asset1.png",
870  #     "//path/asset2.png",
871  #     "$target_gen_dir/foo.dat",
872  #   ]
873  # }
874  #
875  # android_assets("overriding_content_shell_assets") {
876  #   deps = [ ":content_shell_assets" ]
877  #   # Override foo.dat from content_shell_assets.
878  #   sources = [ "//custom/foo.dat" ]
879  #   renaming_sources = [ "//path/asset2.png" ]
880  #   renaming_destinations = [ "renamed/asset2.png" ]
881  # }
882  template("android_assets") {
883    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
884
885    _build_config = "$target_gen_dir/$target_name.build_config.json"
886    _build_config_target_name = "$target_name$build_config_target_suffix"
887
888    _sources = []
889    if (defined(invoker.sources)) {
890      _sources = invoker.sources
891    }
892    _renaming_sources = []
893    if (defined(invoker.renaming_sources)) {
894      _renaming_sources = invoker.renaming_sources
895    }
896    write_build_config(_build_config_target_name) {
897      type = "android_assets"
898      build_config = _build_config
899
900      forward_variables_from(invoker,
901                             [
902                               "disable_compression",
903                               "treat_as_locale_paks",
904                             ])
905
906      if (defined(invoker.deps)) {
907        possible_config_deps = invoker.deps
908      }
909
910      if (_sources != []) {
911        asset_sources = _sources
912      }
913      if (_renaming_sources != []) {
914        assert(defined(invoker.renaming_destinations))
915        _source_count = 0
916        foreach(_, _renaming_sources) {
917          _source_count += 1
918        }
919        _dest_count = 0
920        foreach(_, invoker.renaming_destinations) {
921          _dest_count += 1
922        }
923        assert(
924            _source_count == _dest_count,
925            "android_assets() renaming_sources.length != renaming_destinations.length")
926        asset_renaming_sources = _renaming_sources
927        asset_renaming_destinations = invoker.renaming_destinations
928      }
929    }
930
931    # Use an action in order to mark sources as "inputs" to a GN target so that
932    # GN will fail if the appropriate deps do not exist, and so that "gn refs"
933    # will know about the sources. We do not add these inputs & deps to the
934    # __build_config target because we want building .build_config.json files
935    # to be fast (and because write_build_config.py does not need the files to
936    # exist).
937    _all_sources = _sources + _renaming_sources
938    if (_all_sources != []) {
939      action(target_name) {
940        forward_variables_from(invoker, [ "deps" ])
941        public_deps = [ ":$_build_config_target_name" ]
942
943        script = "//build/android/gyp/validate_inputs.py"
944        inputs = _all_sources
945        outputs = [ "$target_gen_dir/$target_name.stamp" ]
946        args = [
947                 "--stamp",
948                 rebase_path(outputs[0], root_build_dir),
949               ] + rebase_path(_all_sources, root_build_dir)
950      }
951    } else {
952      group(target_name) {
953        forward_variables_from(invoker, [ "deps" ])
954        public_deps = [ ":$_build_config_target_name" ]
955      }
956    }
957  }
958
959  # Declare a group() that supports forwarding java dependency information.
960  #
961  # Example
962  #  java_group("conditional_deps") {
963  #    if (enable_foo) {
964  #      deps = [":foo_java"]
965  #    }
966  #  }
967  template("java_group") {
968    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
969    _build_config_vars = [
970      "input_jars_paths",
971      "preferred_dep",
972      "mergeable_android_manifests",
973      "proguard_configs",
974      "requires_android",
975    ]
976    _invoker_deps = []
977    if (defined(invoker.deps)) {
978      _invoker_deps += invoker.deps
979    }
980    if (defined(invoker.public_deps)) {
981      _invoker_deps += invoker.public_deps
982    }
983    write_build_config("$target_name$build_config_target_suffix") {
984      forward_variables_from(invoker, _build_config_vars)
985      type = "group"
986      build_config = "$target_gen_dir/${invoker.target_name}.build_config.json"
987      supports_android = true
988      possible_config_deps = _invoker_deps
989    }
990
991    _assetres_deps = filter_include(_invoker_deps, java_resource_patterns)
992    _invoker_deps_minus_assetres = filter_exclude(_invoker_deps, _assetres_deps)
993    _lib_deps =
994        filter_include(_invoker_deps_minus_assetres, java_library_patterns)
995    _other_deps = _invoker_deps_minus_assetres - _lib_deps
996
997    _expanded_lib_deps = []
998    foreach(_lib_dep, _lib_deps) {
999      _expanded_lib_deps += [ get_label_info(_lib_dep, "label_no_toolchain") ]
1000    }
1001    foreach(_group_name,
1002            [
1003              "assetres",
1004              "header",
1005              "host",
1006              "validate",
1007            ]) {
1008      group("${target_name}__$_group_name") {
1009        deps = []
1010        foreach(_lib_dep, _expanded_lib_deps) {
1011          deps += [ "${_lib_dep}__${_group_name}" ]
1012        }
1013        if (_group_name == "assetres") {
1014          # _other_deps are necessary when generating mergeable_android_manifests.
1015          deps += _assetres_deps + _other_deps
1016        }
1017      }
1018    }
1019
1020    group(target_name) {
1021      forward_variables_from(invoker,
1022                             "*",
1023                             _build_config_vars + TESTONLY_AND_VISIBILITY)
1024      if (!defined(deps)) {
1025        deps = []
1026      }
1027      deps += [ ":$target_name$build_config_target_suffix" ]
1028      if (is_cronet_build) {
1029        _abs_deps = []
1030        if (defined(invoker.deps)) {
1031          foreach(dep, invoker.deps) {
1032            _abs_deps += [ get_label_info(dep, "label_no_toolchain") ]
1033          }
1034        }
1035        metadata = {
1036          all_deps = _abs_deps
1037          target_type = [ "java_library" ]
1038        }
1039      }
1040    }
1041  }
1042
1043  # Declare a Java executable target
1044  #
1045  # Same as java_library, but also creates a wrapper script within
1046  # $root_out_dir/bin.
1047  #
1048  # Supports all variables of java_library(), plus:
1049  #   main_class: When specified, a wrapper script is created within
1050  #     $root_build_dir/bin to launch the binary with the given class as the
1051  #     entrypoint.
1052  #   wrapper_script_name: Filename for the wrapper script (default=target_name)
1053  #   wrapper_script_args: List of additional arguments for the wrapper script.
1054  #
1055  # Example
1056  #   java_binary("foo") {
1057  #     sources = [ "org/chromium/foo/FooMain.java" ]
1058  #     deps = [ ":bar_java" ]
1059  #     main_class = "org.chromium.foo.FooMain"
1060  #   }
1061  #
1062  #   java_binary("foo") {
1063  #     jar_path = "lib/prebuilt.jar"
1064  #     deps = [ ":bar_java" ]
1065  #     main_class = "org.chromium.foo.FooMain"
1066  #   }
1067  template("java_binary") {
1068    java_library_impl(target_name) {
1069      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1070      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1071      type = "java_binary"
1072    }
1073  }
1074
1075  # Declare a Java Annotation Processor.
1076  #
1077  # Supports all variables of java_library(), plus:
1078  #   jar_path: Path to a prebuilt jar. Mutually exclusive with sources &
1079  #     srcjar_deps.
1080  #   main_class: The fully-quallified class name of the processor's entry
1081  #       point.
1082  #
1083  # Example
1084  #   java_annotation_processor("foo_processor") {
1085  #     sources = [ "org/chromium/foo/FooProcessor.java" ]
1086  #     deps = [ ":bar_java" ]
1087  #     main_class = "org.chromium.foo.FooProcessor"
1088  #   }
1089  #
1090  #   java_annotation_processor("foo_processor") {
1091  #     jar_path = "lib/prebuilt.jar"
1092  #     main_class = "org.chromium.foo.FooMain"
1093  #   }
1094  #
1095  #   java_library("...") {
1096  #     annotation_processor_deps = [":foo_processor"]
1097  #   }
1098  #
1099  template("java_annotation_processor") {
1100    java_library_impl(target_name) {
1101      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1102      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1103      type = "java_annotation_processor"
1104    }
1105  }
1106
1107  # Declare a Robolectric host side test binary.
1108  #
1109  # This target creates an executable from java code for running as a
1110  # Robolectric test suite. The executable will be in the output folder's /bin/
1111  # directory.
1112  #
1113  # Supports all variables of java_binary().
1114  #
1115  # Example
1116  #   robolectric_binary("foo") {
1117  #     sources = [ "org/chromium/foo/FooTest.java" ]
1118  #     deps = [ ":bar_java" ]
1119  #   }
1120  template("robolectric_binary") {
1121    testonly = true
1122
1123    _main_class = "org.chromium.testing.local.JunitTestMain"
1124    _build_config = "$target_gen_dir/$target_name.build_config.json"
1125    _build_config_target_name = "$target_name$build_config_target_suffix"
1126    _java_binary_target_name = "${target_name}__java_binary"
1127
1128    _invoker_deps = [
1129      "//testing/android/junit:junit_test_support",
1130      "//third_party/android_deps:robolectric_all_java",
1131      "//third_party/junit",
1132      "//third_party/mockito:mockito_jvm_java",
1133    ]
1134    if (defined(invoker.deps)) {
1135      _invoker_deps += invoker.deps
1136    }
1137    _non_java_deps = filter_exclude(_invoker_deps, java_target_patterns)
1138    _java_assetres_deps = [ ":${_java_binary_target_name}__assetres" ]
1139
1140    # A package name or a manifest is required to have resources. This is
1141    # added so that junit tests that do not care about the package name can
1142    # still use resources without having to explicitly set one.
1143    if (defined(invoker.package_name)) {
1144      _package_name = invoker.package_name
1145    } else if (!defined(invoker.android_manifest)) {
1146      _package_name = "no.manifest.configured"
1147    }
1148
1149    _merge_manifest_target_name = "${target_name}__merge_manifests"
1150    _android_manifest =
1151        "$target_gen_dir/$target_name.AndroidManifest.merged.xml"
1152
1153    merge_manifests(_merge_manifest_target_name) {
1154      if (defined(invoker.android_manifest)) {
1155        input_manifest = invoker.android_manifest
1156      } else {
1157        input_manifest = "//build/android/AndroidManifest.xml"
1158      }
1159
1160      if (defined(_package_name)) {
1161        manifest_package = _package_name
1162      }
1163      output_manifest = _android_manifest
1164      build_config = _build_config
1165      min_sdk_version = default_min_sdk_version
1166      target_sdk_version = android_sdk_version
1167      deps = _non_java_deps + _java_assetres_deps +
1168             [ ":$_build_config_target_name" ]
1169      if (defined(invoker.android_manifest_dep)) {
1170        deps += [ invoker.android_manifest_dep ]
1171      }
1172    }
1173
1174    _resource_arsc_output = "${target_out_dir}/${target_name}.ap_"
1175    _compile_resources_target_name = "${target_name}__compile_resources"
1176    compile_resources(_compile_resources_target_name) {
1177      deps = _non_java_deps + _java_assetres_deps +
1178             [ ":$_merge_manifest_target_name" ]
1179      build_config_dep = ":$_build_config_target_name"
1180      build_config = _build_config
1181      if (defined(_package_name)) {
1182        rename_manifest_package = _package_name
1183      }
1184      android_manifest = _android_manifest
1185      arsc_output = _resource_arsc_output
1186      min_sdk_version = default_min_sdk_version
1187      target_sdk_version = android_sdk_version
1188    }
1189
1190    # apkbuilder step needed only to add android assets to the .ap_ file.
1191    _apkbuilder_output = "${target_out_dir}/${target_name}.robo.ap_"
1192    _apkbuilder_target_name = "${target_name}__apkbuilder"
1193    package_apk("$_apkbuilder_target_name") {
1194      build_config = _build_config
1195      min_sdk_version = default_min_sdk_version
1196      deps = _java_assetres_deps + [
1197               ":$_build_config_target_name",
1198               ":$_compile_resources_target_name",
1199             ]
1200
1201      is_robolectric_apk = true
1202      packaged_resources_path = _resource_arsc_output
1203      output_apk_path = _apkbuilder_output
1204    }
1205
1206    # Some may want to disable this to remove dependency on //base
1207    # (JNI generator is in //base).
1208    _generate_final_jni =
1209        !defined(invoker.generate_final_jni) || invoker.generate_final_jni
1210    if (_generate_final_jni) {
1211      _jni_srcjar_deps = []
1212      if (defined(invoker.shared_libraries)) {
1213        foreach(_dep, invoker.shared_libraries) {
1214          _dep_no_toolchain = get_label_info(_dep, "label_no_toolchain")
1215          _dep_toolchain = get_label_info(_dep, "toolchain")
1216          assert(
1217              _dep_toolchain == robolectric_toolchain,
1218              "$target_name has shared_libraries with incorrect toolchain. " +
1219                  "Should contain (\$robolectric_toolchain) suffix: $_dep")
1220          _jni_srcjar_deps +=
1221              [ "${_dep_no_toolchain}__jni_registration($default_toolchain)" ]
1222        }
1223
1224        # Write shared library output files of all dependencies to a file. Those
1225        # will be the shared libraries packaged into the APK.
1226        _shared_library_list_file = "$target_gen_dir/$target_name.native_libs"
1227        generated_file("${target_name}__shared_library_list") {
1228          deps = invoker.shared_libraries
1229          outputs = [ _shared_library_list_file ]
1230          data_keys = [ "shared_libraries" ]
1231          walk_keys = [ "shared_libraries_barrier" ]
1232          rebase = root_build_dir
1233        }
1234      } else {
1235        # Needed for generate_jni_registration. Keeping this import guarded so
1236        # that projects who import //build but not //third_party/jni_zero don't
1237        # have issues.
1238        import("//third_party/jni_zero/jni_zero.gni")
1239        _jni_srcjar_target_name = "${target_name}__final_jni"
1240        generate_jni_registration(_jni_srcjar_target_name) {
1241          enable_native_mocks = true
1242          require_native_mocks = true
1243          java_targets = [ ":$_java_binary_target_name" ]
1244          add_stubs_for_missing_jni = true
1245        }
1246        _jni_srcjar_deps = [ ":$_jni_srcjar_target_name" ]
1247      }
1248      _native_libraries_target_name = "${target_name}__native_libraries"
1249      write_native_libraries_java(_native_libraries_target_name) {
1250        enable_chromium_linker = false
1251        use_final_fields = true
1252        if (defined(_shared_library_list_file)) {
1253          native_libraries_list_file = _shared_library_list_file
1254        }
1255      }
1256    }
1257
1258    java_library_impl(_java_binary_target_name) {
1259      forward_variables_from(invoker,
1260                             "*",
1261                             TESTONLY_AND_VISIBILITY + [
1262                                   "deps",
1263                                   "extra_args",
1264                                   "shared_libraries",
1265                                 ])
1266      type = "robolectric_binary"
1267      main_target_name = invoker.target_name
1268
1269      deps = _invoker_deps
1270      testonly = true
1271      main_class = _main_class
1272      wrapper_script_name = "helper/$main_target_name"
1273
1274      # As of April 2021, adding -XX:TieredStopAtLevel=1 does not affect the
1275      # wall time of a single robolectric shard, but does reduce the CPU time by
1276      # 66%, which makes sharding more effective.
1277      tiered_stop_at_level_one = true
1278
1279      is_robolectric = true
1280      include_android_sdk = true
1281      alternative_android_sdk_dep =
1282          "//third_party/robolectric:robolectric_test_sdk_java"
1283
1284      if (!defined(srcjar_deps)) {
1285        srcjar_deps = []
1286      }
1287      srcjar_deps += [
1288        ":$_compile_resources_target_name",
1289        "//build/android:build_config_for_testing_gen",
1290      ]
1291      if (_generate_final_jni) {
1292        srcjar_deps += [ ":$_native_libraries_target_name" ] + _jni_srcjar_deps
1293      }
1294    }
1295
1296    test_runner_script(target_name) {
1297      forward_variables_from(invoker,
1298                             [
1299                               "assert_no_deps",
1300                               "extra_args",
1301                               "visibility",
1302                             ])
1303      test_name = invoker.target_name
1304      test_suite = invoker.target_name
1305      test_type = "junit"
1306      ignore_all_data_deps = true
1307      resource_apk = _apkbuilder_output
1308      deps = [
1309        ":$_apkbuilder_target_name",
1310        ":$_build_config_target_name",
1311        ":${_java_binary_target_name}__host",
1312        ":${_java_binary_target_name}__java_binary_script",
1313        ":${_java_binary_target_name}__validate",
1314        "//third_party/robolectric:robolectric_runtime_jars",
1315      ]
1316      if (defined(invoker.shared_libraries)) {
1317        data_deps = invoker.shared_libraries
1318      }
1319
1320      # Add non-libary deps, since the __host target does not depend on them.
1321      deps += filter_exclude(_invoker_deps, java_library_patterns)
1322
1323      metadata = {
1324        # Allows metadata collection via apk targets that traverse only java deps.
1325        java_walk_keys = [ ":${_java_binary_target_name}__host" ]
1326      }
1327    }
1328  }
1329
1330  # Declare a java library target
1331  #
1332  # Variables
1333  #   deps: Specifies the dependencies of this target. Java targets in this list
1334  #     will be added to the javac classpath.
1335  #   public_deps: Dependencies that this target exposes as part of its public API.
1336  #     public_deps do not need to be listed in both the 'deps' and 'public_deps' lists.
1337  #   annotation_processor_deps: List of java_annotation_processor targets to
1338  #     use when compiling.
1339  #
1340  #   jar_path: Path to a prebuilt jar. Mutually exclusive with sources &
1341  #     srcjar_deps.
1342  #   sources: List of .java files included in this library.
1343  #   srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
1344  #     will be added to sources and be included in this library.
1345  #
1346  #   input_jars_paths: A list of paths to the jars that should be included
1347  #     in the compile-time classpath. These are in addition to library .jars
1348  #     that appear in deps.
1349  #
1350  #   chromium_code: If true, extra analysis warning/errors will be enabled.
1351  #   enable_errorprone: If true, enables the errorprone compiler.
1352  #   skip_build_server: If true, avoids sending tasks to the build server.
1353  #
1354  #   jar_excluded_patterns: List of patterns of .class files to exclude.
1355  #   jar_included_patterns: List of patterns of .class files to include.
1356  #     When omitted, all classes not matched by jar_excluded_patterns are
1357  #     included. When specified, all non-matching .class files are stripped.
1358  #
1359  #   low_classpath_priority: Indicates that the library should be placed at the
1360  #     end of the classpath. The default classpath order has libraries ordered
1361  #     before the libraries that they depend on. 'low_classpath_priority' is
1362  #     useful when one java_library() overrides another via
1363  #     'jar_excluded_patterns' and the overriding library does not depend on
1364  #     the overridee.
1365  #
1366  #   output_name: File name for the output .jar (not including extension).
1367  #     Defaults to the input .jar file name.
1368  #
1369  #   proguard_configs: List of proguard configs to use in final apk step for
1370  #     any apk that depends on this library.
1371  #
1372  #   supports_android: If true, Android targets (android_library, android_apk)
1373  #     may depend on this target. Note: if true, this target must only use the
1374  #     subset of Java available on Android.
1375  #   bypass_platform_checks: Disables checks about cross-platform (Java/Android)
1376  #     dependencies for this target. This will allow depending on an
1377  #     android_library target, for example.
1378  #   enable_desugar: If false, disables desugaring of lambdas, etc. Use this
1379  #     only when you are sure the library does not require desugaring. E.g.
1380  #     to hide warnings shown from desugaring.
1381  #
1382  #   additional_jar_files: Use to package additional files (Java resources)
1383  #     into the output jar. Pass a list of length-2 lists with format:
1384  #         [ [ path_to_file, path_to_put_in_jar ] ]
1385  #
1386  #   javac_args: Additional arguments to pass to javac.
1387  #   errorprone_args: Additional arguments to pass to errorprone.
1388  #
1389  #   data_deps, testonly
1390  #
1391  # Example
1392  #   java_library("foo_java") {
1393  #     sources = [
1394  #       "org/chromium/foo/Foo.java",
1395  #       "org/chromium/foo/FooInterface.java",
1396  #       "org/chromium/foo/FooService.java",
1397  #     ]
1398  #     deps = [
1399  #       ":bar_java"
1400  #     ]
1401  #     srcjar_deps = [
1402  #       ":foo_generated_enum"
1403  #     ]
1404  #     jar_excluded_patterns = [
1405  #       "*/FooService.class", "org/chromium/FooService\$*.class"
1406  #     ]
1407  #   }
1408  template("java_library") {
1409    java_library_impl(target_name) {
1410      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1411      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1412      type = "java_library"
1413    }
1414  }
1415
1416  # Declare a java library target for a prebuilt jar
1417  #
1418  # Supports all variables of java_library().
1419  #
1420  # Example
1421  #   java_prebuilt("foo_java") {
1422  #     jar_path = "foo.jar"
1423  #     deps = [
1424  #       ":foo_resources",
1425  #       ":bar_java"
1426  #     ]
1427  #   }
1428  template("java_prebuilt") {
1429    java_library_impl(target_name) {
1430      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1431      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1432      type = "java_library"
1433    }
1434  }
1435
1436  # Combines all dependent .jar files into a single .jar file.
1437  #
1438  # Variables:
1439  #   output: Path to the output jar.
1440  #   use_interface_jars: Use all dependent interface .jars rather than
1441  #     implementation .jars.
1442  #   use_unprocessed_jars: Use unprocessed / undesugared .jars.
1443  #   direct_deps_only: Do not recurse on deps.
1444  #   jar_excluded_patterns (optional)
1445  #     List of globs for paths to exclude.
1446  #
1447  # Example
1448  #   dist_jar("lib_fatjar") {
1449  #     deps = [ ":my_java_lib" ]
1450  #     output = "$root_build_dir/MyLibrary.jar"
1451  #   }
1452  template("dist_jar") {
1453    # TODO(crbug.com/1042017): Remove.
1454    not_needed(invoker, [ "no_build_hooks" ])
1455    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1456    _use_interface_jars =
1457        defined(invoker.use_interface_jars) && invoker.use_interface_jars
1458    _use_unprocessed_jars =
1459        defined(invoker.use_unprocessed_jars) && invoker.use_unprocessed_jars
1460    _direct_deps_only =
1461        defined(invoker.direct_deps_only) && invoker.direct_deps_only
1462    assert(!(_use_unprocessed_jars && _use_interface_jars),
1463           "Cannot set both use_interface_jars and use_unprocessed_jars")
1464
1465    _jar_target_name = target_name
1466
1467    if (defined(invoker.build_config)) {
1468      _build_config = invoker.build_config
1469      _build_config_dep = invoker.build_config_dep
1470    } else {
1471      _build_config = "$target_gen_dir/$target_name.build_config.json"
1472      _build_config_target_name = "$target_name$build_config_target_suffix"
1473      _build_config_dep = ":$_build_config_target_name"
1474
1475      write_build_config(_build_config_target_name) {
1476        type = "dist_jar"
1477        supports_android =
1478            !defined(invoker.supports_android) || invoker.supports_android
1479        requires_android =
1480            defined(invoker.requires_android) && invoker.requires_android
1481        possible_config_deps = invoker.deps
1482        build_config = _build_config
1483      }
1484    }
1485
1486    _rebased_build_config = rebase_path(_build_config, root_build_dir)
1487    action_with_pydeps(_jar_target_name) {
1488      forward_variables_from(invoker, [ "data" ])
1489      script = "//build/android/gyp/zip.py"
1490      depfile = "$target_gen_dir/$target_name.d"
1491      deps = [ _build_config_dep ]
1492
1493      if (_use_interface_jars) {
1494        _lib_deps =
1495            filter_exclude(filter_include(invoker.deps, java_library_patterns),
1496                           java_resource_patterns)
1497        _other_deps = filter_exclude(invoker.deps, _lib_deps)
1498        foreach(_lib_dep, _lib_deps) {
1499          # Expand //foo/java -> //foo/java:java
1500          _lib_dep = get_label_info(_lib_dep, "label_no_toolchain")
1501          deps += [ "${_lib_dep}__header" ]
1502        }
1503        deps += _other_deps
1504      } else {
1505        deps += invoker.deps
1506      }
1507
1508      inputs = [ _build_config ]
1509
1510      outputs = [ invoker.output ]
1511
1512      args = [
1513        "--depfile",
1514        rebase_path(depfile, root_build_dir),
1515        "--output",
1516        rebase_path(invoker.output, root_build_dir),
1517        "--no-compress",
1518      ]
1519
1520      if (_direct_deps_only) {
1521        if (_use_interface_jars) {
1522          args += [ "--input-zips=@FileArg($_rebased_build_config:javac:interface_classpath)" ]
1523        } else if (_use_unprocessed_jars) {
1524          args += [
1525            "--input-zips=@FileArg($_rebased_build_config:javac:classpath)",
1526          ]
1527        } else {
1528          assert(
1529              false,
1530              "direct_deps_only does not work without use_interface_jars or use_unprocessed_jars")
1531        }
1532      } else {
1533        if (_use_interface_jars) {
1534          args += [ "--input-zips=@FileArg($_rebased_build_config:dist_jar:all_interface_jars)" ]
1535        } else if (_use_unprocessed_jars) {
1536          args += [ "--input-zips=@FileArg($_rebased_build_config:deps_info:javac_full_classpath)" ]
1537        } else {
1538          args += [ "--input-zips=@FileArg($_rebased_build_config:deps_info:device_classpath)" ]
1539        }
1540      }
1541
1542      _excludes = []
1543      if (defined(invoker.jar_excluded_patterns)) {
1544        _excludes += invoker.jar_excluded_patterns
1545      }
1546      if (_use_interface_jars) {
1547        # Turbine adds files like: META-INF/TRANSITIVE/.../Foo.class
1548        # These confuse proguard: https://crbug.com/1081443
1549        _excludes += [ "META-INF/*" ]
1550      } else {
1551        # Manifest files will never be correct when merging jars.
1552        _excludes += [ "META-INF/*.MF" ]
1553      }
1554      if (_excludes != []) {
1555        args += [ "--input-zips-excluded-globs=$_excludes" ]
1556      }
1557    }
1558  }
1559
1560  # Combines all dependent .jar files into a single proguarded .dex file.
1561  #
1562  # Variables:
1563  #   output: Path to the output .dex or .dex.jar.
1564  #   proguard_enabled: Whether to enable R8.
1565  #   proguard_configs: List of proguard configs.
1566  #   proguard_enable_obfuscation: Whether to enable obfuscation (default=true).
1567  #   package_name: Used in the Proguard map ID.
1568  #   version_code: Used in the Proguard map ID.
1569  #
1570  # Example
1571  #   dist_dex("lib_fatjar") {
1572  #     deps = [ ":my_java_lib" ]
1573  #     output = "$root_build_dir/MyLibrary.jar"
1574  #   }
1575  template("dist_dex") {
1576    _deps = [ default_android_sdk_dep ]
1577    if (defined(invoker.deps)) {
1578      _deps += invoker.deps
1579    }
1580
1581    _build_config = "$target_gen_dir/$target_name.build_config.json"
1582    _build_config_target_name = "$target_name$build_config_target_suffix"
1583
1584    write_build_config(_build_config_target_name) {
1585      type = "dist_jar"
1586      forward_variables_from(invoker,
1587                             [
1588                               "proguard_configs",
1589                               "proguard_enabled",
1590                             ])
1591      supports_android = true
1592      requires_android = true
1593      possible_config_deps = _deps
1594      build_config = _build_config
1595    }
1596
1597    dex(target_name) {
1598      forward_variables_from(invoker,
1599                             TESTONLY_AND_VISIBILITY + [
1600                                   "data",
1601                                   "data_deps",
1602                                   "package_name",
1603                                   "proguard_configs",
1604                                   "proguard_enabled",
1605                                   "proguard_enable_obfuscation",
1606                                   "min_sdk_version",
1607                                   "repackage_classes",
1608                                   "version_code",
1609                                 ])
1610      deps = [ ":$_build_config_target_name" ] + _deps
1611      build_config = _build_config
1612      output = invoker.output
1613      if (defined(proguard_enabled) && proguard_enabled) {
1614        # The individual dependencies would have caught real missing deps in
1615        # their respective dex steps. False positives that were suppressed at
1616        # per-target dex steps are emitted here since this is using jar files
1617        # rather than dex files.
1618        ignore_desugar_missing_deps = true
1619      } else {
1620        _rebased_build_config = rebase_path(_build_config, root_build_dir)
1621        input_dex_filearg =
1622            "@FileArg(${_rebased_build_config}:deps_info:all_dex_files)"
1623      }
1624    }
1625  }
1626
1627  # Creates an Android .aar library.
1628  #
1629  # Currently supports:
1630  #   * AndroidManifest.xml
1631  #   * classes.jar
1632  #   * jni/
1633  #   * res/
1634  #   * R.txt
1635  #   * proguard.txt
1636  # Does not yet support:
1637  #   * public.txt
1638  #   * annotations.zip
1639  #   * assets/
1640  # See: https://developer.android.com/studio/projects/android-library.html#aar-contents
1641  #
1642  # Variables:
1643  #   output: Path to the output .aar.
1644  #   proguard_configs: List of proguard configs (optional).
1645  #   android_manifest: Path to AndroidManifest.xml (optional).
1646  #   native_libraries: list of native libraries (optional).
1647  #   direct_deps_only: Do not recurse on deps (optional, defaults false).
1648  #   jar_excluded_patterns: List of globs for paths to exclude (optional).
1649  #   jar_included_patterns: List of globs for paths to include (optional).
1650  #
1651  # Example
1652  #   dist_aar("my_aar") {
1653  #     deps = [ ":my_java_lib" ]
1654  #     output = "$root_build_dir/MyLibrary.aar"
1655  #   }
1656  template("dist_aar") {
1657    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1658
1659    _direct_deps_only =
1660        defined(invoker.direct_deps_only) && invoker.direct_deps_only
1661
1662    _deps = []
1663
1664    if (defined(invoker.deps)) {
1665      _deps += invoker.deps
1666    }
1667
1668    _build_config = "$target_gen_dir/$target_name.build_config.json"
1669    _build_config_target_name = "$target_name$build_config_target_suffix"
1670
1671    write_build_config(_build_config_target_name) {
1672      type = "dist_aar"
1673      forward_variables_from(invoker, [ "proguard_configs" ])
1674      possible_config_deps = _deps
1675      supports_android = true
1676      requires_android = true
1677      build_config = _build_config
1678    }
1679
1680    _deps += [ ":$_build_config_target_name" ]
1681
1682    _rebased_build_config = rebase_path(_build_config, root_build_dir)
1683
1684    action_with_pydeps(target_name) {
1685      forward_variables_from(invoker,
1686                             [
1687                               "data",
1688                               "assert_no_deps",
1689                             ])
1690      depfile = "$target_gen_dir/$target_name.d"
1691      deps = _deps
1692      script = "//build/android/gyp/dist_aar.py"
1693
1694      inputs = [ _build_config ]
1695
1696      # Although these will be listed as deps in the depfile, they must also
1697      # appear here so that "gn analyze" knows about them.
1698      # https://crbug.com/827197
1699      if (defined(invoker.proguard_configs)) {
1700        inputs += invoker.proguard_configs
1701      }
1702
1703      outputs = [ invoker.output ]
1704
1705      args = [
1706        "--depfile",
1707        rebase_path(depfile, root_build_dir),
1708        "--output",
1709        rebase_path(invoker.output, root_build_dir),
1710        "--dependencies-res-zips=@FileArg($_rebased_build_config:deps_info:dependency_zips)",
1711        "--r-text-files=@FileArg($_rebased_build_config:deps_info:dependency_r_txt_files)",
1712        "--proguard-configs=@FileArg($_rebased_build_config:deps_info:proguard_all_configs)",
1713      ]
1714      if (_direct_deps_only) {
1715        args += [ "--jars=@FileArg($_rebased_build_config:javac:classpath)" ]
1716      } else {
1717        args += [
1718          "--jars=@FileArg($_rebased_build_config:deps_info:device_classpath)",
1719        ]
1720      }
1721
1722      if (defined(invoker.android_manifest)) {
1723        args += [
1724          "--android-manifest",
1725          rebase_path(invoker.android_manifest, root_build_dir),
1726        ]
1727      }
1728      if (defined(invoker.native_libraries) && invoker.native_libraries != []) {
1729        inputs += invoker.native_libraries
1730        _rebased_native_libraries =
1731            rebase_path(invoker.native_libraries, root_build_dir)
1732
1733        args += [
1734          "--native-libraries=$_rebased_native_libraries",
1735          "--abi=$android_app_abi",
1736        ]
1737      }
1738      if (defined(invoker.jar_excluded_patterns)) {
1739        args += [ "--jar-excluded-globs=${invoker.jar_excluded_patterns}" ]
1740      }
1741      if (defined(invoker.jar_included_patterns)) {
1742        args += [ "--jar-included-globs=${invoker.jar_included_patterns}" ]
1743      }
1744      if (defined(invoker.resource_included_patterns)) {
1745        args += [
1746          "--resource-included-globs=${invoker.resource_included_patterns}",
1747        ]
1748      }
1749    }
1750  }
1751
1752  # Declare an Android library target
1753  #
1754  # This target creates an Android library containing java code and Android
1755  # resources.
1756  #
1757  # Supports all variables of java_library(), plus:
1758  #   deps: In addition to defining java deps, this can also include
1759  #     android_assets() and android_resources() targets.
1760  #   alternative_android_sdk_dep: android_system_java_prebuilt target to use
1761  #     in place of the default android.jar.
1762  #
1763  # Example
1764  #   android_library("foo_java") {
1765  #     sources = [
1766  #       "android/org/chromium/foo/Foo.java",
1767  #       "android/org/chromium/foo/FooInterface.java",
1768  #       "android/org/chromium/foo/FooService.java",
1769  #     ]
1770  #     deps = [
1771  #       ":bar_java"
1772  #     ]
1773  #     srcjar_deps = [
1774  #       ":foo_generated_enum"
1775  #     ]
1776  #     jar_excluded_patterns = [
1777  #       "*/FooService.class", "org/chromium/FooService\$*.class"
1778  #     ]
1779  #   }
1780  template("android_library") {
1781    java_library(target_name) {
1782      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1783      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1784
1785      supports_android = true
1786      requires_android = true
1787
1788      if (!defined(jar_excluded_patterns)) {
1789        jar_excluded_patterns = []
1790      }
1791      jar_excluded_patterns += [
1792        "*/R.class",
1793        "*/R\$*.class",
1794        "*/Manifest.class",
1795        "*/Manifest\$*.class",
1796        "*/*GEN_JNI.class",
1797      ]
1798    }
1799  }
1800
1801  # Declare an Android robolectric library target
1802  #
1803  # This target creates an Android library containing java code and Android
1804  # resources.
1805  #
1806  # Supports all variables of java_library(), plus:
1807  #   deps: In addition to defining java deps, this can also include
1808  #     android_assets() and android_resources() targets.
1809  #
1810  # Example
1811  #   robolectric_library("foo_junit") {
1812  #     sources = [
1813  #       "android/org/chromium/foo/FooTest.java",
1814  #       "android/org/chromium/foo/FooTestUtils.java",
1815  #       "android/org/chromium/foo/FooMock.java",
1816  #     ]
1817  #     deps = [
1818  #       "//base:base_junit_test_support"
1819  #     ]
1820  #     srcjar_deps = [
1821  #       ":foo_generated_enum"
1822  #     ]
1823  #     jar_excluded_patterns = [
1824  #       "*/FooService.class", "org/chromium/FooService\$*.class"
1825  #     ]
1826  #   }
1827  template("robolectric_library") {
1828    java_library(target_name) {
1829      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1830      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1831
1832      testonly = true
1833
1834      is_robolectric = true
1835      include_android_sdk = true
1836      alternative_android_sdk_dep =
1837          "//third_party/robolectric:robolectric_test_sdk_java"
1838
1839      if (!defined(jar_excluded_patterns)) {
1840        jar_excluded_patterns = []
1841      }
1842      jar_excluded_patterns += [
1843        "*/R.class",
1844        "*/R\$*.class",
1845        "*/Manifest.class",
1846        "*/Manifest\$*.class",
1847        "*/*GEN_JNI.class",
1848      ]
1849
1850      if (!defined(deps)) {
1851        deps = []
1852      }
1853      deps += [ "//third_party/android_deps:robolectric_all_java" ]
1854    }
1855  }
1856
1857  # Declare an Android library target for a prebuilt jar
1858  #
1859  # This target creates an Android library containing java code and Android
1860  # resources.
1861  #
1862  # Supports all variables of android_library().
1863  #
1864  # Example
1865  #   android_java_prebuilt("foo_java") {
1866  #     jar_path = "foo.jar"
1867  #     deps = [
1868  #       ":foo_resources",
1869  #       ":bar_java"
1870  #     ]
1871  #   }
1872  template("android_java_prebuilt") {
1873    android_library(target_name) {
1874      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1875      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1876    }
1877  }
1878
1879  template("android_system_java_prebuilt") {
1880    java_library_impl(target_name) {
1881      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
1882      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1883      supports_android = true
1884      type = "system_java_library"
1885    }
1886  }
1887
1888  # Creates org/chromium/build/BuildConfig.java
1889  # This doesn't really belong in //build since it genates a file for //base.
1890  # However, we don't currently have a better way to include this file in all
1891  # apks that depend on //base:base_java.
1892  #
1893  # Variables:
1894  #   use_final_fields: True to use final fields. When false, all other
1895  #       variables must not be set.
1896  #   min_sdk_version: Value for MIN_SDK_VERSION.
1897  #   bundles_supported: Whether or not this target can be treated as a bundle.
1898  #   resources_version_variable:
1899  #   is_incremental_install:
1900  #   isolated_splits_enabled: Value for ISOLATED_SPLITS_ENABLED.
1901  template("generate_build_config_srcjar") {
1902    java_cpp_template(target_name) {
1903      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
1904      sources = [ "//build/android/java/templates/BuildConfig.template" ]
1905      defines = []
1906
1907      # Set these even when !use_final_fields so that they have correct default
1908      # values within robolectric_binary(), which ignores jar_excluded_patterns.
1909      if ((defined(invoker.assertions_implicitly_enabled) &&
1910           invoker.assertions_implicitly_enabled) || enable_java_asserts) {
1911        defines += [ "_ENABLE_ASSERTS" ]
1912      }
1913      if (use_cfi_diag || is_ubsan || is_ubsan_security || is_ubsan_vptr) {
1914        defines += [ "_IS_UBSAN" ]
1915      }
1916
1917      if (is_chrome_branded) {
1918        defines += [ "_IS_CHROME_BRANDED" ]
1919      }
1920
1921      if (defined(invoker.bundles_supported) && invoker.bundles_supported) {
1922        defines += [ "_BUNDLES_SUPPORTED" ]
1923      }
1924
1925      if (defined(invoker.isolated_splits_enabled) &&
1926          invoker.isolated_splits_enabled) {
1927        defines += [ "_ISOLATED_SPLITS_ENABLED" ]
1928      }
1929
1930      if (defined(invoker.is_incremental_install) &&
1931          invoker.is_incremental_install) {
1932        defines += [ "_IS_INCREMENTAL_INSTALL" ]
1933      }
1934
1935      if (invoker.use_final_fields) {
1936        forward_variables_from(invoker, [ "deps" ])
1937        defines += [ "USE_FINAL" ]
1938        if (defined(invoker.min_sdk_version)) {
1939          defines += [ "_MIN_SDK_VERSION=${invoker.min_sdk_version}" ]
1940        }
1941        if (defined(invoker.resources_version_variable)) {
1942          defines += [
1943            "_RESOURCES_VERSION_VARIABLE=${invoker.resources_version_variable}",
1944          ]
1945        }
1946      }
1947
1948      if (defined(testonly) && testonly) {
1949        defines += [ "_IS_FOR_TEST" ]
1950      }
1951
1952      if (defined(invoker.write_clang_profiling_data) &&
1953          invoker.write_clang_profiling_data) {
1954        defines += [ "_WRITE_CLANG_PROFILING_DATA" ]
1955      }
1956    }
1957  }
1958
1959  # Creates ProductConfig.java, a file containing product-specific configuration.
1960  #
1961  # Currently, this includes the list of locales, both in their compressed and
1962  # uncompressed format, as well as library loading
1963  #
1964  # Variables:
1965  #   build_config: Path to build_config used for locale lists.
1966  #   is_bundle_module: Whether or not this target is part of a bundle build.
1967  #   java_package: Java package for the generated class.
1968  #   use_chromium_linker:
1969  template("generate_product_config_srcjar") {
1970    java_cpp_template(target_name) {
1971      defines = []
1972      _use_final =
1973          defined(invoker.build_config) ||
1974          defined(invoker.use_chromium_linker) || defined(invoker.is_bundle)
1975      if (_use_final) {
1976        defines += [ "USE_FINAL" ]
1977      }
1978
1979      sources = [ "//build/android/java/templates/ProductConfig.template" ]
1980      defines += [ "PACKAGE=${invoker.java_package}" ]
1981
1982      _use_chromium_linker =
1983          defined(invoker.use_chromium_linker) && invoker.use_chromium_linker
1984      _is_bundle = defined(invoker.is_bundle_module) && invoker.is_bundle_module
1985      defines += [
1986        "USE_CHROMIUM_LINKER_VALUE=$_use_chromium_linker",
1987        "IS_BUNDLE_VALUE=$_is_bundle",
1988      ]
1989      if (defined(invoker.build_config)) {
1990        forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
1991        _rebased_build_config =
1992            rebase_path(invoker.build_config, root_build_dir)
1993        defines += [ "LOCALE_LIST=@FileArg($_rebased_build_config:deps_info:locales_java_list)" ]
1994      }
1995    }
1996  }
1997
1998  # Declare an Android app module target, which is used as the basis for an
1999  # Android APK or an Android app bundle module.
2000  #
2001  # Supports all variables of android_library(), plus:
2002  #   android_manifest: Path to AndroidManifest.xml. NOTE: This manifest must
2003  #     not contain a <uses-sdk> element. Use [min|target|max]_sdk_version
2004  #     instead.
2005  #   android_manifest_dep: Target that generates AndroidManifest (if applicable)
2006  #   png_to_webp: If true, pngs (with the exception of 9-patch) are
2007  #     converted to webp during resource packaging.
2008  #   loadable_modules: List of paths to native libraries to include. Different
2009  #     from |shared_libraries| in that:
2010  #       * dependencies of this .so are not automatically included
2011  #       * ".cr.so" is never added
2012  #       * they are not side-loaded when incremental_install=true.
2013  #       * use_chromium_linker, and enable_relocation_packing do not apply
2014  #     Use this instead of shared_libraries when you are going to load the library
2015  #     conditionally, and only when shared_libraries doesn't work for you.
2016  #   secondary_abi_loadable_modules: This is the loadable_modules analog to
2017  #     secondary_abi_shared_libraries.
2018  #   shared_libraries: List shared_library targets to bundle. If these
2019  #     libraries depend on other shared_library targets, those dependencies will
2020  #     also be included in the apk (e.g. for is_component_build).
2021  #   secondary_abi_shared_libraries: secondary abi shared_library targets to
2022  #     bundle. If these libraries depend on other shared_library targets, those
2023  #     dependencies will also be included in the apk (e.g. for is_component_build).
2024  #   native_lib_placeholders: List of placeholder filenames to add to the apk
2025  #     (optional).
2026  #   secondary_native_lib_placeholders: List of placeholder filenames to add to
2027  #     the apk for the secondary ABI (optional).
2028  #   generate_buildconfig_java: If defined and false, skip generating the
2029  #     BuildConfig java class describing the build configuration. The default
2030  #     is true when building with Chromium for non-test APKs.
2031  #   generate_native_libraries_java: If defined, whether NativeLibraries.java is
2032  #     generated is solely controlled by this flag. Otherwise, the default behavior
2033  #     is NativeLibraries.java will only be generated for the base module/apk when
2034  #     its `shared_libraries` is not empty.
2035  #   aapt_locale_allowlist: If set, all locales not in this list will be
2036  #     stripped from resources.arsc.
2037  #   resource_exclusion_regex: Causes all drawable images matching the regex to
2038  #     be excluded (mipmaps are still included).
2039  #   resource_exclusion_exceptions: A list of globs used when
2040  #     resource_exclusion_regex is set. Files that match this list will
2041  #     still be included.
2042  #   resource_values_filter_rules: List of "source_path:name_regex" used to
2043  #     filter out unwanted values/ resources.
2044  #   shared_resources: True if this is a runtime shared library APK, like
2045  #     the system_webview_apk target. Ensures that its resources can be
2046  #     used by the loading application process.
2047  #   app_as_shared_lib: True if this is a regular application apk that can
2048  #     also serve as a runtime shared library, like the monochrome_public_apk
2049  #     target. Ensures that the resources are usable both by the APK running
2050  #     as an application, or by another process that loads it at runtime.
2051  #   shared_resources_allowlist_target: Optional name of a target specifying
2052  #     an input R.txt file that lists the resources that can be exported
2053  #     by the APK when shared_resources or app_as_shared_lib is defined.
2054  #   uncompress_dex: Store final .dex files uncompressed in the apk.
2055  #   omit_dex: If true, do not build or include classes.dex.
2056  #   strip_resource_names: True if resource names should be stripped from the
2057  #     resources.arsc file in the apk or module.
2058  #   strip_unused_resources: True if unused resources should be stripped from
2059  #     the apk or module.
2060  #   short_resource_paths: True if resource paths should be shortened in the
2061  #     apk or module.
2062  #   resources_config_paths: List of paths to the aapt2 optimize config files
2063  #     that tags resources with acceptable/non-acceptable optimizations.
2064  #   expected_android_manifest: Enables verification of expected merged
2065  #     manifest based on a golden file.
2066  #   resource_ids_provider_dep: If passed, this target will use the resource
2067  #     IDs generated by {resource_ids_provider_dep}__compile_res during
2068  #     resource compilation.
2069  #   enforce_resource_overlays_in_tests: Enables check for testonly targets that
2070  #     dependent resource targets which override another target set
2071  #     overlay_resources=true. This check is on for non-test targets and
2072  #     cannot be disabled.
2073  #   static_library_provider: Specifies a single target that this target will
2074  #     use as a static library APK.
2075  #   min_sdk_version: The minimum Android SDK version this target supports.
2076  #     Optional, default $default_min_sdk_version.
2077  #   target_sdk_version: The target Android SDK version for this target.
2078  #     Optional, default to android_sdk_version.
2079  #   max_sdk_version: The maximum Android SDK version this target supports.
2080  #     Optional, default not set.
2081  #   require_native_mocks: Enforce that any native calls using
2082  #     org.chromium.base.annotations.NativeMethods must have a mock set
2083  #     (optional).
2084  #   product_config_java_packages: Optional list of java packages. If given, a
2085  #     ProductConfig.java file will be generated for each package.
2086  #   enable_proguard_checks: Turns on -checkdiscard directives and missing
2087  #     symbols check in the proguard step (default=true).
2088  #   annotation_processor_deps: List of java_annotation_processor targets to
2089  #     use when compiling the sources given to this target (optional).
2090  #   processor_args_javac: List of args to pass to annotation processors when
2091  #     compiling sources given to this target (optional).
2092  #   bundles_supported: Enable Java code to treat this target as a bundle
2093  #     whether (by default determined by the target type).
2094  #   main_component_library: Specifies the name of the base component's library
2095  #     in a component build. If given, the system will find dependent native
2096  #     libraries at runtime by inspecting this library (optional).
2097  #   expected_libs_and_assets: Verify the list of included native libraries
2098  #     and assets is consistent with the given expectation file.
2099  #   expected_libs_and_assets_base: Treat expected_libs_and_assets as a diff
2100  #     with this file as the base.
2101  #   expected_proguard_config: Checks that the merged set of proguard flags
2102  #     matches the given config.
2103  #   expected_proguard_config_base: Treat expected_proguard_config as a diff
2104  #     with this file as the base.
2105  template("android_apk_or_module") {
2106    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
2107    _template_name = target_name
2108    _base_path = "$target_out_dir/$target_name/$target_name"
2109    _build_config = "$target_gen_dir/$target_name.build_config.json"
2110    _build_config_target = "$target_name$build_config_target_suffix"
2111    _java_target_name = "${_template_name}__java"
2112
2113    _min_sdk_version = default_min_sdk_version
2114    _target_sdk_version = android_sdk_version
2115    if (defined(invoker.min_sdk_version)) {
2116      _min_sdk_version = invoker.min_sdk_version
2117    }
2118    if (defined(invoker.target_sdk_version)) {
2119      _target_sdk_version = invoker.target_sdk_version
2120    }
2121
2122    _is_bundle_module =
2123        defined(invoker.is_bundle_module) && invoker.is_bundle_module
2124    if (_is_bundle_module) {
2125      _is_base_module =
2126          defined(invoker.is_base_module) && invoker.is_base_module
2127    }
2128
2129    _omit_dex = defined(invoker.omit_dex) && invoker.omit_dex
2130
2131    if (!_is_bundle_module) {
2132      _final_apk_path = invoker.final_apk_path
2133      _final_rtxt_path = "${_final_apk_path}.R.txt"
2134    }
2135
2136    _res_size_info_path = "$target_out_dir/$target_name.ap_.info"
2137    if (!_is_bundle_module) {
2138      _final_apk_path_no_ext_list =
2139          process_file_template([ _final_apk_path ],
2140                                "{{source_dir}}/{{source_name_part}}")
2141      _final_apk_path_no_ext = _final_apk_path_no_ext_list[0]
2142      not_needed([ "_final_apk_path_no_ext" ])
2143    }
2144
2145    # Non-base bundle modules create only proto resources.
2146    if (!_is_bundle_module || _is_base_module) {
2147      _arsc_resources_path = "$target_out_dir/$target_name.ap_"
2148    }
2149    if (_is_bundle_module) {
2150      # Path to the intermediate proto-format resources zip file.
2151      _proto_resources_path = "$target_out_dir/$target_name.proto.ap_"
2152    } else {
2153      # resource_sizes.py needs to be able to find the unpacked resources.arsc
2154      # file based on apk name to compute normatlized size.
2155      _resource_sizes_arsc_path =
2156          "$root_out_dir/arsc/" +
2157          rebase_path(_final_apk_path_no_ext, root_build_dir) + ".ap_"
2158    }
2159
2160    if (defined(invoker.version_code)) {
2161      _version_code = invoker.version_code
2162    } else {
2163      _version_code = android_default_version_code
2164    }
2165
2166    if (android_override_version_code != "") {
2167      _version_code = android_override_version_code
2168    }
2169
2170    if (defined(invoker.version_name)) {
2171      _version_name = invoker.version_name
2172    } else {
2173      _version_name = android_default_version_name
2174    }
2175
2176    if (android_override_version_name != "") {
2177      _version_name = android_override_version_name
2178    }
2179
2180    if (defined(invoker.deps)) {
2181      _invoker_deps = invoker.deps
2182    } else {
2183      _invoker_deps = []
2184    }
2185    _non_java_deps = filter_exclude(_invoker_deps, java_target_patterns)
2186    _java_assetres_deps = [ ":${_java_target_name}__assetres" ]
2187
2188    _srcjar_deps = []
2189    if (defined(invoker.srcjar_deps)) {
2190      _srcjar_deps = invoker.srcjar_deps
2191    }
2192
2193    _use_chromium_linker =
2194        defined(invoker.use_chromium_linker) && invoker.use_chromium_linker
2195
2196    not_needed([ "_use_chromium_linker" ])
2197
2198    # The dependency that makes the chromium linker, if any is needed.
2199    _native_libs_deps = []
2200    _shared_libraries_is_valid =
2201        defined(invoker.shared_libraries) && invoker.shared_libraries != []
2202
2203    if (_shared_libraries_is_valid) {
2204      _native_libs_deps += invoker.shared_libraries
2205
2206      # Write shared library output files of all dependencies to a file. Those
2207      # will be the shared libraries packaged into the APK.
2208      _shared_library_list_file =
2209          "$target_gen_dir/${_template_name}.native_libs"
2210      generated_file("${_template_name}__shared_library_list") {
2211        deps = _native_libs_deps
2212        outputs = [ _shared_library_list_file ]
2213        data_keys = [ "shared_libraries" ]
2214        walk_keys = [ "shared_libraries_barrier" ]
2215        rebase = root_build_dir
2216      }
2217    } else {
2218      # Must exist for instrumentation_test_apk() to depend on.
2219      group("${_template_name}__shared_library_list") {
2220      }
2221    }
2222
2223    _secondary_abi_native_libs_deps = []
2224
2225    if (defined(invoker.secondary_abi_shared_libraries) &&
2226        invoker.secondary_abi_shared_libraries != []) {
2227      _secondary_abi_native_libs_deps = invoker.secondary_abi_shared_libraries
2228
2229      # Write shared library output files of all dependencies to a file. Those
2230      # will be the shared libraries packaged into the APK.
2231      _secondary_abi_shared_library_list_file =
2232          "$target_gen_dir/${_template_name}.secondary_abi_native_libs"
2233      generated_file("${_template_name}__secondary_abi_shared_library_list") {
2234        deps = _secondary_abi_native_libs_deps
2235        outputs = [ _secondary_abi_shared_library_list_file ]
2236        data_keys = [ "shared_libraries" ]
2237        walk_keys = [ "shared_libraries_barrier" ]
2238        rebase = root_build_dir
2239      }
2240    } else {
2241      # Must exist for instrumentation_test_apk() to depend on.
2242      group("${_template_name}__secondary_abi_shared_library_list") {
2243      }
2244    }
2245
2246    _rebased_build_config = rebase_path(_build_config, root_build_dir)
2247    assert(_rebased_build_config != "")  # Mark as used.
2248
2249    _generate_productconfig_java =
2250        defined(invoker.product_config_java_packages) && !_omit_dex
2251
2252    _proguard_enabled =
2253        defined(invoker.proguard_enabled) && invoker.proguard_enabled
2254
2255    if (!_is_bundle_module && _proguard_enabled) {
2256      _proguard_mapping_path = "$_final_apk_path.mapping"
2257    }
2258
2259    if (defined(invoker.resource_ids_provider_dep)) {
2260      _resource_ids_provider_dep = invoker.resource_ids_provider_dep
2261    }
2262
2263    if (defined(invoker.shared_resources_allowlist_target)) {
2264      _shared_resources_allowlist_target =
2265          invoker.shared_resources_allowlist_target
2266    }
2267
2268    _uses_static_library = defined(invoker.static_library_provider)
2269
2270    # TODO(crbug.com/864142): Allow incremental installs of bundle modules.
2271    _incremental_apk = !_is_bundle_module &&
2272                       !(defined(invoker.never_incremental) &&
2273                         invoker.never_incremental) && incremental_install
2274    if (_incremental_apk) {
2275      _target_dir_name = get_label_info(target_name, "dir")
2276      _incremental_install_json_path = "$root_out_dir/gen.runtime/$_target_dir_name/$target_name.incremental.json"
2277      _incremental_apk_path = "${_final_apk_path_no_ext}_incremental.apk"
2278    }
2279
2280    if (!_incremental_apk && !_omit_dex) {
2281      # Bundle modules don't build the dex here, but need to write this path
2282      # to their .build_config.json file only when proguarding.
2283      if (_proguard_enabled) {
2284        _final_dex_path = "$_base_path.r8dex.jar"
2285      } else if (!_is_bundle_module) {
2286        _final_dex_path = "$_base_path.mergeddex.jar"
2287      }
2288    }
2289
2290    _android_manifest =
2291        "$target_gen_dir/${_template_name}/AndroidManifest.merged.xml"
2292    _merge_manifest_target = "${_template_name}__merge_manifests"
2293    merge_manifests(_merge_manifest_target) {
2294      forward_variables_from(invoker,
2295                             [
2296                               "manifest_package",
2297                               "max_sdk_version",
2298                             ])
2299      input_manifest = invoker.android_manifest
2300      output_manifest = _android_manifest
2301      build_config = _build_config
2302      min_sdk_version = _min_sdk_version
2303      target_sdk_version = _target_sdk_version
2304
2305      # Depend on android_resources() targets that use generated files
2306      # in mergeable_android_manifests (such as android_aar_prebuilt).
2307      deps = _java_assetres_deps + [ ":$_build_config_target" ]
2308      if (defined(invoker.android_manifest_dep)) {
2309        deps += [ invoker.android_manifest_dep ]
2310      }
2311    }
2312
2313    _final_deps = [ ":$_java_target_name" ]
2314
2315    _generated_proguard_config = "$_base_path.resources.proguard.txt"
2316
2317    if (defined(_shared_resources_allowlist_target)) {
2318      _allowlist_gen_dir =
2319          get_label_info(_shared_resources_allowlist_target, "target_gen_dir")
2320      _allowlist_target_name =
2321          get_label_info(_shared_resources_allowlist_target, "name")
2322      _allowlist_r_txt_path =
2323          "${_allowlist_gen_dir}/${_allowlist_target_name}" +
2324          "__compile_resources_R.txt"
2325      _allowlist_deps =
2326          "${_shared_resources_allowlist_target}__compile_resources"
2327    }
2328
2329    if (_incremental_apk) {
2330      _incremental_android_manifest =
2331          "$target_gen_dir/${_template_name}/AndroidManifest.incremental.xml"
2332      _incremental_manifest_target_name = "${target_name}__incremental_manifest"
2333      action_with_pydeps(_incremental_manifest_target_name) {
2334        deps = [ ":$_merge_manifest_target" ]
2335        script =
2336            "//build/android/incremental_install/generate_android_manifest.py"
2337        inputs = [ _android_manifest ]
2338        outputs = [ _incremental_android_manifest ]
2339
2340        args = [
2341          "--disable-isolated-processes",
2342          "--src-manifest",
2343          rebase_path(_android_manifest, root_build_dir),
2344          "--dst-manifest",
2345          rebase_path(_incremental_android_manifest, root_build_dir),
2346        ]
2347      }
2348    }
2349
2350    _compile_resources_target = "${_template_name}__compile_resources"
2351    _compile_resources_rtxt_out =
2352        "${target_gen_dir}/${_compile_resources_target}_R.txt"
2353    _compile_resources_emit_ids_out =
2354        "${target_gen_dir}/${_compile_resources_target}.resource_ids"
2355    compile_resources(_compile_resources_target) {
2356      forward_variables_from(
2357          invoker,
2358          [
2359            "aapt_locale_allowlist",
2360            "app_as_shared_lib",
2361            "enforce_resource_overlays_in_tests",
2362            "expected_android_manifest",
2363            "expected_android_manifest_base",
2364            "expected_android_manifest_library_version_offset",
2365            "expected_android_manifest_version_code_offset",
2366            "manifest_package",
2367            "max_sdk_version",
2368            "package_id",
2369            "png_to_webp",
2370            "r_java_root_package_name",
2371            "resource_exclusion_exceptions",
2372            "resource_exclusion_regex",
2373            "resource_values_filter_rules",
2374            "shared_resources",
2375            "shared_resources_allowlist_locales",
2376            "uses_split",
2377          ])
2378      android_manifest = _android_manifest
2379      android_manifest_dep = ":$_merge_manifest_target"
2380      version_code = _version_code
2381      version_name = _version_name
2382      min_sdk_version = _min_sdk_version
2383      target_sdk_version = _target_sdk_version
2384
2385      if (defined(expected_android_manifest)) {
2386        top_target_name = _template_name
2387      }
2388
2389      if (defined(_resource_ids_provider_dep)) {
2390        resource_ids_provider_dep = _resource_ids_provider_dep
2391      }
2392
2393      if (defined(invoker.module_name)) {
2394        package_name = invoker.module_name
2395      }
2396
2397      if (defined(invoker.post_process_package_resources_script)) {
2398        post_process_script = invoker.post_process_package_resources_script
2399      }
2400      r_text_out_path = _compile_resources_rtxt_out
2401      emit_ids_out_path = _compile_resources_emit_ids_out
2402      size_info_path = _res_size_info_path
2403      proguard_file = _generated_proguard_config
2404
2405      build_config = _build_config
2406      build_config_dep = ":$_build_config_target"
2407      deps = _java_assetres_deps + _non_java_deps
2408
2409      if (_incremental_apk) {
2410        android_manifest = _incremental_android_manifest
2411        android_manifest_dep = ":$_incremental_manifest_target_name"
2412      }
2413
2414      if (defined(invoker.apk_under_test)) {
2415        # Set the arsc package name to match the apk_under_test package name
2416        # So that test resources can references under_test resources via
2417        # @type/name syntax.
2418        r_java_root_package_name = "test"
2419        arsc_package_name =
2420            "@FileArg($_rebased_build_config:deps_info:arsc_package_name)"
2421
2422        # Passing in the --emit-ids mapping will cause aapt2 to assign resources
2423        # IDs that do not conflict with those from apk_under_test.
2424        assert(!defined(resource_ids_provider_dep))
2425        resource_ids_provider_dep = invoker.apk_under_test
2426
2427        _link_against = invoker.apk_under_test
2428      }
2429
2430      if (_is_bundle_module) {
2431        is_bundle_module = true
2432        proto_output = _proto_resources_path
2433
2434        if (defined(invoker.base_module_target)) {
2435          _link_against = invoker.base_module_target
2436        }
2437      }
2438
2439      if (defined(_link_against)) {
2440        deps += [ "${_link_against}__compile_resources" ]
2441        include_resource = get_label_info(_link_against, "target_out_dir") +
2442                           "/" + get_label_info(_link_against, "name") + ".ap_"
2443      }
2444
2445      # Bundle modules have to reference resources from the base module.
2446      if (!_is_bundle_module || _is_base_module) {
2447        arsc_output = _arsc_resources_path
2448      }
2449
2450      if (defined(_shared_resources_allowlist_target)) {
2451        # Used to ensure that the WebView resources are properly shared
2452        # (i.e. are non-final and with package ID 0).
2453        shared_resources_allowlist = _allowlist_r_txt_path
2454        deps += [ _allowlist_deps ]
2455      }
2456    }
2457    _srcjar_deps += [ ":$_compile_resources_target" ]
2458
2459    # We don't ship apks anymore, only optimize bundle builds
2460    if (_is_bundle_module) {
2461      _short_resource_paths =
2462          defined(invoker.short_resource_paths) &&
2463          invoker.short_resource_paths && enable_arsc_obfuscation
2464      _strip_resource_names =
2465          defined(invoker.strip_resource_names) &&
2466          invoker.strip_resource_names && enable_arsc_obfuscation
2467      _strip_unused_resources =
2468          defined(invoker.strip_unused_resources) &&
2469          invoker.strip_unused_resources && enable_unused_resource_stripping
2470      _optimize_resources = _strip_resource_names || _short_resource_paths ||
2471                            _strip_unused_resources
2472    }
2473
2474    if (_is_bundle_module && _optimize_resources) {
2475      _optimized_proto_resources_path =
2476          "$target_out_dir/$target_name.optimized.proto.ap_"
2477      if (_short_resource_paths) {
2478        _resources_path_map_out_path =
2479            "${target_gen_dir}/${_template_name}_resources_path_map.txt"
2480      }
2481      _optimize_resources_target = "${_template_name}__optimize_resources"
2482      optimize_resources(_optimize_resources_target) {
2483        deps = _non_java_deps + [ ":$_compile_resources_target" ]
2484        short_resource_paths = _short_resource_paths
2485        strip_resource_names = _strip_resource_names
2486        if (_short_resource_paths) {
2487          resources_path_map_out_path = _resources_path_map_out_path
2488        }
2489        r_text_path = _compile_resources_rtxt_out
2490        proto_input_path = _proto_resources_path
2491        optimized_proto_output = _optimized_proto_resources_path
2492        if (_strip_unused_resources) {
2493          # These need to be kept in sync with the target names + output paths
2494          # in the android_app_bundle template.
2495          _unused_resources_target = "${_template_name}__unused_resources"
2496          _unused_resources_config_path =
2497              "$target_gen_dir/${_template_name}_unused_resources.config"
2498          resources_config_paths = [ _unused_resources_config_path ]
2499          deps += [ ":$_unused_resources_target" ]
2500        } else {
2501          resources_config_paths = []
2502        }
2503        if (defined(invoker.resources_config_paths)) {
2504          resources_config_paths += invoker.resources_config_paths
2505        }
2506      }
2507
2508      if (_strip_unused_resources) {
2509        # Copy the unused resources config to the final bundle output dir.
2510        _copy_unused_resources_target =
2511            "${_template_name}__copy_unused_resources"
2512        _final_deps += [ ":$_copy_unused_resources_target" ]
2513      }
2514    } else {
2515      not_needed(invoker, [ "resources_config_paths" ])
2516    }
2517
2518    if (!_is_bundle_module) {
2519      # Output the R.txt file to a more easily discoverable location for
2520      # archiving. This is necessary when stripping resource names so that we
2521      # have an archive of resource names to ids for shipped apks (for
2522      # debugging purposes). We copy the file rather than change the location
2523      # of the original because other targets rely on the location of the R.txt
2524      # file.
2525      _copy_rtxt_target = "${_template_name}__copy_rtxt"
2526      copy(_copy_rtxt_target) {
2527        deps = [ ":$_compile_resources_target" ]
2528        sources = [ _compile_resources_rtxt_out ]
2529        outputs = [ _final_rtxt_path ]
2530      }
2531      _final_deps += [ ":$_copy_rtxt_target" ]
2532    }
2533
2534    if (defined(_resource_sizes_arsc_path)) {
2535      _copy_arsc_target = "${_template_name}__copy_arsc"
2536      copy(_copy_arsc_target) {
2537        deps = [ ":$_compile_resources_target" ]
2538
2539        # resource_sizes.py doesn't care if it gets the optimized .arsc.
2540        sources = [ _arsc_resources_path ]
2541        outputs = [ _resource_sizes_arsc_path ]
2542      }
2543      _final_deps += [ ":$_copy_arsc_target" ]
2544    }
2545
2546    if (defined(invoker.generate_native_libraries_java)) {
2547      _generate_native_libraries_java = invoker.generate_native_libraries_java
2548    } else {
2549      _generate_native_libraries_java =
2550          (!_is_bundle_module || _is_base_module) && !_omit_dex &&
2551          !defined(invoker.apk_under_test)
2552    }
2553    if (_generate_native_libraries_java) {
2554      write_native_libraries_java("${_template_name}__native_libraries") {
2555        forward_variables_from(invoker, [ "main_component_library" ])
2556
2557        # Do not add a dep on the generated_file target in order to avoid having
2558        # to build the native libraries before this target. The dependency is
2559        # instead captured via a depfile.
2560        if (_uses_static_library) {
2561          _prefix = get_label_info(invoker.static_library_provider,
2562                                   "target_gen_dir") + "/" +
2563                    get_label_info(invoker.static_library_provider, "name")
2564          if (defined(invoker.static_library_provider_use_secondary_abi) &&
2565              invoker.static_library_provider_use_secondary_abi) {
2566            native_libraries_list_file = "${_prefix}.secondary_abi_native_libs"
2567            _use_secondary_abi = true
2568          } else {
2569            native_libraries_list_file = "${_prefix}.native_libs"
2570            _use_secondary_abi = false
2571          }
2572        } else if (_native_libs_deps != []) {
2573          native_libraries_list_file = _shared_library_list_file
2574          _use_secondary_abi = false
2575        } else if (_secondary_abi_native_libs_deps != []) {
2576          native_libraries_list_file = _secondary_abi_shared_library_list_file
2577          _use_secondary_abi = true
2578        }
2579
2580        if (defined(_use_secondary_abi)) {
2581          if (_use_secondary_abi || !android_64bit_target_cpu) {
2582            native_lib_32_bit = true
2583          } else {
2584            native_lib_64_bit = true
2585          }
2586        }
2587
2588        enable_chromium_linker = _use_chromium_linker
2589        use_final_fields = true
2590      }
2591      _srcjar_deps += [ ":${_template_name}__native_libraries" ]
2592    }
2593
2594    _loadable_modules = []
2595    if (defined(invoker.loadable_modules)) {
2596      _loadable_modules = invoker.loadable_modules
2597    }
2598
2599    if (_native_libs_deps != []) {
2600      _loadable_modules += _sanitizer_runtimes
2601    }
2602
2603    _assertions_implicitly_enabled = defined(invoker.custom_assertion_handler)
2604
2605    # Many possible paths where we wouldn't use this variable.
2606    not_needed([ "_assertions_implicitly_enabled" ])
2607
2608    _generate_buildconfig_java = !defined(invoker.apk_under_test) && !_omit_dex
2609    if (defined(invoker.generate_buildconfig_java)) {
2610      _generate_buildconfig_java = invoker.generate_buildconfig_java
2611    }
2612    if (_generate_buildconfig_java) {
2613      generate_build_config_srcjar("${_template_name}__build_config_srcjar") {
2614        forward_variables_from(invoker,
2615                               [
2616                                 "min_sdk_version",
2617                                 "isolated_splits_enabled",
2618                               ])
2619        _bundles_supported = _is_bundle_module
2620        if (defined(invoker.bundles_supported)) {
2621          _bundles_supported = invoker.bundles_supported
2622        }
2623        bundles_supported = _bundles_supported
2624        use_final_fields = true
2625        assertions_implicitly_enabled = _assertions_implicitly_enabled
2626        is_incremental_install = _incremental_apk
2627        write_clang_profiling_data =
2628            use_clang_coverage && _generate_native_libraries_java
2629        if (defined(invoker.build_config_include_product_version_resource) &&
2630            invoker.build_config_include_product_version_resource) {
2631          resources_version_variable =
2632              "org.chromium.base.R.string.product_version"
2633        }
2634        deps = [ ":$_build_config_target" ]
2635      }
2636      _srcjar_deps += [ ":${_template_name}__build_config_srcjar" ]
2637    }
2638
2639    if (_generate_productconfig_java) {
2640      foreach(_package, invoker.product_config_java_packages) {
2641        _locale_target_name =
2642            "${_template_name}_${_package}__product_config_srcjar"
2643        generate_product_config_srcjar("$_locale_target_name") {
2644          forward_variables_from(invoker, [ "is_bundle_module" ])
2645          build_config = _build_config
2646          java_package = _package
2647          use_chromium_linker = _use_chromium_linker
2648          deps = [ ":$_build_config_target" ]
2649        }
2650        _srcjar_deps += [ ":$_locale_target_name" ]
2651      }
2652    }
2653
2654    if (_is_bundle_module) {
2655      _add_view_trace_events =
2656          defined(invoker.add_view_trace_events) &&
2657          invoker.add_view_trace_events && enable_trace_event_bytecode_rewriting
2658    }
2659
2660    # We cannot skip this target when omit_dex = true because it writes the
2661    # build_config.json.
2662    java_library_impl(_java_target_name) {
2663      forward_variables_from(invoker,
2664                             [
2665                               "alternative_android_sdk_dep",
2666                               "android_manifest",
2667                               "android_manifest_dep",
2668                               "annotation_processor_deps",
2669                               "apk_under_test",
2670                               "base_module_target",
2671                               "chromium_code",
2672                               "deps",
2673                               "jacoco_never_instrument",
2674                               "jar_excluded_patterns",
2675                               "javac_args",
2676                               "mergeable_android_manifests",
2677                               "native_lib_placeholders",
2678                               "parent_module_target",
2679                               "processor_args_javac",
2680                               "secondary_abi_loadable_modules",
2681                               "secondary_native_lib_placeholders",
2682                               "sources",
2683                               "library_always_compress",
2684                             ])
2685      version_code = _version_code
2686      version_name = _version_name
2687      if (_is_bundle_module) {
2688        type = "android_app_bundle_module"
2689        res_size_info_path = _res_size_info_path
2690        if (defined(invoker.module_name)) {
2691          module_name = invoker.module_name
2692        } else {
2693          module_name = "base"
2694        }
2695        add_view_trace_events = _add_view_trace_events
2696      } else {
2697        type = "android_apk"
2698      }
2699      r_text_path = _compile_resources_rtxt_out
2700      main_target_name = _template_name
2701      supports_android = true
2702      requires_android = true
2703      srcjar_deps = _srcjar_deps
2704      merged_android_manifest = _android_manifest
2705      if (defined(_final_dex_path)) {
2706        final_dex_path = _final_dex_path
2707      }
2708
2709      if (_is_bundle_module) {
2710        proto_resources_path = _proto_resources_path
2711        if (_optimize_resources) {
2712          proto_resources_path = _optimized_proto_resources_path
2713          if (_short_resource_paths) {
2714            module_pathmap_path = _resources_path_map_out_path
2715          }
2716        }
2717      } else {
2718        apk_path = _final_apk_path
2719        if (_incremental_apk) {
2720          incremental_apk_path = _incremental_apk_path
2721          incremental_install_json_path = _incremental_install_json_path
2722        }
2723      }
2724
2725      proguard_enabled = _proguard_enabled
2726      if (_proguard_enabled) {
2727        proguard_configs = [ _generated_proguard_config ]
2728        if (defined(invoker.proguard_configs)) {
2729          proguard_configs += invoker.proguard_configs
2730        }
2731        if (!_is_bundle_module) {
2732          proguard_mapping_path = _proguard_mapping_path
2733        }
2734      }
2735
2736      # Do not add a dep on the generated_file target in order to avoid having
2737      # to build the native libraries before this target. The dependency is
2738      # instead captured via a depfile.
2739      if (_native_libs_deps != []) {
2740        shared_libraries_runtime_deps_file = _shared_library_list_file
2741      }
2742      if (defined(_secondary_abi_shared_library_list_file)) {
2743        secondary_abi_shared_libraries_runtime_deps_file =
2744            _secondary_abi_shared_library_list_file
2745      }
2746
2747      loadable_modules = _loadable_modules
2748
2749      if (defined(_allowlist_r_txt_path) && _is_bundle_module) {
2750        # Used to write the file path to the target's .build_config.json only.
2751        base_allowlist_rtxt_path = _allowlist_r_txt_path
2752      }
2753    }
2754
2755    # Old name for variable, mark as not_needed while it is being renamed
2756    # downstream. Remove after all references to baseline_profile_path have been
2757    # changed.
2758    not_needed(invoker, [ "baseline_profile_path" ])
2759
2760    _enable_art_profile_optimizations =
2761        defined(invoker.art_profile_path) && _proguard_enabled
2762
2763    if (_enable_art_profile_optimizations) {
2764      _include_baseline_profile = enable_baseline_profiles
2765      _enable_startup_profile = enable_startup_profiles
2766      if (_include_baseline_profile) {
2767        _obfuscated_art_profile =
2768            "$target_out_dir/${target_name}.obfuscated.hrf"
2769      }
2770    } else {
2771      not_needed(invoker, [ "art_profile_path" ])
2772    }
2773
2774    if (_is_bundle_module || _omit_dex) {
2775      # Dex generation for app bundle modules take place in the
2776      # android_app_bundle template.
2777      not_needed(invoker, [ "custom_assertion_handler" ])
2778    } else if (_incremental_apk) {
2779      not_needed(invoker,
2780                 [
2781                   "enable_proguard_checks",
2782                   "custom_assertion_handler",
2783                 ])
2784    } else {
2785      _final_dex_target_name = "${_template_name}__final_dex"
2786      dex(_final_dex_target_name) {
2787        forward_variables_from(invoker,
2788                               [
2789                                 "enable_proguard_checks",
2790                                 "custom_assertion_handler",
2791                                 "proguard_enable_obfuscation",
2792                                 "repackage_classes",
2793                               ])
2794        min_sdk_version = _min_sdk_version
2795        proguard_enabled = _proguard_enabled
2796        build_config = _build_config
2797        output = _final_dex_path
2798        deps = [
2799          ":$_build_config_target",
2800          ":$_java_target_name",
2801        ]
2802        if (_proguard_enabled) {
2803          # Generates proguard configs
2804          deps += [ ":$_compile_resources_target" ]
2805          proguard_mapping_path = _proguard_mapping_path
2806          has_apk_under_test = defined(invoker.apk_under_test)
2807
2808          if (_enable_art_profile_optimizations) {
2809            input_art_profile = invoker.art_profile_path
2810            if (_include_baseline_profile) {
2811              output_art_profile = _obfuscated_art_profile
2812            }
2813            enable_startup_profile = _enable_startup_profile
2814          }
2815
2816          # Must not be set via write_build_config, because that will cause it
2817          # to be picked up by test apks that use apk_under_test.
2818          if (!_assertions_implicitly_enabled && !enable_java_asserts &&
2819              (!defined(testonly) || !testonly) &&
2820              # Injected JaCoCo code causes -checkdiscards to fail.
2821              !use_jacoco_coverage) {
2822            proguard_configs = [
2823              "//build/android/dcheck_is_off.flags",
2824              "//third_party/jni_zero/checkdiscard_proguard.flags",
2825            ]
2826          }
2827        } else {
2828          if (_min_sdk_version >= default_min_sdk_version) {
2829            # Enable dex merging only when min_sdk_version is >= what the library
2830            # .dex files were created with.
2831            input_dex_filearg =
2832                "@FileArg(${_rebased_build_config}:deps_info:all_dex_files)"
2833
2834            # Pure dex-merge.
2835            enable_desugar = false
2836          } else {
2837            input_classes_filearg =
2838                "@FileArg($_rebased_build_config:deps_info:device_classpath)"
2839          }
2840        }
2841
2842        # The individual dependencies would have caught real missing deps in
2843        # their respective dex steps. False positives that were suppressed at
2844        # per-target dex steps are emitted here since this may use jar files
2845        # rather than dex files.
2846        if (!defined(enable_desugar)) {
2847          ignore_desugar_missing_deps = true
2848        }
2849      }
2850
2851      _final_dex_target_dep = ":$_final_dex_target_name"
2852
2853      if (_enable_art_profile_optimizations && _include_baseline_profile) {
2854        _binary_profile_target = "${_template_name}__binary_baseline_profile"
2855        _binary_baseline_profile_path =
2856            "$target_out_dir/$_template_name.baseline.prof"
2857        _binary_baseline_profile_metadata_path =
2858            _binary_baseline_profile_path + "m"
2859        create_binary_profile(_binary_profile_target) {
2860          forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
2861          binary_baseline_profile_path = _binary_baseline_profile_path
2862          binary_baseline_profile_metadata_path =
2863              _binary_baseline_profile_metadata_path
2864          build_config = _build_config
2865          input_profile_path = _obfuscated_art_profile
2866          deps = [
2867            ":$_build_config_target",
2868            _final_dex_target_dep,
2869          ]
2870        }
2871      }
2872    }
2873
2874    _all_native_libs_deps = _native_libs_deps + _secondary_abi_native_libs_deps
2875    if (_all_native_libs_deps != []) {
2876      _native_libs_filearg_dep = ":$_build_config_target"
2877      _all_native_libs_deps += [ _native_libs_filearg_dep ]
2878
2879      if (!_is_bundle_module) {
2880        _native_libs_filearg =
2881            "@FileArg($_rebased_build_config:native:libraries)"
2882      }
2883    }
2884
2885    if (_is_bundle_module) {
2886      _final_deps += [
2887                       ":$_build_config_target",
2888                       ":$_compile_resources_target",
2889                       ":$_merge_manifest_target",
2890                     ] + _all_native_libs_deps
2891      if (_optimize_resources) {
2892        _final_deps += [ ":$_optimize_resources_target" ]
2893      }
2894      if (defined(_final_dex_target_dep)) {
2895        not_needed([ "_final_dex_target_dep" ])
2896      }
2897    } else {
2898      # Generate size-info/*.jar.info files.
2899      if (defined(invoker.name)) {
2900        # Create size info files for targets that care about size
2901        # (have proguard enabled).
2902        _include_size_info =
2903            defined(invoker.include_size_info) && invoker.include_size_info
2904        if (_include_size_info || _proguard_enabled) {
2905          _size_info_target = "${target_name}__size_info"
2906          create_size_info_files(_size_info_target) {
2907            name = "${invoker.name}.apk"
2908            build_config = _build_config
2909            res_size_info_path = _res_size_info_path
2910            deps = [
2911              ":$_build_config_target",
2912              ":$_compile_resources_target",
2913              ":$_java_target_name",
2914            ]
2915          }
2916          _final_deps += [ ":$_size_info_target" ]
2917        } else {
2918          not_needed(invoker, [ "name" ])
2919        }
2920      }
2921
2922      _create_apk_target = "${_template_name}__create"
2923      _final_deps += [ ":$_create_apk_target" ]
2924      package_apk("$_create_apk_target") {
2925        forward_variables_from(invoker,
2926                               [
2927                                 "expected_libs_and_assets",
2928                                 "expected_libs_and_assets_base",
2929                                 "keystore_name",
2930                                 "keystore_path",
2931                                 "keystore_password",
2932                                 "native_lib_placeholders",
2933                                 "secondary_abi_loadable_modules",
2934                                 "secondary_native_lib_placeholders",
2935                                 "uncompress_dex",
2936                                 "library_always_compress",
2937                               ])
2938
2939        if (defined(expected_libs_and_assets)) {
2940          build_config_dep = ":$_build_config_target"
2941          top_target_name = _template_name
2942        }
2943
2944        build_config = _build_config
2945        min_sdk_version = _min_sdk_version
2946        packaged_resources_path = _arsc_resources_path
2947
2948        # Need full deps rather than _non_java_deps, because loadable_modules
2949        # may include .so files extracted by __unpack_aar targets.
2950        deps = _invoker_deps + [ ":$_build_config_target" ]
2951
2952        if (_incremental_apk) {
2953          _dex_target = "//build/android/incremental_install:apk_dex"
2954
2955          deps += [
2956            ":$_compile_resources_target",
2957            _dex_target,
2958          ]
2959
2960          dex_path = get_label_info(_dex_target, "target_out_dir") + "/apk.dex"
2961
2962          # Incremental APKs cannot be installed via `adb install` as such they
2963          # should be clearly named/labeled "incremental".
2964          output_apk_path = _incremental_apk_path
2965
2966          # All native libraries are side-loaded, so use a placeholder to force
2967          # the proper bitness for the app.
2968          _has_native_libs =
2969              defined(_native_libs_filearg) || _loadable_modules != []
2970          if (_has_native_libs && !defined(native_lib_placeholders)) {
2971            native_lib_placeholders = [ "libfix.crbug.384638.so" ]
2972          }
2973        } else {
2974          loadable_modules = _loadable_modules
2975          deps += _all_native_libs_deps + [
2976                    ":$_compile_resources_target",
2977                    ":$_merge_manifest_target",
2978                  ]
2979
2980          if (defined(_final_dex_path)) {
2981            dex_path = _final_dex_path
2982            deps += [ _final_dex_target_dep ]
2983            if (_enable_art_profile_optimizations &&
2984                _include_baseline_profile) {
2985              # extra_assets is a list of ["{src_path}:{dst_path}"]
2986              extra_assets = [
2987                rebase_path(_binary_baseline_profile_path, root_build_dir) +
2988                    ":dexopt/baseline.prof",
2989                rebase_path(_binary_baseline_profile_metadata_path,
2990                            root_build_dir) + ":dexopt/baseline.profm",
2991              ]
2992              deps += [ ":$_binary_profile_target" ]
2993            }
2994          }
2995
2996          output_apk_path = _final_apk_path
2997
2998          if (defined(_native_libs_filearg)) {
2999            native_libs_filearg = _native_libs_filearg
3000            secondary_abi_native_libs_filearg = "@FileArg($_rebased_build_config:native:secondary_abi_libraries)"
3001          }
3002        }
3003      }
3004    }
3005
3006    if (_incremental_apk) {
3007      _write_installer_json_rule_name = "${_template_name}__incremental_json"
3008      action_with_pydeps(_write_installer_json_rule_name) {
3009        script = "//build/android/incremental_install/write_installer_json.py"
3010        deps = [ ":$_build_config_target" ] + _all_native_libs_deps
3011
3012        data = [ _incremental_install_json_path ]
3013        inputs = [ _build_config ]
3014        outputs = [ _incremental_install_json_path ]
3015
3016        _rebased_incremental_apk_path =
3017            rebase_path(_incremental_apk_path, root_build_dir)
3018        _rebased_incremental_install_json_path =
3019            rebase_path(_incremental_install_json_path, root_build_dir)
3020        args = [
3021          "--apk-path=$_rebased_incremental_apk_path",
3022          "--output-path=$_rebased_incremental_install_json_path",
3023          "--dex-file=@FileArg($_rebased_build_config:deps_info:all_dex_files)",
3024        ]
3025        if (_proguard_enabled) {
3026          args += [ "--show-proguard-warning" ]
3027        }
3028        if (defined(_native_libs_filearg)) {
3029          args += [ "--native-libs=$_native_libs_filearg" ]
3030          deps += [ _native_libs_filearg_dep ]
3031        }
3032        if (_loadable_modules != []) {
3033          _rebased_loadable_modules =
3034              rebase_path(_loadable_modules, root_build_dir)
3035          args += [ "--native-libs=$_rebased_loadable_modules" ]
3036        }
3037      }
3038      _final_deps += [ ":$_write_installer_json_rule_name" ]
3039    }
3040
3041    # Generate apk operation related script.
3042    if (!_is_bundle_module &&
3043        (!defined(invoker.create_apk_script) || invoker.create_apk_script)) {
3044      if (_uses_static_library) {
3045        _install_artifacts_target = "${target_name}__install_artifacts"
3046        _install_artifacts_json =
3047            "${target_gen_dir}/${target_name}.install_artifacts"
3048        generated_file(_install_artifacts_target) {
3049          output_conversion = "json"
3050          deps = [ invoker.static_library_provider ]
3051          outputs = [ _install_artifacts_json ]
3052          data_keys = [ "install_artifacts" ]
3053          rebase = root_build_dir
3054        }
3055      }
3056      _apk_operations_target_name = "${target_name}__apk_operations"
3057      action_with_pydeps(_apk_operations_target_name) {
3058        _generated_script = "$root_build_dir/bin/${invoker.target_name}"
3059        script = "//build/android/gyp/create_apk_operations_script.py"
3060        outputs = [ _generated_script ]
3061        args = [
3062          "--script-output-path",
3063          rebase_path(_generated_script, root_build_dir),
3064          "--target-cpu=$target_cpu",
3065        ]
3066        if (defined(invoker.command_line_flags_file)) {
3067          args += [
3068            "--command-line-flags-file",
3069            invoker.command_line_flags_file,
3070          ]
3071        }
3072        if (_incremental_apk) {
3073          args += [
3074            "--incremental-install-json-path",
3075            rebase_path(_incremental_install_json_path, root_build_dir),
3076          ]
3077        } else {
3078          args += [
3079            "--apk-path",
3080            rebase_path(_final_apk_path, root_build_dir),
3081          ]
3082        }
3083        if (_uses_static_library) {
3084          deps = [ ":$_install_artifacts_target" ]
3085          _rebased_install_artifacts_json =
3086              rebase_path(_install_artifacts_json, root_build_dir)
3087          _static_library_apk_path =
3088              "@FileArg($_rebased_install_artifacts_json[])"
3089          args += [
3090            "--additional-apk",
3091            _static_library_apk_path,
3092          ]
3093        }
3094        data = []
3095        data_deps = [
3096          "//build/android:apk_operations_py",
3097          "//build/android:stack_tools",
3098        ]
3099
3100        if (_proguard_enabled && !_incremental_apk) {
3101          # Required by logcat command.
3102          data_deps += [ "//build/android/stacktrace:java_deobfuscate" ]
3103          data += [ "$_final_apk_path.mapping" ]
3104          args += [
3105            "--proguard-mapping-path",
3106            rebase_path("$_final_apk_path.mapping", root_build_dir),
3107          ]
3108        }
3109      }
3110      _final_deps += [ ":$_apk_operations_target_name" ]
3111    }
3112
3113    _enable_lint = defined(invoker.enable_lint) && invoker.enable_lint &&
3114                   !disable_android_lint
3115    if (_enable_lint) {
3116      android_lint("${target_name}__lint") {
3117        forward_variables_from(invoker,
3118                               [
3119                                 "lint_baseline_file",
3120                                 "lint_gen_dir",
3121                                 "lint_suppressions_file",
3122                                 "min_sdk_version",
3123                               ])
3124        build_config = _build_config
3125        build_config_dep = ":$_build_config_target"
3126
3127        # This will use library subtargets under-the-hood
3128        deps = [ ":$_java_target_name" ]
3129        if (defined(invoker.lint_suppressions_dep)) {
3130          deps += [ invoker.lint_suppressions_dep ]
3131        }
3132        if (defined(invoker.lint_min_sdk_version)) {
3133          min_sdk_version = invoker.lint_min_sdk_version
3134        }
3135      }
3136    } else {
3137      not_needed(invoker,
3138                 [
3139                   "lint_baseline_file",
3140                   "lint_gen_dir",
3141                   "lint_jar_path",
3142                   "lint_min_sdk_version",
3143                   "lint_suppressions_dep",
3144                   "lint_suppressions_file",
3145                 ])
3146    }
3147
3148    group(target_name) {
3149      forward_variables_from(invoker,
3150                             [
3151                               "assert_no_deps",
3152                               "data",
3153                               "data_deps",
3154                             ])
3155      metadata = {
3156        if (defined(invoker.metadata)) {
3157          forward_variables_from(invoker.metadata, "*")
3158        }
3159
3160        # Allows metadata collection via apk targets that traverse only java deps.
3161        java_walk_keys = [ ":$_java_target_name" ]
3162      }
3163
3164      # Generate apk related operations at runtime.
3165      public_deps = _final_deps
3166
3167      if (!defined(data_deps)) {
3168        data_deps = []
3169      }
3170
3171      # Include unstripped native libraries so tests can symbolize stacks.
3172      data_deps += _all_native_libs_deps + [ ":${_java_target_name}__validate" ]
3173      if (_enable_lint) {
3174        data_deps += [ ":${target_name}__lint" ]
3175      }
3176
3177      if (_uses_static_library) {
3178        data_deps += [ invoker.static_library_provider ]
3179      }
3180    }
3181  }
3182
3183  # Declare an Android APK target
3184  #
3185  # This target creates an Android APK containing java code, resources, assets,
3186  # and (possibly) native libraries.
3187  #
3188  # Supports all variables of android_apk_or_module(), plus:
3189  #   apk_name: Name for final apk.
3190  #   final_apk_path: (Optional) path to output APK.
3191  #
3192  # Example
3193  #   android_apk("foo_apk") {
3194  #     android_manifest = "AndroidManifest.xml"
3195  #     sources = [
3196  #       "android/org/chromium/foo/FooApplication.java",
3197  #       "android/org/chromium/foo/FooActivity.java",
3198  #     ]
3199  #     deps = [
3200  #       ":foo_support_java"
3201  #       ":foo_resources"
3202  #     ]
3203  #     srcjar_deps = [
3204  #       ":foo_generated_enum"
3205  #     ]
3206  #     shared_libraries = [
3207  #       ":my_shared_lib",
3208  #     ]
3209  #   }
3210  template("android_apk") {
3211    # TODO(crbug.com/1042017): Remove.
3212    not_needed(invoker, [ "no_build_hooks" ])
3213    android_apk_or_module(target_name) {
3214      forward_variables_from(
3215          invoker,
3216          [
3217            "aapt_locale_allowlist",
3218            "additional_jar_files",
3219            "allow_unused_jni_from_native",
3220            "alternative_android_sdk_dep",
3221            "android_manifest",
3222            "android_manifest_dep",
3223            "annotation_processor_deps",
3224            "apk_under_test",
3225            "app_as_shared_lib",
3226            "art_profile_path",
3227            "assert_no_deps",
3228            "baseline_profile_path",
3229            "build_config_include_product_version_resource",
3230            "bundles_supported",
3231            "chromium_code",
3232            "command_line_flags_file",
3233            "create_apk_script",
3234            "custom_assertion_handler",
3235            "data",
3236            "data_deps",
3237            "deps",
3238            "enable_lint",
3239            "enable_jni_multiplexing",
3240            "enable_proguard_checks",
3241            "enforce_resource_overlays_in_tests",
3242            "expected_android_manifest",
3243            "expected_android_manifest_base",
3244            "expected_android_manifest_library_version_offset",
3245            "expected_android_manifest_version_code_offset",
3246            "expected_libs_and_assets",
3247            "expected_libs_and_assets_base",
3248            "generate_buildconfig_java",
3249            "generate_native_libraries_java",
3250            "include_size_info",
3251            "input_jars_paths",
3252            "jacoco_never_instrument",
3253            "javac_args",
3254            "keystore_name",
3255            "keystore_password",
3256            "keystore_path",
3257            "lint_baseline_file",
3258            "lint_gen_dir",
3259            "lint_min_sdk_version",
3260            "lint_suppressions_dep",
3261            "lint_suppressions_file",
3262            "loadable_modules",
3263            "manifest_package",
3264            "max_sdk_version",
3265            "mergeable_android_manifests",
3266            "product_config_java_packages",
3267            "main_component_library",
3268            "min_sdk_version",
3269            "native_lib_placeholders",
3270            "never_incremental",
3271            "omit_dex",
3272            "png_to_webp",
3273            "post_process_package_resources_script",
3274            "processor_args_javac",
3275            "proguard_configs",
3276            "proguard_enabled",
3277            "proguard_enable_obfuscation",
3278            "r_java_root_package_name",
3279            "repackage_classes",
3280            "resource_exclusion_exceptions",
3281            "resource_exclusion_regex",
3282            "resource_ids_provider_dep",
3283            "resource_values_filter_rules",
3284            "require_native_mocks",
3285            "secondary_abi_loadable_modules",
3286            "secondary_abi_shared_libraries",
3287            "secondary_native_lib_placeholders",
3288            "shared_libraries",
3289            "shared_resources",
3290            "shared_resources_allowlist_locales",
3291            "shared_resources_allowlist_target",
3292            "sources",
3293            "srcjar_deps",
3294            "static_library_provider",
3295            "static_library_provider_use_secondary_abi",
3296            "target_sdk_version",
3297            "testonly",
3298            "uncompress_dex",
3299            "library_always_compress",
3300            "use_chromium_linker",
3301            "version_code",
3302            "version_name",
3303            "visibility",
3304          ])
3305      is_bundle_module = false
3306      name = invoker.apk_name
3307      if (defined(invoker.final_apk_path)) {
3308        final_apk_path = invoker.final_apk_path
3309      } else {
3310        final_apk_path = "$root_build_dir/apks/${invoker.apk_name}.apk"
3311      }
3312      metadata = {
3313        install_artifacts = [ final_apk_path ]
3314        if (defined(invoker.static_library_provider)) {
3315          install_artifacts_barrier = []
3316        }
3317      }
3318
3319      # TODO(smaier) - there were some remaining usages of this in angle. Once
3320      # they are removed, remove this line.
3321      not_needed(invoker, [ "generate_final_jni" ])
3322    }
3323  }
3324
3325  # Declare an Android app bundle module target.
3326  #
3327  # The module can be used for an android_apk_or_module().
3328  #
3329  # Supports all variables of android_library(), plus:
3330  #   module_name: Name of the module.
3331  #   is_base_module: If defined and true, indicates that this is the bundle's
3332  #     base module (optional).
3333  #   base_module_target: Base module target of the bundle this module will be
3334  #     added to (optional). Can only be specified for non-base modules.
3335  template("android_app_bundle_module") {
3336    _is_base_module = defined(invoker.is_base_module) && invoker.is_base_module
3337
3338    if (_is_base_module) {
3339      assert(!defined(invoker.base_module_target))
3340    } else {
3341      assert(!defined(invoker.app_as_shared_lib))
3342      assert(!defined(invoker.shared_resources))
3343      assert(!defined(invoker.shared_resources_allowlist_target))
3344      assert(!defined(invoker.shared_resources_allowlist_locales))
3345      assert(defined(invoker.base_module_target))
3346    }
3347
3348    # android_app_bundle's write_build_config expects module targets to be named
3349    # according to java_target_patterns otherwise it ignores them when listed in
3350    # possible_config_deps. See https://crbug.com/1418398.
3351    if (filter_exclude([ target_name ], [ "*_bundle_module" ]) != []) {
3352      assert(false,
3353             "Invalid android_app_bundle_module target name ($target_name), " +
3354                 "must end in _bundle_module.")
3355    }
3356
3357    # TODO(tiborg): We have several flags that are necessary for workarounds
3358    # that come from the fact that the resources get compiled in the bundle
3359    # module target, but bundle modules have to have certain flags in
3360    # common or bundle modules have to know information about the base module.
3361    # Those flags include version_code, version_name, and base_module_target.
3362    # It would be better to move the resource compile target into the bundle
3363    # target. Doing so would keep the bundle modules independent from the bundle
3364    # and potentially reuse the same bundle modules for multiple bundles.
3365    android_apk_or_module(target_name) {
3366      forward_variables_from(
3367          invoker,
3368          [
3369            "add_view_trace_events",
3370            "aapt_locale_allowlist",
3371            "additional_jar_files",
3372            "allow_unused_jni_from_native",
3373            "alternative_android_sdk_dep",
3374            "android_manifest",
3375            "android_manifest_dep",
3376            "annotation_processor_deps",
3377            "app_as_shared_lib",
3378            "assert_no_deps",
3379            "base_module_target",
3380            "build_config_include_product_version_resource",
3381            "bundle_target",
3382            "chromium_code",
3383            "custom_assertion_handler",
3384            "data",
3385            "data_deps",
3386            "deps",
3387            "enable_jni_multiplexing",
3388            "expected_android_manifest",
3389            "expected_android_manifest_base",
3390            "expected_android_manifest_library_version_offset",
3391            "expected_android_manifest_version_code_offset",
3392            "generate_buildconfig_java",
3393            "generate_native_libraries_java",
3394            "input_jars_paths",
3395            "isolated_splits_enabled",
3396            "is_base_module",
3397            "jacoco_never_instrument",
3398            "jar_excluded_patterns",
3399            "javac_args",
3400            "loadable_modules",
3401            "product_config_java_packages",
3402            "main_component_library",
3403            "manifest_package",
3404            "max_sdk_version",
3405            "min_sdk_version",
3406            "mergeable_android_manifests",
3407            "module_name",
3408            "native_lib_placeholders",
3409            "package_id",
3410            "parent_module_target",
3411            "png_to_webp",
3412            "processor_args_javac",
3413            "proguard_configs",
3414            "proguard_enabled",
3415            "proguard_enable_obfuscation",
3416            "repackage_classes",
3417            "resource_exclusion_exceptions",
3418            "resource_exclusion_regex",
3419            "resource_ids_provider_dep",
3420            "resource_values_filter_rules",
3421            "resources_config_paths",
3422            "secondary_abi_loadable_modules",
3423            "secondary_abi_shared_libraries",
3424            "secondary_native_lib_placeholders",
3425            "shared_libraries",
3426            "shared_resources",
3427            "shared_resources_allowlist_locales",
3428            "shared_resources_allowlist_target",
3429            "short_resource_paths",
3430            "srcjar_deps",
3431            "static_library_provider",
3432            "static_library_provider_use_secondary_abi",
3433            "strip_resource_names",
3434            "strip_unused_resources",
3435            "target_sdk_version",
3436            "testonly",
3437            "library_always_compress",
3438            "use_chromium_linker",
3439            "uses_split",
3440            "version_code",
3441            "version_name",
3442            "visibility",
3443          ])
3444      is_bundle_module = true
3445      generate_buildconfig_java = _is_base_module
3446      if (defined(uses_split)) {
3447        assert(defined(parent_module_target),
3448               "Must set parent_module_target when uses_split is set")
3449      }
3450    }
3451  }
3452
3453  # Declare an Android instrumentation test runner.
3454  #
3455  # This target creates a wrapper script to run Android instrumentation tests.
3456  #
3457  # Arguments:
3458  #   android_test_apk: The target containing the tests.
3459  #
3460  #   The following args are optional:
3461  #   apk_under_test: The target being tested.
3462  #   additional_apks: Additional targets to install on device.
3463  #   data: List of runtime data file dependencies.
3464  #   data_deps: List of non-linked dependencies.
3465  #   deps: List of private dependencies.
3466  #   extra_args: Extra arguments set for test runner.
3467  #   ignore_all_data_deps: Don't build data_deps and additional_apks.
3468  #   modules: Extra dynamic feature modules to install for test target. Can
3469  #     only be used if |apk_under_test| is an Android app bundle.
3470  #   fake_modules: Similar to |modules| but fake installed instead.
3471  #   never_incremental: Disable incremental builds.
3472  #   proguard_enabled: Enable proguard
3473  #   public_deps: List of public dependencies
3474  #
3475  # Example
3476  #   instrumentation_test_runner("foo_test_for_bar") {
3477  #     android_test_apk: ":foo"
3478  #     apk_under_test: ":bar"
3479  #   }
3480  template("instrumentation_test_runner") {
3481    _incremental_apk = !(defined(invoker.never_incremental) &&
3482                         invoker.never_incremental) && incremental_install
3483    _apk_operations_target_name = "${target_name}__apk_operations"
3484    _apk_target = invoker.android_test_apk
3485    if (defined(invoker.apk_under_test) && !_incremental_apk) {
3486      # The actual target is defined in the test_runner_script template.
3487      _install_artifacts_json =
3488          "${target_gen_dir}/${target_name}.install_artifacts"
3489      _install_artifacts_target_name = "${target_name}__install_artifacts"
3490    }
3491
3492    action_with_pydeps(_apk_operations_target_name) {
3493      testonly = true
3494      script = "//build/android/gyp/create_test_apk_wrapper_script.py"
3495      deps = []
3496      _generated_script = "$root_build_dir/bin/${invoker.target_name}"
3497      outputs = [ _generated_script ]
3498      _apk_build_config =
3499          get_label_info(_apk_target, "target_gen_dir") + "/" +
3500          get_label_info(_apk_target, "name") + ".build_config.json"
3501      _rebased_apk_build_config = rebase_path(_apk_build_config, root_build_dir)
3502      args = [
3503        "--script-output-path",
3504        rebase_path(_generated_script, root_build_dir),
3505        "--package-name",
3506        "@FileArg($_rebased_apk_build_config:deps_info:package_name)",
3507      ]
3508      deps += [ "${_apk_target}$build_config_target_suffix" ]
3509      if (_incremental_apk) {
3510        args += [
3511          "--test-apk-incremental-install-json",
3512          "@FileArg($_rebased_apk_build_config:deps_info:incremental_install_json_path)",
3513        ]
3514      } else {
3515        args += [
3516          "--test-apk",
3517          "@FileArg($_rebased_apk_build_config:deps_info:apk_path)",
3518        ]
3519      }
3520      if (defined(invoker.proguard_mapping_path) && !_incremental_apk) {
3521        args += [
3522          "--proguard-mapping-path",
3523          rebase_path(invoker.proguard_mapping_path, root_build_dir),
3524        ]
3525      }
3526      if (defined(invoker.apk_under_test)) {
3527        if (_incremental_apk) {
3528          deps += [ "${invoker.apk_under_test}$build_config_target_suffix" ]
3529          _apk_under_test_build_config =
3530              get_label_info(invoker.apk_under_test, "target_gen_dir") + "/" +
3531              get_label_info(invoker.apk_under_test, "name") +
3532              ".build_config.json"
3533          _rebased_apk_under_test_build_config =
3534              rebase_path(_apk_under_test_build_config, root_build_dir)
3535          _apk_under_test = "@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_apk_path)"
3536        } else {
3537          deps += [ ":${_install_artifacts_target_name}" ]
3538          _rebased_install_artifacts_json =
3539              rebase_path(_install_artifacts_json, root_build_dir)
3540          _apk_under_test = "@FileArg($_rebased_install_artifacts_json[])"
3541        }
3542        args += [
3543          "--additional-apk",
3544          _apk_under_test,
3545        ]
3546      }
3547      if (defined(invoker.additional_apks)) {
3548        foreach(additional_apk, invoker.additional_apks) {
3549          deps += [ "$additional_apk$build_config_target_suffix" ]
3550          _build_config =
3551              get_label_info(additional_apk, "target_gen_dir") + "/" +
3552              get_label_info(additional_apk, "name") + ".build_config.json"
3553          _rebased_build_config = rebase_path(_build_config, root_build_dir)
3554          args += [
3555            "--additional-apk",
3556            "@FileArg($_rebased_build_config:deps_info:apk_path)",
3557          ]
3558        }
3559        deps += invoker.additional_apks
3560      }
3561    }
3562    test_runner_script(target_name) {
3563      forward_variables_from(invoker,
3564                             [
3565                               "additional_apks",
3566                               "additional_locales",
3567                               "apk_under_test",
3568                               "data",
3569                               "data_deps",
3570                               "deps",
3571                               "extra_args",
3572                               "fake_modules",
3573                               "ignore_all_data_deps",
3574                               "is_unit_test",
3575                               "modules",
3576                               "proguard_mapping_path",
3577                               "use_webview_provider",
3578                             ])
3579      test_name = invoker.target_name
3580      test_type = "instrumentation"
3581      apk_target = invoker.android_test_apk
3582      incremental_apk = _incremental_apk
3583
3584      public_deps = [
3585        ":$_apk_operations_target_name",
3586        apk_target,
3587      ]
3588      if (defined(invoker.apk_under_test)) {
3589        public_deps += [ invoker.apk_under_test ]
3590      }
3591      if (defined(invoker.additional_apks)) {
3592        public_deps += invoker.additional_apks
3593      }
3594    }
3595  }
3596
3597  # Declare an Android instrumentation test apk
3598  #
3599  # This target creates an Android instrumentation test apk.
3600  #
3601  # Supports all variables of android_apk(), plus:
3602  #   apk_under_test: The apk being tested (optional).
3603  #
3604  # Example
3605  #   android_test_apk("foo_test_apk") {
3606  #     android_manifest = "AndroidManifest.xml"
3607  #     apk_name = "FooTest"
3608  #     apk_under_test = "Foo"
3609  #     sources = [
3610  #       "android/org/chromium/foo/FooTestCase.java",
3611  #       "android/org/chromium/foo/FooExampleTest.java",
3612  #     ]
3613  #     deps = [
3614  #       ":foo_test_support_java"
3615  #     ]
3616  #   }
3617  template("android_test_apk") {
3618    android_apk(target_name) {
3619      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
3620      testonly = true
3621
3622      # The size info enables the test_runner to find the source file location
3623      # of a test after it is ran.
3624      include_size_info = true
3625      data = [ "$root_build_dir/size-info/${invoker.apk_name}.apk.jar.info" ]
3626      if (defined(invoker.data)) {
3627        data += invoker.data
3628      }
3629
3630      deps = [ "//testing/android/broker:broker_java" ]
3631      if (defined(invoker.deps)) {
3632        deps += invoker.deps
3633      }
3634      data_deps = [
3635        # Ensure unstripped libraries are included in runtime deps so that
3636        # symbolization can be done.
3637        ":${target_name}__secondary_abi_shared_library_list",
3638        ":${target_name}__shared_library_list",
3639      ]
3640      if (defined(invoker.data_deps)) {
3641        data_deps += invoker.data_deps
3642      }
3643      if (defined(invoker.apk_under_test)) {
3644        data_deps += [ invoker.apk_under_test ]
3645      }
3646
3647      if (defined(invoker.apk_under_test)) {
3648        _under_test_label =
3649            get_label_info(invoker.apk_under_test, "label_no_toolchain")
3650        data_deps += [
3651          "${_under_test_label}__secondary_abi_shared_library_list",
3652          "${_under_test_label}__shared_library_list",
3653        ]
3654      }
3655
3656      if (defined(invoker.additional_apks)) {
3657        data_deps += invoker.additional_apks
3658      }
3659      if (defined(invoker.use_webview_provider)) {
3660        data_deps += [ invoker.use_webview_provider ]
3661      }
3662
3663      if (defined(invoker.proguard_enabled) && invoker.proguard_enabled &&
3664          !incremental_install) {
3665        # When ProGuard is on, we use ProGuard to combine the under test java
3666        # code and the test java code. This is to allow us to apply all ProGuard
3667        # optimizations that we ship with, but not have them break tests. The
3668        # apk under test will still have the same resources, assets, and
3669        # manifest, all of which are the ones used in the tests.
3670        proguard_configs = [
3671          "//testing/android/proguard_for_test.flags",
3672          "//third_party/jni_zero/proguard_for_test.flags",
3673        ]
3674        if (defined(invoker.proguard_configs)) {
3675          proguard_configs += invoker.proguard_configs
3676        }
3677        enable_proguard_checks = false
3678        if (defined(invoker.final_apk_path)) {
3679          _final_apk_path = final_apk_path
3680        } else {
3681          _final_apk_path = "$root_build_dir/apks/${invoker.apk_name}.apk"
3682        }
3683        data += [ "$_final_apk_path.mapping" ]
3684      }
3685
3686      create_apk_script = false
3687
3688      forward_variables_from(invoker,
3689                             "*",
3690                             TESTONLY_AND_VISIBILITY + [
3691                                   "data",
3692                                   "data_deps",
3693                                   "deps",
3694                                   "extra_args",
3695                                   "is_unit_test",
3696                                   "proguard_configs",
3697                                 ])
3698    }
3699  }
3700
3701  # Declare an Android instrumentation test apk with wrapper script.
3702  #
3703  # This target creates an Android instrumentation test apk with wrapper script
3704  # to run the test.
3705  #
3706  # Supports all variables of android_test_apk.
3707  template("instrumentation_test_apk") {
3708    assert(defined(invoker.apk_name))
3709    _apk_target_name = "${target_name}__test_apk"
3710    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
3711    android_test_apk(_apk_target_name) {
3712      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
3713    }
3714    instrumentation_test_runner(target_name) {
3715      forward_variables_from(invoker,
3716                             [
3717                               "additional_apks",
3718                               "apk_under_test",
3719                               "data",
3720                               "data_deps",
3721                               "deps",
3722                               "extra_args",
3723                               "ignore_all_data_deps",
3724                               "is_unit_test",
3725                               "modules",
3726                               "never_incremental",
3727                               "public_deps",
3728                               "use_webview_provider",
3729                             ])
3730      android_test_apk = ":${_apk_target_name}"
3731      if (defined(invoker.proguard_enabled) && invoker.proguard_enabled) {
3732        proguard_mapping_path =
3733            "$root_build_dir/apks/${invoker.apk_name}.apk.mapping"
3734      }
3735    }
3736  }
3737
3738  # Declare an Android gtest apk
3739  #
3740  # This target creates an Android apk for running gtest-based unittests.
3741  #
3742  # Variables
3743  #   deps: Specifies the dependencies of this target. These will be passed to
3744  #     the underlying android_apk invocation and should include the java and
3745  #     resource dependencies of the apk.
3746  #   shared_library: shared_library target that contains the unit tests.
3747  #   apk_name: The name of the produced apk. If unspecified, it uses the name
3748  #             of the shared_library target suffixed with "_apk".
3749  #   use_default_launcher: Whether the default activity (NativeUnitTestActivity)
3750  #     should be used for launching tests.
3751  #   allow_cleartext_traffic: (Optional) Whether to allow cleartext network
3752  #     requests during the test.
3753  #   use_native_activity: Test implements ANativeActivity_onCreate().
3754  #
3755  # Example
3756  #   unittest_apk("foo_unittests_apk") {
3757  #     deps = [ ":foo_java", ":foo_resources" ]
3758  #     shared_library = ":foo_unittests"
3759  #   }
3760  template("unittest_apk") {
3761    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
3762    _use_native_activity =
3763        defined(invoker.use_native_activity) && invoker.use_native_activity
3764    _android_manifest = "$target_gen_dir/$target_name/AndroidManifest.xml"
3765    assert(invoker.shared_library != "")
3766
3767    # This trivial assert is needed in case android_manifest is defined,
3768    # as otherwise _use_native_activity and _android_manifest would not be used.
3769    assert(_use_native_activity != "" && _android_manifest != "")
3770
3771    if (!defined(invoker.android_manifest)) {
3772      _allow_cleartext_traffic = defined(invoker.allow_cleartext_traffic) &&
3773                                 invoker.allow_cleartext_traffic
3774      jinja_template("${target_name}_manifest") {
3775        _native_library_name = get_label_info(invoker.shared_library, "name")
3776        if (defined(invoker.android_manifest_template)) {
3777          input = invoker.android_manifest_template
3778        } else {
3779          input =
3780              "//testing/android/native_test/java/AndroidManifest.xml.jinja2"
3781        }
3782        output = _android_manifest
3783        variables = [
3784          "is_component_build=${is_component_build}",
3785          "native_library_name=${_native_library_name}",
3786          "use_native_activity=${_use_native_activity}",
3787          "allow_cleartext_traffic=${_allow_cleartext_traffic}",
3788        ]
3789      }
3790    }
3791
3792    android_apk(target_name) {
3793      data_deps = []
3794      forward_variables_from(invoker, "*", TESTONLY_AND_VISIBILITY)
3795      testonly = true
3796      create_apk_script = false
3797
3798      # TODO(crbug.com/1099849): Figure out why angle tests fail to launch
3799      #     with newer target_sdk_version.
3800      if (!defined(invoker.target_sdk_version) && _use_native_activity) {
3801        target_sdk_version = 24
3802      }
3803
3804      assert(!defined(invoker.proguard_enabled) || !invoker.proguard_enabled ||
3805             invoker.proguard_configs != [])
3806
3807      if (!defined(apk_name)) {
3808        apk_name = get_label_info(invoker.shared_library, "name")
3809      }
3810
3811      if (!defined(android_manifest)) {
3812        android_manifest_dep = ":${target_name}_manifest"
3813        android_manifest = _android_manifest
3814      }
3815
3816      final_apk_path = "$root_build_dir/${apk_name}_apk/${apk_name}-debug.apk"
3817
3818      if (!defined(use_default_launcher) || use_default_launcher) {
3819        deps += [
3820          "//build/android/gtest_apk:native_test_instrumentation_test_runner_java",
3821          "//testing/android/native_test:native_test_java",
3822        ]
3823      }
3824      shared_libraries = [ invoker.shared_library ]
3825      deps += [
3826        ":${target_name}__secondary_abi_shared_library_list",
3827        ":${target_name}__shared_library_list",
3828      ]
3829    }
3830  }
3831
3832  # Generate .java files from .aidl files.
3833  #
3834  # This target will store the .java files in a srcjar and should be included in
3835  # an android_library or android_apk's srcjar_deps.
3836  #
3837  # Variables
3838  #   sources: Paths to .aidl files to compile.
3839  #   import_include: Path to directory containing .java files imported by the
3840  #     .aidl files.
3841  #   interface_file: Preprocessed aidl file to import.
3842  #
3843  # Example
3844  #   android_aidl("foo_aidl") {
3845  #     import_include = "java/src"
3846  #     sources = [
3847  #       "java/src/com/foo/bar/FooBarService.aidl",
3848  #       "java/src/com/foo/bar/FooBarServiceCallback.aidl",
3849  #     ]
3850  #   }
3851  template("android_aidl") {
3852    action_with_pydeps(target_name) {
3853      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
3854
3855      script = "//build/android/gyp/aidl.py"
3856      depfile = "$target_gen_dir/$target_name.d"
3857      sources = invoker.sources
3858
3859      _srcjar_path = "${target_gen_dir}/${target_name}.srcjar"
3860      _aidl_path = "${android_sdk_build_tools}/aidl"
3861      _framework_aidl = "$android_sdk/framework.aidl"
3862      _imports = [ _framework_aidl ]
3863      if (defined(invoker.interface_file)) {
3864        assert(invoker.interface_file != "")
3865        _imports += [ invoker.interface_file ]
3866      }
3867
3868      inputs = [ _aidl_path ] + _imports
3869
3870      outputs = [ _srcjar_path ]
3871      _rebased_imports = rebase_path(_imports, root_build_dir)
3872      args = [
3873        "--aidl-path",
3874        rebase_path(_aidl_path, root_build_dir),
3875        "--imports=$_rebased_imports",
3876        "--srcjar",
3877        rebase_path(_srcjar_path, root_build_dir),
3878        "--depfile",
3879        rebase_path(depfile, root_build_dir),
3880      ]
3881      if (defined(invoker.import_include) && invoker.import_include != []) {
3882        _rebased_import_paths = []
3883        foreach(_import_path, invoker.import_include) {
3884          _rebased_import_path = []
3885          _rebased_import_path = [ rebase_path(_import_path, root_build_dir) ]
3886          _rebased_import_paths += _rebased_import_path
3887        }
3888        args += [ "--includes=$_rebased_import_paths" ]
3889      }
3890      args += rebase_path(sources, root_build_dir)
3891    }
3892  }
3893
3894  # Compile a protocol buffer to java.
3895  #
3896  # This generates java files from protocol buffers and creates an Android library
3897  # containing the classes.
3898  #
3899  # Variables
3900  #   sources (required)
3901  #       Paths to .proto files to compile.
3902  #
3903  #   proto_path (required)
3904  #       Root directory of .proto files.
3905  #
3906  #   deps (optional)
3907  #       Additional dependencies. Passed through to both the action and the
3908  #       android_library targets.
3909  #
3910  #   import_dirs (optional)
3911  #       A list of extra import directories to be passed to protoc compiler.
3912  #       WARNING: This circumvents proto checkdeps, and should only be used
3913  #       when needed, typically when proto files cannot cleanly import through
3914  #       absolute paths, such as for third_party or generated .proto files.
3915  #       http://crbug.com/691451 tracks fixing this.
3916  #
3917  #   generator_plugin_label (optional)
3918  #       GN label for plugin executable which generates custom cc stubs.
3919  #       Don't specify a toolchain, host toolchain is assumed.
3920  #
3921  # Example:
3922  #  proto_java_library("foo_proto_java") {
3923  #    proto_path = "src/foo"
3924  #    sources = [ "$proto_path/foo.proto" ]
3925  #  }
3926  template("proto_java_library") {
3927    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
3928
3929    _template_name = target_name
3930
3931    action_with_pydeps("${_template_name}__protoc_java") {
3932      # The suffix "__protoc_java.srcjar" is used by SuperSize to identify
3933      # protobuf symbols.
3934      _srcjar_path = "$target_gen_dir/$target_name.srcjar"
3935      script = "//build/protoc_java.py"
3936
3937      if (defined(invoker.deps)) {
3938        # Need to care only about targets that might generate .proto files.
3939        # No need to depend on java_library or android_resource targets.
3940        deps = filter_exclude(invoker.deps, java_target_patterns)
3941      }
3942
3943      sources = invoker.sources
3944      depfile = "$target_gen_dir/$target_name.d"
3945      outputs = [ _srcjar_path ]
3946      args = [
3947        "--depfile",
3948        rebase_path(depfile, root_build_dir),
3949        "--protoc",
3950        rebase_path(android_protoc_bin, root_build_dir),
3951        "--proto-path",
3952        rebase_path(invoker.proto_path, root_build_dir),
3953        "--srcjar",
3954        rebase_path(_srcjar_path, root_build_dir),
3955      ]
3956
3957      if (defined(invoker.generator_plugin_label)) {
3958        if (host_os == "win") {
3959          _host_executable_suffix = ".exe"
3960        } else {
3961          _host_executable_suffix = ""
3962        }
3963
3964        _plugin_host_label =
3965            invoker.generator_plugin_label + "($host_toolchain)"
3966        _plugin_path =
3967            get_label_info(_plugin_host_label, "root_out_dir") + "/" +
3968            get_label_info(_plugin_host_label, "name") + _host_executable_suffix
3969        args += [
3970          "--plugin",
3971          rebase_path(_plugin_path, root_build_dir),
3972        ]
3973        deps += [ _plugin_host_label ]
3974        inputs = [ _plugin_path ]
3975      }
3976
3977      args += rebase_path(sources, root_build_dir)
3978
3979      if (defined(invoker.import_dirs)) {
3980        foreach(_import_dir, invoker.import_dirs) {
3981          args += [
3982            "--import-dir",
3983            rebase_path(_import_dir, root_build_dir),
3984          ]
3985        }
3986      }
3987    }
3988
3989    android_library(target_name) {
3990      chromium_code = false
3991      sources = []
3992      srcjar_deps = [ ":${_template_name}__protoc_java" ]
3993      deps = [ "//third_party/android_deps:protobuf_lite_runtime_java" ]
3994      if (defined(invoker.deps)) {
3995        deps += invoker.deps
3996      }
3997    }
3998  }
3999
4000  # Compile a flatbuffer to java.
4001  #
4002  # This generates java files from flat buffers and creates an Android library
4003  # containing the classes.
4004  #
4005  # Variables
4006  #   sources (required)
4007  #       Paths to .fbs files to compile.
4008  #
4009  #   root_dir (required)
4010  #       Root directory of .fbs files.
4011  #
4012  #   deps (optional)
4013  #       Additional dependencies. Passed through to both the action and the
4014  #       android_library targets.
4015  #
4016  #   flatc_include_dirs (optional)
4017  #       A list of extra import directories to be passed to flatc compiler.
4018  #
4019  #
4020  # Example:
4021  #  flatbuffer_java_library("foo_flatbuffer_java") {
4022  #    root_dir = "src/foo"
4023  #    sources = [ "$proto_path/foo.fbs" ]
4024  #  }
4025  template("flatbuffer_java_library") {
4026    forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
4027
4028    _template_name = target_name
4029    _flatc_dep = "//third_party/flatbuffers:flatc($host_toolchain)"
4030    _flatc_out_dir = get_label_info(_flatc_dep, "root_out_dir")
4031    _flatc_bin = "$_flatc_out_dir/flatc"
4032
4033    action_with_pydeps("${_template_name}__flatc_java") {
4034      _srcjar_path = "$target_gen_dir/$target_name.srcjar"
4035      script = "//build/android/gyp/flatc_java.py"
4036
4037      deps = [ _flatc_dep ]
4038      if (defined(invoker.deps)) {
4039        deps += invoker.deps
4040      }
4041      inputs = [ _flatc_bin ]
4042
4043      sources = invoker.sources
4044      outputs = [ _srcjar_path ]
4045      args = [
4046               "--flatc",
4047               rebase_path(_flatc_bin, root_build_dir),
4048               "--import-dir",
4049               rebase_path(invoker.root_dir, root_build_dir),
4050               "--srcjar",
4051               rebase_path(_srcjar_path, root_build_dir),
4052             ] + rebase_path(sources, root_build_dir)
4053
4054      if (defined(invoker.flatc_include_dirs)) {
4055        foreach(_include_dir, invoker.flatc_include_dirs) {
4056          args += [
4057            "--import-dir",
4058            rebase_path(_include_dir, root_build_dir),
4059          ]
4060        }
4061      }
4062    }
4063
4064    android_library(target_name) {
4065      chromium_code = false
4066      sources = []
4067      srcjar_deps = [ ":${_template_name}__flatc_java" ]
4068      deps = [ "//third_party/flatbuffers:flatbuffers_java" ]
4069      if (defined(invoker.deps)) {
4070        deps += invoker.deps
4071      }
4072    }
4073  }
4074
4075  # Declare an Android library target for a prebuilt AAR.
4076  #
4077  # This target creates an Android library containing java code and Android
4078  # resources. For libraries without resources, it will not generate
4079  # corresponding android_resources targets.
4080  #
4081  # To avoid slowing down "gn gen", an associated .info file must be committed
4082  # along with the .aar file. In order to create this file, define the target
4083  # and then run once with the gn arg "update_android_aar_prebuilts = true".
4084  #
4085  # Variables
4086  #   aar_path: Path to the AAR.
4087  #   info_path: Path to the .aar.info file (generated via
4088  #       update_android_aar_prebuilts GN arg).
4089  #   proguard_configs: List of proguard configs to use in final apk step for
4090  #       any apk that depends on this library.
4091  #   ignore_aidl: Whether to ignore .aidl files found with the .aar.
4092  #   ignore_assets: Whether to ignore assets found in the .aar.
4093  #   ignore_manifest: Whether to ignore creating manifest.
4094  #   ignore_native_libraries: Whether to ignore .so files found in the .aar.
4095  #       See also extract_native_libraries.
4096  #   ignore_proguard_configs: Whether to ignore proguard configs.
4097  #   strip_resources: Whether to ignore android resources found in the .aar.
4098  #   custom_package: Java package for generated R.java files.
4099  #   extract_native_libraries: Whether to extract .so files found in the .aar.
4100  #       If the file contains .so, either extract_native_libraries or
4101  #       ignore_native_libraries must be set.
4102  #   TODO(jbudorick@): remove this arguments after crbug.com/522043 is fixed.
4103  #   requires_android: Whether this target can only be used for compiling
4104  #       Android related targets.
4105  #
4106  # Example
4107  #   android_aar_prebuilt("foo_java") {
4108  #     aar_path = "foo.aar"
4109  #   }
4110  template("android_aar_prebuilt") {
4111    _info_path = "$target_name.info"
4112    if (defined(invoker.info_path)) {
4113      _info_path = invoker.info_path
4114    }
4115    _output_path = "${target_out_dir}/${target_name}"
4116
4117    # Some targets only differ by _java with other targets so _java and _junit
4118    # need to be replaced by non-empty strings to avoid duplicate targets. (e.g.
4119    # androidx_window_window_java vs androidx_window_window_java_java).
4120    _target_name_without_java_or_junit =
4121        string_replace(string_replace(target_name, "_java", "_J"),
4122                       "_junit",
4123                       "_U")
4124
4125    # This unpack target is a python action, not a valid java target. Since the
4126    # java targets below depend on it, its name must not match the java patterns
4127    # in internal_rules.gni.
4128    _unpack_target_name = "${_target_name_without_java_or_junit}__unpack_aar"
4129    _ignore_aidl = defined(invoker.ignore_aidl) && invoker.ignore_aidl
4130    _ignore_assets = defined(invoker.ignore_assets) && invoker.ignore_assets
4131    _ignore_manifest =
4132        defined(invoker.ignore_manifest) && invoker.ignore_manifest
4133    _ignore_native_libraries = defined(invoker.ignore_native_libraries) &&
4134                               invoker.ignore_native_libraries
4135    _ignore_proguard_configs = defined(invoker.ignore_proguard_configs) &&
4136                               invoker.ignore_proguard_configs
4137    _extract_native_libraries = defined(invoker.extract_native_libraries) &&
4138                                invoker.extract_native_libraries
4139    _strip_resources =
4140        defined(invoker.strip_resources) && invoker.strip_resources
4141
4142    # Allow 'resource_overlay' parameter even if there are no resources in order
4143    # to keep the logic for generated 'android_aar_prebuilt' rules simple.
4144    not_needed(invoker, [ "resource_overlay" ])
4145
4146    _aar_common_args = [ rebase_path(invoker.aar_path, root_build_dir) ]
4147    if (_strip_resources) {
4148      _aar_common_args += [ "--ignore-resources" ]
4149    }
4150    if (defined(invoker.resource_exclusion_globs)) {
4151      _aar_common_args +=
4152          [ "--resource-exclusion-globs=${invoker.resource_exclusion_globs}" ]
4153    }
4154
4155    # Scan the AAR file and determine the resources and jar files.
4156    # Some libraries might not have resources; others might have two jars.
4157    if (update_android_aar_prebuilts) {
4158      print("Writing " + rebase_path(_info_path, "//"))
4159      exec_script("//build/android/gyp/aar.py",
4160                  [
4161                        "list",
4162                        "--output",
4163                        rebase_path(_info_path, root_build_dir),
4164                      ] + _aar_common_args)
4165    }
4166
4167    # If "gn gen" is failing on the following line, you need to generate an
4168    # .info file for your new target by running:
4169    #   gn gen --args='target_os="android" update_android_aar_prebuilts=true' out/tmp
4170    #   rm -r out/tmp
4171    _scanned_files = read_file(_info_path, "scope")
4172
4173    _use_scanned_assets = !_ignore_assets && _scanned_files.assets != []
4174    _has_resources = _scanned_files.resources != []
4175    _common_deps = [ ":$_unpack_target_name" ]
4176    if (defined(invoker.deps)) {
4177      _common_deps += invoker.deps
4178    }
4179    if (defined(invoker.public_deps)) {
4180      _common_deps += invoker.public_deps
4181    }
4182
4183    assert(_ignore_aidl || _scanned_files.aidl == [],
4184           "android_aar_prebuilt() aidl not yet supported." +
4185               " Implement or use ignore_aidl = true." +
4186               " http://crbug.com/644439")
4187    assert(
4188        !_scanned_files.has_native_libraries ||
4189            (_ignore_native_libraries || _extract_native_libraries),
4190        "android_aar_prebuilt() contains .so files." +
4191            " Please set ignore_native_libraries or extract_native_libraries.")
4192    assert(
4193        !(_ignore_native_libraries && _extract_native_libraries),
4194        "ignore_native_libraries and extract_native_libraries cannot both be set.")
4195    assert(!_scanned_files.has_native_libraries ||
4196           _scanned_files.native_libraries != [])
4197    assert(_scanned_files.has_classes_jar || _scanned_files.subjars == [])
4198
4199    action_with_pydeps(_unpack_target_name) {
4200      script = "//build/android/gyp/aar.py"  # Unzips the AAR
4201      args = [
4202               "extract",
4203               "--output-dir",
4204               rebase_path(_output_path, root_build_dir),
4205               "--assert-info-file",
4206               rebase_path(_info_path, root_build_dir),
4207             ] + _aar_common_args
4208      inputs = [ invoker.aar_path ]
4209      outputs = [ "${_output_path}/AndroidManifest.xml" ]
4210      outputs +=
4211          get_path_info(rebase_path(_scanned_files.resources, "", _output_path),
4212                        "abspath")
4213      if (_scanned_files.has_r_text_file) {
4214        # Certain packages, in particular Play Services have no R.txt even
4215        # though its presence is mandated by AAR spec. Such packages cause
4216        # spurious rebuilds if this output is specified unconditionally.
4217        outputs += [ "${_output_path}/R.txt" ]
4218      }
4219
4220      if (_scanned_files.has_classes_jar) {
4221        outputs += [ "${_output_path}/classes.jar" ]
4222      }
4223      outputs +=
4224          get_path_info(rebase_path(_scanned_files.subjars, "", _output_path),
4225                        "abspath")
4226      if (!_ignore_proguard_configs) {
4227        if (_scanned_files.has_proguard_flags) {
4228          outputs += [ "${_output_path}/proguard.txt" ]
4229        }
4230      }
4231
4232      if (_extract_native_libraries && _scanned_files.has_native_libraries) {
4233        outputs += get_path_info(
4234                rebase_path(_scanned_files.native_libraries, "", _output_path),
4235                "abspath")
4236      }
4237      if (_use_scanned_assets) {
4238        outputs +=
4239            get_path_info(rebase_path(_scanned_files.assets, "", _output_path),
4240                          "abspath")
4241      }
4242    }
4243
4244    _should_process_manifest =
4245        !_ignore_manifest && !_scanned_files.is_manifest_empty
4246
4247    # Create the android_resources target for resources.
4248    if (_has_resources || _should_process_manifest) {
4249      _res_target_name = "${target_name}__resources"
4250      android_resources(_res_target_name) {
4251        forward_variables_from(invoker,
4252                               [
4253                                 "custom_package",
4254                                 "resource_overlay",
4255                                 "testonly",
4256                                 "strip_drawables",
4257                               ])
4258        deps = _common_deps
4259        if (_should_process_manifest) {
4260          android_manifest_dep = ":$_unpack_target_name"
4261          android_manifest = "${_output_path}/AndroidManifest.xml"
4262        } else if (defined(_scanned_files.manifest_package) &&
4263                   !defined(custom_package)) {
4264          custom_package = _scanned_files.manifest_package
4265        }
4266
4267        sources = rebase_path(_scanned_files.resources, "", _output_path)
4268        if (_scanned_files.has_r_text_file) {
4269          r_text_file = "${_output_path}/R.txt"
4270        }
4271      }
4272    } else if (defined(invoker.strip_drawables)) {
4273      not_needed(invoker, [ "strip_drawables" ])
4274    }
4275
4276    if (_ignore_manifest) {
4277      # Having this available can be useful for DFMs that depend on AARs. It
4278      # provides a way to have manifest entries go into the base split while
4279      # the code goes into a DFM.
4280      java_group("${target_name}__ignored_manifest") {
4281        forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
4282        deps = [ ":$_unpack_target_name" ]
4283        mergeable_android_manifests = [ "${_output_path}/AndroidManifest.xml" ]
4284      }
4285    }
4286
4287    # Create the android_assets target for assets
4288    if (_use_scanned_assets) {
4289      _assets_target_name = "${target_name}__assets"
4290      android_assets(_assets_target_name) {
4291        forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
4292        deps = [ ":$_unpack_target_name" ]
4293        renaming_sources = []
4294        renaming_destinations = []
4295        foreach(_asset_file, _scanned_files.assets) {
4296          _original_path =
4297              get_path_info(rebase_path(_asset_file, "", _output_path),
4298                            "abspath")
4299          _updated_path = string_replace(_asset_file, "assets/", "", 1)
4300          renaming_sources += [ _original_path ]
4301          renaming_destinations += [ _updated_path ]
4302        }
4303      }
4304    }
4305
4306    _target_label = get_label_info(":$target_name", "label_no_toolchain")
4307
4308    # Create android_java_prebuilt target for classes.jar.
4309    if (_scanned_files.has_classes_jar) {
4310      _java_library_vars = [
4311        "alternative_android_sdk_dep",
4312        "bytecode_rewriter_target",
4313        "enable_bytecode_checks",
4314        "jar_excluded_patterns",
4315        "jar_included_patterns",
4316        "missing_classes_allowlist",
4317        "requires_android",
4318        "testonly",
4319      ]
4320
4321      # Create android_java_prebuilt target for extra jars within jars/.
4322      _subjar_targets = []
4323      foreach(_tuple, _scanned_files.subjar_tuples) {
4324        _current_target = "${target_name}__subjar_${_tuple[0]}"
4325        _subjar_targets += [ ":$_current_target" ]
4326        java_prebuilt(_current_target) {
4327          forward_variables_from(invoker, _java_library_vars)
4328          deps = _common_deps
4329          if (!defined(requires_android)) {
4330            requires_android = true
4331          }
4332          supports_android = true
4333          jar_path = "$_output_path/${_tuple[1]}"
4334          _base_output_name = get_path_info(jar_path, "name")
4335          output_name = "${invoker.target_name}-$_base_output_name"
4336          public_target_label = _target_label
4337        }
4338      }
4339
4340      _jar_target_name = "${target_name}__classes"
4341      java_prebuilt(_jar_target_name) {
4342        forward_variables_from(invoker, _java_library_vars)
4343        forward_variables_from(invoker,
4344                               [
4345                                 "input_jars_paths",
4346                                 "mergeable_android_manifests",
4347                                 "proguard_configs",
4348                               ])
4349        deps = _common_deps + _subjar_targets
4350        if (defined(_res_target_name)) {
4351          deps += [ ":$_res_target_name" ]
4352        }
4353        if (!defined(requires_android)) {
4354          requires_android = true
4355        }
4356        supports_android = true
4357        jar_path = "$_output_path/classes.jar"
4358        aar_path = invoker.aar_path
4359        output_name = invoker.target_name
4360
4361        if (!_ignore_proguard_configs) {
4362          if (!defined(proguard_configs)) {
4363            proguard_configs = []
4364          }
4365          if (_scanned_files.has_proguard_flags) {
4366            proguard_configs += [ "$_output_path/proguard.txt" ]
4367          }
4368        }
4369        public_target_label = _target_label
4370      }
4371    }
4372
4373    java_group(target_name) {
4374      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
4375      public_deps = [ ":$_unpack_target_name" ]
4376      if (defined(invoker.public_deps)) {
4377        public_deps += invoker.public_deps
4378      }
4379      deps = []
4380      if (defined(_jar_target_name)) {
4381        deps += [ ":$_jar_target_name" ]
4382
4383        # Although subjars are meant to be private, we add them as deps here
4384        # because in practice they seem to contain classes required to be in the
4385        # classpath.
4386        deps += _subjar_targets
4387      }
4388      if (defined(_res_target_name)) {
4389        deps += [ ":$_res_target_name" ]
4390      }
4391      if (defined(_assets_target_name)) {
4392        deps += [ ":$_assets_target_name" ]
4393      }
4394    }
4395  }
4396
4397  # Create an Android application bundle from one base android_apk target,
4398  # and zero or more associated android_apk.
4399  #
4400  # Variables:
4401  #    base_module_target: Name of the android_app_bundle_module target
4402  #      corresponding to the base module for this application bundle. The
4403  #      bundle file will include the same content in its base module, though in
4404  #      a slightly different format.
4405  #
4406  #    bundle_base_path: Optional. If set, the bundle will be output to this
4407  #      directory. Defaults to "$root_build_dir/apks".
4408  #
4409  #    bundle_name: Optional. If set, the bundle will be output to the
4410  #      filename "${bundle_name}.aab".
4411  #
4412  #    extra_modules: Optional list of scopes, one per extra module used by
4413  #      this bundle. Each scope must have a 'name' field that specifies the
4414  #      module name (which cannot be 'base', since this is reserved for the
4415  #      base module), and an 'apk_target' field that specified the
4416  #      corresponding android_apk target name the module is modeled on.
4417  #
4418  #    enable_language_splits: Optional. If true, enable APK splits based
4419  #      on languages.
4420  #
4421  #    keystore_path: optional keystore path, used only when generating APKs.
4422  #    keystore_name: optional keystore name, used only when generating APKs.
4423  #    keystore_password: optional keystore password, used only when
4424  #      generating APKs.
4425  #    rotation_config: optional .textproto to enable key rotation.
4426  #
4427  #    command_line_flags_file: Optional. If provided, named of the on-device
4428  #      file that will be used to store command-line arguments. The default
4429  #      is 'command_line_flags_file', but this is typically redefined to
4430  #      something more specific for certain bundles (e.g. the Chromium based
4431  #      APKs use 'chrome-command-line', the WebView one uses
4432  #      'webview-command-line').
4433  #
4434  #    proguard_enabled: Optional. True if proguarding is enabled for this
4435  #      bundle. Default is to enable this only for release builds. Note that
4436  #      this will always perform synchronized proguarding.
4437  #
4438  #    proguard_enable_obfuscation: Whether to enable obfuscation (default=true)
4439  #
4440  #    proguard_android_sdk_dep: Optional. android_system_java_prebuilt() target
4441  #      used as a library jar for synchronized proguarding.
4442  #
4443  #    system_image_locale_allowlist: List of locales that should be included
4444  #      on system APKs generated from this bundle.
4445  #
4446  #    static_library_provider: Specifies a single target that this target will
4447  #      use as a static library APK.
4448  #      Additionally, when allotting libraries to be packaged into modules, the
4449  #      libraries packaged into the static library will be accounted for to
4450  #      avoid library duplication. Effectively, the static library will be
4451  #      treated as the parent of the base module.
4452  #
4453  #    expected_libs_and_assets: Verify the list of included native libraries
4454  #      and assets is consistent with the given expectation file.
4455  #    expected_libs_and_assets_base: Treat expected_libs_and_assets as a diff
4456  #      with this file as the base.
4457  #    expected_proguard_config: Checks that the merged set of proguard flags
4458  #      matches the given config.
4459  #    expected_proguard_config_base: Treat expected_proguard_config as a diff
4460  #      with this file as the base.
4461  #
4462  #    version_code: Optional. Version code of the target.
4463  #
4464  #    is_multi_abi: If true will add a library placeholder for the missing ABI
4465  #      if either the primary or the secondary ABI has no native libraries set.
4466  #
4467  #    default_modules_for_testing: (optional): A list of DFM that the wrapper
4468  #      script should install. This is for local testing only, and does not
4469  #      affect the actual DFM in production.
4470  #
4471  #    add_view_trace_events: (optional): If true will add an additional step to
4472  #      add trace events to all Android views contained in the bundle. It also
4473  #      requires build argument enable_trace_event_bytecode_rewriting = true.
4474  #
4475  # Example:
4476  #   android_app_bundle("chrome_public_bundle") {
4477  #      base_module_target = "//chrome/android:chrome_public_apk"
4478  #      extra_modules = [
4479  #        { # NOTE: Scopes require one field per line, and no comma separators.
4480  #          name = "my_module"
4481  #          module_target = ":my_module"
4482  #        },
4483  #      ]
4484  #   }
4485  #
4486  template("android_app_bundle") {
4487    _target_name = target_name
4488    _uses_static_library = defined(invoker.static_library_provider)
4489    _proguard_enabled =
4490        defined(invoker.proguard_enabled) && invoker.proguard_enabled
4491
4492    _min_sdk_version = default_min_sdk_version
4493    if (defined(invoker.min_sdk_version)) {
4494      _min_sdk_version = invoker.min_sdk_version
4495    }
4496
4497    _bundle_base_path = "$root_build_dir/apks"
4498    if (defined(invoker.bundle_base_path)) {
4499      _bundle_base_path = invoker.bundle_base_path
4500    }
4501
4502    _bundle_name = _target_name
4503    if (defined(invoker.bundle_name)) {
4504      _bundle_name = invoker.bundle_name
4505    }
4506    _bundle_path = "$_bundle_base_path/${_bundle_name}.aab"
4507    _rebased_bundle_path = rebase_path(_bundle_path, root_build_dir)
4508
4509    _base_target_name = get_label_info(invoker.base_module_target, "name")
4510    _base_target_gen_dir =
4511        get_label_info(invoker.base_module_target, "target_gen_dir")
4512    _base_module_build_config =
4513        "$_base_target_gen_dir/${_base_target_name}.build_config.json"
4514    _base_module_build_config_target =
4515        "${invoker.base_module_target}$build_config_target_suffix"
4516    _rebased_base_module_build_config =
4517        rebase_path(_base_module_build_config, root_build_dir)
4518
4519    _modules = [
4520      {
4521        name = "base"
4522        module_target = invoker.base_module_target
4523        build_config = _base_module_build_config
4524        build_config_target = _base_module_build_config_target
4525        if (_uses_static_library) {
4526          parent = "lib"
4527        }
4528      },
4529    ]
4530
4531    if (_proguard_enabled) {
4532      _dex_target = "${_target_name}__dex"
4533      _proguard_mapping_path = "${_bundle_path}.mapping"
4534    }
4535
4536    if (defined(invoker.extra_modules)) {
4537      _module_count = 0
4538      not_needed([ "_module_count" ])
4539
4540      foreach(_module, invoker.extra_modules) {
4541        _module_count += 1
4542        assert(defined(_module.name),
4543               "Missing 'name' field for extra module #${_module_count}.")
4544        assert(_module.name != "base",
4545               "Module name 'base' is reserved for the main bundle module")
4546        assert(
4547            defined(_module.module_target),
4548            "Missing 'module_target' field for extra module ${_module.name}.")
4549        _module_target = _module.module_target
4550        _module_target_name = get_label_info(_module_target, "name")
4551        _module_target_gen_dir =
4552            get_label_info(_module_target, "target_gen_dir")
4553        _module.build_config =
4554            "$_module_target_gen_dir/${_module_target_name}.build_config.json"
4555        _module.build_config_target =
4556            "$_module_target$build_config_target_suffix"
4557        _module.parent = "base"
4558        _modules += [ _module ]
4559      }
4560    }
4561
4562    # Make build config, which is required for synchronized proguarding.
4563    _module_java_targets = []
4564    _module_build_configs = []
4565    _module_targets = []
4566    foreach(_module, _modules) {
4567      _module_targets += [ _module.module_target ]
4568      _module_java_targets += [ "${_module.module_target}__java" ]
4569      _module_build_configs += [ _module.build_config ]
4570    }
4571
4572    if (_uses_static_library) {
4573      _lib_proxy_module = {
4574        name = "lib"
4575      }
4576      _static_library_target_name =
4577          get_label_info(invoker.static_library_provider, "name")
4578      _static_library_gen_dir =
4579          get_label_info(invoker.static_library_provider, "target_gen_dir")
4580      _lib_proxy_module.build_config = "$_static_library_gen_dir/$_static_library_target_name.build_config.json"
4581      _lib_proxy_module.build_config_target =
4582          "${invoker.static_library_provider}$build_config_target_suffix"
4583    }
4584
4585    # Allot native libraries to modules they should be packaged into. This is
4586    # necessary since all libraries that are depended on by multiple modules
4587    # have to go into base or the static shared library if it exists.
4588    # TODO(crbug.com/1021565): It would be nice if this lived outside the
4589    # android_app_bundle template and the static shared library would pull in
4590    # the libs as allotted by this step.
4591    _native_libraries_config =
4592        "$target_gen_dir/$_target_name.native_libraries_config"
4593    _native_libraries_config_target = "${_target_name}__allot_native_libraries"
4594    allot_native_libraries(_native_libraries_config_target) {
4595      modules = _modules
4596      native_libraries_filearg_keys = [
4597        "native:libraries",
4598        "native:loadable_modules",
4599      ]
4600      output = _native_libraries_config
4601      if (_uses_static_library) {
4602        modules += [ _lib_proxy_module ]
4603      }
4604    }
4605    if (defined(android_app_secondary_abi)) {
4606      _secondary_abi_native_libraries_config =
4607          "$target_gen_dir/$_target_name.secondary_abi_native_libraries_config"
4608      _secondary_abi_native_libraries_config_target =
4609          "${_target_name}__allot_secondary_abi_native_libraries"
4610      allot_native_libraries(_secondary_abi_native_libraries_config_target) {
4611        modules = _modules
4612        native_libraries_filearg_keys = [
4613          "native:secondary_abi_libraries",
4614          "native:secondary_abi_loadable_modules",
4615        ]
4616        output = _secondary_abi_native_libraries_config
4617        if (_uses_static_library) {
4618          modules += [ _lib_proxy_module ]
4619        }
4620      }
4621    }
4622
4623    # Used to expose the module Java targets of the bundle.
4624    group("${_target_name}__java") {
4625      deps = _module_java_targets
4626    }
4627    group("${_target_name}__compile_resources") {
4628      deps = [ "${invoker.base_module_target}__compile_resources" ]
4629    }
4630
4631    _build_config = "$target_gen_dir/${_target_name}.build_config.json"
4632    _rebased_build_config = rebase_path(_build_config, root_build_dir)
4633    _build_config_target = "$_target_name$build_config_target_suffix"
4634    if (defined(invoker.proguard_android_sdk_dep)) {
4635      _android_sdk_dep = invoker.proguard_android_sdk_dep
4636    } else {
4637      _android_sdk_dep = default_android_sdk_dep
4638    }
4639
4640    if (_proguard_enabled) {
4641      _proguard_mapping_path = "${_bundle_path}.mapping"
4642      _add_view_trace_events =
4643          defined(invoker.add_view_trace_events) &&
4644          invoker.add_view_trace_events && enable_trace_event_bytecode_rewriting
4645    } else {
4646      not_needed(invoker, [ "add_view_trace_events" ])
4647    }
4648
4649    write_build_config(_build_config_target) {
4650      type = "android_app_bundle"
4651      possible_config_deps = _module_targets + [ _android_sdk_dep ]
4652      build_config = _build_config
4653      proguard_enabled = _proguard_enabled
4654      module_build_configs = _module_build_configs
4655      modules = _modules
4656
4657      if (_proguard_enabled) {
4658        add_view_trace_events = _add_view_trace_events
4659        proguard_mapping_path = _proguard_mapping_path
4660      }
4661    }
4662
4663    # Old name for variable, mark as not_needed while it is being renamed
4664    # downstream. Remove after all references to baseline_profile_path have been
4665    # changed.
4666    not_needed(invoker, [ "baseline_profile_path" ])
4667
4668    _enable_art_profile_optimizations =
4669        defined(invoker.art_profile_path) && _proguard_enabled
4670
4671    if (_enable_art_profile_optimizations) {
4672      _include_baseline_profile = enable_baseline_profiles
4673      _enable_startup_profile = enable_startup_profiles
4674      if (_include_baseline_profile) {
4675        _obfuscated_art_profile =
4676            "$target_out_dir/${target_name}.obfuscated.hrf"
4677      }
4678    } else {
4679      not_needed(invoker, [ "art_profile_path" ])
4680    }
4681
4682    if (_proguard_enabled) {
4683      if (_add_view_trace_events) {
4684        _trace_event_rewriter_target =
4685            "//build/android/bytecode:trace_event_adder"
4686        _rewritten_jar_target_name = "${target_name}__trace_event_rewritten"
4687        _rewriter_path = root_build_dir + "/bin/helper/trace_event_adder"
4688        _stamp = "${target_out_dir}/${target_name}.trace_event_rewrite.stamp"
4689        action_with_pydeps(_rewritten_jar_target_name) {
4690          script = "//build/android/gyp/trace_event_bytecode_rewriter.py"
4691          inputs = [
4692            java_path_for_inputs,
4693            _rewriter_path,
4694            _build_config,
4695          ]
4696          outputs = [ _stamp ]
4697          depfile = "$target_gen_dir/$_rewritten_jar_target_name.d"
4698          args = [
4699            "--stamp",
4700            rebase_path(_stamp, root_build_dir),
4701            "--depfile",
4702            rebase_path(depfile, root_build_dir),
4703            "--script",
4704            rebase_path(_rewriter_path, root_build_dir),
4705            "--classpath",
4706            "@FileArg($_rebased_build_config:android:sdk_jars)",
4707            "--input-jars",
4708            "@FileArg($_rebased_build_config:deps_info:device_classpath)",
4709            "--output-jars",
4710            "@FileArg($_rebased_build_config:deps_info:trace_event_rewritten_device_classpath)",
4711          ]
4712          deps = [
4713                   ":$_build_config_target",
4714                   _trace_event_rewriter_target,
4715                 ] + _module_java_targets
4716        }
4717      }
4718
4719      dex(_dex_target) {
4720        forward_variables_from(invoker,
4721                               [
4722                                 "custom_assertion_handler",
4723                                 "expected_proguard_config",
4724                                 "expected_proguard_config_base",
4725                                 "proguard_enable_obfuscation",
4726                                 "repackage_classes",
4727                               ])
4728        if (defined(expected_proguard_config)) {
4729          top_target_name = _target_name
4730        }
4731        min_sdk_version = _min_sdk_version
4732        add_view_trace_events = _add_view_trace_events
4733        proguard_enabled = true
4734        proguard_mapping_path = _proguard_mapping_path
4735        build_config = _build_config
4736        if (_enable_art_profile_optimizations) {
4737          input_art_profile = invoker.art_profile_path
4738          if (_include_baseline_profile) {
4739            output_art_profile = _obfuscated_art_profile
4740          }
4741          enable_startup_profile = _enable_startup_profile
4742        }
4743
4744        deps = _module_java_targets + [ ":$_build_config_target" ]
4745        if (_add_view_trace_events) {
4746          deps += [ ":${_rewritten_jar_target_name}" ]
4747        }
4748        modules = _modules
4749
4750        # Must not be set via write_build_config, because that will cause it
4751        # to be picked up by test apks that use apk_under_test.
4752        _assertions_implicitly_enabled =
4753            defined(invoker.custom_assertion_handler)
4754        if (!_assertions_implicitly_enabled && !enable_java_asserts &&
4755            (!defined(testonly) || !testonly) &&
4756            # Injected JaCoCo code causes -checkdiscards to fail.
4757            !use_jacoco_coverage) {
4758          proguard_configs = [
4759            "//build/android/dcheck_is_off.flags",
4760            "//third_party/jni_zero/checkdiscard_proguard.flags",
4761          ]
4762        }
4763      }
4764    }
4765
4766    _all_create_module_targets = []
4767    _all_module_zip_paths = []
4768    _all_module_build_configs = []
4769    _all_module_unused_resources_deps = []
4770    foreach(_module, _modules) {
4771      _module_target = _module.module_target
4772      _module_build_config = _module.build_config
4773      _module_build_config_target = _module.build_config_target
4774
4775      if (!_proguard_enabled) {
4776        _module_target_name = get_label_info(_module_target, "name")
4777        _dex_target = "${_module_target_name}__final_dex"
4778        _dex_path = "$target_out_dir/$_module_target_name/$_module_target_name.mergeddex.jar"
4779        dex(_dex_target) {
4780          forward_variables_from(invoker, [ "custom_assertion_handler" ])
4781          min_sdk_version = _min_sdk_version
4782          output = _dex_path
4783          build_config = _build_config
4784
4785          # This will be a pure dex-merge.
4786          input_dex_filearg = "@FileArg($_rebased_build_config:modules:${_module.name}:all_dex_files)"
4787          enable_desugar = false
4788
4789          deps = [
4790            ":$_build_config_target",
4791            ":${_module_target_name}__java",
4792          ]
4793        }
4794      }
4795      _dex_target_for_module = ":$_dex_target"
4796
4797      if (_enable_art_profile_optimizations && _include_baseline_profile) {
4798        _module_target_name = get_label_info(_module_target, "name")
4799        _binary_profile_target =
4800            "${_module_target_name}__binary_baseline_profile"
4801        _binary_baseline_profile_path = "$target_out_dir/$_module_target_name/$_module_target_name.baseline.prof"
4802        _binary_baseline_profile_metadata_path =
4803            _binary_baseline_profile_path + "m"
4804        create_binary_profile(_binary_profile_target) {
4805          forward_variables_from(invoker, TESTONLY_AND_VISIBILITY)
4806          binary_baseline_profile_path = _binary_baseline_profile_path
4807          binary_baseline_profile_metadata_path =
4808              _binary_baseline_profile_metadata_path
4809          build_config = _module_build_config
4810          input_profile_path = _obfuscated_art_profile
4811          deps = [
4812            _dex_target_for_module,
4813            _module_build_config_target,
4814          ]
4815        }
4816      }
4817
4818      # Generate one module .zip file per bundle module.
4819      #
4820      # Important: the bundle tool uses the module's zip filename as
4821      # the internal module name inside the final bundle, in other words,
4822      # this file *must* be named ${_module.name}.zip
4823      _create_module_target = "${_target_name}__${_module.name}__create"
4824      _module_zip_path = "$target_out_dir/$target_name/${_module.name}.zip"
4825      create_android_app_bundle_module(_create_module_target) {
4826        forward_variables_from(invoker,
4827                               [
4828                                 "is_multi_abi",
4829                                 "uncompress_dex",
4830                               ])
4831        module_name = _module.name
4832        min_sdk_version = _min_sdk_version
4833        build_config = _module_build_config
4834        module_zip_path = _module_zip_path
4835        native_libraries_config = _native_libraries_config
4836        if (!_proguard_enabled) {
4837          dex_path = _dex_path
4838          # dex_path is read from the build_config in the proguard case.
4839        }
4840
4841        if (module_name == "base" &&
4842            defined(invoker.expected_libs_and_assets)) {
4843          forward_variables_from(invoker,
4844                                 [
4845                                   "expected_libs_and_assets",
4846                                   "expected_libs_and_assets_base",
4847                                 ])
4848          top_target_name = _target_name
4849          build_config_target = _module_build_config_target
4850          native_libraries_config_target = ":$_native_libraries_config_target"
4851          if (defined(android_app_secondary_abi)) {
4852            secondary_abi_native_libraries_config_target =
4853                ":$_secondary_abi_native_libraries_config_target"
4854          }
4855        }
4856
4857        deps = [
4858          ":$_native_libraries_config_target",
4859          _dex_target_for_module,
4860          _module_build_config_target,
4861          _module_target,
4862        ]
4863
4864        if (defined(android_app_secondary_abi)) {
4865          secondary_abi_native_libraries_config =
4866              _secondary_abi_native_libraries_config
4867          deps += [ ":$_secondary_abi_native_libraries_config_target" ]
4868        }
4869
4870        if (_enable_art_profile_optimizations && _include_baseline_profile) {
4871          # extra_assets is a list of ["{src_path}:{dst_path}"]
4872          extra_assets = [
4873            rebase_path(_binary_baseline_profile_path, root_build_dir) +
4874                ":dexopt/baseline.prof",
4875            rebase_path(_binary_baseline_profile_metadata_path,
4876                        root_build_dir) + ":dexopt/baseline.profm",
4877          ]
4878          deps += [ ":$_binary_profile_target" ]
4879        }
4880      }
4881
4882      _all_create_module_targets += [
4883        ":$_create_module_target",
4884        _module_build_config_target,
4885        "${_module_target}__compile_resources",
4886      ]
4887      _all_module_zip_paths += [ _module_zip_path ]
4888      _all_module_build_configs += [ _module_build_config ]
4889      _all_module_unused_resources_deps += [
4890        "${_module_target}__compile_resources",
4891        _dex_target_for_module,
4892        _module_build_config_target,
4893      ]
4894    }
4895    _strip_unused_resources = defined(invoker.strip_unused_resources) &&
4896                              invoker.strip_unused_resources
4897    if (_strip_unused_resources) {
4898      # Resources only live in the base module so we define the unused resources
4899      # target only on the base module target.
4900      _unused_resources_target = "${_base_target_name}__unused_resources"
4901      _unused_resources_config =
4902          "${_base_target_gen_dir}/${_base_target_name}_unused_resources.config"
4903      _unused_resources_r_txt_out =
4904          "${_base_target_gen_dir}/${_base_target_name}_unused_resources.R.txt"
4905      unused_resources(_unused_resources_target) {
4906        deps = _all_module_unused_resources_deps
4907        all_module_build_configs = _all_module_build_configs
4908        build_config = _base_module_build_config
4909        if (_proguard_enabled) {
4910          proguard_mapping_path = _proguard_mapping_path
4911        }
4912        output_config = _unused_resources_config
4913        output_r_txt = _unused_resources_r_txt_out
4914      }
4915      _unused_resources_final_path = "${_bundle_path}.unused_resources"
4916      _copy_unused_resources_target =
4917          "${_base_target_name}__copy_unused_resources"
4918      copy(_copy_unused_resources_target) {
4919        deps = [ ":$_unused_resources_target" ]
4920        sources = [ _unused_resources_config ]
4921        outputs = [ _unused_resources_final_path ]
4922      }
4923    }
4924
4925    _all_rebased_module_zip_paths =
4926        rebase_path(_all_module_zip_paths, root_build_dir)
4927
4928    _enable_language_splits = defined(invoker.enable_language_splits) &&
4929                              invoker.enable_language_splits
4930
4931    _split_dimensions = []
4932    if (_enable_language_splits) {
4933      _split_dimensions += [ "language" ]
4934    }
4935
4936    _keystore_path = android_keystore_path
4937    _keystore_password = android_keystore_password
4938    _keystore_name = android_keystore_name
4939
4940    if (defined(invoker.keystore_path)) {
4941      _keystore_path = invoker.keystore_path
4942      _keystore_password = invoker.keystore_password
4943      _keystore_name = invoker.keystore_name
4944    }
4945
4946    _rebased_keystore_path = rebase_path(_keystore_path, root_build_dir)
4947
4948    _bundle_target_name = "${_target_name}__bundle"
4949    action_with_pydeps(_bundle_target_name) {
4950      script = "//build/android/gyp/create_app_bundle.py"
4951      inputs = _all_module_zip_paths + _all_module_build_configs + [
4952                 _BUNDLETOOL_JAR_PATH,
4953                 java_path_for_inputs,
4954               ]
4955      outputs = [ _bundle_path ]
4956      deps = _all_create_module_targets + [ ":$_build_config_target" ]
4957      args = [
4958        "--out-bundle=$_rebased_bundle_path",
4959        "--rtxt-out-path=$_rebased_bundle_path.R.txt",
4960        "--pathmap-out-path=$_rebased_bundle_path.pathmap.txt",
4961        "--module-zips=$_all_rebased_module_zip_paths",
4962      ]
4963      if (_split_dimensions != []) {
4964        args += [ "--split-dimensions=$_split_dimensions" ]
4965      }
4966
4967      # Android P+ support loading from stored dex.
4968      if (_min_sdk_version < 27) {
4969        args += [ "--compress-dex" ]
4970      }
4971
4972      if (defined(invoker.rotation_config)) {
4973        args += [
4974          "--rotation-config",
4975          rebase_path(invoker.rotation_config, root_build_dir),
4976        ]
4977      }
4978
4979      if (treat_warnings_as_errors) {
4980        args += [ "--warnings-as-errors" ]
4981      }
4982
4983      if (_enable_language_splits) {
4984        args += [ "--base-allowlist-rtxt-path=@FileArg($_rebased_base_module_build_config:deps_info:base_allowlist_rtxt_path)" ]
4985        if (_strip_unused_resources) {
4986          # Use the stripped out rtxt file to set resources that are pinned to
4987          # the default language split.
4988          _rebased_unused_resources_r_txt_out =
4989              rebase_path(_unused_resources_r_txt_out, root_build_dir)
4990          inputs += [ _unused_resources_r_txt_out ]
4991          deps += [ ":$_unused_resources_target" ]
4992          args +=
4993              [ "--base-module-rtxt-path=$_rebased_unused_resources_r_txt_out" ]
4994        } else {
4995          args += [ "--base-module-rtxt-path=@FileArg($_rebased_base_module_build_config:deps_info:r_text_path)" ]
4996        }
4997      }
4998      if (defined(invoker.validate_services) && invoker.validate_services) {
4999        args += [ "--validate-services" ]
5000      }
5001
5002      foreach(_module, _modules) {
5003        _rebased_build_config =
5004            rebase_path(_module.build_config, root_build_dir)
5005        args += [
5006          "--uncompressed-assets=@FileArg(" +
5007              "$_rebased_build_config:uncompressed_assets)",
5008          "--rtxt-in-paths=@FileArg(" +
5009              "$_rebased_build_config:deps_info:r_text_path)",
5010          "--pathmap-in-paths=@FileArg(" +
5011              "$_rebased_build_config:deps_info:module_pathmap_path)",
5012          "--module-name=" + _module.name,
5013        ]
5014      }
5015
5016      # http://crbug.com/725224. Fix for bots running out of memory.
5017      if (defined(java_cmd_pool_size)) {
5018        pool = "//build/config/android:java_cmd_pool($default_toolchain)"
5019      } else {
5020        pool = "//build/toolchain:link_pool($default_toolchain)"
5021      }
5022    }
5023
5024    # Create size info files for targets that care about size
5025    # (have proguard enabled).
5026    if (_proguard_enabled) {
5027      # Merge all module targets to obtain size info files for all targets.
5028      _all_module_targets = _module_targets
5029
5030      _size_info_target = "${_target_name}__size_info"
5031      create_size_info_files(_size_info_target) {
5032        name = "$_bundle_name.aab"
5033        deps = _all_module_targets + [ ":$_build_config_target" ]
5034        module_build_configs = _all_module_build_configs
5035      }
5036    }
5037
5038    if (_uses_static_library) {
5039      _install_artifacts_target = "${target_name}__install_artifacts"
5040      _install_artifacts_json =
5041          "${target_gen_dir}/${target_name}.install_artifacts"
5042      generated_file(_install_artifacts_target) {
5043        output_conversion = "json"
5044        deps = [ invoker.static_library_provider ]
5045        outputs = [ _install_artifacts_json ]
5046        data_keys = [ "install_artifacts" ]
5047        rebase = root_build_dir
5048      }
5049    }
5050
5051    # Generate a wrapper script for the bundle.
5052    _android_aapt2_path = android_sdk_tools_bundle_aapt2
5053
5054    _bundle_apks_path = "$_bundle_base_path/$_bundle_name.apks"
5055    _bundle_wrapper_script_dir = "$root_build_dir/bin"
5056    _bundle_wrapper_script_path = "$_bundle_wrapper_script_dir/$_target_name"
5057
5058    action_with_pydeps("${_target_name}__wrapper_script") {
5059      script = "//build/android/gyp/create_bundle_wrapper_script.py"
5060      inputs = [ _base_module_build_config ]
5061      outputs = [ _bundle_wrapper_script_path ]
5062
5063      # Telemetry for bundles uses the wrapper script for installation.
5064      data = [
5065        _bundle_wrapper_script_path,
5066        _android_aapt2_path,
5067        _keystore_path,
5068        _bundle_path,
5069      ]
5070      data_deps = [
5071        "//build/android:apk_operations_py",
5072        "//build/android:stack_tools",
5073      ]
5074
5075      deps = [ _base_module_build_config_target ]
5076      args = [
5077        "--script-output-path",
5078        rebase_path(_bundle_wrapper_script_path, root_build_dir),
5079        "--package-name=@FileArg($_rebased_base_module_build_config:deps_info:package_name)",
5080        "--aapt2",
5081        rebase_path(_android_aapt2_path, root_build_dir),
5082        "--bundle-path",
5083        _rebased_bundle_path,
5084        "--bundle-apks-path",
5085        rebase_path(_bundle_apks_path, root_build_dir),
5086        "--target-cpu=$target_cpu",
5087        "--keystore-path",
5088        _rebased_keystore_path,
5089        "--keystore-password",
5090        _keystore_password,
5091        "--key-name",
5092        _keystore_name,
5093      ]
5094      if (defined(invoker.default_modules_for_testing)) {
5095        args += [ "--default-modules" ] + invoker.default_modules_for_testing
5096      }
5097      if (defined(invoker.system_image_locale_allowlist)) {
5098        args += [
5099          "--system-image-locales=${invoker.system_image_locale_allowlist}",
5100        ]
5101      }
5102      if (defined(invoker.command_line_flags_file)) {
5103        args += [
5104          "--command-line-flags-file",
5105          invoker.command_line_flags_file,
5106        ]
5107      }
5108      if (_uses_static_library) {
5109        deps += [ ":$_install_artifacts_target" ]
5110        _rebased_install_artifacts_json =
5111            rebase_path(_install_artifacts_json, root_build_dir)
5112        _static_library_apk_path =
5113            "@FileArg($_rebased_install_artifacts_json[])"
5114        args += [
5115          "--additional-apk",
5116          _static_library_apk_path,
5117        ]
5118      }
5119
5120      if (_proguard_enabled) {
5121        args += [
5122          "--proguard-mapping-path",
5123          rebase_path(_proguard_mapping_path, root_build_dir),
5124        ]
5125
5126        # Required by logcat command.
5127        data_deps += [ "//build/android/stacktrace:java_deobfuscate" ]
5128        data += [ _proguard_mapping_path ]
5129      }
5130    }
5131
5132    _enable_lint = defined(invoker.enable_lint) && invoker.enable_lint &&
5133                   !disable_android_lint
5134    if (_enable_lint) {
5135      android_lint("${target_name}__lint") {
5136        forward_variables_from(invoker,
5137                               [
5138                                 "lint_baseline_file",
5139                                 "lint_gen_dir",
5140                                 "lint_jar_path",
5141                                 "lint_suppressions_file",
5142                               ])
5143        build_config = _build_config
5144        build_config_dep = ":$_build_config_target"
5145        deps = _module_java_targets
5146        if (defined(invoker.lint_suppressions_dep)) {
5147          deps += [ invoker.lint_suppressions_dep ]
5148        }
5149        if (defined(invoker.lint_min_sdk_version)) {
5150          min_sdk_version = invoker.lint_min_sdk_version
5151        } else {
5152          min_sdk_version = _min_sdk_version
5153        }
5154      }
5155    } else {
5156      not_needed(invoker,
5157                 [
5158                   "lint_baseline_file",
5159                   "lint_gen_dir",
5160                   "lint_jar_path",
5161                   "lint_min_sdk_version",
5162                   "lint_suppressions_dep",
5163                   "lint_suppressions_file",
5164                 ])
5165    }
5166
5167    group(_target_name) {
5168      public_deps = [
5169        ":$_bundle_target_name",
5170        ":${_target_name}__wrapper_script",
5171      ]
5172      if (defined(_size_info_target)) {
5173        public_deps += [ ":$_size_info_target" ]
5174      }
5175      if (_enable_lint) {
5176        if (!defined(data_deps)) {
5177          data_deps = []
5178        }
5179        data_deps += [ ":${target_name}__lint" ]
5180      }
5181    }
5182
5183    _apks_path = "$root_build_dir/apks/$_bundle_name.apks"
5184    action_with_pydeps("${_target_name}_apks") {
5185      script = "//build/android/gyp/create_app_bundle_apks.py"
5186      inputs = [
5187        java_path_for_inputs,
5188        _bundle_path,
5189        _BUNDLETOOL_JAR_PATH,
5190      ]
5191      outputs = [ _apks_path ]
5192      data = [ _apks_path ]
5193      args = [
5194        "--bundle",
5195        _rebased_bundle_path,
5196        "--output",
5197        rebase_path(_apks_path, root_build_dir),
5198        "--aapt2-path",
5199        rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
5200        "--keystore-path",
5201        rebase_path(android_keystore_path, root_build_dir),
5202        "--keystore-name",
5203        android_keystore_name,
5204        "--keystore-password",
5205        android_keystore_password,
5206      ]
5207      if (debuggable_apks) {
5208        args += [ "--local-testing" ]
5209      }
5210      deps = [ ":$_bundle_target_name" ]
5211      metadata = {
5212        install_artifacts = [ _apks_path ]
5213        if (defined(invoker.static_library_provider)) {
5214          install_artifacts_barrier = []
5215        }
5216      }
5217
5218      # http://crbug.com/725224. Fix for bots running out of memory.
5219      if (defined(java_cmd_pool_size)) {
5220        pool = "//build/config/android:java_cmd_pool($default_toolchain)"
5221      } else {
5222        pool = "//build/toolchain:link_pool($default_toolchain)"
5223      }
5224    }
5225  }
5226
5227  # Create an .apks file from an .aab file. The .apks file will contain the
5228  # minimal set of .apk files needed for tracking binary size.
5229  # The file will be created at "$bundle_path_without_extension.minimal.apks".
5230  #
5231  # Variables:
5232  #   bundle_path: Path to the input .aab file.
5233  #
5234  # Example:
5235  #   create_app_bundle_minimal_apks("minimal_apks") {
5236  #     deps = [
5237  #       ":bundle_target",
5238  #     ]
5239  #     bundle_path = "$root_build_dir/apks/Bundle.aab"
5240  #   }
5241  template("create_app_bundle_minimal_apks") {
5242    action_with_pydeps(target_name) {
5243      forward_variables_from(invoker, TESTONLY_AND_VISIBILITY + [ "deps" ])
5244      script = "//build/android/gyp/create_app_bundle_apks.py"
5245      _dir = get_path_info(invoker.bundle_path, "dir")
5246      _name = get_path_info(invoker.bundle_path, "name")
5247      _output_path = "$_dir/$_name.minimal.apks"
5248      outputs = [ _output_path ]
5249      inputs = [
5250        invoker.bundle_path,
5251        java_path_for_inputs,
5252      ]
5253      args = [
5254        "--bundle",
5255        rebase_path(invoker.bundle_path, root_build_dir),
5256        "--output",
5257        rebase_path(_output_path, root_build_dir),
5258        "--aapt2-path",
5259        rebase_path(android_sdk_tools_bundle_aapt2, root_build_dir),
5260        "--keystore-path",
5261        rebase_path(android_keystore_path, root_build_dir),
5262        "--keystore-name",
5263        android_keystore_name,
5264        "--keystore-password",
5265        android_keystore_password,
5266        "--minimal",
5267      ]
5268    }
5269  }
5270
5271  template("alias_with_wrapper_script") {
5272    copy(target_name) {
5273      _aliased_wrapper_script_name =
5274          get_label_info(invoker.alias_target, "name")
5275      _aliased_wrapper_script =
5276          "$root_build_dir/bin/$_aliased_wrapper_script_name"
5277      sources = [ _aliased_wrapper_script ]
5278      deps = [ invoker.alias_target ]
5279
5280      _output_path = "$root_build_dir/bin/$target_name"
5281      outputs = [ _output_path ]
5282    }
5283  }
5284}
5285
5286# Generate an Android resources target that contains localized strings
5287# describing the current locale used by the Android framework to display
5288# UI strings. These are used by
5289# org.chromium.chrome.browser.ChromeLocalizationUtils.
5290#
5291# Variables:
5292#    ui_locales: List of Chromium locale names to generate resources for.
5293#
5294template("generate_ui_locale_resources") {
5295  _generating_target_name = "${target_name}__generate"
5296  _rebased_output_zip_path = rebase_path(target_gen_dir, root_gen_dir)
5297  _output_zip = "${root_out_dir}/resource_zips/${_rebased_output_zip_path}/" +
5298                "${target_name}.zip"
5299
5300  action_with_pydeps(_generating_target_name) {
5301    script = "//build/android/gyp/create_ui_locale_resources.py"
5302    outputs = [ _output_zip ]
5303    args = [
5304      "--locale-list=${invoker.ui_locales}",
5305      "--output-zip",
5306      rebase_path(_output_zip, root_build_dir),
5307    ]
5308  }
5309
5310  android_generated_resources(target_name) {
5311    generating_target = ":$_generating_target_name"
5312    generated_resources_zip = _output_zip
5313  }
5314}
5315