Android Hilt 在启动时崩溃

Android Hilt Crashes on Startup

更新我的项目 Gradle 版本后(我没有保存旧版本)我开始收到此错误:

java.lang.IllegalStateException: Given component holder class MainActivity does not implement interface dagger.hilt.internal.GeneratedComponent or interface dagger.hilt.internal.GeneratedComponentManager
        at dagger.hilt.EntryPoints.get(EntryPoints.java:62)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.createInternal(HiltViewModelFactory.java:129)
        at androidx.hilt.navigation.HiltViewModelFactory.create(HiltNavBackStackEntry.kt:56)
        at androidx.hilt.navigation.compose.HiltViewModelKt.createHiltViewModelFactory(HiltViewModel.kt:107)
        at ServerHomeScreenKt.ServerHomeScreen(ServerHomeScreen.kt:235)
        at NavGraphKt$NavGraph.invoke(NavGraph.kt:53)
        at NavGraphKt$NavGraph.invoke(NavGraph.kt:52)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.navigation.compose.NavHostKt$NavHost.invoke(NavHost.kt:146)
        at androidx.navigation.compose.NavHostKt$NavHost.invoke(NavHost.kt:145)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
        at androidx.compose.runtime.saveable.SaveableStateHolderImpl.SaveableStateProvider(SaveableStateHolder.kt:84)
        at androidx.navigation.compose.NavBackStackEntryProviderKt.SaveableStateProvider(NavBackStackEntryProvider.kt:59)
        at androidx.navigation.compose.NavBackStackEntryProviderKt.access$SaveableStateProvider(NavBackStackEntryProvider.kt:1)
        at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider.invoke(NavBackStackEntryProvider.kt:51)
        at androidx.navigation.compose.NavBackStackEntryProviderKt$LocalOwnersProvider.invoke(NavBackStackEntryProvider.kt:50)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:215)
        at androidx.navigation.compose.NavBackStackEntryProviderKt.LocalOwnersProvider(NavBackStackEntryProvider.kt:46)
        at androidx.navigation.compose.NavHostKt$NavHost.invoke(NavHost.kt:145)
        at androidx.navigation.compose.NavHostKt$NavHost.invoke(NavHost.kt:144)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.animation.CrossfadeKt$Crossfade.invoke(Crossfade.kt:74)
        at androidx.compose.animation.CrossfadeKt$Crossfade.invoke(Crossfade.kt:69)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:107)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.animation.CrossfadeKt.Crossfade(Crossfade.kt:86)
        at androidx.navigation.compose.NavHostKt.NavHost(NavHost.kt:144)
        at androidx.navigation.compose.NavHostKt$NavHost.invoke(Unknown Source:13)
        at androidx.navigation.compose.NavHostKt$NavHost.invoke(Unknown Source:10)
        at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:140)
        at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2156)
        at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2399)
        at androidx.compose.runtime.ComposerImpl$doCompose.invoke(Composer.kt:2580)
        at androidx.compose.runtime.ComposerImpl$doCompose.invoke(Composer.kt:2573)
2021-07-27 16:55:23.029 8998-8998/com.team2052.frckrawler E/AndroidRuntime:     at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(SnapshotState.kt:540)
        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:2566)
        at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:2542)
        at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:613)
        at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:764)
        at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:103)
        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges.invoke(Recomposer.kt:447)
        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges.invoke(Recomposer.kt:416)
        at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$callback.doFrame(AndroidUiFrameClock.android.kt:34)
        at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
        at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
        at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback.doFrame(AndroidUiDispatcher.android.kt:69)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:970)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:727)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

gradle-wrapper.properties:

#Wed Jun 23 20:46:04 CDT 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

应用build.gradle:

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

android {

    compileSdkVersion 30

    buildFeatures {
        viewBinding true
        compose true
    }

    defaultConfig {
        applicationId "com.team2052.frckrawler"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    packagingOptions {
        exclude "META-INF/atomicfu.kotlin_module"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    composeOptions {
        kotlinCompilerVersion "1.5.10"
        kotlinCompilerExtensionVersion "1.0.0-rc02"
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

// All rootProject properties can be found in the build.gradle (FRC-Krawler) file
dependencies {

    // Android
    implementation "androidx.legacy:legacy-support-v4:1.0.0"
    implementation "androidx.core:core-ktx:1.6.0"
    implementation "androidx.appcompat:appcompat:1.4.0-alpha03"
    implementation "androidx.multidex:multidex:2.0.1"
    testImplementation "junit:junit:4.13.2"
    androidTestImplementation "androidx.test.ext:junit:1.1.3"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"

    // Kotlin
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$kotlin_lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$kotlin_lifecycle_version"

    // UI
    implementation "com.google.android.material:material:1.4.0"
    implementation "androidx.constraintlayout:constraintlayout:2.0.4"
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.5"
    implementation "com.google.accompanist:accompanist-swiperefresh:0.13.0"

    // Compose
    //implementation "androidx.compose.compiler:compiler:$compose_version"
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.ui:ui-tooling:$compose_version"
    implementation "androidx.compose.foundation:foundation:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.material:material-icons-core:$compose_version"
    implementation "androidx.compose.material:material-icons-extended:$compose_version"
    implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
    implementation "androidx.constraintlayout:constraintlayout-compose:1.0.0-beta01"
    implementation 'androidx.activity:activity-compose:1.3.0-rc02'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07'
    implementation "androidx.navigation:navigation-compose:2.4.0-alpha05"
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"

    // Retrofit
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-moshi:$retrofit_version"
    implementation "com.squareup.okhttp3:okhttp:$okhttp_version"
    implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_version"
    implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
    kapt "com.squareup.moshi:moshi-kotlin-codegen:$moshi_version"

    // Room
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    testImplementation "androidx.room:room-testing:$room_version"

    // Dagger - Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0"
    implementation "androidx.hilt:hilt-navigation-compose:1.0.0-alpha03"
}

模块build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    ext {
        kotlin_version = '1.5.10'
        kotlin_lifecycle_version = '2.3.1'
        compose_version = '1.0.0-rc02'
        retrofit_version = '2.9.0'
        okhttp_version = '4.9.0'
        moshi_version = '1.12.0'
        room_version = '2.3.0'
        hilt_version = '2.38'
    }

    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.android.tools.build:gradle:7.1.0-alpha03' // Switch to 06 when it comes out
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.38"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

崩溃现场的视图模型:

@HiltViewModel
class ServerViewModel @Inject constructor(
    private val bluetoothController: BluetoothController
) : ViewModel() {}

主要Activity:

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {}

蓝牙模块:

@Module
@InstallIn(SingletonComponent::class)
object BluetoothModule {
    @Singleton
    @Provides
    fun provideBluetoothController(
        @ApplicationContext context: Context
    ): BluetoothController {
        return BluetoothController(
            context = context,
            bluetooth = BluetoothAdapter.getDefaultAdapter(),
        )
    }
}

如果您知道导致此错误的原因,请告诉我,我找不到太多相关信息。同样,我认为它与依赖版本有关,但我不知道从哪里开始。
谢谢

您应该使用 Hilt 注释您的 MainActivity docs

像这样在 MainActivity 上方添加 @AndroidEntryPoint ..

@AndroidEntryPoint class 主活动:AppCompatActivity() { ... }

在修改不同的版本值后,似乎将应用程序依赖项 classpath "com.google.dagger:hilt-android-gradle-plugin:2.38" 降级为 classpath "com.google.dagger:hilt-android-gradle-plugin:2.28-alpha" 解决了问题,但仍然不确定是什么原因造成的,但这目前有效。

这是 2.38 的问题,已在 2.38.1 中修复。参见 https://github.com/google/dagger/releases/tag/dagger-2.38.1