Robolectric + Android Studio + Gradle

6,343 views
Skip to first unread message

Luke St.Clair

unread,
Jun 4, 2013, 6:12:15 PM6/4/13
to robol...@googlegroups.com
Is there any way to have gradle + android studio + robolectic + android resources working together?

I saw https://github.com/stanfy/gradle-plugin-java-robolectric, but it precludes having resources around, correct?

Are people:
1) Just not unit testing?
2) Only "unit" testing on device?
3) Not using gradle?
4) Not building from within Android studio and maintaining 2 separate build systems?
5) Just not testing anything that requires resources?
6) Finding a way to have resources + robolectric + gradle together


Rudolf

unread,
Jun 12, 2013, 4:09:26 AM6/12/13
to robol...@googlegroups.com
I think robolectric has to write support for it. Path to resources and Manifest has been changed and moreover the path is configurable in build.gradle

Maybe robolectric has to switch to gradle build as well

Carl-Gustaf Harroch

unread,
Jun 12, 2013, 4:22:17 AM6/12/13
to robol...@googlegroups.com
The problem lies with the sourceset with the android build system. It s fairly different then the normal build source set. I managed to get the robolectric tests working on my end and will push an example build shortly if anyone interested.


--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matthias

unread,
Jun 14, 2013, 11:35:47 AM6/14/13
to robol...@googlegroups.com
just curious here, why is it that in a structure like this one:

+-- parent-proj
   +-- app-proj
   +-- test-proj

test-proj doesn't see app-proj's classes even with a project(:app-proj) dependency declared? shouldn't importing classpaths in Gradle projects be independent of any plugins being used, whether Android or Java?

Carl-Gustaf Harroch

unread,
Jun 15, 2013, 9:41:47 AM6/15/13
to robol...@googlegroups.com
>>  shouldn't importing classpaths in Gradle projects be independent of any plugins being used, whether Android or Java?
The problem lies with the Android plugin 'extending' the java one and replacing the conventions on sourceSet - which in terms declare src location, class output location, lib location etc... The android plugin relies on build variances so you would have:
normal java plugin (approx): output/main
android plugin: output/main/{debug|market|x86...etc} 

The above means that all plugins that relies on JavaPluginConvention (http://www.gradle.org/docs/current/groovydoc/org/gradle/api/plugins/JavaPluginConvention.html) will fail against an Android project. Most plugins relies on that convention.

On top of the above we have AAR which are build variance dependent and have 'local' jar dependencies.

Getting your classpath correct can be tricky but feasible. 

The following can give you ideas - I welcome any comments - on which classpath to configure against Android plugin ( especially the configureAndroidDependency method)




Luke St.Clair

unread,
Jun 25, 2013, 8:04:18 PM6/25/13
to robol...@googlegroups.com
I'm curious - is anyone actively working on this?

It seems like all the solutions I've seen before don't have IDE support (with Android Studio, for instance). In other words, you can sort of hackishly get something building from the command line, but there's no ide integration, IntelliJ won't understand your test dir is a set of sources in Studio, etc. Not being able to at least auto-complete imports and not having to set up Studio each time I open it is sort of a deal breaker.

The reason I'm asking is that I don't understand why this isn't a bigger deal to people - are people just only testing their java-only code with studio? Only running on-device tests?

Phil Goodwin

unread,
Jun 25, 2013, 8:54:01 PM6/25/13
to robol...@googlegroups.com
My guess is that people aren't using Gradle directly. I imported my Maven project into Android Studio and it works just fine. I know that Studio builds using Gradle under the covers and just assumed that the import mechanism did all the necessary configuration needed to make that happen. 

This thread is missing a clear problem statement with steps to reproduce and the link in the OP is broken now. Would a Maven->Studio->Gradle migration work for you as a way to set up a project? It would be best if using Robolectric integrated well with people's natural workflow, but the landscape is changing rapidly enough that everyone has a different idea of what "natural" means. I hope that once your approach is more clear it will be easy to find a way to get the tools to work together comfortably for you without having to write a new plug-in for anything.
 

Luke St.Clair

unread,
Jun 25, 2013, 9:01:33 PM6/25/13
to robol...@googlegroups.com
Are you using Robolectric? Importing a Maven project into Android Studio doesn't allow me to run tests from studio, nor can I run them from the command line with Gradle. I can't edit the robolectric tests with IDE support either. Are these things not a problem for you?


Here's the problem statement, if it wasn't clear:
There doesn't seem to be a way to get IDE support (for instance, understanding that test files are Java and part of the project across restarts), command line building, IDE building/installing, Android Studio, and Robolectric working simultaneously. 

I'm hoping I'm wrong about that, and someone can set me straight.

Phil Goodwin

unread,
Jun 26, 2013, 12:02:27 AM6/26/13
to robol...@googlegroups.com
Hi Luke,

Yeah, I use Robolectric. Android Studio understands that all my tests are Java files and even knows that while production code is visible to tests, test code isn't visible to production code. I can install and run apps on a device directly from the IDE, and I can build from the command line using Maven. I haven't tried a Gradle command line build, but I expect it would work because the IDE build is Gradle based. 

I don't know Gradle well enough to tell you how to set up a project using it. If you look at Android IntelliJ Starter on GitHub you can see an example project based on Maven that can be opened by Android Studio (double click on the pom.xml file after Open Project...) and has tests that will run. I hope that will give you some kind of starting point.

Phil

Raoul Duke

unread,
Jun 28, 2013, 12:55:33 PM6/28/13
to robol...@googlegroups.com
hi,

I too am lost, now that we're trying to use Android Studio + Gradle +
Robolectric. Might anybody have a working project they can upload &
point us to? That would be a wonderful thing. The SO answers so far
all look to me like scary hacks that only work if you do the voodoo
just right :-)

thank you!

Luke

unread,
Jun 28, 2013, 3:25:51 PM6/28/13
to robol...@googlegroups.com
In particular, even the hacky solutions don't integrate into the IDE.



--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/71ZMRDK1gII/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robolectric...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.





--
Luke

slawomir kulinski

unread,
Jul 1, 2013, 6:43:25 AM7/1/13
to robol...@googlegroups.com
here is build.gradle which works for me http://stackoverflow.com/questions/16649397/robolectric-with-gradle-resources-not-found

still Android Studio requires dependencies to be added manually.

Shubham Goyal

unread,
Aug 9, 2013, 2:23:43 AM8/9/13
to robol...@googlegroups.com
I am facing this issue while implementing the solution provided by Slawomir - http://stackoverflow.com/questions/18122502/robolectric-with-gradle-package-org-robolectric-does-not-exist . 

Nikolay Panfilov

unread,
Aug 9, 2013, 7:23:08 AM8/9/13
to robol...@googlegroups.com
That will be fine... Please, provide us with an example...

Thanks,
Nick

среда, 12 июня 2013 г., 12:22:17 UTC+4 пользователь Carl написал:

Roman Mazur

unread,
Aug 13, 2013, 11:59:12 AM8/13/13
to robol...@googlegroups.com
AFAIK Gradle plugin for Robolectric is currently in development (I saw a corresponding post in adt-dev group). And I hope we'll see it soon.

I use Robolectric with Gradle and Studio with a bunch of hacks.

1. Robolectric must be built with the following merge request: https://github.com/robolectric/robolectric/pull/615
I'm pretty sure this request will not be merged since it will not be necessary after plugin is ready.

2. I use the following snippet for my build.gradle

configurations {
  jvmTest.extendsFrom compile /* tests on JVM */
}

sourceSets {
  jvmTest {
    compileClasspath += configurations.jvmTest
    runtimeClasspath += output + compileClasspath
  }
}

task testJvm(type: Test) {
  testClassesDir = project.sourceSets.jvmTest.output.classesDir
  classpath = project.sourceSets.jvmTest.runtimeClasspath
}

check.dependsOn testJvm

dependencies {
  jvmTest files("$System.env.ANDROID_HOME/platforms/android-17/android.jar")
  jvmTest fileTree(dir: "$buildDir/exploded-bundles", include: "*/classes.jar")
  jvmTest fileTree(dir: "$buildDir/exploded-bundles", include: "*/libs/*.jar")
  jvmTest files("$buildDir/classes/debug") {
    builtBy 'compileDebug'
  }
  jvmTest group: 'junit', name: 'junit', version: '4.11'
  jvmTest group: 'org.robolectric', name: 'robolectric', version: '2.2-SNAPSHOT'
}


This snippet works for default release and debug build types. When custom flavors appear, build script becomes a bit more complicated :)

3. In order to edit tests code in Studio I have to tweak project module settings manually after gradle project import, adding test source folder and test library dependencies (since my custom configuration is not handled automatically).

I'm sure that the first 2 points will become unnecessary after plugin is released. 
As for Studio, it looks like we have to be patient and wait for this support a bit longer :)

Jake Wharton

unread,
Aug 13, 2013, 12:03:40 PM8/13/13
to robol...@googlegroups.com
I have a full unit testing plugin which lets you use Robolectric like you normally would coming out today or tomorrow.

Nick Panfilov

unread,
Aug 13, 2013, 12:10:41 PM8/13/13
to robol...@googlegroups.com

PERFECT news!

That will be really helpful at least for me.

 

Thanks,

Nick

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/71ZMRDK1gII/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robolectric...@googlegroups.com.

Nikolay Panfilov

unread,
Aug 15, 2013, 8:58:54 AM8/15/13
to robol...@googlegroups.com
Hi Jake

any updates about your plugin?

Thanks,
Nick

вторник, 13 августа 2013 г., 20:03:40 UTC+4 пользователь Jake Wharton написал:

Jake Wharton

unread,
Aug 15, 2013, 10:44:41 AM8/15/13
to robol...@googlegroups.com
People are beta testing it now. It'll have a 0.9 release today or tomorrow.

Nick Panfilov

unread,
Aug 15, 2013, 10:51:03 AM8/15/13
to robol...@googlegroups.com

Thanks for updating.

Can you pls provide us with the link here when it will be done?

 

Thanks,

Nick

 

From: robol...@googlegroups.com [mailto:robol...@googlegroups.com] On Behalf Of Jake Wharton
Sent: Thursday, August 15, 2013 6:45 PM
To: robol...@googlegroups.com
Subject: Re: [robolectric] Re: Robolectric + Android Studio + Gradle

 

People are beta testing it now. It'll have a 0.9 release today or tomorrow.

--
You received this message because you are subscribed to a topic in the Google Groups "Robolectric" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/robolectric/71ZMRDK1gII/unsubscribe.
To unsubscribe from this group and all its topics, send an email to robolectric...@googlegroups.com.

mike.ku...@corp.badoo.com

unread,
Aug 15, 2013, 12:12:19 PM8/15/13
to robol...@googlegroups.com
Hi Jake

Any updates on the gradle plugin for robolectric ?

Thanks

Mike

Jake Wharton

unread,
Aug 15, 2013, 1:20:58 PM8/15/13
to robol...@googlegroups.com
People are beta testing it now. It'll have a 0.9 release today or tomorrow.

Mike Kulasinski

unread,
Aug 16, 2013, 7:15:43 AM8/16/13
to robol...@googlegroups.com
Hi Jake

Is it possible to get access to the beta? I would like to beta test it, we are currently in process of migrating from ant to gradle and this plugin would come very handy.


Thanks

Mike

Jake Wharton

unread,
Aug 17, 2013, 3:12:29 AM8/17/13
to robol...@googlegroups.com
https://github.com/square/gradle-android-test-plugin

Go nuts. Send pull requests not issues :)

Poly Takahiro Horikawa

unread,
Aug 17, 2013, 5:01:01 AM8/17/13
to robol...@googlegroups.com
Hi Jake,

Awesome work!
Do you have a plan to push it to maven central? It would be more helpful if all project members can use it without installing it into local maven repo.

Thanks,

Jake Wharton

unread,
Aug 18, 2013, 2:33:56 AM8/18/13
to robol...@googlegroups.com
It is already.

Nikolay Panfilov

unread,
Aug 19, 2013, 9:38:52 AM8/19/13
to robol...@googlegroups.com
Hi Jake

Thanks for your awesome work!
Is any chance that this plugin can be used in the Android Studio? 
Also can you pls extend your sample to support compat lib (com.android.support:appcompat-v7:18.0.0)? 

With added robolectric 2.2 (testCompile files('libs/test/robolectric-2.2-20130802.075524-31-jar-with-dependencies.jar')) 
I see next error:
com.cp.journey.activity.LoginActivityTest > testLogin FAILED
    android.view.InflateException at LoginActivityTest.java:32
        Caused by: java.lang.reflect.InvocationTargetException at LoginActivityTest.java:32
            Caused by: android.content.res.Resources$NotFoundException at LoginActivityTest.java:32
With robolectric 2.1 (testCompile 'org.robolectric:robolectric:2.1.+')
I see just NPE for the same source line

Thanks,
Nick

Markus Perndorfer

unread,
Aug 19, 2013, 1:03:37 PM8/19/13
to robol...@googlegroups.com
Could someone please help me with the setup in android studio? 

I know the tests aren't fully supported ATM, but is there a way to at least build them in android studio?

regards,
Markus


Markus Perndorfer

unread,
Aug 19, 2013, 1:07:57 PM8/19/13
to robol...@googlegroups.com
Nevermind, "mark folder as..."

Time to grok IntelliJ :D

Henrique Rocha

unread,
Aug 21, 2013, 1:12:09 PM8/21/13
to robol...@googlegroups.com
I'm having a real hard time putting this to work with Studio.

I've added the src/test/java folder to my project and right clicked "Mark directory as Source Test Root"
But Studio still can't find RobolectricTestRunner.

Using gradlew provided by the gradle-android-test-plugin from the command line, everything works as expected.

Has someone managed to make this work inside Android Studio?

Thanks in advance.



--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Henrique Rocha
Android Developer @ AndroidPIT.de

SuperJugy

unread,
Aug 21, 2013, 1:48:01 PM8/21/13
to robol...@googlegroups.com
Henrique, the plugin gradle-android-test-plugin is from a third party developer. with this plug in you can run tests FROM GRADLE, NOT FROM ANDROID STUDIO. Since android studio only knows the instrumentation tests not the robolectric tests. Android studio is not gradle so it can't understand the plugin. HOWEVER, this new plug in makes the tests when ever the task check is called. if i remember correctly, this check task is also called when you try to test from android studio. so if you run a test from studio, the robolectric tests will be run, except that Android studio wont know and wont display them. you can find the result of the tests manually in the build folder and review them.

TL;DR Android studio doesn't support Robolectric tests and the plugin is meant to be run from command line not Android studio.

Henrique Rocha

unread,
Aug 21, 2013, 4:06:55 PM8/21/13
to robol...@googlegroups.com

Hi SuperJugy, thanks for the reply. Actually my problem is not running the tests inside Studio. I don't mind having to run them from the CLI.

My problem is being able to write the tests inside Studio, because it's not finding Robolectric, neither FEST.

Obviously, I could do all imports by hand and always compile from the CLI, but I'm just asking if there is a way to compile the tests from Studio.

Thanks again.

SuperJugy

unread,
Aug 21, 2013, 4:25:25 PM8/21/13
to robol...@googlegroups.com
In that case, you just have to add the jars to the classpath.

go to file>project structure
then to libs and add a new lib and add the robolectric jar. (if you are using robolectric from a maven dependency you'll have to search it in the gradle downloaded files). add the library to the module you want to test and configure it to test only. this actually doesn't affect the building since the building is made by gradle and gradle does not see this configuration, this is just for the IDE to know where to find the jars and autocomplete the code and stuff. It wont compile the tests though, that is something that gradle does still.

Hope this helps.

Henrique Rocha

unread,
Aug 22, 2013, 9:36:51 AM8/22/13
to robol...@googlegroups.com
It's working perfectly. Thanks very much! =)

Nikolay Panfilov

unread,
Sep 3, 2013, 7:11:10 AM9/3/13
to robol...@googlegroups.com
Hi Jake

Can you pls or someone else help me with next problem:

If I'm trying to use appcompat lib and an activity, extended ActionBarActivity,
i got next error during tests:
java.lang.NullPointerException
	at java.util.regex.Matcher.getTextLength(Matcher.java:1140)
	at java.util.regex.Matcher.reset(Matcher.java:291)
	at java.util.regex.Matcher.<init>(Matcher.java:211)
	at java.util.regex.Pattern.matcher(Pattern.java:888)
	at org.robolectric.res.ResName.<init>(ResName.java:25)
	at org.robolectric.res.builder.RobolectricPackageManager.getActivityInfo(RobolectricPackageManager.java:68)
	at android.support.v7.app.ActionBarActivityDelegate.getUiOptionsFromMetadata(ActionBarActivityDelegate.java:157)
	at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:53)
	at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
	at com.pnv.simplerobolectricwgradle.MainActivity.onCreate(MainActivity.java:12)
	at android.app.Activity.performCreate(Activity.java:5008)

I tried include appcompat as library, but nothing changed.
I also tried use latest 2.2 Robolectric with the same result.

You can find my simple test project here: SimpleRobolectricWGradleProject
Also is any chance that I will be able to run/debug and see results of unit tests in Android Studio?

Thanks,
Nick

SuperJugy

unread,
Sep 3, 2013, 10:42:42 AM9/3/13
to robol...@googlegroups.com
Hi Nick

I'm using the same plugin and using the ActionBarActivity in my tests and they work fine. my only guess is that you should try not to use the @Config reportSdk = 18. Robolectric doesn't seem to support the appcompat library so must of the time it will return nulls or won't find resources. What I do is not use the reportSdk and everytime I see the ActionBarActivity returning me null, I simply mock that out. For example, every time I call getSupportActionBar I received null, so I inherited from my Activity a "testActivity", which would override all the methods that returned null to return a stub or a mock. So far I have faked the getSupportActionBar and getSupportFragmentManager.

On a side note, if you are using flavors with different package names each, Robolectric won't find the resources. If that is the case you can use a fix I pushed to my repo and sent a pull request, but it hasn't been merged with master. here is the link: https://github.com/square/gradle-android-test-plugin/pull/18

Hope it helps.

SuperJugy

daniell...@gmail.com

unread,
Jun 3, 2014, 11:47:41 AM6/3/14
to robol...@googlegroups.com
Hi SuperJugy,


I configured robolectric as seen in the deckard-gradle template, everything is fine except the fact that I get this error message when starting the robolectric tests via terminal:

java.lang.RuntimeException: android.content.res.Resources$NotFoundException: unknown resource 2131296262

I know that it's related to the different flavors I use in the build.gradle. I am already using a custom TestRunner for Robolectric, but it is not working anyway, I tried 2.3 + 2.4 SNAPSHOT.

My TestRunner looks like this:

@Override
protected AndroidManifest getAppManifest(Config config) {
    String manifestProperty = System.getProperty("android.manifest");
    if (config.manifest().equals(Config.DEFAULT) && 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));
    }
    AndroidManifest appManifest = super.getAppManifest(config);
    appManifest.setPackageName("com.example.android");
    return appManifest;
}

What am I doing wrong? My flavors are declared like that:

 flavors {
            flavor1 {
                packageName = "com.example.android"
                buildConfigField "String", "GCM_SENDER_ID", "\"807347333395\""
                buildConfigField "java.util.Locale", "locale", "java.util.Locale.GERMANY"
                buildName = "./gittag.sh ${name}".execute([], project.rootDir).text.trim()
                buildConfigField "String", "BUILD_NAME", "\"${buildName}\""
                versionName = "2.0.4"
                versionCode = 21
            }
}

It would be awesome if somebody could provide me some more informations on that! Thanks a lot!


SuperJugy

unread,
Jun 3, 2014, 6:45:58 PM6/3/14
to robol...@googlegroups.com
Daniel,

I have not seen the deckard-gradle template. But you may want to try this tutorial out: https://github.com/JCAndKSolutions/android-unit-test

It is not the same plugin, but it should work pretty much the same. Hope it helps.

SuperJugy.
Reply all
Reply to author
Forward
0 new messages