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

标签 android unit-testing gradle android-testing

目前我的android项目使用的是1.2.3版本的gradle插件。我能够使用 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:

enter image description here

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')

关于android - 从 1.2.3 升级 android gradle 插件 -> 1.5.0 = 单元测试损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34169312/

相关文章:

android - "Gradle files have changed since last project sync."消息总是出现

java - Gradle:将一堆 jar 安装到本地 Maven 存储库

java - 如何安全地删除 ArrayList 上的逗号?

android - Dagger 2 从两个组件注入(inject)

php - PDO 模拟对象缺少 getConnection

asp.net-mvc - Machine.Specifications 和 Entity Framework 示例

android - 使用 Android Studio 和 Gradle 进行单元测试?

由于插件错误,Android 构建失败

android - 使用一次<android.support.design.widget时我的应用程序崩溃

c - 使用链接时替换在单元测试中为结构体创建测试替身