Implement transitive JNI library path resolution in android_local_test. PiperOrigin-RevId: 933938111 Change-Id: I3a2c71b428694845bcc8e4d47a6b5e012df2bdf3
diff --git a/rules/android_local_test/impl.bzl b/rules/android_local_test/impl.bzl index 5aaf8eb..fdf6923 100644 --- a/rules/android_local_test/impl.bzl +++ b/rules/android_local_test/impl.bzl
@@ -20,6 +20,10 @@ load("//rules:java.bzl", "java") load("//rules:min_sdk_version.bzl", "min_sdk_version") load( + "//rules:native_deps.bzl", + "merge_transitive_native_libs", +) +load( "//rules:processing_pipeline.bzl", "ProviderInfo", "processing_pipeline", @@ -469,7 +473,23 @@ ) return stub_file +def _get_jni_library_paths(ctx): + cc_info = merge_transitive_native_libs(ctx, ctx.attr.deps + ctx.attr.runtime_deps).link_params + jni_paths = [] + for linker_input in cc_info.linking_context.linker_inputs.to_list(): + for library in linker_input.libraries: + if library.dynamic_library: + path = library.dynamic_library.short_path + dir_path = path.rpartition("/")[0] + if dir_path: + jni_paths.append(dir_path) + return depset(jni_paths).to_list() + def _get_jvm_flags(ctx, main_class, robolectric_properties_path, additional_jvm_flags): + jni_paths = _get_jni_library_paths(ctx) + library_path_flag = "-Djava.library.path=java/jni/lib" + if jni_paths: + library_path_flag += ":" + ":".join(jni_paths) return [ "-ea", "-Dbazel.test_suite=" + main_class, @@ -479,6 +499,7 @@ "-Drobolectric.logging=stdout", "-Drobolectric.logging.enabled=true", "-Dorg.robolectric.packagesToNotAcquire=com.google.testing.junit.runner.util", + library_path_flag, ] + DEFAULT_JIT_FLAGS + DEFAULT_GC_FLAGS + DEFAULT_VERIFY_FLAGS + additional_jvm_flags + [ ctx.expand_make_variables( "jvm_flags",
diff --git a/test/rules/android_local_test/java/com/starlark_resources/BUILD b/test/rules/android_local_test/java/com/starlark_resources/BUILD index 6fd487e..be63334 100644 --- a/test/rules/android_local_test/java/com/starlark_resources/BUILD +++ b/test/rules/android_local_test/java/com/starlark_resources/BUILD
@@ -1,5 +1,7 @@ # Tests that run on head android_local_test rule to verify Starlark resource processing pipeline. +load("@rules_cc//cc:cc_library.bzl", "cc_library") +load("//mobile/build:build_defs.bzl", "android_jni_library") load( "//rules:rules.bzl", "android_library", @@ -190,3 +192,37 @@ "@rules_android_maven//:org_robolectric_robolectric", ], ) + +cc_library( + name = "jni_test_cc_lib", + testonly = True, + srcs = ["jni_test.cc"], + deps = [ + "@local_jdk//:bin/java:jni", + ], +) + +android_jni_library( + name = "jni_test_lib", + testonly = True, + binary_name = "libjni_test.so", + deps = [ + ":jni_test_cc_lib", + ], +) + +android_local_test( + name = "jni_aspect_regression_test", + srcs = [ + "JniAspectRegressionTest.java", + "JniTestLib.java", + ], + manifest = "AndroidManifest.xml", + test_class = "com.starlark_resources.JniAspectRegressionTest", + deps = [ + ":jni_test_lib", + "//java/com/google/thirdparty/robolectric", + "//third_party/java/robolectric", + "@rules_android_maven//:junit_junit", + ], +)
diff --git a/test/rules/android_local_test/java/com/starlark_resources/JniAspectRegressionTest.java b/test/rules/android_local_test/java/com/starlark_resources/JniAspectRegressionTest.java new file mode 100644 index 0000000..1f9b4cc --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/JniAspectRegressionTest.java
@@ -0,0 +1,31 @@ +/* + * Copyright 2026 The Bazel Authors. All rights reserved. + * + * 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. + */ + +package com.starlark_resources; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +@RunWith(RobolectricTestRunner.class) +public final class JniAspectRegressionTest { + @Test + public void testJniLoading() { + assertEquals(5, JniTestLib.add(2, 3)); + } +}
diff --git a/test/rules/android_local_test/java/com/starlark_resources/JniTestLib.java b/test/rules/android_local_test/java/com/starlark_resources/JniTestLib.java new file mode 100644 index 0000000..93c57aa --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/JniTestLib.java
@@ -0,0 +1,25 @@ +/* + * Copyright 2026 The Bazel Authors. All rights reserved. + * + * 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. + */ + +package com.starlark_resources; + +public final class JniTestLib { + static { + System.loadLibrary("jni_test"); + } + + public static native int add(int a, int b); +}
diff --git a/test/rules/android_local_test/java/com/starlark_resources/jni_test.cc b/test/rules/android_local_test/java/com/starlark_resources/jni_test.cc new file mode 100644 index 0000000..4d444c4 --- /dev/null +++ b/test/rules/android_local_test/java/com/starlark_resources/jni_test.cc
@@ -0,0 +1,22 @@ +/* + * Copyright 2026 The Bazel Authors. All rights reserved. + * + * 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. + */ + +#include <jni.h> + +extern "C" JNIEXPORT jint JNICALL Java_com_starlark_1resources_JniTestLib_add( + JNIEnv* env, jclass clazz, jint a, jint b) { + return a + b; +}