1

I am trying to get robolectric working for new Android 0.8.1 since currently there is no robolectric gradle 0.12 plugin so I am hoping that .11 will work for now.

* Problem:

androidTestCompile 'org.robolectric:robolectric:2.3'

Using the following declaration to compile robolectric, I am unable to find "org.robolectric.*" in my test directory, studio gives "cannot resolve symbol..." error.

According to the following projects you have to manually update the iml file. This is probably not the best solution since updating gradle file overwrites any changes you have made.

Would appreciate help on this matter!! thank you.

* Current project structure is:

   src -> main -> java

   src -> test -> java

* Below is my current build.gradle module file:

apply plugin: 'android-library'
apply plugin: 'robolectric'


buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
    }
}

allprojects {
    repositories {
       jcenter()
    }
}

android {
    compileSdkVersion 16
    buildToolsVersion '19.1.0'
    publishNonDefault true

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

    defaultConfig {
        applicationId 'com.myapp.example.main'
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName '1.0'

    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    sourceSets {
        androidTest {
            setRoot('src/test/java')
        }
    }
    lintOptions {
        abortOnError false
        disable 'InvalidPackage'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:+'
    compile files('libs/gson-2.2.4.jar')
    compile 'com.google.android.gms:play-services:+'
    compile 'com.android.support:support-v13:+'

    /* Including testing libraries */
    androidTestCompile 'junit:junit:4.10'
    androidTestCompile 'org.robolectric:robolectric:2.3'
}

robolectric {
    // configure the set of classes for JUnit tests
    include '**/*Test.class'

    // configure max heap size of the test JVM
    maxHeapSize = "2048m"
}
ahmad
  • 2,149
  • 4
  • 21
  • 38

1 Answers1

0

It's a bit later for now. But I ran into this error as well and it turned out the version of Robolectric in my dependencies was outdated. After I replaced it with the newest one found here, I fixed the error. Also, you may change the androidTestCompile to testCompile since you're writing unit test here. You can find the difference between the two here

Community
  • 1
  • 1
Chris.Zou
  • 4,506
  • 6
  • 31
  • 38