简体   繁体   English

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

[英]Build failed after adding Hilt components in Android project

I was trying to explore Hilt with simplest possible example, but my application isn't building.我试图用最简单的例子来探索 Hilt,但我的应用程序没有构建。 I added all the dependencies but when I try to build, it shows an error indicating not finding the hilt gradle plugin.我添加了所有依赖项,但是当我尝试构建时,它显示一个错误,指示找不到 hilt gradle 插件。 Here are my codes:这是我的代码:

build.gradle(project) 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
}

build.gradle(app) build.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'
}

Application Class (included in the Manifest too)应用程序类(也包含在清单中)

import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class TextApplication : Application() {
}

Dependency class (MyTexts.kt)依赖类 (MyTexts.kt)

class MyTexts @Inject constructor(){
    val text = "Hello World"
}

MainActivity.kt主活动.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
    }
}

and the error looks like this:错误如下所示:

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.

Please suggest.请建议。

Kotlin 1.5.20 just came out (24 jun 2021) so there seems to be some compatibility issue. Kotlin 1.5.20刚刚发布(2021 年 6 月 24 日),因此似乎存在一些兼容性问题。 You can bring down to ext.kotlin_version = "1.5.10" in your build.gradle(project)您可以在build.gradle(project) ext.kotlin_version = "1.5.10"

Also, Update hilt to latest 2.37另外,将刀柄更新到最新的2.37

project build项目构建

classpath "com.google.dagger:hilt-android-gradle-plugin:2.37"

app build:应用程序构建:

//Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.37"
    kapt "com.google.dagger:hilt-android-compiler:2.37"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM