android_框架_hilt_配置及可能遇见的问题

蒲昊苍
2023-12-01

hilt

hilt配置官网:https://dagger.dev/hilt/gradle-setup

配置

build.gradle(root)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        //使用其他kotlin版本可能报错: Model was compiled with an incompatible version of kotlin
        // the binary version of metadata is 1.5.1 accident version is 1.1.16
        //将kotlin版本改为1.4.32
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
        //hilt 配置
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28.3-alpha'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

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

build.gradle(app/module)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    //hilt配置
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}
android {
    compileSdk 31
    defaultConfig {
        applicationId "com.example.flashfeso_lwj"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        //hilt 配置: 加上下面的javaCompileOptions
        //否则报错: Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
        javaCompileOptions {
            annotationProcessorOptions {
                arguments += ["foo" : "bar"]
            }
        }
    }
    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'
    }
    viewBinding{
        enabled = true
    }
}

dependencies {
    //hilt配置
    //使用2.28.3-alpha版本, 若低于版本可能无法使用ApplicationComponent::class,只能使用SingletonComponent::class
    implementation 'com.google.dagger:hilt-android:2.28.3-alpha'
    kapt 'com.google.dagger:hilt-android-compiler:2.28.3-alpha'


    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

问题

一、Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?

解决如下:(app/build下build.moudle)

defaultConfig {
        applicationId "com.example.flashfeso_lwj"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        //hilt 配置: 加上下面的javaCompileOptions
        //否则报错: Expected @HiltAndroidApp to have a value. Did you forget to apply the Gradle Plugin?
        javaCompileOptions {
            annotationProcessorOptions {
                arguments += ["foo" : "bar"]
            }
        }
    }

二、Model was compiled with an incompatible version of kotlin the binary version of metadata is 1.5.1 accident version is 1.1.16

解决如下:(root下build.gralde改变kotlin版本)

dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        //使用其他kotlin版本可能报错: Model was compiled with an incompatible version of kotlin
        // the binary version of metadata is 1.5.1 accident version is 1.1.16
        //将kotlin版本改为1.4.32
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
        //hilt 配置
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28.3-alpha'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

三、找不到ApplicationComponent::class

解决如下:(更新hilt版本至2.28.3-alpha以上)

四、Cannot create an instance of class com.example.flashfeso_lwj.flashfeso.viewmodel.ConfiguracionViewModel

如果配置没错的情况下可以检查有没有加

@AndroidEntryPoint
 类似资料: