【问题标题】:"Cannot determine expansion folder" when running android Instrumentation tests运行android Instrumentation测试时“无法确定扩展文件夹”
【发布时间】:2015-08-28 09:19:36
【问题描述】:

我们想为我们的应用设置仪器测试,该应用也有 2 种风格。我们已成功设置 Android Studio 以直接从 IDE 运行插桩测试,但尝试通过“gradle connectedCheck”从命令行运行插桩测试总是会导致以下错误:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDevelopmentDebugAndroidTestJavaResources'.
> Cannot determine expansion folder for /Users/james/Development/AndroidProjects/parkinsons11/app/build/intermediates/packagedJarsJavaResources/androidTest/development/debug/junit-4.12.jar936209038/LICENSE-junit.txt with folders 

我们的测试应用程序也有两种风格,专为插桩测试而设置,可以从 IDE 和命令行运行而不会发生任何事故。

这是我们主项目的 gradle 文件:

buildscript {
    repositories {
        maven { url 'http://download.crashlytics.com/maven' }
    }

    dependencies {
        classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'crashlytics'

repositories {
    maven { url 'http://download.crashlytics.com/maven' }
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    defaultConfig {
        applicationId "com.app.ourapp"
        minSdkVersion 16
        versionCode 11
        versionName "1.1"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            assets.srcDirs = ['src/main/assets',
                              'src/main/assets/font']
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    productFlavors {
        live {
            versionName "1.1 live"
            applicationId "com.app.ourapp.live"
        }
        development {
            versionName '1.1 development'
            applicationId "com.app.ourapp.development"
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':library:datetimepicker')
    compile project(':library:tools')
    compile 'com.android.support:appcompat-v7:+'
    compile 'com.android.support:support-v4:+'
    compile 'com.crashlytics.android:crashlytics:1.+'
    compile 'org.quanqi:mpandroidchart:1.7.+'
    compile 'commons-io:commons-io:2.+'
    compile 'joda-time:joda-time:2.+'
    compile 'com.microsoft.azure.android:azure-storage-android:0.4.+'

    testCompile 'junit:junit:4.12'
    testCompile "org.robolectric:robolectric:${robolectricVersion}"
    testCompile "org.mockito:mockito-core:1.+"

    androidTestCompile 'junit:junit:4.12'
    androidTestCompile "org.mockito:mockito-core:1.+"
}

这是来自我们的测试应用程序的 gradle.build(有效):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.test.picroft.instrumentationtestapp"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        newFlavour {
            applicationId "com.test.picroft.instrumentationtestapp.newflavor"
        }

        oldFlavour {
            applicationId "com.test.picroft.instrumentationtestapp.oldflavor"
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'

    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.+"

    androidTestCompile 'junit:junit:4.12'
    androidTestCompile "org.mockito:mockito-core:1.+"
}

我不知道哪里出错了。我已经比较了两个应用程序的目录结构,没有有意义的区别。以下是我们主要项目结构的大致轮廓:

src
-androidTest
--java
---*
-live
--res
---layout
---values
-main
--java
---*
-test
--java
---*

我完全不明白为什么一个应用程序的插桩测试在 IDE 和命令行中都可以正常工作,而另一个应用程序拒绝通过命令行工作。

【问题讨论】:

  • 你的测试是在 -test/java 还是 androidTest/java 下?这可能是个问题
  • 仪器测试在 androidTest/java 下,tests/java 是我们的单元测试,可以在 IDE 和命令行中工作

标签: java android testing android-studio instrumentation


【解决方案1】:

看来问题自行解决了。我相信这是通过无效/重新启动修复的构建状态的某种损坏。

还值得指出的是,当您在 Build Variation 面板中切换构建风格时,值得等待 Android Studio 完成同步并且在构建或运行之前不执行任何任务。我发现当我点击构建或运行时,Android Studio 还没有完成构建风格的切换,它会导致各种不可预知的问题。

【讨论】:

    【解决方案2】:

    我认为问题可能是您没有定义默认的检测运行器,如 here 所述。您应该将其包含在您的 build.gradle 文件中

    android {
        defaultConfig {
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    }
    

    【讨论】:

    • 已将其添加到我的 build.gradle 中,但仍未解决问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2018-03-13
    相关资源
    最近更新 更多