Skip to content

Commit 20adecb

Browse files
danysantiagoDagger Team
authored andcommitted
Update Hilt's Gradle Plugin detection of AGP
Use both the base id 'com.android.base' and the base type 'com.android.build.gradle.api.AndroidBasePlugin' as to maximize compatibility on cases when one works and the other doesn't. Fixes #4734 RELNOTES=Updated Hilt's Gradle Plugin detection of AGP to maximize compatibility and avoid the confusing 'The Hilt Android Gradle plugin can only be applied to an Android project.' error when the plugin is on an Android project. PiperOrigin-RevId: 797326681
1 parent ec7f76f commit 20adecb

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

  • java/dagger/hilt/android/plugin/main/src/main/kotlin/dagger/hilt/android/plugin

java/dagger/hilt/android/plugin/main/src/main/kotlin/dagger/hilt/android/plugin/HiltGradlePlugin.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.android.build.gradle.AppExtension
3030
import com.android.build.gradle.BaseExtension
3131
import com.android.build.gradle.LibraryExtension
3232
import com.android.build.gradle.TestExtension
33+
import com.android.build.gradle.api.AndroidBasePlugin
3334
import com.android.build.gradle.tasks.JdkImageInput
3435
import dagger.hilt.android.plugin.task.AggregateDepsTask
3536
import dagger.hilt.android.plugin.transform.AggregatedPackagesTransform
@@ -45,6 +46,7 @@ import dagger.hilt.android.plugin.util.getKspConfigName
4546
import dagger.hilt.android.plugin.util.isKspTask
4647
import dagger.hilt.android.plugin.util.onAllVariants
4748
import dagger.hilt.processor.internal.optionvalues.GradleProjectType
49+
import java.util.concurrent.atomic.AtomicBoolean
4850
import javax.inject.Inject
4951
import org.gradle.api.JavaVersion
5052
import org.gradle.api.Plugin
@@ -69,14 +71,21 @@ import org.objectweb.asm.Opcodes
6971
*/
7072
class HiltGradlePlugin @Inject constructor(private val providers: ProviderFactory) :
7173
Plugin<Project> {
74+
7275
override fun apply(project: Project) {
73-
var configured = false
76+
val configured = AtomicBoolean(false)
7477
project.plugins.withId("com.android.base") {
75-
configured = true
76-
configureHilt(project)
78+
if (configured.compareAndSet(false, true)) {
79+
configureHilt(project)
80+
}
81+
}
82+
project.plugins.withType(AndroidBasePlugin::class.java) {
83+
if (configured.compareAndSet(false, true)) {
84+
configureHilt(project)
85+
}
7786
}
7887
project.afterEvaluate {
79-
check(configured) {
88+
check(configured.get()) {
8089
// Check if configuration was applied, if not inform the developer they have applied the
8190
// plugin to a non-android project.
8291
"The Hilt Android Gradle plugin can only be applied to an Android project."

0 commit comments

Comments
 (0)