Search code examples
androidgradlerobolectricdexrobolectric-gradle-plugin

Using Robolectric without Multidexing?


I am working on an Android application and I want to use Robolectric for testing.

The main problem I have is, whenever I include Robolectric (and a few other testing libraries) to my gradle.build file, I get Dex errors so I need to enable the multidexing library (I work on Android 4.4).

Because of that, I just can't compile anymore, it's taking way too long. Without Robolectric and multidex, compiling takes maybe a minute, and I don't get anything after more than 30 minutes when including Robolectric and multidex.

Here is my complete gradle file :

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "cm.smobilpay.testapp"
        minSdkVersion 19
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
            debuggable true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            testCoverageEnabled true
            multiDexEnabled true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            testCoverageEnabled false
        }
    }

    productFlavors {
        unitTest // Creates a new scope which wraps only unit tests
    }

    sourceSets {
        unitTest {
            java {
                srcDir 'src/test/java' // New scope includes our unit test folder
            }
        }
    }

    // Prevent conflicts between Robolectric's dependencies
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'LICENSE.txt'
        exclude 'LICENSE'
    }

    lintOptions {
        abortOnError false
        xmlReport true
    }

}

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(name:'s3papiandroidclient', ext:'aar')
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'joda-time:joda-time:2.8.1'
    compile 'com.android.support:multidex:1.0.0'


    // Unit testing dependencies
    testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
    testCompile group: 'junit', name: 'junit-dep', version: '4.10'
    testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'

    unitTestCompile('org.robolectric:robolectric:3.0-rc3') {
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'
    }
}

My question is : what am I doing wrong that makes me being forced to use multidex when including Robolectric to my project ?


Solution

  • Robolectric should not be included in your dex at all. Robolectric tests run on the JVM, they do not get deployed on devices.

    You should be including it with testCompile as described in the docs.

    testCompile "org.robolectric:robolectric:3.0"