From e6b9571167f427e0a753a9b58228ca2a13a380eb Mon Sep 17 00:00:00 2001 From: Brad Corso Date: Sat, 18 Dec 2021 10:22:20 -0800 Subject: [PATCH] Add kotlin-stdlib to EntryPointAccessors dependencies. Fixes #3119 This CL is a follow-up to CL/417294644. It adds a macro for kt_android_library that adds the kotlin-stdlib dependency if there is a kotlin source and also fixes issues with the jar file locations. RELNOTES=Fixes #3119: Added kotlin-stdlib to pom dependencies to avoid breaking java-only projects. PiperOrigin-RevId: 417157672 --- java/dagger/hilt/android/BUILD | 22 ++----- java/dagger/hilt/android/testing/BUILD | 1 + .../dagger/hilt/android/simple/BuildTest.java | 7 +- tools/bazel_compat.bzl | 65 +++++++++++++++++++ 4 files changed, 71 insertions(+), 24 deletions(-) create mode 100644 tools/bazel_compat.bzl diff --git a/java/dagger/hilt/android/BUILD b/java/dagger/hilt/android/BUILD index 67059a0d619..91586410432 100644 --- a/java/dagger/hilt/android/BUILD +++ b/java/dagger/hilt/android/BUILD @@ -16,7 +16,7 @@ # A library based on Hilt that provides standard components and automated injection for Android. load("//:build_defs.bzl", "POM_VERSION") load("//tools:maven.bzl", "gen_maven_artifact") -load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library") +load("//tools:bazel_compat.bzl", "compat_kt_android_library") package(default_visibility = ["//:src"]) @@ -207,6 +207,7 @@ gen_maven_artifact( "com.google.dagger:dagger", "com.google.dagger:hilt-core", "javax.inject:javax.inject", + "org.jetbrains.kotlin:kotlin-stdlib", ], artifact_target_maven_deps_banned = [ "com.google.guava:guava", @@ -232,8 +233,8 @@ gen_maven_artifact( ], ) -kt_android_library( - name = "entry_point_accessors_internal", +compat_kt_android_library( + name = "entry_point_accessors", srcs = ["EntryPointAccessors.kt"], deps = [ "//java/dagger/hilt:entry_point", @@ -246,21 +247,6 @@ kt_android_library( ], ) -alias( - name = "entry_point_accessors", - actual = ":entry_point_accessors_internal_kt", -) - -alias( - name = "libentry_point_accessors.jar", - actual = ":entry_point_accessors_internal_kt.jar", -) - -alias( - name = "libentry_point_accessors-src.jar", - actual = ":entry_point_accessors_internal_kt-sources.jar", -) - filegroup( name = "srcs_filegroup", srcs = glob(["*"]), diff --git a/java/dagger/hilt/android/testing/BUILD b/java/dagger/hilt/android/testing/BUILD index fcdf28f4e70..85dea9df431 100644 --- a/java/dagger/hilt/android/testing/BUILD +++ b/java/dagger/hilt/android/testing/BUILD @@ -231,6 +231,7 @@ gen_maven_artifact( "com.google.dagger:hilt-android", "javax.inject:javax.inject", "junit:junit", + "org.jetbrains.kotlin:kotlin-stdlib", ], artifact_target_maven_deps_banned = [ "com.google.guava:guava", diff --git a/javatests/artifacts/hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java b/javatests/artifacts/hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java index 1f2ae9fc317..9411852d29a 100644 --- a/javatests/artifacts/hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java +++ b/javatests/artifacts/hilt-android/simple/app-java-only/src/test/java/dagger/hilt/android/simple/BuildTest.java @@ -17,7 +17,6 @@ package dagger.hilt.android.simple; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertThrows; import android.os.Build; import androidx.test.core.app.ApplicationProvider; @@ -39,10 +38,6 @@ public void useAppContext() { .isInstanceOf(SimpleApplication.class); SimpleApplication app = (SimpleApplication) ApplicationProvider.getApplicationContext(); assertThat(app.str).isNotNull(); - - // TODO(bcorso): This should no longer throw after we fix the issue. - assertThrows( - NoClassDefFoundError.class, - () -> assertThat(app.str).isEqualTo(app.getStringEntryPoint())); + assertThat(app.str).isEqualTo(app.getStringEntryPoint()); } } diff --git a/tools/bazel_compat.bzl b/tools/bazel_compat.bzl new file mode 100644 index 00000000000..e9354da65bc --- /dev/null +++ b/tools/bazel_compat.bzl @@ -0,0 +1,65 @@ +# Copyright (C) 202 The Dagger Authors. +# +# 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. + +"""Macros for building with Bazel. +""" + +load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library") + +def compat_kt_android_library(name, **kwargs): + bazel_kt_android_library(name, kwargs) + +def bazel_kt_android_library(name, kwargs): + """A macro that wraps Bazel's kt_android_library. + + This macro wraps Bazel's kt_android_library to output the jars files + in the expected locations (b/203519416). It also adds a dependency on + kotlin_stdlib if there are kotlin sources. + + Args: + name: the name of the library. + kwargs: Additional arguments of the library. + """ + + # If there are any kotlin sources, add the kotlin_stdlib, otherwise + # java-only projects may be missing a required runtime dependency on it. + if any([src.endswith(".kt") for src in kwargs.get("srcs", [])]): + # Add the kotlin_stdlib, otherwise it will be missing from java-only projects. + # We use deps rather than exports because exports isn't picked up by the pom file. + # See https://github.com/google/dagger/issues/3119 + required_deps = ["@maven//:org_jetbrains_kotlin_kotlin_stdlib"] + kwargs["deps"] = kwargs.get("deps", []) + required_deps + + # TODO(b/203519416): Bazel's kt_android_library outputs its jars under a target + # suffixed with "_kt". Thus, we have to do a bit of name aliasing to ensure that + # the jars exist at the expected targets. + kt_android_library( + name = "{}_internal".format(name), + **kwargs + ) + + native.alias( + name = name, + actual = ":{}_internal_kt".format(name), + ) + + native.alias( + name = "lib{}.jar".format(name), + actual = ":{}_internal_kt.jar".format(name), + ) + + native.alias( + name = "lib{}-src.jar".format(name), + actual = ":{}_internal_kt-sources.jar".format(name), + )