Having robolectric test working on android is a headache, if you also migrated to android studio and the gradle build, it's just a nightmare.

Hopefully somebody on robolectric made this example project to see how to make everything run smoothly.

https://github.com/robolectric/deckard-gradle

Update: Unfortunately it only works with the android plugin 0.8.3 and this week android updated android studio and force you to use the last plugin. I wonder when android tools team will take seriously the testing and provide a nice tool for us.

Update2: Seems that there is a pull request to make the plugin work with 0.9 of android plugin Hurra!

I have tested to install it (download the code from github and install into my local maven repo) and it make it finally work pretty well. Althought I have not achieved to execute Android studio to run the test (Be aware to configure Android Studio to use the JDK 7, on my mac it was using the JDK6 and a strange error about incompatible version 51 come up)

Now seems that the plugin is inserting some system properties that android studio is not inserting and robolectric is not able to find the AndroidManifest.xml (so no resource folder etc...)

From b6addef987586ed79b8fac3a91250b4d69b6a2c8 Mon Sep 17 00:00:00 2001  
From: Jordi Coscolla <jordi@coscolla.net>  
Date: Sat, 15 Mar 2014 09:14:24 +0100  
Subject: [PATCH] ADD first version of roboelectric plugin

---
 Pardalet/build.gradle                              | 18 +++++++++++++++-
 .../pardalet/robolectric/RobolectricBasicTest.java | 25 ++++++++++++++++++++++
 .../robolectric/RobolectricGradleTestRunner.java   | 24 +++++++++++++++++++++
 build.gradle                                       |  2 ++
 gradlew                                            |  0
 5 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 Pardalet/src/androidTest/java/net/coscolla/pardalet/robolectric/RobolectricBasicTest.java
 create mode 100644 Pardalet/src/androidTest/java/net/coscolla/pardalet/robolectric/RobolectricGradleTestRunner.java
 mode change 100644 => 100755 gradlew

diff --git a/Pardalet/build.gradle b/Pardalet/build.gradle  
index 161c058..3adc95c 100644  
--- a/Pardalet/build.gradle
+++ b/Pardalet/build.gradle
@@ -1,4 +1,5 @@
 apply plugin: 'android'
+apply plugin: 'android-test'

 android {
     compileSdkVersion 19
@@ -27,6 +28,10 @@ android {
     }
 }

+androidTest {
+  exclude '**/BasicTest*'
+}
+
 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:+'
@@ -36,10 +41,21 @@ dependencies {
     compile 'com.squareup.dagger:dagger-compiler:+'
     compile 'com.squareup:otto:+'

-    androidTestCompile 'com.google.guava:guava:14.0.1'
+    androidTestCompile 'junit:junit:4.10'
+    androidTestCompile 'org.robolectric:robolectric:2.1.+'
+    androidTestCompile 'com.squareup:fest-android:1.0.+'
+
     androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
     androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
     androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
     androidTestCompile 'org.hamcrest:hamcrest-library:1.1'

 }
+
+apply plugin: 'idea'
+
+idea {
+    module {
+        testOutputDir = file('build/test-classes/debug')
+    }
+}
diff --git a/Pardalet/src/androidTest/java/net/coscolla/pardalet/robolectric/RobolectricBasicTest.java b/Pardalet/src/androidTest/java/net/coscolla/pardalet/robolectric/RobolectricBasicTest.java  
new file mode 100644  
index 0000000..8da0bbe  
--- /dev/null
+++ b/Pardalet/src/androidTest/java/net/coscolla/pardalet/robolectric/RobolectricBasicTest.java
@@ -0,0 +1,25 @@
+package net.coscolla.pardalet.robolectric;
+import android.app.Activity;
+import android.widget.Button;
+
+import net.coscolla.pardalet.MainActivity;
+import net.coscolla.pardalet.R;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.robolectric.Robolectric;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+
+import static org.junit.Assert.assertTrue;
+
+@RunWith(RobolectricGradleTestRunner.class)
+  public class RobolectricBasicTest {
+
+    @Test
+      public void testSomething() throws Exception {
+        Activity activity = org.robolectric.Robolectric.buildActivity(MainActivity.class).create().get();
+        Button btn = (Button) activity.findViewById(R.id.btn);
+        assertTrue(activity != null);
+      }
+  }
diff --git a/Pardalet/src/androidTest/java/net/coscolla/pardalet/robolectric/RobolectricGradleTestRunner.java b/Pardalet/src/androidTest/java/net/coscolla/pardalet/robolectric/RobolectricGradleTestRunner.java  
new file mode 100644  
index 0000000..c71ca4d  
--- /dev/null
+++ b/Pardalet/src/androidTest/java/net/coscolla/pardalet/robolectric/RobolectricGradleTestRunner.java
@@ -0,0 +1,24 @@
+package net.coscolla.pardalet.robolectric;
+
+import org.junit.runners.model.InitializationError;
+        import org.robolectric.AndroidManifest;
+        import org.robolectric.RobolectricTestRunner;
+        import org.robolectric.annotation.Config;
+        import org.robolectric.res.Fs;
+
+public class RobolectricGradleTestRunner extends RobolectricTestRunner {
+    public RobolectricGradleTestRunner(Class<!--?--> testClass) throws InitializationError {
+        super(testClass);
+    }
+
+    @Override protected AndroidManifest getAppManifest(Config config) {
+        String manifestProperty = System.getProperty("android.manifest");
+        if (config.manifest().equals(Config.DEFAULT) &amp;&amp; manifestProperty != null) {
+            String resProperty = System.getProperty("android.resources");
+            String assetsProperty = System.getProperty("android.assets");
+            return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
+                    Fs.fileFromPath(assetsProperty));
+        }
+        return super.getAppManifest(config);
+    }
+}
\ No newline at end of file
diff --git a/build.gradle b/build.gradle  
index 80eec1a..15f403e 100644  
--- a/build.gradle
+++ b/build.gradle
@@ -3,9 +3,11 @@
 buildscript {
     repositories {
         mavenCentral()
+        mavenLocal()
     }
     dependencies {
         classpath 'com.android.tools.build:gradle:0.9.+'
+        classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.4-SNAPSHOT'
     }
 }

diff --git a/gradlew b/gradlew  
old mode 100644  
new mode 100755  
-- 
1.8.3.4 (Apple Git-47)