From 53ceb9167774c2b8aff268d7b07325abf3e87916 Mon Sep 17 00:00:00 2001 From: Daniel Santiago Date: Mon, 8 Feb 2021 05:41:56 -0800 Subject: [PATCH] Filter out the configuring project from the artifact view in the Hilt Gradle plugin during classpath aggregation to avoid interfering with kotlinc's classpath configuration to the modude's classes. This avoids a situation where `internal` members would seem to not be accessible from local host unit tests in an Android library. Fixes: https://github.com/google/dagger/issues/2306 RELNOTES=Fix an issue where internal Kotlin classes where not accessible with `enableExperimentalClasspathAggregation` turned ON. PiperOrigin-RevId: 356243936 --- .../hilt/android/plugin/HiltGradlePlugin.kt | 10 ++++++ .../deep-android-lib/build.gradle | 17 ++++++++++ .../deep/InternalAccessEmulatorTest.kt | 32 +++++++++++++++++++ .../simpleKotlin/deep/DeepAndroidLib.kt | 2 ++ .../deep/InternalAccessLocalTest.kt | 29 +++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/androidTest/java/dagger/hilt/android/simpleKotlin/deep/InternalAccessEmulatorTest.kt create mode 100644 javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/test/java/dagger/hilt/android/simpleKotlin/deep/InternalAccessLocalTest.kt diff --git a/java/dagger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt b/java/dagger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt index c3a492905a9..e26edb5fde3 100644 --- a/java/dagger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt +++ b/java/dagger/hilt/android/plugin/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt @@ -34,6 +34,7 @@ import dagger.hilt.android.plugin.util.SimpleAGPVersion import java.io.File import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.artifacts.component.ProjectComponentIdentifier import org.gradle.api.attributes.Attribute /** @@ -175,6 +176,15 @@ class HiltGradlePlugin : Plugin { } val artifactView = runtimeConfiguration.incoming.artifactView { view -> view.attributes.attribute(ARTIFACT_TYPE_ATTRIBUTE, DAGGER_ARTIFACT_TYPE_VALUE) + view.componentFilter { identifier -> + // Filter out the project's classes from the aggregated view since this can cause + // issues with Kotlin internal members visibility. b/178230629 + if (identifier is ProjectComponentIdentifier) { + identifier.projectName != project.name + } else { + true + } + } } // CompileOnly config names don't follow the usual convention: diff --git a/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/build.gradle b/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/build.gradle index 1774e8fb5e2..3a1923afff6 100644 --- a/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/build.gradle +++ b/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/build.gradle @@ -2,6 +2,7 @@ plugins { id 'com.android.library' id 'kotlin-android' id 'kotlin-kapt' + id 'dagger.hilt.android.plugin' } android { @@ -13,6 +14,8 @@ android { targetSdkVersion 30 versionCode 1 versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -21,9 +24,23 @@ android { kotlinOptions { jvmTarget = '1.8' } + lintOptions { + checkReleaseBuilds = false + } } dependencies { implementation 'com.google.dagger:hilt-android:LOCAL-SNAPSHOT' kapt 'com.google.dagger:hilt-compiler:LOCAL-SNAPSHOT' + + testImplementation 'androidx.test.ext:junit:1.1.2' + testImplementation 'com.google.truth:truth:1.0.1' + + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test:runner:1.3.0' + androidTestImplementation 'com.google.truth:truth:1.0.1' +} + +hilt { + enableExperimentalClasspathAggregation = true } \ No newline at end of file diff --git a/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/androidTest/java/dagger/hilt/android/simpleKotlin/deep/InternalAccessEmulatorTest.kt b/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/androidTest/java/dagger/hilt/android/simpleKotlin/deep/InternalAccessEmulatorTest.kt new file mode 100644 index 00000000000..4ea4c0942e7 --- /dev/null +++ b/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/androidTest/java/dagger/hilt/android/simpleKotlin/deep/InternalAccessEmulatorTest.kt @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2021 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. + */ +package dagger.hilt.android.simpleKotlin.deep + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.google.common.truth.Truth.assertThat +import org.junit.Test +import org.junit.runner.RunWith + +/** + * Verifies internal Kotlin classes are accessible with classpath aggregation in Android library. + */ +@RunWith(AndroidJUnit4::class) +class InternalAccessEmulatorTest { + @Test + fun verifyInternalMembersAreAccessible() { + assertThat(DeepAndroidLib.internalFunction()).isNotNull() + } +} diff --git a/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/main/java/dagger/hilt/android/simpleKotlin/deep/DeepAndroidLib.kt b/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/main/java/dagger/hilt/android/simpleKotlin/deep/DeepAndroidLib.kt index 58d5e41f417..a2fcb73b154 100644 --- a/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/main/java/dagger/hilt/android/simpleKotlin/deep/DeepAndroidLib.kt +++ b/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/main/java/dagger/hilt/android/simpleKotlin/deep/DeepAndroidLib.kt @@ -34,5 +34,7 @@ class DeepAndroidLib @Inject constructor() { fun getInstance(context: Context) = EntryPointAccessors.fromApplication(context, LibEntryPoint::class.java) .getDeepAndroidInstance() + + internal fun internalFunction() = Any() } } diff --git a/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/test/java/dagger/hilt/android/simpleKotlin/deep/InternalAccessLocalTest.kt b/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/test/java/dagger/hilt/android/simpleKotlin/deep/InternalAccessLocalTest.kt new file mode 100644 index 00000000000..508e7d502f6 --- /dev/null +++ b/javatests/artifacts/hilt-android/simpleKotlin/deep-android-lib/src/test/java/dagger/hilt/android/simpleKotlin/deep/InternalAccessLocalTest.kt @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2021 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. + */ +package dagger.hilt.android.simpleKotlin.deep + +import com.google.common.truth.Truth.assertThat +import org.junit.Test + +/** + * Verifies internal Kotlin classes are accessible with classpath aggregation in Android library. + */ +class InternalAccessLocalTest { + @Test + fun verifyInternalMembersAreAccessible() { + assertThat(DeepAndroidLib.internalFunction()).isNotNull() + } +}