从 1.2.3 升级 android gradle 插件 -> 1.5.0 = 单元测试损坏

upgrading android gradle plugin from 1.2.3 -> 1.5.0 = unit tests broken

目前,我的android项目使用的是gradle插件版本1.2.3。我能够使用 gradle 命令“./gradlew clean connectedProductionAndroidTest”成功 运行 单元测试(android 测试),其中 "Production" 是一种风格.

但是,当我将 gradle 插件更新到 1.5.0 时,出现以下错误。

17:35:16 Tests on Android SDK built for x86 - 4.2 failed: Instrumentation run failed due to 'java.lang.IllegalAccessError'
17:35:16 
17:35:16 com.android.builder.testing.ConnectedDevice > No tests found.[Android SDK built for x86 - 4.2] [31mFAILED [0m
17:35:16 No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).

当我更新 gradle 插件时,我没有对目录结构进行任何更改,也没有对我的任何测试进行任何更改。

这是我用来更新 gradle 插件的代码。这是在我的 root build.gradle 文件中。

buildscript {
    repositories { 
        jcenter()
    }


    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
    }
}

我只是想知道这两个版本之间是否有任何变化可能导致我的测试失败?

请注意,我已将我的测试放在文档中指定的“$ROOT/app/src/androidTest/java/...”中。此外,在 gradle 1.2.3 中,我不得不使用命令“./gradlew clean connectedProductionAndroidTest”,但在插件版本 1.5.0 中,情况似乎发生了变化。我现在必须做:“./gradlew clean connectedAndroidTestProduction”。注意 "Production" 和 "AndroidTest".

的交换

如有任何帮助,我们将不胜感激。

编辑:

这是我应用程序的 build.gradle 文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.compileSdkVersion
    buildToolsVersion rootProject.buildToolsVersion

    lintOptions {
        disable 'OldTargetApi'
        abortOnError false
    }

    defaultConfig {
        applicationId "com.myapp"

        minSdkVersion rootProject.minSdkVersion
        targetSdkVersion rootProject.targetSdkVersion

        versionCode rootProject.getVersionCode()
        versionName rootProject.getVersionName()

        testApplicationId "com.myapp.test"
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles 'proguard.cfg'
            signingConfig signingConfigs.release

            multiDexEnabled false
        }
        debug {
            debuggable true
            testCoverageEnabled true
            signingConfig signingConfigs.release

            multiDexEnabled true
        }
    }

    productFlavors {
       production {

       }
    }

    dexOptions {
        javaMaxHeapSize "4g"
    }
}

在您的 project.properties 文件中添加此行:

manifestmerger.enabled=true 

并重建您的应用程序。

如果它不起作用,请检查此解决方案:

I have finally found a solution. The problem was with dependencies indeed, it is still unknown why it used to work and then suddenly refused, but here is how the dependencies should look like for your test module:

So all in all you need to make sure all your libraries and project libraries are listed for your test module and marked as "Provided" except Robotium lib, which is "Compile".

发件人:Instrumentation run failed due to 'java.lang.IllegalAccessError'

最后,如果您仍然卡在这个问题上:

Older devices have problems running tests when you have the same dependency in your app and test app that instruments the app.

To work around this issue, you will have to figure out which dependencies cause the problem.

In my case it was both Dagger and Espresso depending on javax.inject and this is how it can be "fixed":

androidTestCompile('com.android.support.test.espresso:espresso-core:2.0')

{ exclude group: 'javax.inject' }

If you include more or weirder dependencies, you may have a look at this build.gradle.

When using espresso-contrib, you may need to do this:

androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.0')
{
    exclude group: 'javax.inject'
    exclude group: 'com.android.support'
}

发件人:Tests fail after Espresso 2 upgrade (failed: Instrumentation run failed due to 'java.lang.IllegalAccessError')