1.功能位置迁移

旧版 Project build.gradlebuildscriptallprojects的移动至setting.gradle并改名为pluginManagementdependencyResolutionManagement

pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "Hilt"
include ':app'

如果修改rootProject.name的内容,会在当前工程下新建该内容的文件夹,把原来的文件夹内容移动到新文件夹中

2. dependencies变化

Project/build.gradle文件中之前的dependencies文件

dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}

变为:

plugins {
id 'com.android.application' version '7.1.3' apply false
id 'com.android.library' version '7.1.3' apply false
id("org.jetbrains.kotlin.android") version "1.5.30" apply false
}

引入Hilt依赖的变化

Module/build.gradle中:

plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}

依赖中添加:

dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
...
//Hilt支持
implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"

}

最重要的一步

setting.gradle中添加resolutionStrategy

pluginManagement {
resolutionStrategy{
eachPlugin {
if( requested.id.id == 'dagger.hilt.android.plugin') {
useModule("com.google.dagger:hilt-android-gradle-plugin:2.28-alpha")
}
}
}
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}

Done!