android - 在 Android 项目中添加 Hilt 组件后构建失败

标签 android kotlin dependency-injection dagger-hilt

我试图用最简单的例子来探索 Hilt,但我的应用程序没有构建。我添加了所有依赖项,但是当我尝试构建时,它显示一个错误,表明找不到 hilt gradle 插件。
这是我的代码:
build.gradle(项目)

    ext.kotlin_version = "1.5.20"
    ext.hilt_version = '2.35'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
构建.gradle(应用程序)
plugins{
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.bonny.tutorial.hilttest"
        minSdkVersion 23
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.5.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    //hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
应用程序类(也包含在 list 中)
import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class TextApplication : Application() {
}
依赖类 (MyTexts.kt)
class MyTexts @Inject constructor(){
    val text = "Hello World"
}
MainActivity.kt
@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    private lateinit var tv: TextView
    @Inject lateinit var myTexts: MyTexts
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tv = findViewById(R.id.myText)
        tv.text = myTexts.text
    }
}
错误如下所示:
public final class MainActivity extends androidx.appcompat.app.AppCompatActivity {
             ^
  Expected @AndroidEntryPoint to have a value. Did you forget to apply the Gradle Plugin? (dagger.hilt.android.plugin)
  See https://dagger.dev/hilt/gradle-setup.html
  [Hilt] Processing did not complete. See error above for details.
请建议。

最佳答案

Kotlin 1.5.20刚出来(2021 年 6 月 24 日),所以似乎存在一些兼容性问题。您可以转至 ext.kotlin_version = "1.5.10"在您的 build.gradle(project)此外,将刀柄更新到最新 2.37项目 build

classpath "com.google.dagger:hilt-android-gradle-plugin:2.37"
应用程序构建:
//Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.37"
    kapt "com.google.dagger:hilt-android-compiler:2.37"

关于android - 在 Android 项目中添加 Hilt 组件后构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68142574/

相关文章:

java - Android解析日期问题

kotlin - 如何遍历数组,其中每次迭代都是子数组而不是元素

c# - 使用 Ninject 进行深度大于 1 的上下文/条件依赖注入(inject)?

java - 表达式时删除 Kotlin 中的中断

java - 在自己的 Java 框架中支持多个 DI 容器

asp.net - 在 MVC 3 中使用 DependencyResolver 进行 Controller 实例化时出错

android - 如何在安卓手机上运行arp命令

java - Picasso 中的错误 get() 无法应用于 (android.content.Context)

Android在ImageView中绘制边框

kotlin - 如何检查字符串是否是android中的有效电子邮件?