Adds Bazel android_sdk and toolchains to integrate the Bazel Android rules with a prebuilt SDK.

Test: Built example Android app at build/bazel/examples/android_app in #1644124.
Change-Id: I654753cc9d10ffa3ef3be663d0ff402507d503a4
diff --git a/BUILD.bazel b/BUILD.bazel
new file mode 100644
index 0000000..5c6084c
--- /dev/null
+++ b/BUILD.bazel
@@ -0,0 +1,111 @@
+load("@rules_android//toolchains/android:toolchain.bzl", "android_toolchain")
+
+exports_files(["tools/linux/bin/aapt2"])
+
+android_sdk(
+    name = "android_sdk",
+    aapt = "tools/linux/bin/aapt",
+    aapt2 = "tools/linux/bin/aapt2",
+    adb = ":fail",  # TODO: use system/core/adb ?
+    aidl = "tools/linux/bin/aidl",
+    android_jar = "30/public/android.jar",
+    apksigner = ":apksigner",
+    dx = "tools/linux/bin/dx",  # TODO: add D8
+    framework_aidl = "30/public/framework.aidl",
+    main_dex_classes = "tools/mainDexClasses.rules",
+    main_dex_list_creator = ":fail",
+    proguard = ":fail",  # TODO: add R8
+    shrinked_android_jar = "30/public/android.jar",
+    visibility = ["//visibility:public"],
+    zipalign = "tools/linux/bin/zipalign",
+)
+
+java_import(
+    name = "dx_jar_import",
+    jars = ["tools/linux/lib/dx.jar"],
+    visibility = ["//visibility:public"],
+)
+
+java_binary(
+    name = "apksigner",
+    main_class = "com.android.apksigner.ApkSignerTool",
+    runtime_deps = ["tools/linux/lib/apksigner.jar"],
+)
+
+toolchain(
+    name = "android_sdk_tools",
+    exec_compatible_with = [
+        "@bazel_tools//platforms:x86_64",
+        "@bazel_tools//platforms:linux",
+    ],
+    # TODO(b/175833893): This causes the toolchain to not be selected, so
+    # disable for now.
+    #target_compatible_with = [
+    #    "@bazel_tools//platforms:android",
+    #],
+    toolchain = "@//prebuilts/sdk:android_sdk",
+    toolchain_type = "@rules_android//toolchains/android_sdk:toolchain_type",
+    visibility = ["//visibility:public"],
+)
+
+android_toolchain(
+    name = "android_default",
+    aapt2 = "//prebuilts/sdk:tools/linux/bin/aapt2",
+    adb = ":fail",
+    android_kit = ":fail",
+    apk_to_bundle_tool = ":fail",
+    bundletool = ":fail",
+    data_binding_annotation_processor = ":compiler_annotation_processor",
+    jacocorunner = ":fail",
+    java_stub = ":fail",
+    jdeps_tool = ":fail",
+    testsupport = ":fail",
+)
+
+toolchain(
+    name = "android_default_toolchain",
+    toolchain = ":android_default",
+    toolchain_type = "@rules_android//toolchains/android:toolchain_type",
+)
+
+# The native version of android_binary specifies a different toolchain_type
+# from the Starlark rules, so it needs a separate toolchain to point
+# android_binary to the android_sdk.
+toolchain(
+    name = "android_sdk_tools_for_native_android_binary",
+    exec_compatible_with = [
+        "@bazel_tools//platforms:x86_64",
+        "@bazel_tools//platforms:linux",
+    ],
+    target_compatible_with = [
+        "@bazel_tools//platforms:android",
+    ],
+    toolchain = ":android_sdk",
+    toolchain_type = "@bazel_tools//tools/android:sdk_toolchain_type",
+    visibility = ["//visibility:public"],
+)
+
+# TODO: all_android_tools comes from Android remote tools which should be
+# put into AOSP instead of downloaded.
+java_plugin(
+    name = "compiler_annotation_processor",
+    generates_api = True,
+    processor_class = "android.databinding.annotationprocessor.ProcessDataBinding",
+    visibility = ["//visibility:public"],
+    deps = [
+        "@bazel_tools//src/tools/android/java/com/google/devtools/build/android:all_android_tools",
+    ],
+)
+
+# Stubs for tools dependencies that are currently unused.
+genrule(
+    name = "gen_fail",
+    outs = ["fail.sh"],
+    cmd = "echo 'exit 1' > $@",
+    executable = 1,
+)
+
+sh_binary(
+    name = "fail",
+    srcs = [":fail.sh"],
+)