hilt
hilt配置官网:https://dagger.dev/hilt/gradle-setup
配置
build.gradle(root)
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28.3-alpha'
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(app/module)
plugins {
id 'com.android.application'
id 'kotlin-android'
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"
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 {
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"
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"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28.3-alpha'
}
三、找不到ApplicationComponent::class
解决如下:(更新hilt版本至2.28.3-alpha以上)
|