Skip to content

Commit

Permalink
Filter out the configuring project from the artifact view in the Hilt…
Browse files Browse the repository at this point in the history
… 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: #2306
RELNOTES=Fix an issue where internal Kotlin classes where not accessible with `enableExperimentalClasspathAggregation` turned ON.
PiperOrigin-RevId: 356243936
  • Loading branch information
danysantiago authored and Dagger Team committed Feb 8, 2021
1 parent 606c89b commit 53ceb91
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -175,6 +176,15 @@ class HiltGradlePlugin : Plugin<Project> {
}
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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}

android {
Expand All @@ -13,6 +14,8 @@ android {
targetSdkVersion 30
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
Expand All @@ -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
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ class DeepAndroidLib @Inject constructor() {
fun getInstance(context: Context) =
EntryPointAccessors.fromApplication(context, LibEntryPoint::class.java)
.getDeepAndroidInstance()

internal fun internalFunction() = Any()
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 53ceb91

Please sign in to comment.