android - 使用Robolectric运行JUnit测试时出现Gradle和Android Studio问题

标签 android android-studio gradle

该应用程序的build.gradle为:

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.0"

    defaultConfig {
        applicationId "myapp.robolectricexample2"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'org.robolectric:robolectric:3.0-rc2'
    compile 'junit:junit:4.12'
}

因此,出现以下错误:

Error:Gradle: Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_31\bin\java.exe'' finished with non-zero exit value 2

最佳答案

试试这个build.gradle

android {
    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1" // <-- was 22.0.0

    defaultConfig {
        applicationId "myapp.robolectricexample2"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.1' // <-- was 22.0.0
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.0-rc2'
}

现在,您可以运行 gradlew testDebug

将您的测试放入 src/test 中。

关于android - 使用Robolectric运行JUnit测试时出现Gradle和Android Studio问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29584361/

相关文章:

Android Studio - 基于 Git 的多平台项目的 CRLF 与 LF

android - 如何将空气库打包到另一个 Android 库

Java jar 文件无法识别从 gradle 添加的外部库

android - 在 Google Play 中,它显示 "You need to upload an APK or Android App Bundle for this application"

安卓工作室 : ClassNotFoundException

android - View.post(),以及Runnables何时执行

android - Flutter- "extendBody: true"完全没有任何作用。我想扩展导航栏后面的主体

android - Build.Gradle(Module App)未知属性

android - 其他解决方案无法解决-错误:无法解决:com.android.support:support-v13.27.0.1:

android - 工具栏中占用剩余空间的搜索栏