相关疑难解决方法(0)

Android Studio - 清除Instrumentation Test的应用程序数据

如何在不手动运行adb命令的情况下让Android Studio(AndroidJunitRunner)清除检测测试之前的应用程序数据?

我发现了android.support.test.runner.AndroidJUnitRunner一种作弊的-它从来没有真正调用connectedCheckconnectedAndroidTest.

  1. 从命令行运行时 $ gradle connectedCheck

    :MyMainApp:assembleDebug UP-TO-DATE
    :MyMainApp:assembleDebugTest UP-TO-DATE
    :MyMainApp:clearMainAppData
    :MyMainApp:connectedCheck
    
    Run Code Online (Sandbox Code Playgroud)
  2. 通过单击检测测试配置从IDE内部运行(带有红色/绿色箭头的绿色Android机器人徽标)

    **Executing tasks: [:MyMainAppApp:assembleDebug, :MyMainAppApp:assembleDebugTest]**
    
    Run Code Online (Sandbox Code Playgroud)

    如您所见,最后一个gradle目标是 assembleDebugTest

我已经添加了一个钩到connectedCheckbuild.gradle开始仪表测试之前清除主要的应用程序的数据.

// Run 'adb' shell command to clear application data of main app for 'debug' variant
task clearMainAppData(type: Exec) {
    // we have to iterate to find the 'debug' variant to obtain a variant reference
    android.applicationVariants.all { variant ->
        if (variant.name.equals("debug")) {
            def clearDataCommand = ['adb', 'shell', 'pm', …
Run Code Online (Sandbox Code Playgroud)

gradle android-studio android-gradle-plugin android-instrumentation

13
推荐指数
2
解决办法
9250
查看次数