Merge #537 by keith: Return repo/extension metadata from rules

This allows the remote repo contents cache to cache the result of this
repo

Closes #537

COPYBARA_INTEGRATE_REVIEW=https://github.com/bazelbuild/rules_android/pull/537 from keith:ks/return-repo-extension-metadata-from-rules a514acb3f06a11cb8183f249ff516249227f54ae
PiperOrigin-RevId: 971999361
Change-Id: I443f368773beda9f3c8bc5c174de9c7ae3364b93
diff --git a/bzlmod_extensions/apksig.bzl b/bzlmod_extensions/apksig.bzl
index 39babbd..c73f0ec 100644
--- a/bzlmod_extensions/apksig.bzl
+++ b/bzlmod_extensions/apksig.bzl
@@ -20,7 +20,7 @@
 
 APKSIG_COMMIT = "24e3075e68ebe17c0b529bb24bfda819db5e2f3b"
 
-def apksig(_ctx = None):
+def apksig(mctx = None):
     # NOTE(b/317109605): Cannot depend on a stable sha256 hash for googlesource repositories.
     http_archive(
         name = "apksig",
@@ -33,6 +33,12 @@
         build_file = Label("//bzlmod_extensions:apksig.BUILD"),
     )
 
+    # NOTE: 'watch' is the best approximation for the extension_metadata
+    # function accepting the reproducible paramater
+    if mctx and hasattr(mctx, "watch"):
+        return mctx.extension_metadata(reproducible = True)
+    return None
+
 apksig_extension = module_extension(
     implementation = apksig,
 )
diff --git a/bzlmod_extensions/com_android_dex.bzl b/bzlmod_extensions/com_android_dex.bzl
index 0c052db..8ddb0af 100644
--- a/bzlmod_extensions/com_android_dex.bzl
+++ b/bzlmod_extensions/com_android_dex.bzl
@@ -18,7 +18,7 @@
 
 visibility(PROJECT_VISIBILITY)
 
-def com_android_dex(_ctx = None):
+def com_android_dex(mctx = None):
     # NOTE(b/317109605): Cannot depend on a stable sha256 hash for googlesource repositories.
     http_archive(
         name = "com_android_dex",
@@ -31,6 +31,12 @@
         sha256 = "86b4848c038bf687fadc812239cb01fb8d1d15cef3125b480a0448360992b95d",
     )
 
+    # NOTE: 'watch' is the best approximation for the extension_metadata
+    # function accepting the reproducible paramater
+    if mctx and hasattr(mctx, "watch"):
+        return mctx.extension_metadata(reproducible = True)
+    return None
+
 com_android_dex_extension = module_extension(
     implementation = com_android_dex,
 )
diff --git a/rules/android_sdk_repository/rule.bzl b/rules/android_sdk_repository/rule.bzl
index 34de2b5..04999c9 100644
--- a/rules/android_sdk_repository/rule.bzl
+++ b/rules/android_sdk_repository/rule.bzl
@@ -107,6 +107,9 @@
     return system_images
 
 def _android_sdk_repository_impl(repo_ctx):
+    repo_metadata = None
+    if hasattr(repo_ctx, "repo_metadata"):
+        repo_metadata = repo_ctx.repo_metadata(reproducible = True)
     # Determine the SDK path to use, either from the attribute or the environment.
     android_sdk_path = repo_ctx.attr.path
     if not android_sdk_path:
@@ -114,7 +117,7 @@
     if not android_sdk_path:
         # Create an empty repository that allows non-Android code to build.
         repo_ctx.template("BUILD.bazel", _EMPTY_SDK_REPO_TEMPLATE)
-        return None
+        return repo_metadata
     if android_sdk_path.startswith("$WORKSPACE_ROOT"):
         android_sdk_path = str(repo_ctx.workspace_root) + android_sdk_path.removeprefix("$WORKSPACE_ROOT")
 
@@ -184,7 +187,7 @@
     )
 
     # repo is reproducible
-    return None
+    return repo_metadata
 
 _android_sdk_repository = repository_rule(
     implementation = _android_sdk_repository_impl,
@@ -245,6 +248,12 @@
         **kwargs
     )
 
+    # NOTE: 'watch' is the best approximation for the extension_metadata
+    # function accepting the reproducible paramater
+    if hasattr(module_ctx, "watch"):
+        return module_ctx.extension_metadata(reproducible = True)
+    return None
+
 android_sdk_repository_extension = module_extension(
     implementation = _android_sdk_repository_extension_impl,
     tag_classes = {