使用 dagger hilt 尝试代码时出现此错误,错误:[Hilt] java.lang.reflect.InvocationTargetException(无错误消息)

时间:2021-03-15 18:39:34

标签: android mvvm retrofit build.gradle dagger-hilt

我尝试了使用 API 在原生 Android 中制作货币转换器的代码。我使用过 Retrofit、Dagger-Hilt 和 MVVM 架构设计模式。完成所有编码后,这是我得到的错误。 API 获取实时货币汇率,将其从 json 转换为 kotlin 并使用。

Gradle 文件 这是我添加了所有依赖项的 gradle 文件。

plugins {

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

android {
    compileSdkVersion 29

    defaultConfig {
        applicationId "com.example.currency"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }

    buildTypes {

        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }


    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }

    buildFeatures{
        viewBinding true
        dataBinding true
    }

}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.3.0-beta01'
    implementation 'androidx.appcompat:appcompat:1.3.0-beta01'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.3.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'

    //dagger - hilt
    implementation "com.google.dagger:hilt-android:2.31-alpha"
    kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
    implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
    kapt "androidx.hilt:hilt-compiler:1.0.0-beta01"

    //Activity KTX for viewModels()
    implementation "androidx.activity:activity-ktx:1.2.1"

    //Architectural components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"

    //Lifecycle
    implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.0"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.0"

    //Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.okhttp3:okhttp:4.9.0'

    //Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'

    //Coroutines Lifecycle Scopes
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.0"
 }

2 个答案:

答案 0 :(得分:1)

就像我在评论和我的 github PR 中提到的那样。解决办法是使用同版本的hilt-android,编译器

答案 1 :(得分:0)

我使用了 hilt 版本 2.33-beta 和相同版本的 hilt-android,compiler,但我遇到了同样的问题。然后我参考 Google Codelab 将 hilt 版本更改为 2.3.5,一切正常,因为我发现版本与 kotlin 1.5 或更高版本不兼容。