android testBuildType无效

Oha*_*von 7 java testing android gradle

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)无法从单元测试中访问,因此测试无法运行.

小智 7

我在 AS 3.1.3 上发现了这个问题。

问题是 Build Variant 和 testBuildType 不匹配。因此,在将 testBuildType 添加到您的 gradle 文件后,转到“构建 -> 选择构建变体”并为您的应用选择相同的变体。

在正确运行 androidTest 之前,您可能还需要设置签名密钥或解决 proguard 问题,但现在应该可以访问依赖项。

可以在此处跟踪更多详细信息:https : //issuetracker.google.com/issues/36995546