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 {
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
酷似谷歌文档意味着这里.但是,这会导致测试依赖性(如JUnit)无法从单元测试中访问,因此测试无法运行.
是否可以仅更改“ BuildConfig仪器测试”中的字段?
这背后的当前动机是,我想将应用程序的URL指向仅用于Instrumentation测试的另一台服务器(即本地模拟服务器)。这些网址是通过buildConfigField中的build.gradle进行配置的,用于多种应用。