android {
...
testBuildType "deviceTest"
buildTypes {
debug {
// Using 10.0.2.2 (the desktop's localhost), as the app normally runs on an Emulator
// in debug mode.
buildConfigField "String", "BACKEND_URL", '"http://10.0.2.2"'
buildConfigField "Integer", "PORT", "8080"
applicationIdSuffix ".debug"
}
// Use local host for testing, for MockWebServer
deviceTest {
initWith debug
buildConfigField "String", "BACKEND_URL", '"http://localhost"'
}
release {
...
}
}
}
就像谷歌文档暗示的那样 here .但是,这会导致单元测试无法访问测试依赖项(如 JUnit),因此测试无法运行。
最佳答案
我在 AS 3.1.3 上解决了这个问题。
问题是 Build Variant 和 testBuildType 不匹配。因此,在将 testBuildType 添加到您的 gradle 文件后,转到“构建 -> 选择构建变体”并为您的应用选择相同的变体。
在正确运行 androidTest 之前,您可能还需要设置签名 key 或解决混淆问题,但现在应该可以访问依赖项。
可以在此处跟踪更多详细信息: https://issuetracker.google.com/issues/36995546
关于java - android testBuildType 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44368130/