我试图在使用 hilt 时进行测试,但是每当我添加任何 hilt-test 注释时,它都会给我这个错误。这是我的 gradle 文件
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
id 'kotlin-android-extensions'
id "androidx.navigation.safeargs.kotlin"}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
kapt {
correctErrorTypes true
}
dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
def hiltDaggerVersion = '2.29-alpha'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
/* Testing */
// test kotlin
implementation 'androidx.test:core-ktx:1.3.0'
// fragment
debugImplementation "androidx.fragment:fragment-testing:1.2.5"
// hilt
androidTestImplementation "com.google.dagger:hilt-android-testing:2.29-alpha"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:2.29-alpha"
}
我在代码中使用 hilt 没有问题,但是当我想在测试中使用它时,android studio 无法识别注释或函数,直到我自己编写包名称,然后当我运行测试时它给了我这个错误。 有什么问题吗?
最佳答案
事实证明,我正在使用本地单元测试类的“仪器测试”依赖项,我应该像本指南那样使用“Robolectric 测试”Hilt testing guide
我正在学习教程,但他没有指出这一点(初学者的错误)。
关于android - 未解析的引用 : HiltAndroidTest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65144325/