Add the SDK annotations jar to the classpath of AOSP-generated AIDL sources.

The AOSP AIDL compiler stamps `@android.annotation.Hide` onto the Java sources it generates, and that annotation is not part of `android.jar`. Add an `aidl_annotations_lib` dependency to `android_library` and include it in `idl_deps` when `idl_uses_aosp_compiler` is set, so that the generated sources compile.

PiperOrigin-RevId: 984143625
Change-Id: I8def45d999afd6267c703cd1bf67d5d8a2e2fbf7
diff --git a/rules/BUILD b/rules/BUILD
index 4c45293..1e2f331 100644
--- a/rules/BUILD
+++ b/rules/BUILD
@@ -180,3 +180,10 @@
     name = "aidl_lib",
     visibility = ["//visibility:public"],
 )
+
+# Empty AIDL annotations library for OSS. The SDK annotations jar containing
+# @android.annotation.Hide is not available outside google3.
+java_library(
+    name = "aidl_annotations_lib",
+    visibility = ["//visibility:public"],
+)
diff --git a/rules/android_library/attrs.bzl b/rules/android_library/attrs.bzl
index 61b368b..a713ff1 100644
--- a/rules/android_library/attrs.bzl
+++ b/rules/android_library/attrs.bzl
@@ -243,6 +243,11 @@
         _aidl_lib = attr.label(
             default = Label("//rules:aidl_lib"),
         ),
+        # Annotations stamped onto the sources generated by the AOSP AIDL
+        # compiler, e.g. @android.annotation.Hide.
+        _aidl_annotations_lib = attr.label(
+            default = Label("//rules:aidl_annotations_lib"),
+        ),
     ),
     _attrs.compilation_attributes(),
     _attrs.DATA_CONTEXT,
diff --git a/rules/android_library/impl.bzl b/rules/android_library/impl.bzl
index c410e83..cdd9778 100644
--- a/rules/android_library/impl.bzl
+++ b/rules/android_library/impl.bzl
@@ -221,6 +221,7 @@
             exports = utils.collect_providers(AndroidIdlInfo, ctx.attr.exports),
             aidl = get_android_sdk(ctx).aidl,
             aidl_lib = ctx.attr._aidl_lib,
+            aidl_annotations_lib = ctx.attr._aidl_annotations_lib,
             aidl_framework = get_android_sdk(ctx).framework_aidl,
             uses_aosp_compiler = ctx.attr.idl_uses_aosp_compiler,
             idlopts = ctx.attr.idlopts,
diff --git a/rules/idl.bzl b/rules/idl.bzl
index e22b2dc..35e2b7e 100644
--- a/rules/idl.bzl
+++ b/rules/idl.bzl
@@ -136,6 +136,30 @@
         idl_imports,
     )
 
+def _idl_deps(
+        idl_java_srcs,
+        aidl_lib,
+        aidl_annotations_lib,
+        uses_aosp_compiler):
+    """Determines the libraries the generated Java sources need to compile.
+
+    Args:
+      idl_java_srcs: sequence of Files. The Java sources generated from IDL.
+      aidl_lib: Target or None. The google3 AIDL runtime library.
+      aidl_annotations_lib: Target or None. The SDK annotations library.
+      uses_aosp_compiler: boolean. Whether the AOSP AIDL compiler was used.
+
+    Returns:
+      A list of Targets to add to the Java compilation.
+    """
+    if not idl_java_srcs:
+        return []
+    if uses_aosp_compiler:
+        # The AOSP compiler stamps the sources it generates with SDK annotations
+        # such as @android.annotation.Hide, which are not part of android.jar.
+        return [aidl_annotations_lib] if aidl_annotations_lib else []
+    return [aidl_lib] if aidl_lib else []
+
 def _process(
         ctx,
         idl_srcs = [],
@@ -146,6 +170,7 @@
         exports = [],
         aidl = None,
         aidl_lib = None,
+        aidl_annotations_lib = None,
         aidl_framework = None,
         uses_aosp_compiler = False,
         idlopts = []):
@@ -196,6 +221,10 @@
       aidl_lib: Target. A target pointing to the aidl_lib library required
         during Java compilation when Java code is generated from idl sources using the google aidl
         compiler. Optional.
+      aidl_annotations_lib: Target. A target pointing to the SDK annotations
+        library (@android.annotation.Hide and friends) required during Java
+        compilation when Java code is generated from idl sources using the AOSP
+        aidl compiler. Optional.
       aidl_framework: Target. A target pointing to the aidl framework. Optional,
         unless idl_srcs are supplied.
       uses_aosp_compiler: boolean. If True, the upstream AOSP AIDL compiler is
@@ -259,7 +288,12 @@
         idl_srcs = idl_srcs,
         idl_import_root = idl_import_root,
         idl_java_srcs = idl_java_srcs,
-        idl_deps = [aidl_lib] if (idl_java_srcs and aidl_lib and not uses_aosp_compiler) else [],
+        idl_deps = _idl_deps(
+            idl_java_srcs,
+            aidl_lib,
+            aidl_annotations_lib,
+            uses_aosp_compiler,
+        ),
         providers = [
             AndroidIdlInfo(
                 transitive_idl_import_roots = depset(