无法在同一编译单元Hilt Android中处理测试根和应用程序根



我得到这个构建错误:

Cannot process test roots and app roots in the same compilation unit:

当我试图写这样的仪器测试时:

@HiltAndroidTest
class DataRecognitionRepositoryImplTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Test
fun first() {
}
}

我还创建了自己的跑步者,如下所示:

class HiltTestRunner: AndroidJUnitRunner() {
override fun newApplication(
cl: ClassLoader?,
className: String?,
context: Context?
): Application {
return super.newApplication(cl, HiltTestApplication::class.java.name, context)
}
} 

并设置为:

testInstrumentationRunner "com.mayurg.helpers.HiltTestRunner"

我的根构建文件:

// 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:4.2.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.40.5"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

我试着搜索了很多,但除了这个之外,没有发现任何有用的东西:https://dagger.dev/hilt/flags

但我不知道如何/在哪里使用这些。感谢您的帮助。

要修复此问题,您需要将其添加到您的build.gradle文件中:

android {
defaultConfig {
javaCompileOptions.annotationProcessorOptions.arguments['dagger.hilt.disableCrossCompilationRootValidation'] = 'true'
}
}

最新更新