# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Do not add any imports to non-//build directories here.
# Some projects (e.g. V8) do not have non-build directories DEPS'ed in.
import("//build_overrides/build.gni")
import("//build/config/android/config.gni")
import("//build/config/dcheck_always_on.gni")
import("//build/config/sanitizers/sanitizers.gni")

assert(is_android)

# These identify targets that have .build_config files (except for android_apk,
# java_binary, resource_rewriter, since we never need to depend on these).
_java_target_whitelist = [
  "*:*_java",
  "*:*_javalib",
  "*:*_java_*",  # e.g. java_test_support
  "*:java",
  "*:junit",
  "*:junit_*",
  "*:*_junit_*",
  "*:*javatests",
  "*:*_assets",
  "*android*:assets",
  "*:*_apk_*resources",
  "*android*:resources",
  "*:*_resources",
  "*:*_grd",
  "*:*locale_paks",

  # TODO(agrieve): Rename targets below to match above patterns.
  "*android_webview/glue:glue",
  "//chrome/test/android/cast_emulator:cast_emulator",
]

# Targets that match the whitelist but are not actually java targets.
_java_target_blacklist = [
  "//chrome:packed_resources",
  "*:*_unpack_aar",
]

_default_proguard_jar_path = "//third_party/proguard/lib/proguard.jar"

# Write the target's .build_config file. This is a json file that contains a
# dictionary of information about how to build this target (things that
# require knowledge about this target's dependencies and cannot be calculated
# at gn-time). There is a special syntax to add a value in that dictionary to
# an action/action_foreachs args:
#   --python-arg=@FileArg($rebased_build_config_path:key0:key1)
# At runtime, such an arg will be replaced by the value in the build_config.
# See build/android/gyp/write_build_config.py and
# build/android/gyp/util/build_utils.py:ExpandFileArgs
template("write_build_config") {
  type = invoker.type
  _is_prebuilt_binary =
      defined(invoker.is_prebuilt_binary) && invoker.is_prebuilt_binary

  # Don't need to enforce naming scheme for these targets since we never
  # consider them in dependency chains.
  if (!_is_prebuilt_binary && type != "android_apk" && type != "java_binary" &&
      type != "resource_rewriter" && type != "dist_jar") {
    set_sources_assignment_filter(_java_target_whitelist)
    _parent_invoker = invoker.invoker
    _target_label =
        get_label_info(":${_parent_invoker.target_name}", "label_no_toolchain")
    sources = [
      _target_label,
    ]
    if (sources != []) {
      set_sources_assignment_filter(_java_target_blacklist)
      sources = []
      sources = [
        _target_label,
      ]
      if (sources != []) {
        assert(false, "Invalid java target name: $_target_label")
      }
    }
    sources = []
  }

  action(target_name) {
    set_sources_assignment_filter([])
    build_config = invoker.build_config

    assert(type == "android_apk" || type == "java_library" ||
           type == "android_resources" || type == "deps_dex" ||
           type == "dist_jar" || type == "android_assets" ||
           type == "resource_rewriter" || type == "java_binary" ||
           type == "group" || type == "java_prebuilt" || type == "junit_binary")

    forward_variables_from(invoker,
                           [
                             "deps",
                             "testonly",
                           ])
    if (!defined(deps)) {
      deps = []
    }

    script = "//build/android/gyp/write_build_config.py"
    depfile = "$target_gen_dir/$target_name.d"
    inputs = []

    _deps_configs = []
    if (defined(invoker.possible_config_deps)) {
      foreach(_possible_dep, invoker.possible_config_deps) {
        set_sources_assignment_filter(_java_target_whitelist)
        _target_label = get_label_info(_possible_dep, "label_no_toolchain")
        sources = [
          _target_label,
        ]
        if (sources == []) {
          set_sources_assignment_filter(_java_target_blacklist)
          sources = []
          sources = [
            _target_label,
          ]
          if (sources != []) {
            deps += [ "${_target_label}__build_config" ]
            _dep_gen_dir = get_label_info(_possible_dep, "target_gen_dir")
            _dep_name = get_label_info(_possible_dep, "name")
            _deps_configs += [ "$_dep_gen_dir/$_dep_name.build_config" ]
          }
        }
        sources = []
      }
      set_sources_assignment_filter([])
    }
    _rebased_deps_configs = rebase_path(_deps_configs, root_build_dir)

    outputs = [
      build_config,
    ]

    args = [
      "--type",
      type,
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--deps-configs=$_rebased_deps_configs",
      "--build-config",
      rebase_path(build_config, root_build_dir),
    ]

    is_apk = type == "android_apk"
    is_android_assets = type == "android_assets"
    is_android_resources = type == "android_resources"
    is_deps_dex = type == "deps_dex"
    is_group = type == "group"

    supports_android =
        is_apk || is_android_assets || is_android_resources || is_deps_dex ||
        is_group ||
        (defined(invoker.supports_android) && invoker.supports_android)
    requires_android =
        is_apk || is_android_assets || is_android_resources || is_deps_dex ||
        (defined(invoker.requires_android) && invoker.requires_android)

    assert(!requires_android || supports_android,
           "requires_android requires" + " supports_android")

    # Mark these variables as used.
    assert(is_apk || true)
    assert(is_android_resources || true)
    assert(is_deps_dex || true)
    assert(is_group || true)

    if (defined(invoker.jar_path)) {
      args += [
        "--jar-path",
        rebase_path(invoker.jar_path, root_build_dir),
      ]
    }

    if (defined(invoker.java_resources_jar)) {
      args += [
        "--java-resources-jar-path",
        rebase_path(invoker.java_resources_jar, root_build_dir),
      ]
    }
    if (defined(invoker.dex_path)) {
      args += [
        "--dex-path",
        rebase_path(invoker.dex_path, root_build_dir),
      ]
    }
    if (supports_android) {
      args += [ "--supports-android" ]
    }
    if (requires_android) {
      args += [ "--requires-android" ]
    }
    if (defined(invoker.bypass_platform_checks) &&
        invoker.bypass_platform_checks) {
      args += [ "--bypass-platform-checks" ]
    }

    if (defined(invoker.apk_under_test)) {
      deps += [ "${invoker.apk_under_test}__build_config" ]
      apk_under_test_gen_dir =
          get_label_info(invoker.apk_under_test, "target_gen_dir")
      apk_under_test_name = get_label_info(invoker.apk_under_test, "name")
      apk_under_test_config =
          "$apk_under_test_gen_dir/$apk_under_test_name.build_config"
      args += [
        "--tested-apk-config",
        rebase_path(apk_under_test_config, root_build_dir),
      ]
    }

    if (is_android_assets) {
      if (defined(invoker.asset_sources)) {
        _rebased_asset_sources =
            rebase_path(invoker.asset_sources, root_build_dir)
        args += [ "--asset-sources=$_rebased_asset_sources" ]
      }
      if (defined(invoker.asset_renaming_sources)) {
        _rebased_asset_renaming_sources =
            rebase_path(invoker.asset_renaming_sources, root_build_dir)
        args += [ "--asset-renaming-sources=$_rebased_asset_renaming_sources" ]

        # These are zip paths, so no need to rebase.
        args += [ "--asset-renaming-destinations=${invoker.asset_renaming_destinations}" ]
      }
      if (defined(invoker.disable_compression) && invoker.disable_compression) {
        args += [ "--disable-asset-compression" ]
      }
      if (defined(invoker.treat_as_locale_paks) &&
          invoker.treat_as_locale_paks) {
        args += [ "--treat-as-locale-paks" ]
      }
    }

    if (is_android_resources || is_apk) {
      if (defined(invoker.resources_zip)) {
        args += [
          "--resources-zip",
          rebase_path(invoker.resources_zip, root_build_dir),
        ]
      }
      if (defined(invoker.android_manifest)) {
        inputs += [ invoker.android_manifest ]
        args += [
          "--android-manifest",
          rebase_path(invoker.android_manifest, root_build_dir),
        ]
      } else {
        assert(!is_apk, "apk build configs require an android_manifest")
      }
      if (defined(invoker.custom_package)) {
        args += [
          "--package-name",
          invoker.custom_package,
        ]
      }
      if (defined(invoker.r_text)) {
        args += [
          "--r-text",
          rebase_path(invoker.r_text, root_build_dir),
        ]
      }
    }

    if (is_android_resources && defined(invoker.resource_dirs)) {
      resource_dirs = rebase_path(invoker.resource_dirs, root_build_dir)
      args += [ "--resource-dirs=$resource_dirs" ]
    }

    if (is_apk) {
      if (defined(invoker.shared_libraries_runtime_deps_file)) {
        # Don't list shared_libraries_runtime_deps_file as an input in order to
        # avoid having to depend on the runtime_deps target. See comment in
        # rules.gni for why we do this.
        args += [
          "--shared-libraries-runtime-deps",
          rebase_path(invoker.shared_libraries_runtime_deps_file,
                      root_build_dir),
        ]
      }

      if (defined(invoker.secondary_abi_shared_libraries_runtime_deps_file)) {
        # Don't list secondary_abi_shared_libraries_runtime_deps_file as an
        # input in order to avoid having to depend on the runtime_deps target.
        # See comment in rules.gni for why we do this.
        args += [
          "--secondary-abi-shared-libraries-runtime-deps",
          rebase_path(invoker.secondary_abi_shared_libraries_runtime_deps_file,
                      root_build_dir),
        ]
      }

      if (defined(invoker.apk_path)) {
        _rebased_apk_path = rebase_path(invoker.apk_path, root_build_dir)
        _rebased_incremental_apk_path =
            rebase_path(invoker.incremental_apk_path, root_build_dir)
        _rebased_incremental_install_json_path =
            rebase_path(invoker.incremental_install_json_path, root_build_dir)
        _incremental_allowed =
            defined(invoker.incremental_allowed) && invoker.incremental_allowed
        args += [ "--apk-path=$_rebased_apk_path" ]
        args += [ "--incremental-install-json-path=$_rebased_incremental_install_json_path" ]

        assert(_rebased_incremental_apk_path != "")  # Mark as used.
        if (_incremental_allowed) {
          args += [ "--incremental-apk-path=$_rebased_incremental_apk_path" ]
        }
      }
    }
    if (defined(invoker.non_native_packed_relocations) &&
        invoker.non_native_packed_relocations) {
      args += [ "--non-native-packed-relocations" ]
    }
    if (defined(invoker.java_sources_file)) {
      args += [
        "--java-sources-file",
        rebase_path(invoker.java_sources_file, root_build_dir),
      ]
    }
    if (defined(invoker.srcjar)) {
      args += [
        "--srcjar",
        rebase_path(invoker.srcjar, root_build_dir),
      ]
    }
    if (defined(invoker.bundled_srcjars)) {
      _rebased_bundled_srcjars =
          rebase_path(invoker.bundled_srcjars, root_build_dir)
      args += [ "--bundled-srcjars=$_rebased_bundled_srcjars" ]
    }
    if (defined(invoker.classpath_deps)) {
      _classpath_deps_configs = []
      foreach(d, invoker.classpath_deps) {
        _target_label = get_label_info(d, "label_no_toolchain")
        deps += [ "${_target_label}__build_config" ]
        _dep_gen_dir = get_label_info(d, "target_gen_dir")
        _dep_name = get_label_info(d, "name")
        _classpath_deps_configs += [ "$_dep_gen_dir/$_dep_name.build_config" ]
      }
      _rebased_classpath_deps_configs =
          rebase_path(_classpath_deps_configs, root_build_dir)
      args += [ "--classpath-deps-configs=$_rebased_classpath_deps_configs" ]
    }
    if (defined(invoker.input_jars_paths)) {
      _rebased_input_jars_paths =
          rebase_path(invoker.input_jars_paths, root_build_dir)
      args += [ "--extra-classpath-jars=$_rebased_input_jars_paths" ]
    }
    if (defined(invoker.proguard_enabled) && invoker.proguard_enabled) {
      args += [
        "--proguard-enabled",
        "--proguard-info",
        rebase_path(invoker.proguard_info, root_build_dir),
      ]
    }
    if (defined(invoker.proguard_configs)) {
      _rebased_proguard_configs =
          rebase_path(invoker.proguard_configs, root_build_dir)
      args += [ "--proguard-configs=$_rebased_proguard_configs" ]
    }
    if (defined(invoker.gradle_treat_as_prebuilt) &&
        invoker.gradle_treat_as_prebuilt) {
      args += [ "--gradle-treat-as-prebuilt" ]
    }
    if (defined(invoker.main_class)) {
      args += [
        "--main-class",
        invoker.main_class,
      ]
    }
    if (defined(invoker.alternative_android_sdk_ijar)) {
      args += [
        "--bootclasspath",
        rebase_path(invoker.alternative_android_sdk_ijar, root_build_dir),
      ]
    }
    if (current_toolchain != default_toolchain) {
      # This has to be a built-time error rather than a GN assert because many
      # packages have a mix of java and non-java targets. For example, the
      # following would fail even though nothing depends on :bar(//baz):
      #
      # shared_library("foo") {
      # }
      #
      # android_library("bar") {
      #   deps = [ ":foo(//baz)" ]
      #   assert(current_toolchain == default_toolchain)
      # }
      _msg = [
        "Tried to build an Android target in a non-default toolchain.",
        "target: " + get_label_info(":$target_name", "label_with_toolchain"),
        "default_toolchain: $default_toolchain",
      ]
      args += [ "--fail=$_msg" ]
    }
  }
}

template("copy_ex") {
  set_sources_assignment_filter([])
  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "data",
                             "deps",
                             "inputs",
                             "outputs",
                             "sources",
                             "testonly",
                             "visibility",
                           ])
    if (!defined(sources)) {
      sources = []
    }
    script = "//build/android/gyp/copy_ex.py"

    args = [
      "--dest",
      rebase_path(invoker.dest, root_build_dir),
    ]
    rebased_sources = rebase_path(sources, root_build_dir)
    args += [ "--files=$rebased_sources" ]

    if (defined(invoker.args)) {
      args += invoker.args
    }

    if (defined(invoker.renaming_sources) &&
        defined(invoker.renaming_destinations)) {
      sources += invoker.renaming_sources
      rebased_renaming_sources =
          rebase_path(invoker.renaming_sources, root_build_dir)
      args += [ "--renaming-sources=$rebased_renaming_sources" ]

      renaming_destinations = invoker.renaming_destinations
      args += [ "--renaming-destinations=$renaming_destinations" ]
    }
  }
}

# Generates a script in the build bin directory which runs the test
# target using the test runner script in build/android/test_runner.py.
template("test_runner_script") {
  testonly = true
  _test_name = invoker.test_name
  _test_type = invoker.test_type
  _incremental_install =
      defined(invoker.incremental_install) && invoker.incremental_install

  _runtime_deps =
      !defined(invoker.ignore_all_data_deps) || !invoker.ignore_all_data_deps

  if (_runtime_deps) {
    # This runtime_deps file is used at runtime and thus cannot go in
    # target_gen_dir.
    _target_dir_name = get_label_info(":$target_name", "dir")
    _runtime_deps_file =
        "$root_out_dir/gen.runtime/$_target_dir_name/$target_name.runtime_deps"
    _runtime_deps_target = "${target_name}__write_deps"
    group(_runtime_deps_target) {
      forward_variables_from(invoker,
                             [
                               "data",
                               "deps",
                               "public_deps",
                             ])
      data_deps = []
      if (defined(invoker.data_deps)) {
        data_deps += invoker.data_deps
      }
      if (defined(invoker.additional_apks)) {
        data_deps += invoker.additional_apks
      }
      write_runtime_deps = _runtime_deps_file
    }
  }

  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "data_deps",
                             "deps",
                           ])
    if (!defined(deps)) {
      deps = []
    }
    if (!defined(data_deps)) {
      data_deps = []
    }

    script = "//build/android/gyp/create_test_runner_script.py"
    depfile = "$target_gen_dir/$target_name.d"

    data_deps += [
      "//build/android:test_runner_py",
      "//build/android:logdog_wrapper_py",
    ]

    data = []

    test_runner_args = [
      _test_type,
      "--output-directory",
      rebase_path(root_build_dir, root_build_dir),
    ]

    if (_runtime_deps) {
      deps += [ ":$_runtime_deps_target" ]
      data += [ _runtime_deps_file ]
      test_runner_args += [
        "--runtime-deps-path",
        rebase_path(_runtime_deps_file, root_build_dir),
      ]
    }

    # apk_target is not used for native executable tests
    # (e.g. breakpad_unittests).
    if (defined(invoker.apk_target)) {
      assert(!defined(invoker.executable_dist_dir))
      deps += [ "${invoker.apk_target}__build_config" ]
      _apk_build_config =
          get_label_info(invoker.apk_target, "target_gen_dir") + "/" +
          get_label_info(invoker.apk_target, "name") + ".build_config"
      _rebased_apk_build_config = rebase_path(_apk_build_config, root_build_dir)
      assert(_rebased_apk_build_config != "")  # Mark as used.
    } else if (_test_type == "gtest") {
      assert(
          defined(invoker.executable_dist_dir),
          "Must define either apk_target or executable_dist_dir for test_runner_script()")
      test_runner_args += [
        "--executable-dist-dir",
        rebase_path(invoker.executable_dist_dir, root_build_dir),
      ]
    }

    _device_test = true
    if (_test_type == "gtest") {
      assert(defined(invoker.test_suite))
      test_runner_args += [
        "--suite",
        invoker.test_suite,
      ]
    } else if (_test_type == "instrumentation") {
      _test_apk = "@FileArg($_rebased_apk_build_config:deps_info:apk_path)"
      if (_incremental_install) {
        _test_apk = "@FileArg($_rebased_apk_build_config:deps_info:incremental_apk_path)"
      }
      test_runner_args += [
        "--test-apk=$_test_apk",
        "--test-jar",
        rebase_path(invoker.test_jar, root_build_dir),
      ]
      if (defined(invoker.apk_under_test)) {
        deps += [ "${invoker.apk_under_test}__build_config" ]
        _apk_under_test_build_config =
            get_label_info(invoker.apk_under_test, "target_gen_dir") + "/" +
            get_label_info(invoker.apk_under_test, "name") + ".build_config"
        _rebased_apk_under_test_build_config =
            rebase_path(_apk_under_test_build_config, root_build_dir)
        _apk_under_test =
            "@FileArg($_rebased_apk_under_test_build_config:deps_info:apk_path)"
        if (_incremental_install) {
          _apk_under_test = "@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_apk_path)"
        }
        test_runner_args += [ "--apk-under-test=$_apk_under_test" ]
        test_runner_args += [
          "--non-native-packed-relocations",
          "@FileArg($_rebased_apk_under_test_build_config:deps_info:non_native_packed_relocations)",
        ]
      }
      if (defined(invoker.proguard_enabled) && invoker.proguard_enabled) {
        test_runner_args += [ "--enable-java-deobfuscation" ]
      }
      if (emma_coverage) {
        # Set a default coverage output directory (can be overridden by user
        # passing the same flag).
        test_runner_args += [
          "--coverage-dir",
          rebase_path("$root_out_dir/coverage", root_build_dir),
        ]
      }
    } else if (_test_type == "junit") {
      assert(defined(invoker.test_suite))
      _device_test = false
      test_runner_args += [
        "--test-suite",
        invoker.test_suite,
      ]
      if (defined(invoker.android_manifest_path)) {
        test_runner_args += [
          "--android-manifest-path",
          rebase_path(invoker.android_manifest_path, root_build_dir),
        ]
      }

      if (defined(invoker.package_name)) {
        test_runner_args += [
          "--package-name",
          invoker.package_name,
        ]

        deps += [ ":${invoker.test_suite}__build_config" ]
        _junit_binary_build_config =
            "${target_gen_dir}/${invoker.test_suite}.build_config"
        _rebased_build_config =
            rebase_path("$_junit_binary_build_config", root_build_dir)
        test_runner_args += [
          "--resource-zips",
          "@FileArg($_rebased_build_config:resources:dependency_zips)",
        ]
      }

      test_runner_args += [
        "--robolectric-runtime-deps-dir",
        rebase_path("$root_build_dir/lib.java/third_party/robolectric",
                    root_build_dir),
      ]
    } else if (_test_type == "linker") {
      test_runner_args += [
        "--test-apk",
        "@FileArg($_rebased_apk_build_config:deps_info:apk_path)",
      ]
    } else {
      assert(false, "Invalid test type: $_test_type.")
    }

    if (defined(invoker.additional_apks)) {
      foreach(additional_apk, invoker.additional_apks) {
        deps += [ "${additional_apk}__build_config" ]
        _build_config = get_label_info(additional_apk, "target_gen_dir") + "/" +
                        get_label_info(additional_apk, "name") + ".build_config"
        _rebased_build_config = rebase_path(_build_config, root_build_dir)
        test_runner_args += [
          "--additional-apk",
          "@FileArg($_rebased_build_config:deps_info:apk_path)",
          "--additional-apk-incremental",
          "@FileArg($_rebased_build_config:deps_info:incremental_apk_path)",
        ]
      }
    }
    if (defined(invoker.shard_timeout)) {
      test_runner_args += [ "--shard-timeout=${invoker.shard_timeout}" ]
    }
    if (_incremental_install) {
      test_runner_args += [
        "--test-apk-incremental-install-json",
        "@FileArg($_rebased_apk_build_config:deps_info:incremental_install_json_path)",
      ]
      if (defined(invoker.apk_under_test)) {
        test_runner_args += [
          "--apk-under-test-incremental-install-json",
          "@FileArg($_rebased_apk_under_test_build_config:deps_info:incremental_install_json_path)",
        ]
      }
      test_runner_args += [ "--fast-local-dev" ]
    }
    if (_device_test && is_asan) {
      test_runner_args += [ "--tool=asan" ]
    }

    if (defined(invoker.generated_script)) {
      assert(_test_name != "" || true)  # Mark _test_name as used.
      generated_script = invoker.generated_script
    } else {
      generated_script = "$root_build_dir/bin/run_${_test_name}"
    }
    outputs = [
      generated_script,
    ]
    data += [ generated_script ]

    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--script-output-path",
      rebase_path(generated_script, root_build_dir),
    ]
    if (defined(android_test_runner_script)) {
      args += [
        "--test-runner-path",
        android_test_runner_script,
      ]
    }

    args += test_runner_args
  }
}

template("stack_script") {
  forward_variables_from(invoker, [ "testonly" ])

  _stack_target_name = invoker.stack_target_name

  action(target_name) {
    forward_variables_from(invoker,
                           [
                             "data_deps",
                             "deps",
                           ])
    if (!defined(deps)) {
      deps = []
    }
    if (!defined(data_deps)) {
      data_deps = []
    }

    data_deps +=
        [ "//third_party/android_platform/development/scripts:stack_py" ]

    script = "//build/android/gyp/create_stack_script.py"
    depfile = "$target_gen_dir/$target_name.d"

    _stack_script = "//third_party/android_platform/development/scripts/stack"

    _generated_script = "$root_build_dir/bin/stack_${_stack_target_name}"

    outputs = [
      _generated_script,
    ]
    data = [
      _generated_script,
    ]

    args = [
      "--depfile",
      rebase_path(depfile, root_build_dir),
      "--output-directory",
      rebase_path(root_build_dir, root_build_dir),
      "--script-path",
      rebase_path(_stack_script, root_build_dir),
      "--script-output-path",
      rebase_path(_generated_script, root_build_dir),
      "--arch=$target_cpu",
    ]
    if (defined(invoker.packed_libraries)) {
      args += [
        "--packed-libs",
        invoker.packed_libraries,
      ]
    }
  }
}

if (enable_java_templates) {
  import("//build/config/zip.gni")
  import("//third_party/ijar/ijar.gni")
  import("//third_party/android_platform/config.gni")

  android_sdk_jar = "$android_sdk/android.jar"
  android_default_aapt_path = "$android_sdk_build_tools/aapt"

  template("android_lint") {
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "data_deps",
                               "public_deps",
                               "testonly",
                             ])
      if (!defined(deps)) {
        deps = []
      }

      if (defined(invoker.lint_suppressions_file)) {
        lint_suppressions_file = invoker.lint_suppressions_file
      } else if (!defined(lint_suppressions_file)) {
        lint_suppressions_file = "//build/android/lint/suppressions.xml"
      }

      _lint_path = "$lint_android_sdk_root/tools-lint/bin/lint"
      _cache_dir = "$root_build_dir/android_lint_cache"
      _result_path = "$target_gen_dir/$target_name/result.xml"
      _config_path = "$target_gen_dir/$target_name/config.xml"
      _suppressions_file = lint_suppressions_file
      _platform_xml_path =
          "${android_sdk_root}/platform-tools/api/api-versions.xml"

      script = "//build/android/gyp/lint.py"
      depfile = "$target_gen_dir/$target_name.d"
      inputs = [
        _platform_xml_path,
        _suppressions_file,
        invoker.android_manifest,
      ]

      outputs = [
        _result_path,
        _config_path,
      ]

      args = [
        "--lint-path",
        rebase_path(_lint_path, root_build_dir),
        "--cache-dir",
        rebase_path(_cache_dir, root_build_dir),
        "--platform-xml-path",
        rebase_path(_platform_xml_path, root_build_dir),
        "--android-sdk-version=${lint_android_sdk_version}",
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--config-path",
        rebase_path(_suppressions_file, root_build_dir),
        "--manifest-path",
        rebase_path(invoker.android_manifest, root_build_dir),
        "--product-dir=.",
        "--processed-config-path",
        rebase_path(_config_path, root_build_dir),
        "--result-path",
        rebase_path(_result_path, root_build_dir),
      ]

      if (defined(invoker.disable)) {
        args += [ "--disable=${invoker.disable}" ]
      }

      if (defined(invoker.create_cache) && invoker.create_cache) {
        args += [
          "--create-cache",
          "--silent",
        ]
      } else {
        inputs += invoker.java_files
        inputs += [
          invoker.jar_path,
          invoker.build_config,
        ]
        if (invoker.java_files != []) {
          inputs += [ invoker.java_sources_file ]
          _rebased_java_sources_file =
              rebase_path(invoker.java_sources_file, root_build_dir)
          args += [ "--java-sources-file=$_rebased_java_sources_file" ]
        }
        deps += [ "//build/android:prepare_android_lint_cache" ]

        _rebased_build_config =
            rebase_path(invoker.build_config, root_build_dir)
        args += [
          "--jar-path",
          rebase_path(invoker.jar_path, root_build_dir),
          "--classpath=@FileArg($_rebased_build_config:javac:interface_classpath)",
          "--resource-sources=@FileArg($_rebased_build_config:deps_info:owned_resources_dirs)",
          "--resource-sources=@FileArg($_rebased_build_config:deps_info:owned_resources_zips)",
          "--srcjars=@FileArg($_rebased_build_config:gradle:bundled_srcjars)",
          "--can-fail-build",
        ]
      }
    }
  }

  template("proguard") {
    action(target_name) {
      set_sources_assignment_filter([])
      forward_variables_from(invoker,
                             [
                               "data",
                               "data_deps",
                               "deps",
                               "public_deps",
                               "testonly",
                             ])
      script = "//build/android/gyp/proguard.py"

      # http://crbug.com/725224. Fix for bots running out of memory.
      pool = "//build/toolchain:link_pool($default_toolchain)"

      _output_jar_path = invoker.output_jar_path
      _proguard_jar_path = _default_proguard_jar_path
      if (defined(invoker.proguard_jar_path)) {
        _proguard_jar_path = invoker.proguard_jar_path
      }
      _android_sdk_jar = android_sdk_jar
      if (defined(invoker.alternative_android_sdk_jar)) {
        _android_sdk_jar = invoker.alternative_android_sdk_jar
      }

      inputs = [
        _android_sdk_jar,
        _proguard_jar_path,
      ]
      if (defined(invoker.inputs)) {
        inputs += invoker.inputs
      }
      depfile = "${target_gen_dir}/${target_name}.d"
      outputs = [
        _output_jar_path,
        "$_output_jar_path.flags",
        "$_output_jar_path.mapping",
        "$_output_jar_path.seeds",
        "$_output_jar_path.usage",
      ]
      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--proguard-path",
        rebase_path(_proguard_jar_path, root_build_dir),
        "--output-path",
        rebase_path(_output_jar_path, root_build_dir),
        "--classpath",
        rebase_path(_android_sdk_jar, root_build_dir),
      ]
      if (proguard_verbose) {
        args += [ "--verbose" ]
      }
      if (defined(invoker.args)) {
        args += invoker.args
      }
      if (defined(invoker.proguard_jar_path)) {
        # We assume that if we are using a different ProGuard, this new version
        # can handle the 'dangerous' optimizaions.
        args += [ "--enable-dangerous-optimizations" ]
      }
    }
  }

  # Generates a script in the build bin directory to run a java binary.
  #
  # Variables
  #   main_class: The class containing the program entry point.
  #   jar_path: The path to the jar to run.
  #   script_name: Name of the script to generate.
  #   build_config: Path to .build_config for the jar (contains classpath).
  #   wrapper_script_args: List of extra arguments to pass to the executable.
  #
  template("java_binary_script") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    _main_class = invoker.main_class
    _build_config = invoker.build_config
    _jar_path = invoker.jar_path
    _script_name = invoker.script_name

    action(target_name) {
      script = "//build/android/gyp/create_java_binary_script.py"
      depfile = "$target_gen_dir/$_script_name.d"
      java_script = "$root_build_dir/bin/$_script_name"
      inputs = [
        _build_config,
      ]
      outputs = [
        java_script,
      ]
      forward_variables_from(invoker, [ "deps" ])
      _rebased_build_config = rebase_path(_build_config, root_build_dir)
      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--output",
        rebase_path(java_script, root_build_dir),
        "--classpath=@FileArg($_rebased_build_config:deps_info:java:full_classpath)",
        "--jar-path",
        rebase_path(_jar_path, root_build_dir),
        "--main-class",
        _main_class,
      ]
      if (emma_coverage) {
        args += [
          "--classpath",
          rebase_path("//third_party/android_tools/sdk/tools/lib/emma.jar",
                      root_build_dir),
        ]
        args += [ "--noverify" ]
      }
      if (defined(invoker.wrapper_script_args)) {
        args += [ "--" ] + invoker.wrapper_script_args
      }
      if (defined(invoker.bootclasspath)) {
        args += [
          "--bootclasspath",
          rebase_path(invoker.bootclasspath, root_build_dir),
        ]
      }
    }
  }

  template("dex") {
    set_sources_assignment_filter([])

    _enable_multidex =
        defined(invoker.enable_multidex) && invoker.enable_multidex

    if (_enable_multidex) {
      _main_dex_list_path = invoker.output + ".main_dex_list"
      _main_dex_list_target_name = "${target_name}__main_dex_list"
      action(_main_dex_list_target_name) {
        forward_variables_from(invoker,
                               [
                                 "deps",
                                 "inputs",
                                 "sources",
                                 "testonly",
                               ])

        script = "//build/android/gyp/main_dex_list.py"
        depfile = "$target_gen_dir/$target_name.d"

        # http://crbug.com/725224. Fix for bots running out of memory.
        pool = "//build/toolchain:link_pool($default_toolchain)"

        main_dex_rules = "//build/android/main_dex_classes.flags"

        if (defined(invoker.proguard_jar_path)) {
          _proguard_jar_path = invoker.proguard_jar_path
        } else {
          _proguard_jar_path = _default_proguard_jar_path
        }

        if (!defined(inputs)) {
          inputs = []
        }
        inputs += [
          main_dex_rules,
          _proguard_jar_path,
        ]

        outputs = [
          _main_dex_list_path,
        ]

        args = [
          "--depfile",
          rebase_path(depfile, root_build_dir),
          "--android-sdk-tools",
          rebase_path(android_sdk_build_tools, root_build_dir),
          "--main-dex-list-path",
          rebase_path(_main_dex_list_path, root_build_dir),
          "--main-dex-rules-path",
          rebase_path(main_dex_rules, root_build_dir),
          "--proguard-path",
          rebase_path(_proguard_jar_path, root_build_dir),
        ]

        if (defined(invoker.extra_main_dex_proguard_config)) {
          inputs += [ invoker.extra_main_dex_proguard_config ]
          args += [
            "--main-dex-rules-path",
            rebase_path(invoker.extra_main_dex_proguard_config, root_build_dir),
          ]
        }

        if (defined(invoker.args)) {
          args += invoker.args
        }

        if (defined(invoker.sources)) {
          args += rebase_path(invoker.sources, root_build_dir)
        }
      }
    }

    assert(defined(invoker.output))
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "inputs",
                               "sources",
                               "testonly",
                             ])
      script = "//build/android/gyp/dex.py"
      depfile = "$target_gen_dir/$target_name.d"
      outputs = [
        invoker.output,
      ]

      if (defined(invoker.use_pool) && invoker.use_pool) {
        pool = "//build/toolchain:link_pool($default_toolchain)"
      }

      rebased_output = rebase_path(invoker.output, root_build_dir)

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--android-sdk-tools",
        rebase_path(android_sdk_build_tools, root_build_dir),
        "--dex-path",
        rebased_output,
      ]

      if (enable_incremental_dx) {
        args += [ "--incremental" ]
      }

      # EMMA requires --no-locals.
      if (emma_coverage) {
        args += [ "--no-locals=1" ]
      }

      if (_enable_multidex) {
        args += [
          "--multi-dex",
          "--main-dex-list-path",
          rebase_path(_main_dex_list_path, root_build_dir),
        ]
        deps += [ ":${_main_dex_list_target_name}" ]
        inputs += [ _main_dex_list_path ]
      }

      if (defined(invoker.args)) {
        args += invoker.args
      }

      if (defined(invoker.sources)) {
        args += rebase_path(invoker.sources, root_build_dir)
      }
    }
  }

  template("process_java_prebuilt") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    assert(invoker.build_config != "")
    _build_config = invoker.build_config
    _rebased_build_config = rebase_path(_build_config, root_build_dir)
    assert(_rebased_build_config != "" || true)  # Mark used.

    _input_jar_path = invoker.input_jar_path
    _output_jar_path = invoker.output_jar_path

    _enable_assert =
        defined(invoker.enable_build_hooks) && invoker.enable_build_hooks &&
        (is_java_debug || dcheck_always_on || report_java_assert)

    _enable_custom_resources = defined(invoker.enable_build_hooks_android) &&
                               invoker.enable_build_hooks_android

    _desugar = defined(invoker.supports_android) && invoker.supports_android

    _jar_excluded_patterns = []
    if (defined(invoker.jar_excluded_patterns)) {
      _jar_excluded_patterns = invoker.jar_excluded_patterns
    }
    _strip_resource_classes = defined(invoker.strip_resource_classes) &&
                              invoker.strip_resource_classes
    _filter_jar = _jar_excluded_patterns != [] || _strip_resource_classes

    _deps = []
    _previous_output_jar = _input_jar_path

    assert(!defined(invoker.alternative_android_sdk_ijar) ||
           invoker.alternative_android_sdk_ijar != "")
    assert(!defined(invoker.alternative_android_sdk_ijar_dep) ||
           invoker.alternative_android_sdk_ijar_dep != "")
    assert(!defined(invoker.alternative_android_sdk_jar) ||
           invoker.alternative_android_sdk_jar != "")

    if (_enable_assert || _enable_custom_resources) {
      _java_bytecode_rewriter_target = "${target_name}__bytecode_rewrite"
      _java_bytecode_rewriter_input_jar = _previous_output_jar
      _java_bytecode_rewriter_output_jar =
          "$target_out_dir/$target_name-bytecode-rewritten.jar"

      action(_java_bytecode_rewriter_target) {
        script = "//build/android/gyp/bytecode_processor.py"
        depfile = "$target_gen_dir/$target_name.d"
        _bytecode_rewriter_script =
            "$root_build_dir/bin/helper/java_bytecode_rewriter"
        deps = [
          "//build/android/bytecode:java_bytecode_rewriter($default_toolchain)",
        ]
        deps += _deps
        if (defined(invoker.deps)) {
          deps += invoker.deps
        }
        if (defined(invoker.public_deps)) {
          public_deps = invoker.public_deps
        }
        _android_sdk_jar = android_sdk_jar
        if (defined(invoker.alternative_android_sdk_jar)) {
          _android_sdk_jar = invoker.alternative_android_sdk_jar
        }
        inputs = [
          _android_sdk_jar,
          _java_bytecode_rewriter_input_jar,
          _build_config,
        ]
        outputs = [
          _java_bytecode_rewriter_output_jar,
        ]
        args = [
          "--depfile",
          rebase_path(depfile, root_build_dir),
          "--script",
          rebase_path(_bytecode_rewriter_script, root_build_dir),
          "--input-jar",
          rebase_path(_java_bytecode_rewriter_input_jar, root_build_dir),
          "--output-jar",
          rebase_path(_java_bytecode_rewriter_output_jar, root_build_dir),
        ]
        if (_enable_assert) {
          args += [ "--enable-assert" ]
        }
        if (_enable_custom_resources) {
          args += [ "--enable-custom-resources" ]
        }
        args += [
          "--extra-classpath-jar",
          rebase_path(_android_sdk_jar, root_build_dir),
          "--extra-classpath-jar",
          "@FileArg($_rebased_build_config:deps_info:java:full_classpath)",
        ]
      }

      _deps = []
      _deps = [ ":$_java_bytecode_rewriter_target" ]
      _previous_output_jar = _java_bytecode_rewriter_output_jar
    }

    if (_desugar) {
      _desugar_target = "${target_name}__desugar"
      _desugar_input_jar = _previous_output_jar
      _desugar_output_jar = "$target_out_dir/$target_name-desugar.jar"

      action(_desugar_target) {
        script = "//build/android/gyp/desugar.py"
        deps = _deps
        if (defined(invoker.deps)) {
          deps += invoker.deps
        }
        if (defined(invoker.public_deps)) {
          public_deps = invoker.public_deps
        }
        inputs = [
          _build_config,
          _desugar_input_jar,
        ]
        outputs = [
          _desugar_output_jar,
        ]
        if (defined(invoker.alternative_android_sdk_ijar)) {
          deps += [ invoker.alternative_android_sdk_ijar_dep ]
          _android_sdk_ijar = invoker.alternative_android_sdk_ijar
        } else {
          deps += [ "//build/android:android_ijar" ]
          _android_sdk_ijar = "$root_out_dir/lib.java/android.interface.jar"
        }
        inputs += [ _android_sdk_ijar ]
        args = [
          "--input-jar",
          rebase_path(_desugar_input_jar, root_build_dir),
          "--output-jar",
          rebase_path(_desugar_output_jar, root_build_dir),
          "--classpath=@FileArg($_rebased_build_config:javac:interface_classpath)",
          "--bootclasspath-entry",
          rebase_path(_android_sdk_ijar, root_build_dir),
        ]
      }

      _deps = []
      _deps = [ ":$_desugar_target" ]
      _previous_output_jar = _desugar_output_jar
    }

    if (_filter_jar) {
      _filter_target = "${target_name}__filter"
      _filter_input_jar = _previous_output_jar
      _filter_output_jar = "$target_out_dir/$target_name-filtered.jar"

      action(_filter_target) {
        script = "//build/android/gyp/jar.py"
        deps = _deps
        if (defined(invoker.deps)) {
          deps += invoker.deps
        }
        if (defined(invoker.public_deps)) {
          public_deps = invoker.public_deps
        }
        inputs = [
          _build_config,
          _filter_input_jar,
        ]
        outputs = [
          _filter_output_jar,
        ]
        args = [
          "--input-jar",
          rebase_path(_filter_input_jar, root_build_dir),
          "--jar-path",
          rebase_path(_filter_output_jar, root_build_dir),
          "--excluded-classes=$_jar_excluded_patterns",
        ]
        if (_strip_resource_classes) {
          args += [ "--strip-resource-classes-for=@FileArg($_rebased_build_config:javac:resource_packages)" ]
        }
      }

      _deps = []
      _deps = [ ":$_filter_target" ]
      _previous_output_jar = _filter_output_jar
    }

    _output_jar_target = "${target_name}__copy"

    # This is copy_ex rather than copy to ensure that JARs (rather than
    # possibly broken symlinks to them) get copied into the output
    # directory.
    copy_ex(_output_jar_target) {
      deps = _deps
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
      if (defined(invoker.public_deps)) {
        public_deps = invoker.public_deps
      }
      dest = _output_jar_path
      sources = [
        _previous_output_jar,
      ]
      outputs = [
        _output_jar_path,
      ]
    }

    group(target_name) {
      forward_variables_from(invoker,
                             [
                               "data_deps",
                               "visibility",
                             ])
      public_deps = [
        ":$_output_jar_target",
      ]
    }
  }

  template("emma_instr") {
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "testonly",
                             ])

      _coverage_file = "$target_out_dir/${target_name}.em"
      _source_dirs_listing_file = "$target_out_dir/${target_name}_sources.txt"
      _emma_jar = "${android_sdk_root}/tools/lib/emma.jar"

      script = "//build/android/gyp/emma_instr.py"
      depfile = "${target_gen_dir}/${target_name}.d"
      inputs = invoker.java_files + [
                 _emma_jar,
                 invoker.input_jar_path,
               ]
      outputs = [
        _coverage_file,
        _source_dirs_listing_file,
        invoker.output_jar_path,
      ]
      args = [
        "instrument_jar",
        "--input-path",
        rebase_path(invoker.input_jar_path, root_build_dir),
        "--output-path",
        rebase_path(invoker.output_jar_path, root_build_dir),
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--coverage-file",
        rebase_path(_coverage_file, root_build_dir),
        "--sources-list-file",
        rebase_path(_source_dirs_listing_file, root_build_dir),
        "--src-root",
        rebase_path("//", root_build_dir),
        "--emma-jar",
        rebase_path(_emma_jar, root_build_dir),
      ]
      _rebased_java_sources_file =
          rebase_path(invoker.java_sources_file, root_build_dir)
      args += [ "--java-sources-file=$_rebased_java_sources_file" ]

      if (emma_filter != "") {
        args += [
          "--filter-string",
          emma_filter,
        ]
      }
    }
  }

  # Runs process_resources.py
  template("process_resources") {
    action(target_name) {
      set_sources_assignment_filter([])
      forward_variables_from(invoker,
                             [
                               "deps",
                               "testonly",
                               "visibility",
                             ])
      script = "//build/android/gyp/process_resources.py"

      depfile = "$target_gen_dir/$target_name.d"
      outputs = []
      _all_resource_dirs = []
      sources = []

      if (defined(invoker.resource_dirs)) {
        _all_resource_dirs += invoker.resource_dirs

        # Speed up "gn gen" by short-circuiting the empty directory.
        if (invoker.resource_dirs != [ "//build/android/empty" ] &&
            invoker.resource_dirs != []) {
          _sources_build_rel =
              exec_script("//build/android/gyp/find.py",
                          rebase_path(invoker.resource_dirs, root_build_dir),
                          "list lines")
          sources += rebase_path(_sources_build_rel, ".", root_build_dir)
        }
      }

      if (defined(invoker.generated_resource_dirs)) {
        assert(defined(invoker.generated_resource_files))
        _all_resource_dirs += invoker.generated_resource_dirs
        sources += invoker.generated_resource_files
      }

      _android_aapt_path = android_default_aapt_path

      _android_sdk_jar = android_sdk_jar
      if (defined(invoker.alternative_android_sdk_jar)) {
        _android_sdk_jar = invoker.alternative_android_sdk_jar
      }

      inputs = [
        invoker.build_config,
        invoker.android_manifest,
        _android_aapt_path,
        _android_sdk_jar,
      ]

      _rebased_all_resource_dirs =
          rebase_path(_all_resource_dirs, root_build_dir)
      _rebased_build_config = rebase_path(invoker.build_config, root_build_dir)

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--android-sdk-jar",
        rebase_path(_android_sdk_jar, root_build_dir),
        "--aapt-path",
        rebase_path(_android_aapt_path, root_build_dir),
        "--android-manifest",
        rebase_path(invoker.android_manifest, root_build_dir),
        "--resource-dirs=$_rebased_all_resource_dirs",
        "--dependencies-res-zips=@FileArg($_rebased_build_config:resources:dependency_zips)",
        "--extra-res-packages=@FileArg($_rebased_build_config:resources:extra_package_names)",
        "--extra-r-text-files=@FileArg($_rebased_build_config:resources:extra_r_text_files)",
      ]

      if (defined(invoker.zip_path)) {
        outputs += [ invoker.zip_path ]
        args += [
          "--resource-zip-out",
          rebase_path(invoker.zip_path, root_build_dir),
        ]
      }

      if (defined(invoker.all_resources_zip_path)) {
        _all_resources_zip = invoker.all_resources_zip_path
        outputs += [ _all_resources_zip ]
        args += [
          "--all-resources-zip-out",
          rebase_path(_all_resources_zip, root_build_dir),
        ]
      }

      if (defined(invoker.r_text_out_path)) {
        outputs += [ invoker.r_text_out_path ]
        args += [
          "--r-text-out",
          rebase_path(invoker.r_text_out_path, root_build_dir),
        ]
      }

      if (defined(invoker.srcjar_path)) {
        outputs += [ invoker.srcjar_path ]
        args += [
          "--srcjar-out",
          rebase_path(invoker.srcjar_path, root_build_dir),
        ]
      }

      if (defined(invoker.r_text_in_path)) {
        _r_text_in_path = invoker.r_text_in_path
        inputs += [ _r_text_in_path ]
        args += [
          "--r-text-in",
          rebase_path(_r_text_in_path, root_build_dir),
        ]
      }

      if (!defined(invoker.generate_constant_ids) ||
          !invoker.generate_constant_ids) {
        args += [ "--non-constant-id" ]
      }

      if (defined(invoker.custom_package)) {
        args += [
          "--custom-package",
          invoker.custom_package,
        ]
      }

      if (defined(invoker.v14_skip) && invoker.v14_skip) {
        args += [ "--v14-skip" ]
      }

      if (defined(invoker.shared_resources) && invoker.shared_resources) {
        args += [ "--shared-resources" ]
      }

      if (defined(invoker.app_as_shared_lib) && invoker.app_as_shared_lib) {
        args += [ "--app-as-shared-lib" ]
      }

      if (defined(invoker.include_all_resources) &&
          invoker.include_all_resources) {
        args += [ "--include-all-resources" ]
      }

      if (defined(invoker.proguard_file)) {
        outputs += [ invoker.proguard_file ]
        args += [
          "--proguard-file",
          rebase_path(invoker.proguard_file, root_build_dir),
        ]
      }

      if (defined(invoker.proguard_file_main_dex)) {
        outputs += [ invoker.proguard_file_main_dex ]
        args += [
          "--proguard-file-main-dex",
          rebase_path(invoker.proguard_file_main_dex, root_build_dir),
        ]
      }

      if (defined(invoker.support_zh_hk) && invoker.support_zh_hk) {
        args += [ "--support-zh-hk" ]
      }

      if (defined(invoker.args)) {
        args += invoker.args
      }
    }
  }

  # Runs aapt to create an .ap_ file, which is a zip file containing
  # compiled xml and a resources.arsc file.
  #
  # Required Variables:
  #   output: Path to .ap_ to create.
  #   android_manifest: The AndroidManifest.xml for the apk.
  #   version_code: The verison code to use.
  #   version_name: The verison name to use.
  # Optional Variables:
  #   aapt_locale_whitelist: If set, all locales not in this list will be
  #     stripped from resources.arsc.
  #   alternative_android_sdk_jar: An alternative android sdk jar.
  #   app_as_shared_lib: Enables --app-as-shared-lib.
  #   exclude_xxxhdpi: Causes all drawable-xxxhdpi images to be excluded
  #     (mipmaps are still included).
  #   png_to_webp: If true, pngs (with the exception of 9-patch) are
  #     converted to webp.
  #   post_process_script: Script to call to post-process the .ap_.
  #   resources_zip: Resource .zip file created by process_resources to package.
  #   shared_resources: Enables --shared-lib.
  #   density_splits: A list of densities to create apk splits for.
  #   language_splits: A list of language codes to create apk splits for.
  #   xxxhdpi_whitelist: A list of globs used when exclude_xxxhdpi=true. Files
  #     that match this whitelist will still be included.
  template("package_resources") {
    _post_process = defined(invoker.post_process_script)
    _package_resources_target_name = target_name
    _packaged_resources_path = invoker.output
    if (_post_process) {
      _package_resources_target_name = "${target_name}__intermediate"
      _packaged_resources_path =
          get_path_info(_packaged_resources_path, "dir") + "/" +
          get_path_info(_packaged_resources_path, "name") + ".intermediate.ap_"
    }

    action(_package_resources_target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "testonly",
                               "visibility",
                             ])

      script = "//build/android/gyp/package_resources.py"
      depfile = "${target_gen_dir}/${target_name}.d"
      outputs = [
        _packaged_resources_path,
      ]

      _android_sdk_jar = android_sdk_jar
      if (defined(invoker.alternative_android_sdk_jar)) {
        _android_sdk_jar = invoker.alternative_android_sdk_jar
      }

      inputs = [
        android_default_aapt_path,
        _android_sdk_jar,
        invoker.android_manifest,
      ]

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--android-sdk-jar",
        rebase_path(_android_sdk_jar, root_build_dir),
        "--aapt-path",
        rebase_path(android_default_aapt_path, root_build_dir),
        "--android-manifest",
        rebase_path(invoker.android_manifest, root_build_dir),
        "--version-code",
        invoker.version_code,
        "--version-name",
        invoker.version_name,
        "--apk-path",
        rebase_path(_packaged_resources_path, root_build_dir),
      ]

      # Useful to have android:debuggable in the manifest even for Release
      # builds. Just omit it for officai
      if (debuggable_apks) {
        args += [ "--debuggable" ]
      }

      if (defined(invoker.resources_zip)) {
        inputs += [ invoker.resources_zip ]
        args += [
          "--resource-zips",
          rebase_path(invoker.resources_zip, root_build_dir),
        ]
      }
      if (defined(invoker.shared_resources) && invoker.shared_resources) {
        args += [ "--shared-resources" ]
      }
      if (defined(invoker.app_as_shared_lib) && invoker.app_as_shared_lib) {
        args += [ "--app-as-shared-lib" ]
      }
      if (defined(invoker.density_splits) && invoker.density_splits != []) {
        args += [ "--create-density-splits" ]
        foreach(_density, invoker.density_splits) {
          outputs += [ "${invoker.output}_${_density}" ]
        }
      }
      if (defined(invoker.language_splits) && invoker.language_splits != []) {
        args += [ "--language-splits=${invoker.language_splits}" ]
        foreach(_language, invoker.language_splits) {
          outputs += [ "${invoker.output}_${_language}" ]
        }
      }
      if (defined(invoker.aapt_locale_whitelist)) {
        args += [ "--locale-whitelist=${invoker.aapt_locale_whitelist}" ]
      }
      if (defined(invoker.png_to_webp) && invoker.png_to_webp) {
        _webp_target = "//third_party/libwebp:cwebp($host_toolchain)"
        deps += [ _webp_target ]
        args += [
          "--png-to-webp",
          "--webp-binary",
          rebase_path(get_label_info(_webp_target, "root_out_dir") + "/cwebp",
                      root_build_dir),
        ]
      }
      if (defined(invoker.exclude_xxxhdpi) && invoker.exclude_xxxhdpi) {
        args += [ "--exclude-xxxhdpi" ]
        if (defined(invoker.xxxhdpi_whitelist)) {
          args += [ "--xxxhdpi-whitelist=${invoker.xxxhdpi_whitelist}" ]
        }
      }
      if (defined(invoker.support_zh_hk) && invoker.support_zh_hk) {
        args += [ "--support-zh-hk" ]
      }
    }

    if (_post_process) {
      action(target_name) {
        depfile = "${target_gen_dir}/${target_name}.d"
        script = invoker.post_process_script
        args = [
          "--depfile",
          rebase_path(depfile, root_build_dir),
          "--apk-path",
          rebase_path(_packaged_resources_path, root_build_dir),
          "--output",
          rebase_path(invoker.output, root_build_dir),
        ]
        inputs = [
          _packaged_resources_path,
        ]
        outputs = [
          invoker.output,
        ]
        public_deps = [
          ":${_package_resources_target_name}",
        ]
      }
    }
  }

  # Creates an unsigned .apk.
  #
  # Variables
  #   assets_build_config: Path to android_apk .build_config containing merged
  #       asset information.
  #   deps: Specifies the dependencies of this target.
  #   dex_path: Path to classes.dex file to include (optional).
  #   packaged_resources_path: Path to .ap_ to use.
  #   output_apk_path: Output path for the generated .apk.
  #   native_lib_placeholders: List of placeholder filenames to add to the apk
  #     (optional).
  #   native_libs: List of native libraries.
  #   native_libs_filearg: @FileArg() of additionally native libraries.
  #   write_asset_list: Adds an extra file to the assets, which contains a list of
  #     all other asset files.
  template("package_apk") {
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "public_deps",
                               "testonly",
                             ])
      _native_lib_placeholders = []
      if (defined(invoker.native_lib_placeholders)) {
        _native_lib_placeholders = invoker.native_lib_placeholders
      }

      script = "//build/android/gyp/apkbuilder.py"
      depfile = "$target_gen_dir/$target_name.d"
      data_deps = [
        "//tools/android/md5sum",
      ]  # Used when deploying APKs

      inputs = invoker.native_libs + [ invoker.packaged_resources_path ]
      if (defined(invoker.dex_path)) {
        inputs += [ invoker.dex_path ]
      }

      outputs = [
        invoker.output_apk_path,
      ]

      _rebased_packaged_resources_path =
          rebase_path(invoker.packaged_resources_path, root_build_dir)
      _rebased_packaged_apk_path =
          rebase_path(invoker.output_apk_path, root_build_dir)
      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--resource-apk=$_rebased_packaged_resources_path",
        "--output-apk=$_rebased_packaged_apk_path",
      ]
      if (defined(invoker.assets_build_config)) {
        inputs += [ invoker.assets_build_config ]
        _rebased_build_config =
            rebase_path(invoker.assets_build_config, root_build_dir)
        args += [
          "--assets=@FileArg($_rebased_build_config:assets)",
          "--uncompressed-assets=@FileArg($_rebased_build_config:uncompressed_assets)",
        ]

        # TODO(mlopatkin) We are relying on the fact that assets_build_config is
        # an APK build_config.
        args += [ "--java-resources=@FileArg($_rebased_build_config:java_resources_jars)" ]

        if (defined(invoker.apk_name)) {
          # The supersize tool will search in this directory for each apk.
          _apk_pak_info_path = "size-info/${invoker.apk_name}.apk.pak.info"
          args += [
            "--apk-pak-info-path",
            _apk_pak_info_path,
          ]
        }
      }
      if (defined(invoker.write_asset_list) && invoker.write_asset_list) {
        args += [ "--write-asset-list" ]
      }
      if (defined(invoker.dex_path)) {
        _rebased_dex_path = rebase_path(invoker.dex_path, root_build_dir)
        args += [ "--dex-file=$_rebased_dex_path" ]
      }
      if (invoker.native_libs != [] || defined(invoker.native_libs_filearg) ||
          _native_lib_placeholders != []) {
        args += [ "--android-abi=$android_app_abi" ]
      }
      if (invoker.native_libs != []) {
        _rebased_native_libs = rebase_path(invoker.native_libs, root_build_dir)
        args += [ "--native-libs=$_rebased_native_libs" ]
      }
      if (defined(invoker.native_libs_filearg)) {
        args += [ "--native-libs=${invoker.native_libs_filearg}" ]
      }
      if (_native_lib_placeholders != []) {
        args += [ "--native-lib-placeholders=$_native_lib_placeholders" ]
      }

      # TODO (michaelbai): Remove the secondary_native_libs variable.
      if (defined(invoker.secondary_abi_native_libs_filearg)) {
        assert(defined(android_app_secondary_abi))
        args += [
          "--secondary-native-libs=${invoker.secondary_abi_native_libs_filearg}",
          "--secondary-android-abi=$android_app_secondary_abi",
        ]
      } else if (defined(invoker.secondary_native_libs) &&
                 invoker.secondary_native_libs != []) {
        assert(defined(android_app_secondary_abi))
        inputs += invoker.secondary_native_libs
        _secondary_native_libs = rebase_path(invoker.secondary_native_libs)
        args += [
          "--secondary-native-libs=$_secondary_native_libs",
          "--secondary-android-abi=$android_app_secondary_abi",
        ]
      }

      if (defined(invoker.uncompress_shared_libraries) &&
          invoker.uncompress_shared_libraries) {
        args += [ "--uncompress-shared-libraries" ]
      }
    }
  }

  # Signs & zipaligns an apk.
  #
  # Variables
  #   input_apk_path: Path of the .apk to be finalized.
  #   output_apk_path: Output path for the generated .apk.
  #   keystore_path: Path to keystore to use for signing.
  #   keystore_name: Key alias to use.
  #   keystore_password: Keystore password.
  template("finalize_apk") {
    action(target_name) {
      deps = []
      script = "//build/android/gyp/finalize_apk.py"
      depfile = "$target_gen_dir/$target_name.d"
      forward_variables_from(invoker,
                             [
                               "deps",
                               "data_deps",
                               "public_deps",
                               "testonly",
                             ])

      sources = [
        invoker.input_apk_path,
      ]
      inputs = [
        invoker.keystore_path,
      ]
      outputs = [
        invoker.output_apk_path,
      ]
      data = [
        invoker.output_apk_path,
      ]

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--zipalign-path",
        rebase_path(zipalign_path, root_build_dir),
        "--unsigned-apk-path",
        rebase_path(invoker.input_apk_path, root_build_dir),
        "--final-apk-path",
        rebase_path(invoker.output_apk_path, root_build_dir),
        "--key-path",
        rebase_path(invoker.keystore_path, root_build_dir),
        "--key-name",
        invoker.keystore_name,
        "--key-passwd",
        invoker.keystore_password,
      ]
    }
  }

  # Packages resources, assets, dex, and native libraries into an apk. Signs and
  # zipaligns the apk.
  template("create_apk") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    _android_manifest = invoker.android_manifest
    _base_path = invoker.base_path
    _final_apk_path = invoker.apk_path
    _incremental_final_apk_path_helper =
        process_file_template(
            [ _final_apk_path ],
            "{{source_dir}}/{{source_name_part}}_incremental.apk")
    _incremental_final_apk_path = _incremental_final_apk_path_helper[0]

    if (defined(invoker.dex_path)) {
      _dex_path = invoker.dex_path
    }
    _load_library_from_apk = invoker.load_library_from_apk

    _deps = []
    if (defined(invoker.deps)) {
      _deps = invoker.deps
    }
    _incremental_deps = []
    if (defined(invoker.incremental_deps)) {
      _incremental_deps = invoker.incremental_deps
    }
    _native_libs = []
    if (defined(invoker.native_libs)) {
      _native_libs = invoker.native_libs
    }
    _native_libs_even_when_incremental = []
    if (defined(invoker.native_libs_even_when_incremental)) {
      _native_libs_even_when_incremental =
          invoker.native_libs_even_when_incremental
    }

    _incremental_packaged_resources_path = "${_base_path}_incremental.ap_"
    _packaged_apk_path = "${_base_path}_unsigned.apk"
    _incremental_packaged_apk_path = "${_base_path}_incremental_unsigned.apk"
    _shared_resources =
        defined(invoker.shared_resources) && invoker.shared_resources
    assert(_shared_resources || true)  # Mark as used.
    _app_as_shared_lib =
        defined(invoker.app_as_shared_lib) && invoker.app_as_shared_lib
    assert(_app_as_shared_lib || true)  # Mark as used.
    assert(!(_shared_resources && _app_as_shared_lib))

    _keystore_path = invoker.keystore_path
    _keystore_name = invoker.keystore_name
    _keystore_password = invoker.keystore_password

    _incremental_package_resources_target_name =
        "${target_name}_incremental__package_resources"
    _incremental_android_manifest =
        get_label_info(_incremental_package_resources_target_name,
                       "target_gen_dir") + "/AndroidManifest.xml"
    action(_incremental_package_resources_target_name) {
      deps = _incremental_deps
      script =
          "//build/android/incremental_install/generate_android_manifest.py"
      inputs = [
        # Save on a depfile by listing only .py dep here.
        "//build/android/gyp/util/build_utils.py",
        _android_manifest,
        invoker.packaged_resources_path,
      ]
      outputs = [
        # Output the non-compiled manifest for easy debugging (as opposed to
        # generating to a temp file).
        _incremental_android_manifest,
        _incremental_packaged_resources_path,
      ]

      _android_sdk_jar = android_sdk_jar
      if (defined(invoker.alternative_android_sdk_jar)) {
        _android_sdk_jar = invoker.alternative_android_sdk_jar
      }
      args = [
        "--src-manifest",
        rebase_path(_android_manifest, root_build_dir),
        "--out-manifest",
        rebase_path(_incremental_android_manifest, root_build_dir),
        "--in-apk",
        rebase_path(invoker.packaged_resources_path, root_build_dir),
        "--out-apk",
        rebase_path(_incremental_packaged_resources_path, root_build_dir),
        "--aapt-path",
        rebase_path(android_default_aapt_path, root_build_dir),
        "--android-sdk-jar",
        rebase_path(_android_sdk_jar, root_build_dir),
      ]
      if (disable_incremental_isolated_processes) {
        args += [ "--disable-isolated-processes" ]
      }
    }

    _package_target = "${target_name}__package_apk"
    package_apk(_package_target) {
      forward_variables_from(invoker,
                             [
                               "assets_build_config",
                               "native_lib_placeholders",
                               "native_libs_filearg",
                               "packaged_resources_path",
                               "secondary_abi_native_libs_filearg",
                               "secondary_native_libs",
                               "uncompress_shared_libraries",
                               "write_asset_list",
                             ])
      if (!defined(uncompress_shared_libraries)) {
        uncompress_shared_libraries = _load_library_from_apk
      }
      deps = _deps
      native_libs = _native_libs + _native_libs_even_when_incremental

      if (defined(_dex_path)) {
        dex_path = _dex_path
      }

      if (defined(invoker.apk_name)) {
        apk_name = invoker.apk_name
      }

      output_apk_path = _packaged_apk_path
    }

    _incremental_package_target = "${target_name}_incremental__package_apk"
    package_apk(_incremental_package_target) {
      forward_variables_from(invoker,
                             [
                               "assets_build_config",
                               "secondary_native_libs",
                               "uncompress_shared_libraries",
                             ])
      if (!defined(uncompress_shared_libraries)) {
        uncompress_shared_libraries = _load_library_from_apk
      }
      _dex_target = "//build/android/incremental_install:bootstrap_java__dex"
      deps = _incremental_deps + [
               ":${_incremental_package_resources_target_name}",
               _dex_target,
             ]

      if (defined(_dex_path)) {
        dex_path =
            get_label_info(_dex_target, "target_gen_dir") + "/bootstrap.dex"
      }

      native_libs = _native_libs_even_when_incremental

      # http://crbug.com/384638
      _has_native_libs =
          defined(invoker.native_libs_filearg) || _native_libs != []
      if (_has_native_libs && _native_libs_even_when_incremental == []) {
        native_lib_placeholders = [ "libfix.crbug.384638.so" ]
      }

      output_apk_path = _incremental_packaged_apk_path
      packaged_resources_path = _incremental_packaged_resources_path
    }

    _finalize_apk_rule_name = "${target_name}__finalize"
    finalize_apk(_finalize_apk_rule_name) {
      input_apk_path = _packaged_apk_path
      output_apk_path = _final_apk_path
      keystore_path = _keystore_path
      keystore_name = _keystore_name
      keystore_password = _keystore_password

      public_deps = [
        # Generator of the _packaged_apk_path this target takes as input.
        ":$_package_target",
      ]
    }

    _incremental_finalize_apk_rule_name = "${target_name}_incremental__finalize"
    finalize_apk(_incremental_finalize_apk_rule_name) {
      input_apk_path = _incremental_packaged_apk_path
      output_apk_path = _incremental_final_apk_path
      keystore_path = _keystore_path
      keystore_name = _keystore_name
      keystore_password = _keystore_password

      public_deps = [
        ":$_incremental_package_target",
      ]
    }

    _split_deps = []

    template("finalize_split") {
      finalize_apk(target_name) {
        _config = invoker.split_config
        _type = invoker.split_type
        input_apk_path = "${_packaged_resources_path}_${_config}"
        _output_paths = process_file_template(
                [ _final_apk_path ],
                "{{source_dir}}/{{source_name_part}}-${_type}-${_config}.apk")
        output_apk_path = _output_paths[0]
        keystore_path = _keystore_path
        keystore_name = _keystore_name
        keystore_password = _keystore_password
        deps = _deps
      }
    }

    foreach(_split, invoker.density_splits) {
      _split_rule = "${target_name}__finalize_${_split}_split"
      finalize_split(_split_rule) {
        split_type = "density"
        split_config = _split
      }
      _split_deps += [ ":$_split_rule" ]
    }
    foreach(_split, invoker.language_splits) {
      _split_rule = "${target_name}__finalize_${_split}_split"
      finalize_split(_split_rule) {
        split_type = "lang"
        split_config = _split
      }
      _split_deps += [ ":$_split_rule" ]
    }

    group(target_name) {
      public_deps = [ ":${_finalize_apk_rule_name}" ] + _split_deps
    }
    group("${target_name}_incremental") {
      public_deps = [ ":${_incremental_finalize_apk_rule_name}" ] + _split_deps
    }
  }

  template("java_prebuilt_impl") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])
    _supports_android =
        defined(invoker.supports_android) && invoker.supports_android
    _requires_android =
        defined(invoker.requires_android) && invoker.requires_android

    assert(defined(invoker.jar_path))
    if (defined(invoker.output_name)) {
      _output_name = invoker.output_name
    } else {
      _output_name = get_path_info(invoker.jar_path, "name")
    }
    _base_path = "${target_gen_dir}/$target_name"

    # Jar files can be needed at runtime (by Robolectric tests or java binaries),
    # so do not put them under gen/.
    _target_dir_name = get_label_info(":$target_name", "dir")
    _jar_path = "$root_out_dir/lib.java$_target_dir_name/$_output_name.jar"
    _ijar_path =
        "$root_out_dir/lib.java$_target_dir_name/$_output_name.interface.jar"
    _build_config = _base_path + ".build_config"

    if (_supports_android) {
      _dex_path = _base_path + ".dex.jar"
    }
    _deps = []
    if (defined(invoker.deps)) {
      _deps = invoker.deps
    }
    _jar_deps = []
    if (defined(invoker.jar_dep)) {
      _jar_deps = [ invoker.jar_dep ]
    }

    _template_name = target_name

    _build_config_target_name = "${_template_name}__build_config"
    _process_jar_target_name = "${_template_name}__process_jar"
    _ijar_target_name = "${_template_name}__ijar"
    if (_supports_android) {
      _dex_target_name = "${_template_name}__dex"
    }

    _enable_build_hooks =
        _supports_android &&
        (!defined(invoker.no_build_hooks) || !invoker.no_build_hooks)
    if (_enable_build_hooks) {
      _deps += [ "//build/android/buildhooks:build_hooks_java" ]
    }

    # Some testonly targets use their own resources and the code being
    # tested will use custom resources so there's no need to enable this
    # for testonly targets.
    _enable_build_hooks_android =
        _enable_build_hooks && _requires_android &&
        (!defined(invoker.testonly) || !invoker.testonly)
    if (_enable_build_hooks_android) {
      _deps += [ "//build/android/buildhooks:build_hooks_android_java" ]
    }

    write_build_config(_build_config_target_name) {
      type = "java_prebuilt"
      is_prebuilt_binary = defined(invoker.main_class)
      forward_variables_from(invoker,
                             [
                               "input_jars_paths",
                               "proguard_configs",
                             ])
      supports_android = _supports_android
      requires_android = _requires_android

      if (defined(invoker.deps)) {
        possible_config_deps = _deps
      }
      build_config = _build_config
      jar_path = _jar_path
      if (_supports_android) {
        dex_path = _dex_path
      }
      if (defined(invoker.include_java_resources) &&
          invoker.include_java_resources) {
        # Use original jar_path because _jar_path points to a library without
        # resources.
        java_resources_jar = invoker.jar_path
      }
    }

    process_java_prebuilt(_process_jar_target_name) {
      forward_variables_from(invoker,
                             [
                               "jar_excluded_patterns",
                               "strip_resource_classes",
                             ])

      visibility = [
        ":$_ijar_target_name",
        ":$_template_name",
      ]
      if (_supports_android) {
        visibility += [ ":$_dex_target_name" ]
      }

      supports_android = _supports_android
      enable_build_hooks = _enable_build_hooks
      enable_build_hooks_android = _enable_build_hooks_android
      build_config = _build_config
      input_jar_path = invoker.jar_path
      output_jar_path = _jar_path

      deps = [ ":$_build_config_target_name" ] + _deps + _jar_deps
    }

    generate_interface_jar(_ijar_target_name) {
      # Always used the unfiltered .jar to create the interface jar so that
      # other targets will resolve filtered classes when depending on
      # BuildConfig, NativeLibraries, etc.
      input_jar = invoker.jar_path
      deps = _deps + _jar_deps
      output_jar = _ijar_path
    }

    if (_supports_android) {
      dex(_dex_target_name) {
        sources = [
          _jar_path,
        ]
        output = _dex_path
        deps = [ ":$_process_jar_target_name" ] + _deps + _jar_deps
      }
    }

    if (defined(invoker.main_class)) {
      _binary_script_target_name = "${_template_name}__java_binary_script"
      java_binary_script(_binary_script_target_name) {
        forward_variables_from(invoker,
                               [
                                 "bootclasspath",
                                 "deps",
                                 "main_class",
                                 "wrapper_script_args",
                               ])
        if (!defined(deps)) {
          deps = []
        }
        build_config = _build_config
        jar_path = _jar_path
        script_name = _template_name
        if (defined(invoker.wrapper_script_name)) {
          script_name = invoker.wrapper_script_name
        }
        deps += [ ":$_build_config_target_name" ]
      }
    }

    group(target_name) {
      forward_variables_from(invoker,
                             [
                               "data",
                               "data_deps",
                               "visibility",
                             ])
      public_deps = [
        ":$_ijar_target_name",
        ":$_process_jar_target_name",
      ]
      if (_supports_android) {
        public_deps += [ ":$_dex_target_name" ]
      }
      if (defined(invoker.main_class)) {
        # Some targets use the generated script while building, so make it a dep
        # rather than a data_dep.
        public_deps += [ ":$_binary_script_target_name" ]
      }
    }
  }

  # Compiles and jars a set of java files.
  #
  # Outputs:
  #  $jar_path.jar
  #  $jar_path.interface.jar
  #
  # Variables
  #   java_files: List of .java files to compile (same as exists in java_sources_file)
  #   java_sources_file: Path to file containing list of files to compile.
  #   chromium_code: If true, enable extra warnings.
  #   srcjar_deps: List of srcjar dependencies. The .java files contained in the
  #     dependencies srcjar outputs will be compiled and added to the output jar.
  #   jar_path: Use this to explicitly set the output jar path. Defaults to
  #     "${target_gen_dir}/${target_name}.jar.
  #   javac_args: Additional arguments to pass to javac.
  template("compile_java") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])

    assert(defined(invoker.build_config))
    assert(defined(invoker.jar_path))

    _build_config = invoker.build_config

    _chromium_code = false
    if (defined(invoker.chromium_code)) {
      _chromium_code = invoker.chromium_code
    }

    _supports_android = true
    if (defined(invoker.supports_android)) {
      _supports_android = invoker.supports_android
    }

    _requires_android =
        defined(invoker.requires_android) && invoker.requires_android

    _enable_errorprone = use_errorprone_java_compiler
    if (!_chromium_code) {
      _enable_errorprone = false
    } else if (defined(invoker.enable_errorprone)) {
      _enable_errorprone = invoker.enable_errorprone
    }

    _provider_configurations = []
    if (defined(invoker.provider_configurations)) {
      _provider_configurations = invoker.provider_configurations
    }

    _processors = []
    _enable_interface_jars_javac = true
    if (defined(invoker.processors_javac)) {
      _processors = invoker.processors_javac
      _enable_interface_jars_javac = _processors == []
    }

    _processor_args = []
    if (defined(invoker.processor_args_javac)) {
      _processor_args = invoker.processor_args_javac
    }

    _additional_jar_files = []
    if (defined(invoker.additional_jar_files)) {
      _additional_jar_files = invoker.additional_jar_files
    }

    if (defined(invoker.enable_incremental_javac_override)) {
      # Use invoker-specified override.
      _enable_incremental_javac = invoker.enable_incremental_javac_override
    } else {
      # Default to build arg if not overridden.
      _enable_incremental_javac = enable_incremental_javac
    }

    _manifest_entries = []
    if (defined(invoker.manifest_entries)) {
      _manifest_entries = invoker.manifest_entries
    }

    _srcjar_deps = []
    if (defined(invoker.srcjar_deps)) {
      _srcjar_deps += invoker.srcjar_deps
    }

    _java_srcjars = []
    if (defined(invoker.srcjars)) {
      _java_srcjars = invoker.srcjars
    }
    foreach(dep, _srcjar_deps) {
      _dep_gen_dir = get_label_info(dep, "target_gen_dir")
      _dep_name = get_label_info(dep, "name")
      _java_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
    }

    _javac_args = []
    if (defined(invoker.javac_args)) {
      _javac_args = invoker.javac_args
    }

    # Mark srcjar_deps as used.
    assert(_srcjar_deps == [] || true)

    _javac_target_name = "${target_name}__javac"
    _process_prebuilt_target_name = "${target_name}__process_prebuilt"
    _ijar_target_name = "${target_name}__ijar"
    _final_target_name = target_name

    _final_jar_path = invoker.jar_path
    _javac_jar_path = "$target_gen_dir/$target_name.javac.jar"
    _process_prebuilt_jar_path = _final_jar_path
    _final_ijar_path = get_path_info(_final_jar_path, "dir") + "/" +
                       get_path_info(_final_jar_path, "name") + ".interface.jar"

    _emma_instrument = defined(invoker.emma_instrument) &&
                       invoker.emma_instrument && invoker.java_files != []
    if (_emma_instrument) {
      _emma_instr_target_name = "${target_name}__emma_instr"
      _process_prebuilt_jar_path =
          "$target_gen_dir/$target_name.process_prebuilt.jar"
    }

    _rebased_build_config = rebase_path(_build_config, root_build_dir)
    _rebased_jar_path = rebase_path(_javac_jar_path, root_build_dir)

    action(_javac_target_name) {
      script = "//build/android/gyp/javac.py"
      depfile = "$target_gen_dir/$target_name.d"
      deps = _srcjar_deps
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }

      outputs = [
        _javac_jar_path,
        _javac_jar_path + ".md5.stamp",
      ]
      sources = invoker.java_files + _java_srcjars
      inputs = [
        _build_config,
      ]
      if (invoker.java_files != []) {
        inputs += [ invoker.java_sources_file ]
      }

      _rebased_java_srcjars = rebase_path(_java_srcjars, root_build_dir)
      _rebased_depfile = rebase_path(depfile, root_build_dir)
      args = [
        "--depfile=$_rebased_depfile",
        "--jar-path=$_rebased_jar_path",
        "--java-srcjars=$_rebased_java_srcjars",
        "--java-srcjars=@FileArg($_rebased_build_config:javac:srcjars)",
        "--java-version=1.8",
      ]
      if (_enable_interface_jars_javac) {
        args += [ "--classpath=@FileArg($_rebased_build_config:javac:interface_classpath)" ]
      } else {
        args +=
            [ "--classpath=@FileArg($_rebased_build_config:javac:classpath)" ]
      }
      if (_enable_incremental_javac) {
        args += [ "--incremental" ]
        deps += [ "//third_party/jmake($default_toolchain)" ]
        inputs += [ "$root_build_dir/bin/jmake" ]
        outputs += [ "${_javac_jar_path}.pdb" ]
      }
      if (_requires_android) {
        if (defined(invoker.alternative_android_sdk_ijar)) {
          deps += [ invoker.alternative_android_sdk_ijar_dep ]
          _android_sdk_ijar = invoker.alternative_android_sdk_ijar
        } else {
          deps += [ "//build/android:android_ijar" ]
          _android_sdk_ijar = "$root_out_dir/lib.java/android.interface.jar"
        }
        inputs += [ _android_sdk_ijar ]
        _rebased_android_sdk_ijar =
            rebase_path(_android_sdk_ijar, root_build_dir)
        args += [ "--bootclasspath=$_rebased_android_sdk_ijar" ]
      }
      foreach(e, _manifest_entries) {
        args += [ "--manifest-entry=" + e ]
      }
      if (_chromium_code) {
        args += [ "--chromium-code=1" ]
      }
      if (_enable_errorprone) {
        deps +=
            [ "//third_party/errorprone:errorprone_java($default_toolchain)" ]
        deps += [ "//tools/android/errorprone_plugin:errorprone_plugin_java($default_toolchain)" ]
        args += [
          "--use-errorprone-path",
          "bin/errorprone_java",
          "--processorpath",
          "lib.java/tools/android/errorprone_plugin/errorprone_plugin_java.jar",
        ]
      }
      foreach(e, _provider_configurations) {
        args += [ "--provider-configuration=" + rebase_path(e, root_build_dir) ]
      }
      foreach(e, _processors) {
        args += [ "--processor=" + e ]
      }
      foreach(e, _processor_args) {
        args += [ "--processor-arg=" + e ]
      }

      foreach(file_tuple, _additional_jar_files) {
        # Each element is of length two, [ path_to_file, path_to_put_in_jar ]
        inputs += [ file_tuple[0] ]
        args +=
            [ "--additional-jar-file=" + file_tuple[0] + ":" + file_tuple[1] ]
      }
      if (invoker.java_files != []) {
        args += [ "@" + rebase_path(invoker.java_sources_file, root_build_dir) ]
      }
      foreach(e, _javac_args) {
        args += [ "--javac-arg=" + e ]
      }
    }

    process_java_prebuilt(_process_prebuilt_target_name) {
      forward_variables_from(invoker,
                             [
                               "alternative_android_sdk_ijar",
                               "alternative_android_sdk_ijar_dep",
                               "alternative_android_sdk_jar",
                               "enable_build_hooks",
                               "enable_build_hooks_android",
                               "jar_excluded_patterns",
                             ])
      supports_android = _supports_android
      build_config = _build_config
      input_jar_path = _javac_jar_path
      output_jar_path = _process_prebuilt_jar_path

      deps = [
        ":$_javac_target_name",
      ]
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
    }

    if (_emma_instrument) {
      emma_instr(_emma_instr_target_name) {
        forward_variables_from(invoker,
                               [
                                 "deps",
                                 "java_files",
                                 "java_sources_file",
                               ])

        input_jar_path = _process_prebuilt_jar_path
        output_jar_path = _final_jar_path

        if (!defined(deps)) {
          deps = []
        }
        deps += [ ":$_process_prebuilt_target_name" ]
      }
    }

    generate_interface_jar(_ijar_target_name) {
      # Always used the unfiltered .jar to create the interface jar so that
      # other targets will resolve filtered classes when depending on
      # BuildConfig, NativeLibraries, etc.
      input_jar = _javac_jar_path
      deps = [
        ":$_javac_target_name",
      ]
      output_jar = _final_ijar_path
    }

    group(_final_target_name) {
      forward_variables_from(invoker, [ "visibility" ])
      public_deps = [
        ":$_ijar_target_name",
        ":$_javac_target_name",
      ]
      if (_emma_instrument) {
        public_deps += [ ":$_emma_instr_target_name" ]
      } else {
        public_deps += [ ":$_process_prebuilt_target_name" ]
      }
    }
  }

  template("java_library_impl") {
    set_sources_assignment_filter([])
    forward_variables_from(invoker, [ "testonly" ])
    _accumulated_deps = []
    if (defined(invoker.deps)) {
      _accumulated_deps = invoker.deps
    }

    # Caller overriding build config must have valid java sources file if it has
    # java files.
    assert(!defined(invoker.override_build_config) ||
           !defined(invoker.java_files) || defined(invoker.java_sources_file))

    assert(defined(invoker.java_files) || defined(invoker.srcjars) ||
           defined(invoker.srcjar_deps))
    _base_path = "$target_gen_dir/$target_name"
    assert(_base_path != "")  # Mark as used

    if (defined(invoker.output_name)) {
      _output_name = invoker.output_name
    } else {
      _output_name = target_name
    }

    # Jar files can be needed at runtime (by Robolectric tests or java binaries),
    # so do not put them under gen/.
    target_dir_name = get_label_info(":$target_name", "dir")
    _jar_path = "$root_out_dir/lib.java$target_dir_name/$_output_name.jar"
    if (defined(invoker.jar_path)) {
      _jar_path = invoker.jar_path
    }
    _template_name = target_name

    _final_deps = []

    _supports_android =
        defined(invoker.supports_android) && invoker.supports_android
    _requires_android =
        defined(invoker.requires_android) && invoker.requires_android
    assert(_requires_android || true)  # Mark as used.
    _android_manifest = "//build/android/AndroidManifest.xml"
    if (defined(invoker.android_manifest)) {
      _android_manifest = invoker.android_manifest
    }
    assert(_android_manifest != "")  # Mark as used.

    _enable_build_hooks =
        _supports_android &&
        (!defined(invoker.no_build_hooks) || !invoker.no_build_hooks)
    if (_enable_build_hooks) {
      _accumulated_deps += [ "//build/android/buildhooks:build_hooks_java" ]
    }

    # Some testonly targets use their own resources and the code being
    # tested will use custom resources so there's no need to enable this
    # for testonly targets.
    _enable_build_hooks_android =
        _enable_build_hooks && _requires_android &&
        (!defined(invoker.testonly) || !invoker.testonly)
    if (_enable_build_hooks_android) {
      _accumulated_deps +=
          [ "//build/android/buildhooks:build_hooks_android_java" ]
    }

    # Don't enable coverage or lint unless the target has some non-generated
    # files.
    if (defined(invoker.chromium_code)) {
      _chromium_code = invoker.chromium_code
    } else {
      _chromium_code = defined(invoker.java_files) && invoker.java_files != []
      if (_chromium_code) {
        # Make chromium_code = false be the default for targets within
        # third_party which contain no chromium-namespaced java files.
        set_sources_assignment_filter([ "*\bthird_party\b*" ])
        sources = [
          get_label_info(":$target_name", "dir"),
        ]
        if (sources == []) {
          set_sources_assignment_filter([ "*\bchromium\b*" ])
          sources = invoker.java_files
          _chromium_code = invoker.java_files != sources
        }
        set_sources_assignment_filter([])
        sources = []
      }
    }

    _emma_never_instrument = !_chromium_code
    if (defined(invoker.emma_never_instrument)) {
      _emma_never_instrument = invoker.emma_never_instrument
    }
    assert(_emma_never_instrument || true)  # Mark as used
    _emma_instrument = emma_coverage && !_emma_never_instrument

    if (_supports_android) {
      _dex_path = _base_path + ".dex.jar"
      if (defined(invoker.dex_path)) {
        _dex_path = invoker.dex_path
      }
    }

    _java_files = []
    if (defined(invoker.java_files)) {
      _java_files += invoker.java_files
    }
    if (_java_files != []) {
      if (defined(invoker.java_sources_file)) {
        _java_sources_file = invoker.java_sources_file
      } else {
        _java_sources_file = "$_base_path.sources"
      }
      write_file(_java_sources_file, rebase_path(_java_files, root_build_dir))
    }

    # Define build_config_deps which will be a list of targets required to
    # build the _build_config.
    if (defined(invoker.override_build_config)) {
      _build_config = invoker.override_build_config
    } else {
      _build_config = _base_path + ".build_config"
      build_config_target_name = "${_template_name}__build_config"

      write_build_config(build_config_target_name) {
        forward_variables_from(invoker,
                               [
                                 "alternative_android_sdk_ijar",
                                 "classpath_deps",
                                 "gradle_treat_as_prebuilt",
                                 "input_jars_paths",
                                 "main_class",
                                 "proguard_configs",
                               ])
        if (defined(invoker.is_java_binary) && invoker.is_java_binary) {
          type = "java_binary"
        } else {
          type = "java_library"
        }
        possible_config_deps = _accumulated_deps
        supports_android = _supports_android
        requires_android = _requires_android
        bypass_platform_checks = defined(invoker.bypass_platform_checks) &&
                                 invoker.bypass_platform_checks

        build_config = _build_config
        jar_path = _jar_path
        if (_supports_android) {
          dex_path = _dex_path
        }
        if (_java_files != []) {
          java_sources_file = _java_sources_file
        }

        if (defined(invoker.srcjar_deps)) {
          bundled_srcjars = []
          foreach(d, invoker.srcjar_deps) {
            _dep_gen_dir = get_label_info(d, "target_gen_dir")
            _dep_name = get_label_info(d, "name")
            bundled_srcjars += [ "$_dep_gen_dir/$_dep_name.srcjar" ]
          }
        }
      }
      _accumulated_deps += [ ":$build_config_target_name" ]
    }
    if (defined(invoker.classpath_deps)) {
      _accumulated_deps += invoker.classpath_deps
    }

    _srcjar_deps = []
    if (defined(invoker.srcjar_deps)) {
      _srcjar_deps = invoker.srcjar_deps
    }

    _srcjars = []
    if (defined(invoker.srcjars)) {
      _srcjars = invoker.srcjars
    }

    assert(_java_files != [] || _srcjar_deps != [] || _srcjars != [])

    _compile_java_target = "${_template_name}__compile_java"
    _final_deps += [ ":$_compile_java_target" ]
    compile_java(_compile_java_target) {
      forward_variables_from(invoker,
                             [
                               "additional_jar_files",
                               "alternative_android_sdk_ijar",
                               "alternative_android_sdk_ijar_dep",
                               "alternative_android_sdk_jar",
                               "dist_jar_path",
                               "enable_errorprone",
                               "enable_incremental_javac_override",
                               "jar_excluded_patterns",
                               "manifest_entries",
                               "processors_javac",
                               "processor_args_javac",
                               "provider_configurations",
                               "javac_args",
                             ])
      jar_path = _jar_path
      build_config = _build_config
      java_files = _java_files
      if (_java_files != []) {
        java_sources_file = _java_sources_file
      }
      srcjar_deps = _srcjar_deps
      srcjars = _srcjars
      chromium_code = _chromium_code
      supports_android = _supports_android
      requires_android = _requires_android
      emma_instrument = _emma_instrument
      enable_build_hooks = _enable_build_hooks
      enable_build_hooks_android = _enable_build_hooks_android
      deps = _accumulated_deps
    }
    _accumulated_deps += [ ":$_compile_java_target" ]
    assert(_accumulated_deps != [])  # Mark used.

    if (defined(invoker.main_class)) {
      # Targets might use the generated script while building, so make it a dep
      # rather than a data_dep.
      _final_deps += [ ":${_template_name}__java_binary_script" ]
      java_binary_script("${_template_name}__java_binary_script") {
        forward_variables_from(invoker,
                               [
                                 "bootclasspath",
                                 "main_class",
                                 "wrapper_script_args",
                               ])
        build_config = _build_config
        jar_path = _jar_path
        script_name = _template_name
        if (defined(invoker.wrapper_script_name)) {
          script_name = invoker.wrapper_script_name
        }
        deps = _accumulated_deps
      }
    }

    _has_lint_target = false
    if (_supports_android) {
      if (_chromium_code) {
        _has_lint_target = true
        android_lint("${_template_name}__lint") {
          if (defined(invoker.lint_suppressions_file)) {
            lint_suppressions_file = invoker.lint_suppressions_file
          }

          android_manifest = _android_manifest
          build_config = _build_config

          # Run lint on javac output.
          jar_path = "$target_gen_dir/$_compile_java_target.javac.jar"

          java_files = _java_files
          if (_java_files != []) {
            java_sources_file = _java_sources_file
          }
          deps = _accumulated_deps
          if (_emma_instrument) {
            # Disable the NewApi lint warning when building with coverage
            # enabled. Coverage seems to mess with how the linter detects
            # the usages of a new API within a conditional. See
            # crbug.com/677320 for more.
            disable = [ "NewApi" ]
          }
        }

        # Use an intermediate group() rather as the data_deps target in order to
        # avoid lint artifacts showing up as runtime_deps (while still having lint
        # run in parallel to other targets).
        group("${_template_name}__analysis") {
          public_deps = [
            ":${_template_name}__lint",
          ]
        }
      }

      _final_deps += [ ":${_template_name}__dex" ]
      dex("${_template_name}__dex") {
        sources = [
          _jar_path,
        ]
        output = _dex_path
        deps = [
          ":$_compile_java_target",
        ]
      }
    }

    group(target_name) {
      forward_variables_from(invoker,
                             [
                               "data",
                               "data_deps",
                               "visibility",
                             ])
      if (!defined(data_deps)) {
        data_deps = []
      }
      public_deps = _final_deps
      if (_has_lint_target) {
        data_deps += [ ":${_template_name}__analysis" ]
      }
    }
  }

  # Produces a single .dex.jar out of a set of Java dependencies.
  template("deps_dex") {
    set_sources_assignment_filter([])
    build_config = "$target_gen_dir/${target_name}.build_config"
    build_config_target_name = "${target_name}__build_config"

    write_build_config(build_config_target_name) {
      forward_variables_from(invoker, [ "dex_path" ])
      if (defined(invoker.deps)) {
        possible_config_deps = invoker.deps
      }
      type = "deps_dex"
      build_config = build_config
    }

    rebased_build_config = rebase_path(build_config, root_build_dir)
    dex(target_name) {
      inputs = [
        build_config,
      ]
      output = invoker.dex_path
      dex_arg_key = "${rebased_build_config}:final_dex:dependency_dex_files"
      args = [ "--inputs=@FileArg($dex_arg_key)" ]
      if (defined(invoker.excluded_jars)) {
        excluded_jars = rebase_path(invoker.excluded_jars, root_build_dir)
        args += [ "--excluded-paths=${excluded_jars}" ]
      }
      deps = [
        ":$build_config_target_name",
      ]
    }
  }

  # Creates an AndroidManifest.xml for an APK split.
  template("generate_split_manifest") {
    assert(defined(invoker.main_manifest))
    assert(defined(invoker.out_manifest))
    assert(defined(invoker.split_name))

    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "testonly",
                             ])
      depfile = "$target_gen_dir/$target_name.d"
      args = [
        "--main-manifest",
        rebase_path(invoker.main_manifest, root_build_dir),
        "--out-manifest",
        rebase_path(invoker.out_manifest, root_build_dir),
        "--split",
        invoker.split_name,
      ]
      if (defined(invoker.version_code)) {
        args += [
          "--version-code",
          invoker.version_code,
        ]
      }
      if (defined(invoker.version_name)) {
        args += [
          "--version-name",
          invoker.version_name,
        ]
      }
      if (defined(invoker.has_code)) {
        args += [
          "--has-code",
          invoker.has_code,
        ]
      }
      args += [
        "--depfile",
        rebase_path(depfile, root_build_dir),
      ]

      script = "//build/android/gyp/generate_split_manifest.py"
      outputs = [
        invoker.out_manifest,
      ]
      inputs = [
        invoker.main_manifest,
      ]
    }
  }

  template("pack_relocation_section") {
    assert(defined(invoker.file_list_json))
    assert(defined(invoker.libraries_filearg))
    action(target_name) {
      forward_variables_from(invoker,
                             [
                               "deps",
                               "public_deps",
                               "inputs",
                               "testonly",
                             ])
      script = "//build/android/gyp/pack_relocations.py"
      depfile = "$target_gen_dir/$target_name.d"
      _packed_libraries_dir = "$target_gen_dir/$target_name/packed-libs"
      outputs = [
        invoker.file_list_json,
      ]
      deps += [ relocation_packer_target ]

      args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--enable-packing=1",
        "--android-pack-relocations",
        rebase_path(relocation_packer_exe, root_build_dir),
        "--stripped-libraries-dir",
        rebase_path(root_build_dir, root_build_dir),
        "--packed-libraries-dir",
        rebase_path(_packed_libraries_dir, root_build_dir),
        "--libraries=${invoker.libraries_filearg}",
        "--filelistjson",
        rebase_path(invoker.file_list_json, root_build_dir),
      ]
    }
  }
}