Make android_sdk_repository create one android_sdk per api level in the SDK.

api_level is no longer required. It now sets the default android_sdk that is used if the --android_sdk flag is not passed. If it is not set, the highest api_level will be used as the default.

The new behavior is demonstrated by the following example:

    $ ls $ANDROID_HOME/platforms
    android-21  android-22  android-23
    $ cat WORKSPACE
    android_sdk_repository(
        name = "foo",
    )
    $ bazel build //java/my/app                              # uses api level 23
    $ bazel build --android_sdk=@foo//:sdk-22 //java/my/app  # uses api level 22
    $ cat > WORKSPACE <<EOF
    android_sdk_repository(
        name = "foo",
        api_level = 21,
    )
    EOF
    $ bazel build //java/my/app                              # uses api level 21
    $ bazel build --android_sdk=@foo//:sdk-23 //java/my/app  # uses api level 23

See https://github.com/bazelbuild/bazel/issues/2284 for the master plan for android_sdk_repository.

RELNOTES: android_sdk_repository no longer requires api_level. If one is not specified, the highest android platform installed will be used. Furthermore, android_sdk's are created for all android platforms installed and can be specified with the --android_sdk flag.

--
PiperOrigin-RevId: 144258881
MOS_MIGRATED_REVID=144258881
diff --git a/tools/android/android_sdk_repository_template.bzl b/tools/android/android_sdk_repository_template.bzl
index 497b1c8..ec6de6d 100644
--- a/tools/android/android_sdk_repository_template.bzl
+++ b/tools/android/android_sdk_repository_template.bzl
@@ -17,7 +17,8 @@
     name,
     build_tools_version,
     build_tools_directory,
-    api_level):
+    api_levels,
+    default_api_level):
   """Generate the contents of the android_sdk_repository.
 
   Args:
@@ -25,7 +26,10 @@
     build_tools_version: string, the version of Android's build tools to use.
     build_tools_directory: string, the directory name of the build tools in
         sdk's build-tools directory.
-    api_level: int, the API level from which to get android.jar et al.
+    api_levels: list of ints, the API levels from which to get android.jar
+        et al. and create android_sdk rules.
+    default_api_level: int, the API level to alias the default sdk to if
+        --android_sdk is not specified on the command line.
   """
 
   # This filegroup is used to pass the contents of the SDK to the Android
@@ -49,34 +53,45 @@
       ], exclude_directories = 0),
   )
 
-  if api_level >= 23:
-    # Android 23 removed most of org.apache.http from android.jar and moved it
-    # to a separate jar.
-    native.java_import(
-        name = "org_apache_http_legacy",
-        jars = ["platforms/android-%d/optional/org.apache.http.legacy.jar" % api_level]
+  for api_level in api_levels:
+    if api_level >= 23:
+      # Android 23 removed most of org.apache.http from android.jar and moved it
+      # to a separate jar.
+      native.java_import(
+          name = "org_apache_http_legacy-%d" % api_level,
+          jars = ["platforms/android-%d/optional/org.apache.http.legacy.jar" % api_level]
+      )
+
+    native.android_sdk(
+        name = "sdk-%d" % api_level,
+        build_tools_version = build_tools_version,
+        proguard = ":proguard_binary",
+        aapt = ":aapt_binary",
+        dx = ":dx_binary",
+        main_dex_list_creator = ":main_dex_list_creator",
+        adb = "platform-tools/adb",
+        framework_aidl = "platforms/android-%d/framework.aidl" % api_level,
+        aidl = ":aidl_binary",
+        android_jar = "platforms/android-%d/android.jar" % api_level,
+        shrinked_android_jar = "platforms/android-%d/android.jar" % api_level,
+        annotations_jar = "tools/support/annotations.jar",
+        main_dex_classes = "build-tools/%s/mainDexClasses.rules" % build_tools_directory,
+        apkbuilder = "@bazel_tools//third_party/java/apkbuilder:embedded_apkbuilder",
+        apksigner = ":apksigner",
+        zipalign = ":zipalign_binary",
+        jack = ":fail",
+        jill = ":fail",
+        resource_extractor = "@bazel_tools//tools/android:resource_extractor",
     )
 
-  native.android_sdk(
+  native.alias(
+      name = "org_apache_http_legacy",
+      actual = ":org_apache_http_legacy-%d" % default_api_level,
+  )
+
+  native.alias(
       name = "sdk",
-      build_tools_version = build_tools_version,
-      proguard = ":proguard_binary",
-      aapt = ":aapt_binary",
-      dx = ":dx_binary",
-      main_dex_list_creator = ":main_dex_list_creator",
-      adb = "platform-tools/adb",
-      framework_aidl = "platforms/android-%d/framework.aidl" % api_level,
-      aidl = ":aidl_binary",
-      android_jar = "platforms/android-%d/android.jar" % api_level,
-      shrinked_android_jar = "platforms/android-%d/android.jar" % api_level,
-      annotations_jar = "tools/support/annotations.jar",
-      main_dex_classes = "build-tools/%s/mainDexClasses.rules" % build_tools_directory,
-      apkbuilder = "@bazel_tools//third_party/java/apkbuilder:embedded_apkbuilder",
-      apksigner = ":apksigner",
-      zipalign = ":zipalign_binary",
-      jack = ":fail",
-      jill = ":fail",
-      resource_extractor = "@bazel_tools//tools/android:resource_extractor",
+      actual = ":sdk-%d" % default_api_level,
   )
 
   native.java_import(