chromium/components/cronet/android/BUILD.gn

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

import("//build/android/resource_sizes.gni")
import("//build/buildflag_header.gni")
import("//build/config/android/config.gni")
import("//build/config/android/rules.gni")
import("//build/config/coverage/coverage.gni")
import("//build/config/cronet/config.gni")
import("//build/config/zip.gni")
import("//build/util/lastchange.gni")
import("//build/util/process_version.gni")
import("//chrome/version.gni")
import("//components/cronet/native/include/headers.gni")
import("//components/grpc_support/include/headers.gni")
import("//testing/test.gni")
import("//third_party/jni_zero/jni_zero.gni")
import("//third_party/netty4/netty4.gni")
import("//third_party/protobuf/proto_library.gni")
import("//tools/binary_size/sizes.gni")
import("//url/features.gni")

_templates_dir = "$target_gen_dir/templates"
_gn_path = "//buildtools/linux64/gn"

assert(!is_component_build)
assert(is_cronet_build)

generate_jni("cronet_jni_headers") {
  sources = [
    "java/src/org/chromium/net/impl/CronetBidirectionalStream.java",
    "java/src/org/chromium/net/impl/CronetLibraryLoader.java",
    "java/src/org/chromium/net/impl/CronetUploadDataStream.java",
    "java/src/org/chromium/net/impl/CronetUrlRequest.java",
    "java/src/org/chromium/net/impl/CronetUrlRequestContext.java",
  ]
}

generate_jni_registration("cronet_jni_registration") {
  java_targets = [ ":cronet_impl_native_java" ]
  manual_jni_registration = true

  # JNI generated is used for test and non-test apks.
  include_testonly = true

  _subdir = rebase_path(target_gen_dir, root_gen_dir)
  cpp_codegen_output =
      "$jni_headers_dir/$_subdir/cronet_jni_registration_generated.h"
}

android_library("cronet_jni_registration_java") {
  srcjar_deps = [ ":cronet_jni_registration" ]
}

java_cpp_enum("effective_connection_type_java") {
  sources = [ "//net/nqe/effective_connection_type.h" ]
}

java_cpp_enum("rtt_throughput_values_java") {
  sources = [ "//net/nqe/network_quality.h" ]
}

java_cpp_enum("net_request_priority_java") {
  sources = [ "//net/base/request_priority.h" ]
}

java_cpp_enum("net_idempotency_java") {
  sources = [ "//net/base/idempotency.h" ]
}

java_cpp_enum("network_quality_observation_source_java") {
  sources = [ "//net/nqe/network_quality_observation_source.h" ]
}

java_cpp_enum("url_request_error_java") {
  sources = [ "url_request_error.h" ]
}

java_cpp_enum("url_request_close_source_java") {
  sources = [ "url_request_close_source.h" ]
}

java_cpp_enum("http_cache_type_java") {
  sources = [ "//components/cronet/url_request_context_config.h" ]
}

java_cpp_template("load_states_list") {
  sources = [ "java/src/org/chromium/net/impl/LoadState.template" ]
  inputs = [ "//net/base/load_states_list.h" ]
}

_api_level = read_file("api_version.txt", "value")

_generated_api_version_java = "$_templates_dir/org/chromium/net/ApiVersion.java"

process_version("api_version") {
  process_only = true
  template_file = "api/src/org/chromium/net/ApiVersion.template"
  sources = [
    "//chrome/VERSION",
    lastchange_file,
  ]
  extra_args = [
    "-e",
    "API_LEVEL=$_api_level",
  ]
  output = _generated_api_version_java
}

_generated_impl_version_java =
    "$_templates_dir/org/chromium/net/impl/ImplVersion.java"

process_version("impl_version") {
  process_only = true
  template_file = "java/src/org/chromium/net/impl/ImplVersion.template"
  sources = [
    "//chrome/VERSION",
    lastchange_file,
  ]
  extra_args = [
    "-e",
    "API_LEVEL=$_api_level",
  ]
  output = _generated_impl_version_java
}

_cronet_version_header_include_dir = "$target_gen_dir/cronet_version_header"

proto_library("base_feature_overrides_proto") {
  sources = [ "proto/base_feature_overrides.proto" ]
}

proto_java_library("base_feature_overrides_java_proto") {
  proto_path = "."
  sources = [ "proto/base_feature_overrides.proto" ]
}

proto_java_library("flags_java_proto") {
  proto_path = "."
  sources = [ "proto/flags.proto" ]
}

proto_library("request_context_config_proto") {
  sources = [ "proto/request_context_config.proto" ]
}

proto_java_library("request_context_config_java_proto") {
  proto_path = "."
  sources = [ "proto/request_context_config.proto" ]
}

static_library("cronet_base_feature") {
  sources = [
    "cronet_base_feature.cc",
    "cronet_base_feature.h",
  ]
  deps = [
    ":base_feature_overrides_proto",
    "//base",
  ]
}

static_library("cronet_base_feature_unittest") {
  testonly = true
  sources = [ "cronet_base_feature_unittest.cc" ]
  deps = [
    ":base_feature_overrides_proto",
    ":cronet_base_feature",
    "//base/test:test_support",
    "//testing/gtest",
  ]
}

source_set("cronet_static") {
  deps = [
    ":base_feature_overrides_proto",
    ":cronet_base_feature",
    ":cronet_jni_headers",
    ":cronet_jni_registration",
    ":request_context_config_proto",
    "//base",
    "//components/cronet:cronet_common",
    "//components/cronet:cronet_version_header",
    "//components/cronet:metrics_util",
    "//components/prefs",
    "//net",
    "//third_party/zlib:zlib",
    "//url",
    "//url:buildflags",
  ]
  if (!is_cronet_for_aosp_build) {
    deps += [ "//components/cronet/native:cronet_native_impl" ]
  }
  sources = [
    "//components/cronet/android/cronet_bidirectional_stream_adapter.cc",
    "//components/cronet/android/cronet_bidirectional_stream_adapter.h",
    "//components/cronet/android/cronet_context_adapter.cc",
    "//components/cronet/android/cronet_context_adapter.h",
    "//components/cronet/android/cronet_library_loader.cc",
    "//components/cronet/android/cronet_library_loader.h",
    "//components/cronet/android/cronet_upload_data_stream_adapter.cc",
    "//components/cronet/android/cronet_upload_data_stream_adapter.h",
    "//components/cronet/android/cronet_url_request_adapter.cc",
    "//components/cronet/android/cronet_url_request_adapter.h",
    "//components/cronet/android/io_buffer_with_byte_buffer.cc",
    "//components/cronet/android/io_buffer_with_byte_buffer.h",
    "//components/cronet/android/shared_dictionary_with_byte_buffer.cc",
    "//components/cronet/android/shared_dictionary_with_byte_buffer.h",
    "//components/cronet/android/url_request_close_source.cc",
    "//components/cronet/android/url_request_close_source.h",
    "//components/cronet/android/url_request_error.cc",
    "//components/cronet/android/url_request_error.h",
  ]

  include_dirs = [ _cronet_version_header_include_dir ]

  cflags = [
    "-DLOGGING=1",
    "-Wno-sign-promo",
  ]

  libs = [
    "android",
    "log",
  ]

  if (!use_platform_icu_alternatives) {
    deps += [ "//base:i18n" ]
  }
}

config("hide_all_but_jni_onload_and_cronet") {
  ldflags = [ "-Wl,--version-script=" +
              rebase_path("android_only_jni_onload_and_cronet_exports.lst",
                          root_out_dir) ]
}

_cronet_shared_lib_name = "cronet.$chrome_version_full"

shared_library("cronet") {
  output_name = _cronet_shared_lib_name
  sources = [ "cronet_jni.cc" ]
  deps = [
    ":cronet_static",
    "//base",
    "//net:net",
  ]
  configs -= [ "//build/config/android:hide_all_but_jni_onload" ]
  configs += [ ":hide_all_but_jni_onload_and_cronet" ]
}

sizes_test("cronet_sizes") {
  data_deps = [ ":cronet" ]
  data = [ "${root_out_dir}/lib${_cronet_shared_lib_name}.so" ]
  executable_args = [
    "--platform",
    "android-cronet",
  ]
}

cronet_shared_java_deps_to_package = [ ":cronet_stats_log_java" ]

# Common Cronet code that is used in Cronet API *and* implementations.
#
# Note that this target should not be used to ship classes that define the
# Cronet API/impl boundary, because API and impl code can end up linking against
# separate copies of these classes at runtime (e.g. if Cronet is loaded from
# Google Play Services), making it impossible for references to these classes to
# cross the boundary. These classes should be shipped in `cronet_api_java`
# instead.
android_library("cronet_shared_java") {
  srcjar_deps = [ ":url_request_close_source_java" ]
  sources = [
    "java/src/org/chromium/net/impl/CronetLogger.java",
    "java/src/org/chromium/net/impl/CronetLoggerFactory.java",
    "java/src/org/chromium/net/impl/CronetManifest.java",
    "java/src/org/chromium/net/impl/NoOpLogger.java",
    "java/src/org/chromium/net/telemetry/CronetLoggerImpl.java",
    "java/src/org/chromium/net/telemetry/ExperimentalOptions.java",
    "java/src/org/chromium/net/telemetry/Hash.java",
    "java/src/org/chromium/net/telemetry/OptionalBoolean.java",
    "java/src/org/chromium/net/telemetry/RateLimiter.java",
    "java/src/org/chromium/net/telemetry/SizeBuckets.java",
  ]

  # Adding deps here won't include those deps in the cronet_shared_java.jar.
  # Please add to cronet_shared_java_deps_to_package instead.
  deps = [ "//third_party/androidx:androidx_annotation_annotation_java" ] +
         cronet_shared_java_deps_to_package

  proguard_configs =
      [ "//components/cronet/android/cronet_shared_proguard.cfg" ]
}

# cronet_api_java.jar defines Cronet API.
#
# Note that, in some cases (specifically, the case where Cronet is loaded from
# Google Play Services), the API classes are not bundled with the
# implementation, and at runtime the impl classes may end up deriving from API
# classes that are from a different build/version. This means you have to pay
# attention to backward/forward Java ABI compatibility concerns. If you don't
# want your class to be part of the Cronet API/impl boundary, ship your class in
# `cronet_shared_java` instead.
android_library("cronet_api_java") {
  sources = [
    "api/src/org/chromium/net/BidirectionalStream.java",
    "api/src/org/chromium/net/CallbackException.java",
    "api/src/org/chromium/net/ConnectionMigrationOptions.java",
    "api/src/org/chromium/net/CronetEngine.java",
    "api/src/org/chromium/net/CronetException.java",
    "api/src/org/chromium/net/CronetProvider.java",
    "api/src/org/chromium/net/DnsOptions.java",
    "api/src/org/chromium/net/ExperimentalBidirectionalStream.java",
    "api/src/org/chromium/net/ExperimentalCronetEngine.java",
    "api/src/org/chromium/net/ExperimentalOptionsTranslator.java",
    "api/src/org/chromium/net/ExperimentalUrlRequest.java",
    "api/src/org/chromium/net/ICronetEngineBuilder.java",
    "api/src/org/chromium/net/InlineExecutionProhibitedException.java",
    "api/src/org/chromium/net/NetworkException.java",
    "api/src/org/chromium/net/NetworkQualityRttListener.java",
    "api/src/org/chromium/net/NetworkQualityThroughputListener.java",
    "api/src/org/chromium/net/QuicException.java",
    "api/src/org/chromium/net/QuicOptions.java",
    "api/src/org/chromium/net/RequestFinishedInfo.java",
    "api/src/org/chromium/net/UploadDataProvider.java",
    "api/src/org/chromium/net/UploadDataSink.java",
    "api/src/org/chromium/net/UrlRequest.java",
    "api/src/org/chromium/net/UrlResponseInfo.java",
    _generated_api_version_java,
  ]

  deps = [
    ":api_version",
    ":cronet_shared_java",
    "//third_party/androidx:androidx_annotation_annotation_experimental_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
  ]
}

android_library("cronet_api_helpers_java") {
  sources = [
    # This API file is a special case because it depends on methods that exists
    # within the ApiHelper package.
    "api/src/org/chromium/net/UploadDataProviders.java",
    "api/src/org/chromium/net/apihelpers/ByteArrayCronetCallback.java",
    "api/src/org/chromium/net/apihelpers/ContentTypeParametersParser.java",
    "api/src/org/chromium/net/apihelpers/CronetRequestCompletionListener.java",
    "api/src/org/chromium/net/apihelpers/CronetResponse.java",
    "api/src/org/chromium/net/apihelpers/ImplicitFlowControlCallback.java",
    "api/src/org/chromium/net/apihelpers/InMemoryTransformCronetCallback.java",
    "api/src/org/chromium/net/apihelpers/JsonCronetCallback.java",
    "api/src/org/chromium/net/apihelpers/RedirectHandler.java",
    "api/src/org/chromium/net/apihelpers/RedirectHandlers.java",
    "api/src/org/chromium/net/apihelpers/StringCronetCallback.java",
    "api/src/org/chromium/net/apihelpers/UploadDataProviders.java",
    "api/src/org/chromium/net/apihelpers/UrlRequestCallbacks.java",
  ]
  deps = [
    ":cronet_api_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
  ]
}

# CronetStatsLog uses `android.util.StatsLog` which is not in the public SDK,
# hence this separate target pointed at the system SDK.
android_library("cronet_stats_log_java") {
  sources = [ "java/src/org/chromium/net/telemetry/CronetStatsLog.java" ]
  alternative_android_sdk_dep =
      "//third_party/android_sdk:public_framework_system_java"
  deps = [ "//third_party/androidx:androidx_annotation_annotation_java" ]
}

cronet_impl_common_java_srcjar_deps = [
  ":effective_connection_type_java",
  ":http_cache_type_java",
  ":load_states_list",
  ":rtt_throughput_values_java",
]

cronet_impl_common_java_deps_to_package =
    [ "//net/android:net_thread_stats_uid_java" ]

# cronet_impl_common_java.jar - common Cronet code that is shared
# by all Cronet engine implementations.
android_library("cronet_impl_common_java") {
  sources = [
    "java/src/org/chromium/net/impl/BidirectionalStreamBuilderImpl.java",
    "java/src/org/chromium/net/impl/CallbackExceptionImpl.java",
    "java/src/org/chromium/net/impl/CronetEngineBase.java",
    "java/src/org/chromium/net/impl/CronetEngineBuilderImpl.java",
    "java/src/org/chromium/net/impl/CronetExceptionImpl.java",
    "java/src/org/chromium/net/impl/CronetMetrics.java",
    "java/src/org/chromium/net/impl/NetworkExceptionImpl.java",
    "java/src/org/chromium/net/impl/Preconditions.java",
    "java/src/org/chromium/net/impl/QuicExceptionImpl.java",
    "java/src/org/chromium/net/impl/RefCountDelegate.java",
    "java/src/org/chromium/net/impl/RequestFinishedInfoImpl.java",
    "java/src/org/chromium/net/impl/UrlRequestBuilderImpl.java",
    "java/src/org/chromium/net/impl/UrlRequestUtil.java",
    "java/src/org/chromium/net/impl/UrlResponseInfoImpl.java",
    "java/src/org/chromium/net/impl/UserAgent.java",
    "java/src/org/chromium/net/impl/VersionSafeCallbacks.java",
    _generated_impl_version_java,
  ]

  # Adding deps here won't include those deps in the cronet_impl_common_java.jar.
  # Please add to cronet_impl_common_java_deps_to_package instead.
  deps = [
    ":cronet_api_java",
    ":cronet_shared_java",
    ":impl_version",
    "//third_party/androidx:androidx_annotation_annotation_experimental_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
  ]
  deps += cronet_impl_common_java_deps_to_package

  srcjar_deps = cronet_impl_common_java_srcjar_deps
  proguard_configs = [ "cronet_impl_common_proguard.cfg" ]
}

# cronet_impl_java_util_java.jar - Classes shared between Java Cronet implementations.
android_library("cronet_impl_java_util_java") {
  sources = [
    "java/src/org/chromium/net/impl/JavaUploadDataSinkBase.java",
    "java/src/org/chromium/net/impl/JavaUrlRequestUtils.java",
  ]

  deps = [
    ":cronet_api_java",
    ":cronet_impl_common_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
  ]
}

# cronet_impl_platform_java.jar - Java platform based implementation of the Cronet engine.
android_library("cronet_impl_platform_java") {
  sources = [
    "java/src/org/chromium/net/impl/InputStreamChannel.java",
    "java/src/org/chromium/net/impl/JavaCronetEngine.java",
    "java/src/org/chromium/net/impl/JavaCronetEngineBuilderImpl.java",
    "java/src/org/chromium/net/impl/JavaCronetProvider.java",
    "java/src/org/chromium/net/impl/JavaUrlRequest.java",
  ]

  deps = [
    ":cronet_api_java",
    ":cronet_impl_common_java",
    ":cronet_impl_java_util_java",
    ":cronet_shared_java",
    "//net/android:net_thread_stats_uid_java",
    "//third_party/android_deps:com_google_code_findbugs_jsr305_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
  ]
}

# cronet_impl_fake_java.jar - Fake implementation of Cronet.
android_library("cronet_impl_fake_java") {
  sources = [
    "fake/java/org/chromium/net/test/FakeCronetController.java",
    "fake/java/org/chromium/net/test/FakeCronetEngine.java",
    "fake/java/org/chromium/net/test/FakeCronetProvider.java",
    "fake/java/org/chromium/net/test/FakeRequestFinishedInfo.java",
    "fake/java/org/chromium/net/test/FakeUrlRequest.java",
    "fake/java/org/chromium/net/test/FakeUrlResponse.java",
    "fake/java/org/chromium/net/test/ResponseMatcher.java",
    "fake/java/org/chromium/net/test/UrlResponseMatcher.java",
  ]

  deps = [
    ":cronet_api_java",
    ":cronet_impl_common_java",
    ":cronet_impl_java_util_java",
    ":cronet_shared_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
  ]
}

android_library("cronet_urlconnection_impl_java") {
  sources = [
    "java/src/org/chromium/net/urlconnection/CronetBufferedOutputStream.java",
    "java/src/org/chromium/net/urlconnection/CronetChunkedOutputStream.java",
    "java/src/org/chromium/net/urlconnection/CronetFixedModeOutputStream.java",
    "java/src/org/chromium/net/urlconnection/CronetHttpURLConnection.java",
    "java/src/org/chromium/net/urlconnection/CronetHttpURLStreamHandler.java",
    "java/src/org/chromium/net/urlconnection/CronetInputStream.java",
    "java/src/org/chromium/net/urlconnection/CronetOutputStream.java",
    "java/src/org/chromium/net/urlconnection/CronetURLStreamHandlerFactory.java",
    "java/src/org/chromium/net/urlconnection/MessageLoop.java",
  ]
  deps = [
    ":cronet_api_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
  ]
}

cronet_impl_native_java_srcjar_deps = [
  ":net_idempotency_java",
  ":net_request_priority_java",
  ":network_quality_observation_source_java",
  ":url_request_error_java",
]

cronet_impl_native_java_deps_to_package = [
  ":cronet_urlconnection_impl_java",
  ":base_feature_overrides_java_proto",
  ":flags_java_proto",
  ":request_context_config_java_proto",
  "//base:base_java",
  "//third_party/jni_zero:jni_zero_java",
  "//build/android:build_java",
  "//net/android:net_java",
  "//url:url_java",
]

# cronet_impl_native_java.jar - native implementation of the Cronet engine.
android_library("cronet_impl_native_java") {
  sources = [
    "java/src/org/chromium/net/httpflags/BaseFeature.java",
    "java/src/org/chromium/net/httpflags/HttpFlagsLoader.java",
    "java/src/org/chromium/net/httpflags/ResolvedFlags.java",
    "java/src/org/chromium/net/impl/BidirectionalStreamNetworkException.java",
    "java/src/org/chromium/net/impl/CronetBidirectionalStream.java",
    "java/src/org/chromium/net/impl/CronetLibraryLoader.java",
    "java/src/org/chromium/net/impl/CronetRequestCommon.java",
    "java/src/org/chromium/net/impl/CronetUploadDataStream.java",
    "java/src/org/chromium/net/impl/CronetUrlRequest.java",
    "java/src/org/chromium/net/impl/CronetUrlRequestContext.java",
    "java/src/org/chromium/net/impl/NativeCronetEngineBuilderImpl.java",
    "java/src/org/chromium/net/impl/NativeCronetEngineBuilderWithLibraryLoaderImpl.java",
    "java/src/org/chromium/net/impl/NativeCronetProvider.java",
  ]

  # Adding deps here won't include those deps in the cronet_impl_native_java.jar.
  # Please add to cronet_impl_native_java_deps_to_package instead.
  deps = [
    ":cronet_api_java",
    ":cronet_impl_common_java",
    ":cronet_shared_java",
    "//build/android:build_java",
    "//third_party/android_deps:com_google_code_findbugs_jsr305_java",
    "//third_party/android_deps:protobuf_lite_runtime_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
    "//third_party/jni_zero:jni_zero_java",
  ]
  deps += cronet_impl_native_java_deps_to_package

  srcjar_deps = cronet_impl_native_java_srcjar_deps
  srcjar_deps += [ ":cronet_jni_headers" ]
  proguard_configs =
      [ "//components/cronet/android/cronet_impl_native_proguard.cfg" ]
}

# httpengine_native_provider_java.jar - native implementation of Cronet engine
# loaded from AOSP (only for Android U onwards)
android_library("httpengine_native_provider_java") {
  sources = [
    "java/src/org/chromium/net/impl/AndroidBidirectionalStreamCallbackWrapper.java",
    "java/src/org/chromium/net/impl/AndroidBidirectionalStreamWrapper.java",
    "java/src/org/chromium/net/impl/AndroidCallbackExceptionWrapper.java",
    "java/src/org/chromium/net/impl/AndroidHeaderBlockWrapper.java",
    "java/src/org/chromium/net/impl/AndroidHttpEngineBuilderWrapper.java",
    "java/src/org/chromium/net/impl/AndroidHttpEngineWrapper.java",
    "java/src/org/chromium/net/impl/AndroidHttpExceptionWrapper.java",
    "java/src/org/chromium/net/impl/AndroidNetworkExceptionWrapper.java",
    "java/src/org/chromium/net/impl/AndroidQuicExceptionWrapper.java",
    "java/src/org/chromium/net/impl/AndroidRequestFinishedInfoWrapper.java",
    "java/src/org/chromium/net/impl/AndroidUploadDataProviderWrapper.java",
    "java/src/org/chromium/net/impl/AndroidUploadDataSinkWrapper.java",
    "java/src/org/chromium/net/impl/AndroidUrlRequestCallbackWrapper.java",
    "java/src/org/chromium/net/impl/AndroidUrlRequestStatusListenerWrapper.java",
    "java/src/org/chromium/net/impl/AndroidUrlRequestWrapper.java",
    "java/src/org/chromium/net/impl/AndroidUrlResponseInfoWrapper.java",
    "java/src/org/chromium/net/impl/CronetExceptionTranslationUtils.java",
    "java/src/org/chromium/net/impl/HttpEngineNativeProvider.java",
  ]

  deps = [
    ":cronet_api_java",
    ":cronet_impl_common_java",
    ":cronet_shared_java",
    "//build/android:build_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
    "//third_party/androidx:androidx_core_core_java",
    "//third_party/jni_zero:jni_zero_java",
  ]
}

# Groups all Cronet code (API and impl) into a single Java dependency.
java_group("cronet_all_java") {
  deps = [
    ":cronet_api_java",
    ":cronet_impl_common_java",
    ":cronet_impl_native_java",
    ":cronet_impl_platform_java",
    ":cronet_shared_java",
    ":httpengine_native_provider_java",
  ]
}

android_resources("cronet_sample_apk_resources") {
  sources = [
    "sample/res/drawable/ic_action_home.xml",
    "sample/res/drawable/ic_flag.xml",
    "sample/res/layout/main_activity.xml",
    "sample/res/layout/main_fragment.xml",
    "sample/res/layout/options_fragment.xml",
    "sample/res/layout/toggle_view.xml",
    "sample/res/values/colors.xml",
    "sample/res/values/dimens.xml",
    "sample/res/values/strings.xml",
    "sample/res/values/styles.xml",
  ]
  android_manifest = "sample/AndroidManifest.xml"
  deps = [
    "//third_party/androidx:androidx_appcompat_appcompat_java",
    "//third_party/androidx:androidx_appcompat_appcompat_resources_java",
    "//third_party/androidx:androidx_drawerlayout_drawerlayout_java",
    "//third_party/androidx:androidx_fragment_fragment_java",
    "//third_party/androidx:androidx_interpolator_interpolator_java",
    "//third_party/androidx:androidx_lifecycle_lifecycle_common_java",
    "//third_party/androidx:androidx_vectordrawable_vectordrawable_animated_java",
    "//third_party/androidx:androidx_vectordrawable_vectordrawable_java",
  ]
}

android_library("cronet_sample_apk_java") {
  sources = [
    "sample/src/org/chromium/cronet_sample_apk/Action.java",
    "sample/src/org/chromium/cronet_sample_apk/ActionData.java",
    "sample/src/org/chromium/cronet_sample_apk/CronetSampleActivity.java",
    "sample/src/org/chromium/cronet_sample_apk/CronetSampleApplication.java",
    "sample/src/org/chromium/cronet_sample_apk/MainFragment.java",
    "sample/src/org/chromium/cronet_sample_apk/Options.java",
    "sample/src/org/chromium/cronet_sample_apk/OptionsFragment.java",
    "sample/src/org/chromium/cronet_sample_apk/OptionsRecyclerViewAdapter.java",
    "sample/src/org/chromium/cronet_sample_apk/SampleActivityViewModel.java",
  ]

  resources_package = "org.chromium.cronet_sample_apk"
  deps = [
    ":cronet_sample_apk_resources",
    ":package_api_java",
    ":package_impl_native_java",
    "//third_party/androidx:androidx_annotation_annotation_experimental_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
    "//third_party/androidx:androidx_appcompat_appcompat_java",
    "//third_party/androidx:androidx_fragment_fragment_java",
    "//third_party/androidx:androidx_lifecycle_lifecycle_viewmodel_java",
    "//third_party/androidx:androidx_preference_preference_java",
    "//third_party/androidx:androidx_recyclerview_recyclerview_java",
  ]
}

android_apk("cronet_sample_apk") {
  apk_name = "CronetSample"
  android_manifest = "sample/AndroidManifest.xml"
  shared_libraries = [ ":cronet" ]

  deps = [
    ":cronet_combine_proguard_flags",
    ":cronet_sample_apk_java",
    ":cronet_sample_apk_resources",
    "//third_party/android_deps:protobuf_lite_runtime_java",
  ]

  # Cronet jars will include this, so don't duplicate.
  generate_buildconfig_java = false

  if (!is_java_debug) {
    proguard_enabled = true
    proguard_configs = [
      "$target_gen_dir/cronet_impl_native_proguard.cfg",
      "cronet_impl_common_proguard.cfg",
      "cronet_shared_proguard.cfg",
    ]
  }
}

android_resource_sizes_test("resource_sizes_cronet_sample_apk") {
  apk_name = "CronetSample"
  data_deps = [ ":cronet_sample_apk" ]
}

# TODO(b/313416274): Change the output name to something else
# that doesn't actually exist.
action("cronet_combine_proguard_flags") {
  script = "//components/cronet/tools/generate_proguard_file.py"

  # This will combine all the proguard configs found through the
  # transitive dependencies of those targets.
  deps = [
    ":cronet_jni_registration_java",
    ":repackage_api",
    ":repackage_native_impl",
  ]

  # TODO(b/313416274): Change the output name to something else
  # that doesn't actually exist.
  outputs = [ "$target_gen_dir/cronet_impl_native_proguard.cfg" ]
  sources = []
  depfile = "$target_out_dir/$target_name.d"
  _build_config_deps = []
  foreach(dep, deps) {
    _dep_label = get_label_info(dep, "label_no_toolchain")
    _build_config_deps += [ "$_dep_label$build_config_target_suffix" ]
    _dep_gen_dir = get_label_info(dep, "target_gen_dir")
    _dep_name = get_label_info(dep, "name")
    sources += [ "$_dep_gen_dir/$_dep_name.build_config.json" ]
  }
  deps += _build_config_deps
  args = [
           "--output_file",
           rebase_path(outputs[0], root_build_dir),
           "--dep_file",
           rebase_path(depfile, root_build_dir),
         ] + rebase_path(sources, root_build_dir)
}

action("cronet_combined_proguard_flags_golden_test") {
  script = "//components/cronet/tools/check_combined_proguard_file.py"
  deps = [ ":cronet_combine_proguard_flags" ]
  sources = [
    "$target_gen_dir/cronet_impl_native_proguard.cfg",
    "//components/cronet/android/cronet_combined_impl_native_proguard_golden.cfg",
  ]
  outputs = [ "$target_gen_dir/$target_name.stamp" ]
  args = [
    "--input_generated_file",
    rebase_path(sources[0], root_build_dir),
    "--input_golden_file",
    rebase_path(sources[1], root_build_dir),
    "--stamp",
    rebase_path(outputs[0], root_build_dir),
  ]
}

_package_dir = "$root_out_dir/cronet"

# These package_* targets represent how Cronet is used in production code.
# Using these targets is preferred to using the underlying targets like
# :cronet_api_java or :cronet_all_java, as it better tests production
# usage.
android_java_prebuilt("package_shared_java") {
  jar_path = "$_package_dir/cronet_shared_java.jar"
  deps = [ ":repackage_shared" ]
}

android_java_prebuilt("package_api_java") {
  jar_path = "$_package_dir/cronet_api.jar"
  deps = [
    ":package_shared_java",
    ":repackage_api",
  ]
}

android_java_prebuilt("package_impl_common_java") {
  jar_path = "$_package_dir/cronet_impl_common_java.jar"
  deps = [
    ":package_api_java",
    ":package_shared_java",
    ":repackage_impl_common",
  ]
}

java_prebuilt("package_impl_native_java") {
  # This target is a java_prebuilt instead of a android_java_prebuilt so we
  # don't filter out GEN_JNI which is part of jar_excluded_patterns in
  # android_java_prebuilt.
  jar_path = "$_package_dir/cronet_impl_native_java.jar"
  supports_android = true
  requires_android = true

  # To avoid dual definition of classes some dependencies are added
  # downstream (Maven deps, BUILD dependencies), and the bytecode check
  # doesn't like that.
  enable_bytecode_checks = false

  deps = [
    ":package_api_java",
    ":package_impl_common_java",
    ":package_shared_java",
    ":repackage_native",
    "//third_party/android_deps:com_google_code_findbugs_jsr305_java",
    "//third_party/androidx:androidx_drawerlayout_drawerlayout_java",
    "//third_party/androidx:androidx_interpolator_interpolator_java",
  ]
  jar_excluded_patterns = [ "androidx/*/R*" ]
}

android_java_prebuilt("package_impl_util_java") {
  jar_path = "$_package_dir/cronet_impl_util_java.jar"
  deps = [
    ":package_api_java",
    ":repackage_util",
  ]
}

android_java_prebuilt("package_impl_platform_java") {
  jar_path = "$_package_dir/cronet_impl_platform_java.jar"
  deps = [
    ":package_api_java",
    ":package_impl_common_java",
    ":package_shared_java",
    ":repackage_platform",
  ]
}

android_java_prebuilt("package_impl_fake_java") {
  jar_path = "$_package_dir/cronet_impl_fake_java.jar"
  deps = [
    ":package_api_java",
    ":package_impl_common_java",
    ":package_shared_java",
    ":repackage_fake",
  ]
}

android_java_prebuilt("package_httpengine_native_provider_java") {
  jar_path = "$_package_dir/httpengine_native_provider_java.jar"
  deps = [
    ":package_api_java",
    ":repackage_httpengine_native_provider",
  ]
}

template("jar_src") {
  action_with_pydeps(target_name) {
    _rebased_src_search_dirs =
        rebase_path(invoker.src_search_dirs, root_build_dir)

    script = "//components/cronet/tools/jar_src.py"
    depfile = "$target_gen_dir/$target_name.d"
    outputs = [ invoker.jar_path ]
    args = [
      "--src-search-dirs=${_rebased_src_search_dirs}",
      "--jar-path",
      rebase_path(invoker.jar_path, root_build_dir),
      "--depfile",
      rebase_path(depfile, root_build_dir),
    ]

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

    _excluded_patterns = []
    if (defined(invoker.excluded_patterns)) {
      _excluded_patterns = invoker.excluded_patterns
    }
    _src_jars = []

    # Add src-jar files that are listed in "src_jars".
    if (defined(invoker.src_jars)) {
      _rebased_src_jars = rebase_path(invoker.src_jars, root_build_dir)
      _src_jars += _rebased_src_jars
    }

    # Add src-jar files that are generated by dependencies in "srcjar_deps".
    if (defined(invoker.srcjar_deps)) {
      foreach(_srcjar_dep, invoker.srcjar_deps) {
        _dep_gen_dir = get_label_info(_srcjar_dep, "target_gen_dir")
        _dep_name = get_label_info(_srcjar_dep, "name")
        _src_jars += rebase_path([ "$_dep_gen_dir/$_dep_name.srcjar" ])
        deps += [ _srcjar_dep ]
      }
    }

    # Create the list of all source files that are given in "src_files".
    _src_files = []
    if (defined(invoker.src_files)) {
      _src_files += invoker.src_files
    }

    # Handle "source_deps".
    _src_list_files = []
    if (defined(invoker.source_deps)) {
      foreach(_source_dep, invoker.source_deps) {
        _dep_gen_dir = get_label_info(_source_dep, "target_gen_dir")
        _dep_name = get_label_info(_source_dep, "name")
        _src_list_files += rebase_path([ "$_dep_gen_dir/$_dep_name.sources" ])
        deps += [ _source_dep ]
      }
    }
    args += [ "--src-jar=${_src_jars}" ]
    args += [ "--src-files=${_src_files}" ]
    args += [ "--src-list-files=${_src_list_files}" ]
    args += [ "--excluded-classes=$_excluded_patterns" ]

    inputs = _src_jars
    inputs += _src_files
    inputs += _src_list_files
  }
}

jar_src("jar_cronet_shared_source") {
  src_search_dirs = [ "java/src" ]
  source_deps = [ ":cronet_shared_java" ]
  jar_path = "$_package_dir/cronet_shared-src.jar"
}

jar_src("jar_cronet_api_source") {
  src_search_dirs = [
    "api/src",
    _templates_dir,
  ]
  source_deps = [ ":cronet_api_java" ]
  jar_path = "$_package_dir/cronet_api-src.jar"
}

jar_src("jar_cronet_impl_common_java_source") {
  src_search_dirs = [
    "java/src",
    _templates_dir,
  ]
  source_deps = [ ":cronet_impl_common_java" ]
  srcjar_deps = cronet_impl_common_java_srcjar_deps
  jar_path = "$_package_dir/cronet_impl_common_java-src.jar"
}

jar_src("jar_cronet_impl_platform_java_source") {
  src_search_dirs = [ "java/src" ]
  source_deps = [ ":cronet_impl_platform_java" ]
  jar_path = "$_package_dir/cronet_impl_platform_java-src.jar"
}

jar_src("jar_cronet_impl_fake_java_source") {
  src_search_dirs = [ "fake/java" ]
  source_deps = [ ":cronet_impl_fake_java" ]
  jar_path = "$_package_dir/cronet_impl_fake_java-src.jar"
}

jar_src("jar_cronet_impl_util_java_source") {
  src_search_dirs = [ "java/src" ]
  source_deps = [ ":cronet_impl_java_util_java" ]
  jar_path = "$_package_dir/cronet_impl_util_java-src.jar"
}

jar_src("jar_httpengine_native_provider_java_source") {
  src_search_dirs = [ "java/src" ]
  source_deps = [ ":httpengine_native_provider_java" ]
  jar_path = "$_package_dir/httpengine_native_provider_java-src.jar"
}

# List of patterns of .class files to exclude from the jar.
_jar_excluded_patterns = [
  # Excludes Android support libraries crbug.com/832770.
  "android/*",
  "androidx/*",
  "*/library_loader/*.class",
  "*/multidex/*.class",
  "*/process_launcher/*.class",
  "*/SysUtils*.class",
  "org/chromium/base/Feature*.class",
  "org/chromium/base/PowerMonitor*.class",
  "org/chromium/base/jank_tracker/*.class",
  "org/chromium/base/memory/MemoryPurgeManager*.class",
  "org/chromium/base/memory/MemoryPressureMonitor*.class",
]

# Excluded from repackage_jars, but not from jar_src().
_build_jar_excluded_patterns = [ "org/chromium/build/NativeLibraries.class" ]

# See crbug.com/1401015 for more info on why GEN_JNI is excluded by default
template("repackage_jars") {
  _excludes = []
  if (defined(invoker.jar_excluded_patterns)) {
    _excludes += invoker.jar_excluded_patterns
  }
  if (!defined(invoker.remove_gen_jni) || invoker.remove_gen_jni) {
    _excludes += [ "*/*GEN_JNI.class" ]
  }
  dist_jar(target_name) {
    requires_android = true
    direct_deps_only = true
    use_unprocessed_jars = true
    no_build_hooks = true
    jar_excluded_patterns = _excludes
    forward_variables_from(invoker, "*", [ "jar_excluded_patterns" ])
  }
}

repackage_jars("repackage_shared") {
  output = "$_package_dir/cronet_shared_java.jar"
  deps = cronet_shared_java_deps_to_package + [ ":cronet_shared_java" ]
}

repackage_jars("repackage_api") {
  output = "$_package_dir/cronet_api.jar"
  deps = [
    ":cronet_api_helpers_java",
    ":cronet_api_java",
  ]
}

repackage_jars("repackage_platform") {
  output = "$_package_dir/cronet_impl_platform_java.jar"
  deps = [ ":cronet_impl_platform_java" ]
}

repackage_jars("repackage_fake") {
  output = "$_package_dir/cronet_impl_fake_java.jar"
  deps = [ ":cronet_impl_fake_java" ]
}

repackage_jars("repackage_util") {
  output = "$_package_dir/cronet_impl_util_java.jar"
  deps = [ ":cronet_impl_java_util_java" ]
}

repackage_jars("repackage_httpengine_native_provider") {
  output = "$_package_dir/httpengine_native_provider_java.jar"
  deps = [ ":httpengine_native_provider_java" ]
}

# See crbug.com/1005836 for more info on why repackage_native requires 2 extra
# targets. These 3 targets exist to ensure the correct version of GEN_JNI
# (a generated class containing native method definitions) is included.
repackage_jars("repackage_native") {
  output = "$_package_dir/cronet_impl_native_java.jar"
  deps = [
    ":cronet_jni_registration_java",
    ":repackage_native_java",
  ]
  remove_gen_jni = false
  jar_excluded_patterns = _jar_excluded_patterns + _build_jar_excluded_patterns
}

_native_intermediate_jar_path = "$target_out_dir/repackage_native_impl.jar"

# Do not depend on this target directly. Use :repackage_native.
repackage_jars("repackage_native_impl") {
  output = _native_intermediate_jar_path
  deps =
      cronet_impl_native_java_deps_to_package + [ ":cronet_impl_native_java" ]
  jar_excluded_patterns = _jar_excluded_patterns + _build_jar_excluded_patterns
}

# Do not depend on this target directly. Use :repackage_native.
# This target exists to provide :repackage_native with a suitable target to
# depend on (since dist_aar only pulls in deps of type "java_library").
android_java_prebuilt("repackage_native_java") {
  jar_path = _native_intermediate_jar_path

  # Since only the unprocessed jar is used, no need to complete the bytecode
  # processing step.
  enable_bytecode_checks = false
  deps = [ ":repackage_native_impl" ]
}

repackage_jars("repackage_impl_common") {
  output = "$_package_dir/cronet_impl_common_java.jar"
  deps = cronet_impl_common_java_deps_to_package + [
           ":cronet_impl_common_java",
           ":cronet_impl_java_util_java",
         ]
}

_cronet_shared_lib_file_name = "lib" + _cronet_shared_lib_name + ".so"

instrumentation_test_apk("cronet_sample_test_apk") {
  apk_name = "CronetSampleTest"
  apk_under_test = ":cronet_sample_apk"
  android_manifest = "sample/javatests/AndroidManifest.xml"
  sources = [
    "sample/javatests/src/org/chromium/cronet_sample_apk/CronetSampleTest.java",
  ]
  deps = [
    ":cronet_sample_apk_java",
    ":package_impl_common_java",
    "//third_party/android_deps:com_googlecode_java_diff_utils_diffutils_java",
    "//third_party/android_deps:espresso_java",
    "//third_party/androidx:androidx_test_monitor_java",
    "//third_party/androidx:androidx_test_rules_java",
    "//third_party/androidx:androidx_test_runner_java",
    "//third_party/google-truth:google_truth_java",
    "//third_party/junit",
  ]

  if (!is_java_debug) {
    proguard_enabled = true
    proguard_configs = [ "sample/javatests/proguard.cfg" ]
  }
}

shared_library("cronet_tests") {
  testonly = true
  assert_no_deps = [ ":cronet_static" ]
  sources = [
    # While "cronet_tests" cannot depend on "cronet_static", and hence cannot
    # call any Cronet functions, it can access fields of Cronet objects, so add
    # Cronet header files to facilitate accessing these fields.
    "//components/cronet/android/cronet_context_adapter.h",
    "//components/cronet/android/cronet_url_request_adapter.h",
    "//components/cronet/cronet_url_request.h",
    "//components/cronet/url_request_context_config.h",
    "test/cronet_test_jni.cc",
    "test/cronet_test_util.cc",
    "test/cronet_test_util.h",
    "test/cronet_url_request_context_config_test.cc",
    "test/cronet_url_request_context_config_test.h",
    "test/cronet_url_request_test.cc",
    "test/experimental_options_test.cc",
    "test/mock_cert_verifier.cc",
    "test/mock_url_request_job_factory.cc",
    "test/native_test_server.cc",
    "test/quic_test_server.cc",
    "test/test_upload_data_stream_handler.cc",
    "test/test_upload_data_stream_handler.h",
    "test/url_request_intercepting_job_factory.cc",
    "test/url_request_intercepting_job_factory.h",
  ]

  deps = [
    ":cronet_test_apk_jni",
    ":cronet_tests_jni_headers",
    "//base",
    "//base:i18n",
    "//base/test:test_support",
    "//components/cronet:cronet_common",
    "//components/cronet:cronet_version_header",
    "//components/cronet/testing:test_support",
    "//components/prefs",
    "//net",
    "//net:simple_quic_tools",
    "//net:test_support",
    "//third_party/icu",
  ]

  include_dirs = [ _cronet_version_header_include_dir ]

  configs -= [ "//build/config/android:hide_all_but_jni_onload" ]
  configs += [ "//build/config/android:hide_all_but_jni" ]
}

android_resources("cronet_test_apk_resources") {
  testonly = true
  sources = [
    # TODO(crbug.com/40283951): Modify quicroot.pem to become a build time copy of
    # net/data/ssl/certificates/cronet-quic-chain.pem.
    "test/res/raw/quicroot.pem",
    "test/res/values/cronet-test-rule-configuration.xml",
    "test/res/values/strings.xml",
    "test/res/xml/network_security_config.xml",
  ]
}

generate_jni("cronet_test_apk_jni") {
  testonly = true
  sources = [
    "test/src/org/chromium/net/CronetTestUtil.java",
    "test/src/org/chromium/net/MockCertVerifier.java",
    "test/src/org/chromium/net/MockUrlRequestJobFactory.java",
    "test/src/org/chromium/net/NativeTestServer.java",
    "test/src/org/chromium/net/QuicTestServer.java",
    "test/src/org/chromium/net/TestUploadDataStreamHandler.java",
  ]
}

android_library("cronet_test_apk_java") {
  testonly = true

  srcjar_deps = [ ":cronet_test_apk_jni" ]
  sources = [
    "test/src/org/chromium/net/CronetTestApplication.java",
    "test/src/org/chromium/net/CronetTestUtil.java",
    "test/src/org/chromium/net/Http2TestHandler.java",
    "test/src/org/chromium/net/Http2TestServer.java",
    "test/src/org/chromium/net/MockCertVerifier.java",
    "test/src/org/chromium/net/MockUrlRequestJobFactory.java",
    "test/src/org/chromium/net/NativeTestServer.java",
    "test/src/org/chromium/net/QuicTestServer.java",
    "test/src/org/chromium/net/ReportingCollector.java",
    "test/src/org/chromium/net/TestFilesInstaller.java",
    "test/src/org/chromium/net/TestUploadDataStreamHandler.java",
    "test/src/org/chromium/net/truth/UrlResponseInfoSubject.java",
  ]

  deps = [
    ":cronet_all_java",
    "//base:base_java",
    "//base:base_java_test_support",
    "//build/android:build_java",
    "//net/android:net_java_test_support",
    "//third_party/androidx:androidx_annotation_annotation_java",
    "//third_party/google-truth:google_truth_java",
    "//third_party/jni_zero:jni_zero_java",
    "//third_party/junit",
    "//third_party/netty4:netty_all_java",
  ]
}

cronet_smoketests_platform_only_common_srcs = [
  "test/smoketests/src/org/chromium/net/smoke/ChromiumPlatformOnlyTestSupport.java",
  "test/smoketests/src/org/chromium/net/smoke/CronetSmokeTestRule.java",
  "test/smoketests/src/org/chromium/net/smoke/HttpTestServer.java",
  "test/smoketests/src/org/chromium/net/smoke/SmokeTestRequestCallback.java",
  "test/smoketests/src/org/chromium/net/smoke/TestSupport.java",
  "test/smoketests/src/org/chromium/net/smoke/CronetPlatformSmokeTestRule.java",
]

cronet_smoketests_native_common_srcs = cronet_smoketests_platform_only_common_srcs + [
                                         "test/smoketests/src/org/chromium/net/smoke/ChromiumNativeTestSupport.java",
                                         "test/smoketests/src/org/chromium/net/smoke/NativeCronetTestRule.java",
                                       ]

cronet_tests_androidx_common_srcs = [
  "//third_party/androidx:androidx_annotation_annotation_java",
  "//third_party/androidx:androidx_test_core_java",
  "//third_party/androidx:androidx_test_ext_junit_java",
  "//third_party/androidx:androidx_test_runner_java",
]

# cronet_common_javatests.jar - Cronet Java test common files.
# TODO(crbug.com/40919033) - refactor to cronet_api only dependency
android_library("cronet_common_javatests") {
  testonly = true
  sources = [
    "test/javatests/src/org/chromium/net/ContextInterceptor.java",
    "test/javatests/src/org/chromium/net/CronetLoggerTestRule.java",
    "test/javatests/src/org/chromium/net/CronetTestRule.java",
    "test/javatests/src/org/chromium/net/TestRequestFinishedListener.java",
    "test/javatests/src/org/chromium/net/TestUploadDataProvider.java",
    "test/javatests/src/org/chromium/net/TestUrlRequestCallback.java",
    "test/javatests/src/org/chromium/net/httpflags/HttpFlagsInterceptor.java",
    "test/javatests/src/org/chromium/net/impl/CronetManifestInterceptor.java",
  ]
  deps = [
           ":cronet_all_java",
           ":cronet_test_apk_java",
           ":flags_java_proto",
           "//base:base_java",
           "//base:base_java_test_support",
           "//build/android:build_java",
           "//third_party/androidx:androidx_core_core_java",
           "//third_party/google-truth:google_truth_java",
           "//third_party/jni_zero:jni_zero_java",
           "//third_party/junit",
         ] + cronet_tests_androidx_common_srcs
}

# cronet_fake_javatests.jar - Java tests for the fake implementation of Cronet.
android_library("cronet_fake_javatests") {
  testonly = true
  sources = [
    "fake/javatests/org/chromium/net/test/FakeCronetControllerTest.java",
    "fake/javatests/org/chromium/net/test/FakeCronetEngineTest.java",
    "fake/javatests/org/chromium/net/test/FakeCronetProviderTest.java",
    "fake/javatests/org/chromium/net/test/FakeUrlRequestTest.java",
    "fake/javatests/org/chromium/net/test/FakeUrlResponseTest.java",
    "fake/javatests/org/chromium/net/test/UrlResponseMatcherTest.java",
  ]

  deps = [
           ":cronet_api_helpers_java",
           ":cronet_api_java",
           ":cronet_common_javatests",
           ":cronet_impl_common_java",
           ":cronet_impl_fake_java",
           ":cronet_impl_platform_java",
           ":cronet_shared_java",
           ":cronet_test_apk_java",
           "//base:base_java_test_support",
           "//third_party/android_deps:guava_android_java",
           "//third_party/google-truth:google_truth_java",
           "//third_party/junit",
         ] + cronet_tests_androidx_common_srcs
}

cronet_javatests_deps_to_package = [
  ":cronet_common_javatests",
  ":cronet_test_apk_java",
  ":flags_java_proto",
  "//base:base_java",
  "//third_party/jni_zero:jni_zero_java",
  "//base:base_java_test_support",

  "//net/android:embedded_test_server_aidl_java",
  "//net/android:net_java",
  "//net/android:net_java_test_support",
  "//url:url_java",
  "//third_party/androidx:androidx_annotation_annotation_experimental_java",
]

if (!is_cronet_for_aosp_build) {
  # Don't bundle FakeCronetEngine or its tests for AOSP.
  cronet_javatests_deps_to_package += [ ":cronet_fake_javatests" ]
}

generate_jni("cronet_tests_jni_headers") {
  testonly = true
  sources = [
    "test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java",
    "test/javatests/src/org/chromium/net/CronetUrlRequestTest.java",
    "test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java",
  ]
}

android_library("cronet_javatests") {
  testonly = true

  srcjar_deps = [ ":cronet_tests_jni_headers" ]
  sources = [
    "test/javatests/src/org/chromium/net/BidirectionalStreamQuicTest.java",
    "test/javatests/src/org/chromium/net/BidirectionalStreamTest.java",
    "test/javatests/src/org/chromium/net/BrotliTest.java",
    "test/javatests/src/org/chromium/net/Criteria.java",
    "test/javatests/src/org/chromium/net/CronetEngineBuilderTest.java",
    "test/javatests/src/org/chromium/net/CronetOnboardingTest.java",
    "test/javatests/src/org/chromium/net/CronetStressTest.java",
    "test/javatests/src/org/chromium/net/CronetTestRuleTest.java",
    "test/javatests/src/org/chromium/net/CronetUploadTest.java",
    "test/javatests/src/org/chromium/net/CronetUrlRequestContextTest.java",
    "test/javatests/src/org/chromium/net/CronetUrlRequestH2Test.java",
    "test/javatests/src/org/chromium/net/CronetUrlRequestHTTPSTest.java",
    "test/javatests/src/org/chromium/net/CronetUrlRequestTest.java",
    "test/javatests/src/org/chromium/net/DiskStorageTest.java",
    "test/javatests/src/org/chromium/net/ExperimentalOptionsTest.java",
    "test/javatests/src/org/chromium/net/ExperimentalOptionsTranslationTest.java",
    "test/javatests/src/org/chromium/net/ExperimentalOptionsTranslationTestUtil.java",
    "test/javatests/src/org/chromium/net/FileUtils.java",
    "test/javatests/src/org/chromium/net/GetStatusTest.java",
    "test/javatests/src/org/chromium/net/LogcatCapture.java",
    "test/javatests/src/org/chromium/net/MetricsTestUtil.java",
    "test/javatests/src/org/chromium/net/MockCertVerifierTest.java",
    "test/javatests/src/org/chromium/net/NQETest.java",
    "test/javatests/src/org/chromium/net/NetworkChangesTest.java",
    "test/javatests/src/org/chromium/net/NetworkErrorLoggingTest.java",
    "test/javatests/src/org/chromium/net/PkpTest.java",
    "test/javatests/src/org/chromium/net/QuicTest.java",
    "test/javatests/src/org/chromium/net/RequestFinishedInfoTest.java",
    "test/javatests/src/org/chromium/net/SharedDictionaryTest.java",
    "test/javatests/src/org/chromium/net/TestBidirectionalStreamCallback.java",
    "test/javatests/src/org/chromium/net/TestDrivenDataProvider.java",
    "test/javatests/src/org/chromium/net/TestNetworkQualityRttListener.java",
    "test/javatests/src/org/chromium/net/TestNetworkQualityThroughputListener.java",
    "test/javatests/src/org/chromium/net/UrlResponseInfoTest.java",
    "test/javatests/src/org/chromium/net/apihelpers/ContentTypeParametersParserTest.java",
    "test/javatests/src/org/chromium/net/apihelpers/UploadDataProvidersTest.java",
    "test/javatests/src/org/chromium/net/httpflags/BaseFeatureTest.java",
    "test/javatests/src/org/chromium/net/httpflags/HttpFlagsLoaderTest.java",
    "test/javatests/src/org/chromium/net/httpflags/ResolvedFlagsTest.java",
    "test/javatests/src/org/chromium/net/impl/AndroidHttpEngineBuilderWrapperTest.java",
    "test/javatests/src/org/chromium/net/impl/AndroidHttpEngineWrapperTest.java",
    "test/javatests/src/org/chromium/net/impl/CronetLoggerTest.java",
    "test/javatests/src/org/chromium/net/impl/CronetManifestTest.java",
    "test/javatests/src/org/chromium/net/telemetry/CronetLoggerImplTest.java",
    "test/javatests/src/org/chromium/net/telemetry/ExperimentalOptionsTest.java",
    "test/javatests/src/org/chromium/net/telemetry/HashTest.java",
    "test/javatests/src/org/chromium/net/telemetry/RateLimiterTest.java",
    "test/javatests/src/org/chromium/net/telemetry/SizeBucketsTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/CronetBufferedOutputStreamTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/CronetChunkedOutputStreamTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/CronetFixedModeOutputStreamTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLConnectionTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/CronetHttpURLStreamHandlerTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/CronetInputStreamTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/CronetURLStreamHandlerFactoryTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/MessageLoopTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/QuicUploadTest.java",
    "test/javatests/src/org/chromium/net/urlconnection/TestUtil.java",
  ]

  # Adding deps here won't include those deps in the cronet_tests_java.jar.
  # Please add to cronet_javatests_deps_to_package instead.
  deps = [
    ":cronet_all_java",
    ":cronet_api_helpers_java",
    ":cronet_urlconnection_impl_java",
    "//third_party/android_deps:guava_android_java",
    "//third_party/android_deps:protobuf_lite_runtime_java",
    "//third_party/androidx:androidx_annotation_annotation_java",
    "//third_party/androidx:androidx_test_core_java",
    "//third_party/androidx:androidx_test_ext_junit_java",
    "//third_party/androidx:androidx_test_runner_java",
    "//third_party/google-truth:google_truth_java",
    "//third_party/hamcrest:hamcrest_core_java",
    "//third_party/junit",
    "//third_party/mockito:mockito_java",
  ]
  deps += cronet_javatests_deps_to_package
  data = [ "//components/cronet/testing/test_server/data/" ]
}

instrumentation_test_apk("cronet_test_instrumentation_apk") {
  # This is the only Cronet APK with lint enabled. This one was chosen because
  # it depends on basically all source files.
  enable_lint = true
  lint_baseline_file = "lint-baseline.xml"
  lint_suppressions_file = "lint-suppressions.xml"

  lint_min_sdk_version = 21

  apk_name = "CronetTestInstrumentation"
  android_manifest = "test/javatests/AndroidManifest.xml"

  shared_libraries = [
    ":cronet",
    ":cronet_tests",
  ]
  loadable_modules = [ "$root_out_dir/libnetty-tcnative.so" ]

  sources = cronet_smoketests_native_common_srcs + [
              "test/smoketests/src/org/chromium/net/smoke/Http2Test.java",
              "test/smoketests/src/org/chromium/net/smoke/QuicTest.java",
            ]

  deps = [
           ":cronet_all_java",
           ":cronet_combine_proguard_flags",
           ":cronet_javatests",
           ":cronet_test_apk_java",
           ":cronet_test_apk_resources",
           "//base:base_java",
           "//base:base_java_test_support",
           "//build/android:build_java",
           "//net/android:net_java",
           "//net/android:net_java_test_support",
           "//third_party/android_sdk:android_test_mock_java",
           "//third_party/google-truth:google_truth_java",
           "//third_party/hamcrest:hamcrest_core_java",
           "//third_party/jni_zero:jni_zero_java",
           "//third_party/junit",
           "//third_party/netty-tcnative:netty-tcnative-so",
           "//third_party/netty4:netty_all_java",
         ] + cronet_tests_androidx_common_srcs

  data_deps = [ "//net:test_support" ]

  if (!is_java_debug) {
    proguard_enabled = true

    proguard_configs = [
      "$target_gen_dir/cronet_impl_native_proguard.cfg",
      "cronet_shared_proguard.cfg",
      "cronet_impl_common_proguard.cfg",
      "cronet_impl_platform_proguard.cfg",
      "httpengine_native_provider_proguard.cfg",
      "test/proguard.cfg",
    ]
  }
  srcjar_deps = [
    ":cronet_jni_registration",
    ":cronet_tests_jni_registration",
  ]
}

instrumentation_test_apk(
    "cronet_smoketests_platform_only_instrumentation_apk") {
  apk_name = "PlatformOnlyEngineSmokeTestInstrumentation"
  android_manifest = "test/javatests/AndroidManifest.xml"

  sources = cronet_smoketests_platform_only_common_srcs + [
              "test/smoketests/src/org/chromium/net/smoke/PlatformOnlyEngineTest.java",
              "test/src/org/chromium/net/CronetTestApplication.java",
              "test/src/org/chromium/net/truth/UrlResponseInfoSubject.java",
            ]

  deps = [
           ":cronet_api_java",
           ":cronet_impl_common_java",
           ":cronet_impl_platform_java",
           ":cronet_test_apk_resources",
           "//base:base_java_test_support",
           "//third_party/android_sdk:android_test_mock_java",
           "//third_party/google-truth:google_truth_java",
           "//third_party/junit",
           "//third_party/netty4:netty_all_java",
         ] + cronet_tests_androidx_common_srcs

  if (!is_java_debug) {
    proguard_enabled = true

    proguard_configs = [
      "cronet_impl_common_proguard.cfg",
      "cronet_impl_platform_proguard.cfg",
      "test/proguard.cfg",
    ]
  }

  # There is no native in this apk. However, since we depend on //base, we
  # need something that provides a GEN_JNI, as //base uses GEN_JNI.
  srcjar_deps = [ ":cronet_jni_registration" ]
}

instrumentation_test_apk(
    "cronet_smoketests_missing_native_library_instrumentation_apk") {
  apk_name = "MissingNativeLibrarySmokeTestInstrumentation"
  android_manifest = "test/javatests/AndroidManifest.xml"

  sources = cronet_smoketests_native_common_srcs + [ "test/smoketests/src/org/chromium/net/smoke/MissingNativeLibraryTest.java" ]
  deps = [
           ":cronet_api_java",
           ":cronet_combine_proguard_flags",
           ":cronet_impl_common_java",
           ":cronet_impl_platform_java",
           ":cronet_shared_java",
           ":cronet_test_apk_java",
           ":cronet_test_apk_resources",
           "//base:base_java",
           "//base:base_java_test_support",
           "//build/android:build_java",
           "//third_party/android_sdk:android_test_mock_java",
           "//third_party/google-truth:google_truth_java",
           "//third_party/jni_zero:jni_zero_java",
           "//third_party/junit",
           "//third_party/netty4:netty_all_java",
         ] + cronet_tests_androidx_common_srcs

  if (!is_java_debug) {
    proguard_enabled = true
    proguard_configs = [
      "$target_gen_dir/cronet_impl_native_proguard.cfg",
      "cronet_shared_proguard.cfg",
      "cronet_impl_common_proguard.cfg",
      "cronet_impl_platform_proguard.cfg",
      "test/proguard.cfg",
    ]
  }

  # There is no native in this apk. However, since we depend on //base, we
  # need something that provides a GEN_JNI, as //base uses GEN_JNI.
  srcjar_deps = [ ":cronet_jni_registration" ]
}

# The smoketests included in this test target are already included in
# cronet_test_instrumentation_apk, but they are proguarded here using
# the same proguard rules for which we release Cronet with. This is
# to ensure that any changes to the proguard does not break Cronet.
# Note: The proguard rules includes the test proguard configs as
# the build system automatically pulls in all transitive proguard
# configs that can be found through the dependency chain.
instrumentation_test_apk("cronet_smoketests_apk") {
  apk_name = "CronetSmokeTestInstrumentation"
  android_manifest = "test/javatests/AndroidManifest.xml"
  shared_libraries = [
    ":cronet",
    ":cronet_tests",
  ]
  loadable_modules = [ "$root_out_dir/libnetty-tcnative.so" ]
  sources = cronet_smoketests_native_common_srcs + [
              "test/smoketests/src/org/chromium/net/smoke/Http2Test.java",
              "test/smoketests/src/org/chromium/net/smoke/QuicTest.java",
            ]
  deps = [
           ":cronet_api_java",

           # cronet_test_apk_java pulls all implementation of Cronet along with their proguard cfgs.
           ":cronet_test_apk_java",
           ":cronet_test_apk_resources",
           "//base:base_java",
           "//base:base_java_test_support",
           "//build/android:build_java",
           "//third_party/android_sdk:android_test_mock_java",
           "//third_party/google-truth:google_truth_java",
           "//third_party/junit",
           "//third_party/netty-tcnative:netty-tcnative-so",
           "//third_party/netty4:netty_all_java",
         ] + cronet_tests_androidx_common_srcs

  proguard_enabled = true
  proguard_configs = [ "test/proguard.cfg" ]
  srcjar_deps = [
    ":cronet_jni_registration",
    ":cronet_tests_jni_registration",
  ]
}

android_apk("cronet_perf_test_apk") {
  testonly = true
  apk_name = "CronetPerfTest"
  android_manifest = "test/javaperftests/AndroidManifest.xml"
  shared_libraries = [
    ":cronet",
    ":cronet_tests",
  ]

  sources =
      [ "test/javaperftests/src/org/chromium/net/CronetPerfTestActivity.java" ]

  deps = [
    ":cronet_all_java",
    ":cronet_combine_proguard_flags",
    ":cronet_javatests",
    ":cronet_test_apk_java",
    "//base:base_java",
    "//build/android:build_java",
    "//third_party/android_sdk:android_test_mock_java",
    "//third_party/jni_zero:jni_zero_java",
    "//third_party/junit",
  ]

  if (!is_java_debug) {
    proguard_enabled = true
    proguard_configs = [
      "$target_gen_dir/cronet_impl_native_proguard.cfg",
      "cronet_shared_proguard.cfg",
      "cronet_impl_common_proguard.cfg",
      "httpengine_native_provider_proguard.cfg",
      "test/proguard.cfg",
      "//testing/android/proguard_for_test.flags",
      "//third_party/jni_zero/proguard_for_test.flags",
    ]
    enable_proguard_checks = false
  }
  srcjar_deps = [
    ":cronet_jni_registration",
    ":cronet_tests_jni_registration",
  ]
}

test("cronet_unittests_android") {
  deps = [
    ":cronet_base_feature_unittest",
    ":cronet_impl_native_java",
    ":cronet_static",
    "//base",
    "//base/test:test_support",
    "//components/cronet:cronet_common_unittests",
    "//components/prefs:test_support",
    "//net",
    "//net:test_support",
    "//net/android:net_java",
    "//testing/gtest",
  ]

  if (!is_cronet_for_aosp_build) {
    deps += [ "//components/cronet/native:cronet_native_unittests" ]
  }

  sources = [ "../run_all_unittests.cc" ]

  data = [ "//components/cronet/testing/test_server/data/" ]

  if (is_android) {
    shard_timeout = 180
  }
}

test("cronet_tests_android") {
  deps = [
    ":cronet_impl_native_java",
    ":cronet_static",
    "//base",
    "//base/test:test_support",
    "//components/prefs:test_support",
    "//net",
    "//net:test_support",
    "//net/android:net_java",
    "//testing/gtest",
  ]

  if (!is_cronet_for_aosp_build) {
    deps += [ "//components/cronet/native/test:cronet_native_tests" ]
  }

  allow_cleartext_traffic = true

  sources = [ "../run_all_unittests.cc" ]

  defines = [ "CRONET_TESTS_IMPLEMENTATION" ]

  data = [ "//components/cronet/testing/test_server/data/" ]

  if (is_android) {
    shard_timeout = 180
  }
}

_test_package_dir = "$root_out_dir/cronet/test"
_tests_java_jars_without_jni_path =
    "$target_out_dir/cronet_tests_java_without_jni.jar"
_test_deps = cronet_javatests_deps_to_package + [
               ":cronet_javatests",
               "//third_party/netty4:netty_all_java",
             ]

# First, generate an intermediate jar with the various source GEN_JNI classes
# removed to avoid conflicts...
repackage_jars("repackage_test_jars_without_jni") {
  output = _tests_java_jars_without_jni_path
  testonly = true
  deps = _test_deps
  remove_gen_jni = true
}

# This target exists to provide :repackage_test_jars with a suitable target to
# depend on (since dist_aar only pulls in deps of type "java_library").
android_java_prebuilt("repackage_test_jars_without_jni_java") {
  jar_path = _tests_java_jars_without_jni_path
  testonly = true

  # Since only the unprocessed jar is used, no need to complete the bytecode
  # processing step.
  enable_bytecode_checks = false
  deps = [ ":repackage_test_jars_without_jni" ]
}

# ...then, generate the final, merged GEN_JNI class...
generate_jni_registration("cronet_tests_jni_registration") {
  java_targets = _test_deps
  manual_jni_registration = true
  testonly = true
  module_name = "cronet_tests"
  _subdir = rebase_path(target_gen_dir, root_gen_dir)
  cpp_codegen_output =
      "$jni_headers_dir/$_subdir/cronet_tests_jni_registration_generated.h"
}
android_library("cronet_tests_jni_registration_java") {
  srcjar_deps = [ ":cronet_tests_jni_registration" ]
  testonly = true
}

# ...and finally, generate the test jar with the final GEN_JNI class.
repackage_jars("repackage_test_jars") {
  output = "$_test_package_dir/cronet_tests_java.jar"
  testonly = true
  deps = [
    ":cronet_tests_jni_registration_java",
    ":repackage_test_jars_without_jni_java",
  ]
  remove_gen_jni = false
}

zip("jar_cronet_sample_source") {
  inputs = [
    "sample/AndroidManifest.xml",
    "sample/README",
    "sample/res/drawable/ic_action_home.xml",
    "sample/res/drawable/ic_flag.xml",
    "sample/res/layout/main_activity.xml",
    "sample/res/layout/main_fragment.xml",
    "sample/res/layout/options_fragment.xml",
    "sample/res/layout/toggle_view.xml",
    "sample/res/values/colors.xml",
    "sample/res/values/dimens.xml",
    "sample/res/values/strings.xml",
    "sample/res/values/styles.xml",
    "sample/src/org/chromium/cronet_sample_apk/Action.java",
    "sample/src/org/chromium/cronet_sample_apk/ActionData.java",
    "sample/src/org/chromium/cronet_sample_apk/CronetSampleActivity.java",
    "sample/src/org/chromium/cronet_sample_apk/CronetSampleApplication.java",
    "sample/src/org/chromium/cronet_sample_apk/MainFragment.java",
    "sample/src/org/chromium/cronet_sample_apk/Options.java",
    "sample/src/org/chromium/cronet_sample_apk/OptionsFragment.java",
    "sample/src/org/chromium/cronet_sample_apk/OptionsRecyclerViewAdapter.java",
    "sample/src/org/chromium/cronet_sample_apk/SampleActivityViewModel.java",
  ]
  output = "$_package_dir/cronet-sample-src.jar"
  base_dir = "sample"
}

jar_src("jar_cronet_impl_native_java_source") {
  src_search_dirs = [
    "//base/android/java/src",
    "//base/test/android/javatests/src/stub",
    "//build/android/java/src",
    "//components/cronet/android/java/src",
    "//net/android/java/src",
    "//url/android/java/src",
    "//third_party/jni_zero/java/src",
  ]
  source_deps = [
    ":cronet_impl_native_java",
    "//base:base_java",
    "//build/android:build_java",
    "//net/android:net_java",
    "//third_party/jni_zero:jni_zero_java",
    "//url:url_java",
  ]

  srcjar_deps = cronet_impl_native_java_srcjar_deps
  srcjar_deps += [
    ":cronet_jni_registration",
    "//base:base_android_java_enums_srcjar",
    "//net/android:net_android_java_enums_srcjar",
    "//net/android:net_errors_java",
  ]
  excluded_patterns = _jar_excluded_patterns
  jar_path = "$_package_dir/cronet_impl_native_java-src.jar"
}

action("generate_licenses") {
  _license_path = "$_package_dir/LICENSE"

  script = "//tools/licenses/licenses.py"
  outputs = [ _license_path ]
  args = [
    "license_file",
    rebase_path(_license_path, root_build_dir),
    "--gn-target",
    "//components/cronet/android:cronet_non_test_package",
    "--gn-out-dir",
    ".",
  ]
}

copy("cronet_package_copy") {
  sources = [
    "$target_gen_dir/cronet_impl_native_proguard.cfg",
    "//AUTHORS",
    "//chrome/VERSION",
    "api_version.txt",
    "cronet_impl_common_proguard.cfg",
    "cronet_impl_fake_proguard.cfg",
    "cronet_impl_platform_proguard.cfg",
    "cronet_shared_proguard.cfg",
    "httpengine_native_provider_proguard.cfg",
  ]
  outputs = [ "$_package_dir/{{source_file_part}}" ]

  deps = [
    ":cronet_api_java",
    ":cronet_combine_proguard_flags",
    ":cronet_impl_common_java",
    ":cronet_impl_fake_java",
    ":cronet_impl_platform_java",
    ":cronet_shared_java",
    ":httpengine_native_provider_java",
  ]
}

copy("cronet_package_copy_native_headers") {
  sources = cronet_native_public_headers + grpc_public_headers

  outputs = [ "$_package_dir/include/{{source_file_part}}" ]
}

copy("cronet_package_copy_native_lib") {
  sources = [ "$root_out_dir/" + _cronet_shared_lib_file_name ]
  outputs = [ "$_package_dir/libs/${android_app_abi}/" +
              _cronet_shared_lib_file_name ]
  deps = [ ":cronet" ]
}

copy("cronet_package_copy_native_lib_unstripped") {
  sources = [ "$root_out_dir/lib.unstripped/" + _cronet_shared_lib_file_name ]
  outputs = [ "$_package_dir/symbols/${android_app_abi}/" +
              _cronet_shared_lib_file_name ]
  deps = [ ":cronet" ]
}

copy("cronet_package_copy_native_test_lib") {
  testonly = true
  sources = [
    "$root_out_dir/libcronet_tests.so",
    "$root_out_dir/libnetty-tcnative.so",
  ]
  outputs =
      [ "$_test_package_dir/libs/${android_app_abi}/{{source_file_part}}" ]
  deps = [
    ":cronet_tests",
    "//third_party/netty-tcnative:netty-tcnative-so",
  ]
}

copy("cronet_package_copy_native_test_lib_unstripped") {
  testonly = true
  sources = [
    "$root_out_dir/lib.unstripped/libcronet_tests.so",
    "$root_out_dir/lib.unstripped/libnetty-tcnative.so",
  ]
  outputs =
      [ "$_test_package_dir/symbols/${android_app_abi}/{{source_file_part}}" ]
  deps = [
    ":cronet_tests",
    "//third_party/netty-tcnative:netty-tcnative-so",
  ]
}

copy("cronet_package_copy_test_assets") {
  testonly = true
  sources = [ "//components/cronet/testing/test_server/data" ]
  outputs = [ "$_test_package_dir/assets/test" ]
}

copy("cronet_package_copy_test_resources") {
  testonly = true
  sources = [ "//components/cronet/android/test/res" ]
  outputs = [ "$_test_package_dir/res" ]
}

copy("cronet_package_copy_test_files") {
  testonly = true
  sources = [
    "//net/data/ssl/certificates/cronet-quic-chain.pem",
    "//net/data/ssl/certificates/cronet-quic-leaf-cert.key",
    "//net/data/ssl/certificates/cronet-quic-leaf-cert.key.pkcs8.pem",
    "//net/data/ssl/certificates/expired_cert.pem",
    "//net/data/ssl/certificates/quic-chain.pem",
    "//net/data/ssl/certificates/quic-leaf-cert.key",
    "//net/data/ssl/certificates/quic-leaf-cert.key.pkcs8.pem",
    "//net/data/ssl/certificates/root_ca_cert.pem",
  ]
  outputs = [ "$_test_package_dir/assets/test_files/net/data/ssl/certificates/{{source_file_part}}" ]
  deps = [
    # Not really dependent, but builds can fail if these two targets attempt
    # to create the "assets" subdirectory simultaneously.
    ":cronet_package_copy_test_assets",
  ]
}

copy("cronet_package_copy_resources") {
  sources = [ "api/res/raw/keep_cronet_api.xml" ]
  outputs = [ "$_package_dir/res/raw/{{source_file_part}}" ]
}

# Enforce that ARM Neon is not used when building for ARMv7
if (target_cpu == "arm" && arm_version == 7 && !arm_use_neon) {
  action("enforce_no_neon") {
    script = "//components/cronet/tools/check_no_neon.py"
    outputs = [ "$target_gen_dir/$target_name.stamp" ]
    args = [
      rebase_path("${android_tool_prefix}objdump", root_build_dir),

      # libcronet.so may contain ARM Neon instructions from BoringSSL, but these
      # are only used after checking whether the CPU supports NEON at runtime,
      # so instead check base/ as it represents a large swath of code that only
      # contains Neon instructions when Neon is enabled by default.
      rebase_path("$root_out_dir/obj/base/base/*.o", root_build_dir),
      "--stamp",
      rebase_path(outputs[0], root_build_dir),
    ]
    deps = [ "//base:base" ]
  }
}

# Enforce restrictions for API<->impl boundary.
action("api_static_checks") {
  script = "//components/cronet/tools/api_static_checks.py"
  outputs = [ "$target_gen_dir/$target_name.stamp" ]
  _api_jar = "$_package_dir/cronet_api.jar"
  _common_jar = "$_package_dir/cronet_impl_common_java.jar"
  _platform_jar = "$_package_dir/cronet_impl_platform_java.jar"
  _native_jar = "$_package_dir/cronet_impl_native_java.jar"
  args = [
    "--api_jar",
    rebase_path(_api_jar, root_build_dir),
    "--impl_jar",
    rebase_path(_common_jar, root_build_dir),
    "--impl_jar",
    rebase_path(_platform_jar, root_build_dir),
    "--impl_jar",
    rebase_path(_native_jar, root_build_dir),
    "--stamp",
    rebase_path(outputs[0], root_build_dir),
  ]
  deps = [
    ":repackage_api",
    ":repackage_impl_common",
    ":repackage_native",
    ":repackage_platform",
  ]
  inputs = [
    _api_jar,
    _common_jar,
    _platform_jar,
    _native_jar,
    "//components/cronet/tools/update_api.py",
  ]
  sources = [
    "//components/cronet/android/api.txt",
    "//components/cronet/android/api_version.txt",
  ]
}

action("gn2bp_check") {
  script = "//components/cronet/gn2bp/validate_gn2bp.py"
  sources = [
    "$_gn_path",
    "//components/cronet/gn2bp/gen_android_bp.py",
    "//tools/mb/mb.py",
  ]
  outputs = [ "$target_gen_dir/$target_name.stamp" ]
  args = [
    "--stamp",
    rebase_path(outputs[0], root_build_dir),
  ]
}

if (!using_sanitizer) {
  action("dependencies_checks") {
    script = "//components/cronet/tools/check_cronet_dependencies.py"
    inputs = [ "//components/cronet/android/dependencies.txt" ]
    sources = [ "$_gn_path" ]
    outputs = [ "$target_gen_dir/$target_name.stamp" ]
    deps = [
      ":cronet",
      ":cronet_api_helpers_java",
      ":cronet_api_java",
      ":cronet_impl_native_java",
    ]
    abs_deps = []
    foreach(dep, deps) {
      abs_deps += [ get_label_info(dep, "label_no_toolchain") ]
    }
    args = [
      "--old-dependencies",
      rebase_path(inputs[0], root_build_dir),
      "--stamp",
      rebase_path(outputs[0], root_build_dir),
      "--root-deps",
    ]
    foreach(abs_dep, abs_deps) {
      args += [ abs_dep ]
    }
  }
}

group("cronet_non_test_package") {
  # Target containing all of cronet's non-test targets.
  # Enforce building with ICU alternatives, crbug.com/611621.
  # Enforce that arm_use_neon==false when building for ARMv7 by
  # not including any deps in cronet_package target otherwise.
  if (use_platform_icu_alternatives &&
      (!(target_cpu == "arm" && arm_version == 7) || !arm_use_neon)) {
    deps = [
      ":cronet_package_copy",
      ":cronet_package_copy_native_headers",
      ":cronet_package_copy_native_lib",
      ":cronet_package_copy_native_lib_unstripped",
      ":cronet_package_copy_resources",
      ":jar_cronet_api_source",
      ":jar_cronet_impl_common_java_source",
      ":jar_cronet_impl_fake_java_source",
      ":jar_cronet_impl_native_java_source",
      ":jar_cronet_impl_platform_java_source",
      ":jar_cronet_sample_source",
      ":jar_cronet_shared_source",
      ":jar_httpengine_native_provider_java_source",
      ":repackage_api",
      ":repackage_fake",
      ":repackage_httpengine_native_provider",
      ":repackage_impl_common",
      ":repackage_native",
      ":repackage_platform",
      ":repackage_shared",
    ]
    if (current_cpu == "arm" && arm_version == 7) {
      deps += [ ":enforce_no_neon" ]
    }
  }
}

group("cronet_test_package") {
  # Target containing all of cronet's test targets.
  testonly = true

  # Don't build for MIPS where tests aren't run.
  if (current_cpu != "mipsel" && current_cpu != "mips64el") {
    deps = [
      ":api_static_checks",
      ":cronet_package_copy_native_test_lib",
      ":cronet_package_copy_native_test_lib_unstripped",
      ":cronet_package_copy_test_assets",
      ":cronet_package_copy_test_files",
      ":cronet_package_copy_test_resources",
      ":cronet_sizes",
      ":repackage_test_jars",
    ]
  }
}

# Everything added here will be executed in the CQ.
group("cronet_package_android") {
  # Target containing all of cronet's targets both test and non-test ones.
  # Marked as testonly as it contains test-only targets too.
  testonly = true
  deps = [
    ":cronet_non_test_package",
    ":cronet_test_package",
    ":generate_licenses",
  ]
  if (!using_sanitizer && !use_jacoco_coverage) {
    deps += [ ":dependencies_checks" ]
  }
  if (!use_jacoco_coverage) {
    deps += [ ":cronet_combined_proguard_flags_golden_test" ]
  }
}

# Everything added here will be executed in the CI.
group("cronet_package_ci") {
  deps = [ ":gn2bp_check" ]
}

group("cronet_android_mainline") {
  # Target for android mainline which removed unnecessary deps from
  # "cronet_package_android"

  # Enforce building with ICU alternatives, crbug.com/611621.
  # Enforce that arm_use_neon==false when building for ARMv7 by
  # not including any deps in cronet_package target otherwise.
  if (use_platform_icu_alternatives &&
      (!(target_cpu == "arm" && arm_version == 7) || !arm_use_neon)) {
    deps = [
      ":cronet_package_copy_native_lib",
      ":jar_cronet_api_source",
      ":repackage_api",
      ":repackage_impl_common",
      ":repackage_native",
      ":repackage_shared",
    ]
    if (current_cpu == "arm" && arm_version == 7) {
      deps += [ ":enforce_no_neon" ]
    }
  }
}