Reconcile Android.bp with AOSP

The versions of I5b1f7fdac0270c86807b4838cefb1e69f5e0ddff that went into
tm-qpr-dev and master were different.  The tm-qpr-dev one has propagated
to AOSP main, but everything else downstream is different.  Cherry-pick
the differences to reconcile them.

Bug: 290933559
Test: builds
Change-Id: I7556076a278bf3cf3626eb6093c1ee4fc7c04498
Merged-In: I7556076a278bf3cf3626eb6093c1ee4fc7c04498
(cherry picked from commit efd3fceb6fe6c83b791b038f032addde8a722b87)
diff --git a/Android.bp b/Android.bp
index db94bab..a0a2c3b 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,5 +1,6 @@
 package {
     default_applicable_licenses: ["external_kotlinx.coroutines_license"],
+    default_visibility: ["//visibility:private"],
 }
 
 // Added automatically by a large-scale-change that took the approach of
@@ -35,11 +36,11 @@
     ],
 }
 
-java_library {
-    name: "kotlinx_coroutines",
-    host_supported: true,
-    sdk_version: "current",
-    min_sdk_version: "28",
+// Upstream compiles this lib against the JVM bootclasspath; compiling against the Android
+// bootclasspath will fail. Work around this by defining this as a java_library_host, and use
+// java_host_for_device to expose it to Android targets.
+java_library_host {
+    name: "kotlinx_coroutines-host",
     srcs: ["kotlinx-coroutines-core/jvm/src/**/*.kt"],
     common_srcs: [
         "kotlinx-coroutines-core/common/src/**/*.kt",
@@ -53,7 +54,10 @@
     static_libs: [
         "kotlinx_atomicfu",
     ],
-    libs: ["kotlinx-coroutines-android-annotation-stubs"],
+    libs: [
+        "annotations", // for android.annotation.SuppressLint
+        "kotlinx-coroutines-android-annotation-stubs",
+    ],
     kotlincflags: [
         "-Xmulti-platform",
         "-opt-in=kotlin.RequiresOptIn",
@@ -67,13 +71,37 @@
     ],
     apex_available: [
         "//apex_available:platform",
-        "//apex_available:anyapex"
+        "//apex_available:anyapex",
     ],
+}
+
+// Expose the host library to Android targets. This is generally an unsafe operation; in using
+// this, we are asserting that any host-only code will never be evaluated at runtime on Android.
+// If we're wrong, we will see runtime exceptions.
+java_host_for_device {
+    name: "kotlinx_coroutines-device",
+    libs: ["kotlinx_coroutines-host"],
+}
+
+// Combine host and Android libs back into a single target.
+java_library {
+    name: "kotlinx_coroutines",
+    host_supported: true,
+    sdk_version: "core_current",
+    min_sdk_version: "28",
     target: {
         host: {
-            libs: ["annotations"], // for android.annotation.SuppressLint
+            static_libs: ["kotlinx_coroutines-host"],
+        },
+        android: {
+            static_libs: ["kotlinx_coroutines-device"],
         },
     },
+    apex_available: [
+        "//apex_available:platform",
+        "//apex_available:anyapex",
+    ],
+    visibility: ["//visibility:public"],
 }
 
 java_library {
@@ -94,8 +122,9 @@
     ],
     apex_available: [
         "//apex_available:platform",
-        "//apex_available:anyapex"
+        "//apex_available:anyapex",
     ],
+    visibility: ["//visibility:public"],
 }
 
 java_library {
@@ -110,13 +139,12 @@
         "-opt-in=kotlinx.coroutines.InternalCoroutinesApi",
         "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
     ],
-    libs: [
-        "kotlinx_coroutines",
-    ],
+    libs: ["kotlinx_coroutines"],
     apex_available: [
         "//apex_available:platform",
-        "//apex_available:anyapex"
+        "//apex_available:anyapex",
     ],
+    visibility: ["//visibility:public"],
 }
 
 // Compile stub implementations of annotations used by kotlinx-coroutines but not present in the
@@ -134,25 +162,27 @@
 java_library {
     name: "kotlinx-coroutines-core",
     host_supported: true,
-    sdk_version: "current",
+    sdk_version: "core_current",
     min_sdk_version: "28",
     static_libs: ["kotlinx_coroutines"],
     apex_available: [
         "//apex_available:platform",
-        "//apex_available:anyapex"
+        "//apex_available:anyapex",
     ],
+    visibility: ["//visibility:public"],
 }
 
 java_library {
     name: "kotlinx-coroutines-core-jvm",
     host_supported: true,
-    sdk_version: "current",
+    sdk_version: "core_current",
     min_sdk_version: "28",
     static_libs: ["kotlinx_coroutines"],
     apex_available: [
         "//apex_available:platform",
-        "//apex_available:anyapex"
+        "//apex_available:anyapex",
     ],
+    visibility: ["//visibility:public"],
 }
 
 java_library {
@@ -165,8 +195,9 @@
     ],
     apex_available: [
         "//apex_available:platform",
-        "//apex_available:anyapex"
+        "//apex_available:anyapex",
     ],
+    visibility: ["//visibility:public"],
 }
 
 filegroup {