Android 仪器测试清除我的应用程序中的设置

Android instrumentation tests clear settings in my application

问题是:

  1. 我运行我的申请。设置存在,一切正常。
  2. I 运行 仪器测试(gradle 任务 connectedCheck)。测试不使用设置进行操作(不读取,不删除,甚至不触摸)。测试用 @RunWith(AndroidJUnit4.class) 注释并且根本不使用活动。
  3. 我再次 运行 我的应用程序,我必须指定设置,就像我重新安装我的应用程序一样。

我尝试在 gradle 构建脚本中指定 testApplicationId

android {
...
    defaultConfig {
    ...
    testApplicationId "something.package"

但是没有效果

为什么会这样,如何解决这个问题?

根据 Gradle User-guide:

As mentioned previously, checks requiring a connected device are launched with the anchor task called connectedCheck. This depends on the task connectedDebugAndroidTest and therefore will run it. This task does the following:

  • Ensure the app and the test app are built (depending on assembleDebug and assembleDebugAndroidTest).
  • Install both apps.
  • Run the tests.
  • Uninstall both apps.

您可以使用常规 运行 配置来启动测试而不是 connectedCheck