resources refactor

Test: b build //external/emma:emmma

Change-Id: Ic57436c6f8e35f78f2a664f739e9d65117ae15d8
diff --git a/rules/java/java_resources.bzl b/rules/java/java_resources.bzl
new file mode 100644
index 0000000..464b1d5
--- /dev/null
+++ b/rules/java/java_resources.bzl
@@ -0,0 +1,60 @@
+"""
+Copyright (C) 2023 The Android Open Source Project
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+"""
+
+load("@bazel_skylib//lib:paths.bzl", "paths")
+
+def _java_resources_impl(ctx):
+    java_runtime = ctx.attr._runtime[java_common.JavaRuntimeInfo]
+
+    output_file = ctx.actions.declare_file(ctx.attr.name + "_java_resources.jar")
+
+    ctx.actions.run_shell(
+        outputs = [output_file],
+        inputs = ctx.files.resources,
+        tools = java_runtime.files,
+        command = "{} cvf {} -C {} .".format(
+            paths.join(java_runtime.java_home, "bin", "jar"),
+            output_file.path,
+            ctx.attr.resource_strip_prefix,
+        ),
+    )
+
+    return [DefaultInfo(files = depset([output_file]))]
+
+java_resources = rule(
+    doc = """
+    Package srcs into a jar, with the option of stripping a path prefix
+    """,
+    implementation = _java_resources_impl,
+    attrs = {
+        "resources": attr.label_list(allow_files = True),
+        "resource_strip_prefix": attr.string(
+            doc = """The path prefix to strip from resources.
+                   If specified, this path prefix is stripped from every fil
+                   in the resources attribute. It is an error for a resource
+                   file not to be under this directory. If not specified
+                   (the default), the path of resource file is determined
+                   according to the same logic as the Java package of source
+                   files. For example, a source file at stuff/java/foo/bar/a.txt
+                    will be located at foo/bar/a.txt.""",
+        ),
+        "_runtime": attr.label(
+            default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
+            cfg = "exec",
+            providers = [java_common.JavaRuntimeInfo],
+        ),
+    },
+)
diff --git a/rules/java/library.bzl b/rules/java/library.bzl
index 9083008..bd3d1d0 100644
--- a/rules/java/library.bzl
+++ b/rules/java/library.bzl
@@ -19,6 +19,7 @@
     _java_library = "java_library",
 )
 load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition_attrs")
+load(":import.bzl", "java_import")
 
 # TODO(b/277801336): document these attributes.
 def java_library(
@@ -32,6 +33,7 @@
         tags = [],
         target_compatible_with = [],
         visibility = None,
+        additional_resources = None,
         **kwargs):
     """ java_library macro wrapper that handles custom attrs needed in AOSP
 
@@ -48,6 +50,12 @@
         # TODO (b/227504307) temporarily disable errorprone until environment variable is handled
         opts = opts + ["-XepDisableAllChecks"]
 
+    if additional_resources != None:
+        java_import(
+            name = name + "__additional_resources",
+            jars = additional_resources,
+        )
+
     _java_library(
         name = lib_name,
         srcs = srcs,
diff --git a/rules/kotlin/kt_jvm_library.bzl b/rules/kotlin/kt_jvm_library.bzl
index 91d0be0..99d3d28 100644
--- a/rules/kotlin/kt_jvm_library.bzl
+++ b/rules/kotlin/kt_jvm_library.bzl
@@ -14,55 +14,12 @@
 limitations under the License.
 """
 
-load("@bazel_skylib//lib:paths.bzl", "paths")
 load("@rules_kotlin//kotlin:compiler_opt.bzl", "kt_compiler_opt")
 load("@rules_kotlin//kotlin:jvm_library.bzl", _kt_jvm_library = "kt_jvm_library")
 load("//build/bazel/rules/java:import.bzl", "java_import")
+load("//build/bazel/rules/java:java_resources.bzl", "java_resources")
 load("//build/bazel/rules/java:sdk_transition.bzl", "sdk_transition_attrs")
 
-def _kotlin_resources_impl(ctx):
-    java_runtime = ctx.attr._runtime[java_common.JavaRuntimeInfo]
-
-    output_file = ctx.actions.declare_file(ctx.attr.name + "_kt_resources.jar")
-
-    ctx.actions.run_shell(
-        outputs = [output_file],
-        inputs = ctx.files.srcs,
-        tools = java_runtime.files,
-        command = "{} cvf {} -C {} .".format(
-            paths.join(java_runtime.java_home, "bin", "jar"),
-            output_file.path,
-            ctx.attr.resource_strip_prefix,
-        ),
-    )
-
-    return [DefaultInfo(files = depset([output_file]))]
-
-kotlin_resources = rule(
-    doc = """
-    Package srcs into a jar, with the option of stripping a path prefix
-    """,
-    implementation = _kotlin_resources_impl,
-    attrs = {
-        "srcs": attr.label_list(allow_files = True),
-        "resource_strip_prefix": attr.string(
-            doc = """The path prefix to strip from resources.
-                   If specified, this path prefix is stripped from every fil
-                   in the resources attribute. It is an error for a resource
-                   file not to be under this directory. If not specified
-                   (the default), the path of resource file is determined
-                   according to the same logic as the Java package of source
-                   files. For example, a source file at stuff/java/foo/bar/a.txt
-                    will be located at foo/bar/a.txt.""",
-        ),
-        "_runtime": attr.label(
-            default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
-            cfg = "exec",
-            providers = [java_common.JavaRuntimeInfo],
-        ),
-    },
-)
-
 def make_kt_compiler_opt(
         name,
         kotlincflags = None):
@@ -101,16 +58,17 @@
             module by adding the "-XepDisableAllChecks" flag to javacopts
         """
     if resource_strip_prefix != None:
-        java_import_name = name + "resources"
-        kt_res_jar_name = name + "resources_jar"
+        java_import_name = name + "__kt_res"
+        kt_res_jar_name = name + "__kt_res_jar"
+
         java_import(
             name = java_import_name,
             jars = [":" + kt_res_jar_name],
         )
 
-        kotlin_resources(
+        java_resources(
             name = kt_res_jar_name,
-            srcs = resources,
+            resources = resources,
             resource_strip_prefix = resource_strip_prefix,
         )