Android studio Bumblebee build.gradle根项目无法添加类路径依赖项

Loï*_*tin 8 groovy android kotlin android-studio dagger-hilt

我正在尝试在我的新项目中实现 dagger-hilt,但我发现新的 Android studio 版本(Bumblebee 2021.1.1)存在一些差异:

buildscript {
    ext {
        compose_version = '1.0.5'
        hilt_version = '2.40.5'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
    id 'com.android.application' version '7.1.0' apply false
    id 'com.android.library' version '7.1.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false

}
dependencies {
    classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
}    

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

当我尝试实现 hilt 和带有类路径的依赖块时,它告诉我:

 Caused by: org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method classpath() for arguments [com.google.dagger:hilt-android-gradle-plugin:2.40.5] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
        }
Run Code Online (Sandbox Code Playgroud)

Loï*_*tin 24

通过将 depcies{} 块添加到 buildScript 块中来解决:

buildscript {
    ext {
        compose_version = '1.0.5'
        hilt_version = '2.40.5'
    }

    dependencies {
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
        classpath 'com.google.gms:google-services:4.3.10'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
    id 'com.android.application' version '7.1.0' apply false
    id 'com.android.library' version '7.1.0' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.10' apply false

}
task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)