I used to be able to use roboelectric with gradle just fine until recently. I keep getting an error Error:(6, 17) error: package org.junit does not exist. I'm not quite sure and have dug into this quite a bit.
Below is my project build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
}
}
Below is my app build.gradle:
repositories {
mavenCentral()
jcenter()
}
apply plugin: 'com.android.application'
android {
...
sourceSets {
androidTest.setRoot('src/test')
}
}
dependencies {
...
// Testing
compile project(':core')
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'junit:junit:4.+'
testCompile 'org.easytesting:fest:1.0.16'
testCompile 'com.squareup:fest-android:1.0.8'
}
my core project build.gradle:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
}
I have read the below regarding this and nothing here has worked for me:
https://www.bignerdranch.com/blog/all-in-together-android-studio-gradle-and-robolectric/ - the android studio plugin that is to be used crashes on newer android studio versions.
https://www.bignerdranch.com/blog/triumph-android-studio-1-2-sneaks-in-full-testing-support/ - this simply doesn't solve the problem. It cannot find org.junit.
https://discuss.gradle.org/t/why-cant-my-simplest-one-line-build-gradle-compile-tests-with-junit-jar/1868
Can anybody point me in the right direction for this? Why is it not able to detect org.junit from the build.gradle?
You have to set the build variant test artifact to Unit Tests.
I no longer needed the "core" project, so I deleted it. My build.gradle for my app looks like this:
repositories {
jcenter()
}
apply plugin: 'com.android.application'
android {
...
sourceSets {
androidTest.setRoot('src/test')
}
}
dependencies {
...
// Testing
testCompile 'junit:junit:4.12'
testCompile 'org.easytesting:fest:1.0.16'
testCompile 'com.squareup:fest-android:1.0.8'
testCompile('org.robolectric:robolectric:3.0-rc2') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
}
last day i am also facing the problem.Atlast i got the solution.
The error is due to you created the java folder for your test class as java folder which appear blue color in android studio.Actually you need it in green color
For testing you just need to create folder with java name,everything else will do by android studio itself.
for removing java folder dependency you can remove
sourceSets {
androidTest.setRoot('src/test')
}
code from the build.gradle
then everything will work fine
Related
I was adding firebase to my project as documented in the official website.
In the 4th step it says to add compile 'com.google.firebase:firebase-core:16.0.0'.
But trying to synch gradle I would get errors:
and by trying to download them (install repository abd synch project) I would get this error:
here is my gradle dependencies:
dependencies {
implementation 'com.google.firebase:firebase-core:16.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.google.android.gms:play-services-gcm:15.0.1'
compile 'com.google.android.gms:play-services-location:15.0.1'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:multidex:1.0.2'
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.google.firebase:firebase-auth:11.6.2'
compile 'com.google.android.gms:play-services-auth:15.0.1'
compile 'com.google.code.gson:gson:2.7'
compile('io.socket:socket.io-client:1.0.0') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}
compile 'com.onesignal:OneSignal:3.6.5'
compile 'com.android.volley:volley:1.0.0'
testCompile 'junit:junit:4.12'
}
this answer did not work either:
this
update
My project-level gradle:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:4.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
mavenCentral()
google()
}
}
Upgrade the following:
implementation 'com.google.firebase:firebase-auth:11.6.2'
into this:
implementation 'com.google.firebase:firebase-auth:16.0.2'
Add google service plugin version 4.0.1 and google() repo in top level gradle file:
buildscript {
// ...
dependencies {
// ...
classpath 'com.google.gms:google-services:4.0.1' // google-services plugin
}
}
allprojects {
// ...
repositories {
// ...
google() // Google's Maven repository
}
}
Use same versions of firebase services to avoid conflicts.
Refer https://firebase.google.com/docs/android/setup
To solve your problem.
Before you proceed, clean and rebuild your project.
Then at app/build.gradle,
add apply plugin: 'com.google.gms.google-services'
like the code snippet down below.
android {
// ...
}
dependencies {
// ...
}
// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'
And make sure all the library used are as here.
Hope it helps!
Add firebase-core to your dependencies block:
implementation 'com.google.firebase:firebase-core:16.0.1'
The Firebase SDK release notes for the June 12 release explain:
Your app gradle file now has to explicitly list
com.google.firebase:firebase-core as a dependency for Firebase
services to work as expected.
It's also safer to list google() first in repository lists:
repositories {
google()
jcenter()
...
}
I had this error
Error:(45, 0) Gradle DSL method not found: 'implementation()'
Possible causes:<ul><li>The project 'LaTaxi2' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
Upgrade plugin to version 2.3.3 and sync project</li><li>The project 'LaTaxi2' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file</li><li>The build file may be missing a Gradle plugin.
Apply Gradle plugin</li>
build.gradle content
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven {
url 'https://maven.google.com'
}
// google()
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
/* CHANGE to classpath 'com.android.tools.build:gradle:2.3.3' for STABLE BUILD TOOL VERSION*/
// classpath 'com.android.tools.build:gradle:3.0.0-alpha7'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:3.1.0'
// We recommend changing it to the latest version from our changelog:
// https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
classpath 'io.fabric.tools:gradle:1+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven {
url 'https://maven.google.com'
}
// google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build .gradle module app
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven {
url 'https://maven.google.com'
}
// google()
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 25
buildToolsVersion '26.0.0'
defaultConfig {
applicationId "in.techware.lataxi"
minSdkVersion 17
targetSdkVersion 25
versionCode 5
versionName "1.0.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
/* Remove This to remove Crashlytics and Fabric */
compile('com.crashlytics.sdk.android:crashlytics:2.6.7#aar') {
transitive = true;
}
/* compile('com.digits.sdk.android:digits:2.0.6#aar') {
transitive = true;
}*/
implementation 'com.android.support:appcompat-v7:25.4.0'
implementation 'com.android.support:design:25.4.0'
implementation 'com.android.support:recyclerview-v7:25.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.android.support:cardview-v7:25.4.0'
implementation 'com.github.bumptech.glide:glide:3.8.0'
implementation 'com.google.android.gms:play-services-maps:11.0.2'
implementation 'com.google.android.gms:play-services-location:11.0.2'
implementation 'com.google.android.gms:play-services-places:11.0.2'
implementation 'com.google.firebase:firebase-auth:11.0.2'
implementation 'com.google.firebase:firebase-messaging:11.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta1'
implementation 'com.google.code.gson:gson:2.8.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
I had such a simple reason for this not working that I must post my answer to prevent anybody else going through this.
Don't put repositories and dependencies in the file called build.gradle (Project: YourProjectName)! There is a comment in that file that says:
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Good job to whoever put that note in there. Instead, place your repositories and dependencies in the similarly named module file build.gradle (Module: app).
There should already be a dependencies function. You may need to add repositories function. In my case, it was:
repositories {
maven { url "https://jitpack.io" }
}
This should let you sync and recognize implementation.
To use the DSL implementation() you have to use:
The updated gradle plugin for Android 3.0.0
The gradle version 3.4 or later
Then in your build.gradle you have to use:
buildscript {
repositories {
...
// You need to add the following repository to download the
// new plugin.
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta1'
}
}
In your gradle-wrapper.properties
distributionUrl=\
https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip
More detailed info here.
or if you don't want to update to the latest Gradle, go back to using DSL method compile() instead of implementation()
As a beginner, I discovered that there's 2 build.gradle files
It's worth checking that you didn't add the line in the wrong file.
I added mine in the project file whereas I should have added it in the module one.
You using Turkish workspace,
below change solves the problem
testImplementation -> testİmplementation,
androidTestImplementation -> androidTestİmplementation
androidTestImplementation -> androidTestİmplementation
implementation() has replaced compile() configuration, which will be DEPRECATED by the end of 2018.
In order to fix your errors and use it, you must update to Android Gradle Plugin 3.0.0 (or higher). As a result of this update, you also need to update Gradle to Gradle 4.1 (or higher). You also need to use Build Tools 26.0.2 (or higher), meaning to update your buildToolsVersion in your build.gradle, or just completely remove it from there (as, since 3.0.0, the minimum required version is used by default). You also need to update to Android Studio 3 (or higher) in order to use those advanced versions.
You can read about the changelog of android gradle plugin 3.0.0, the improved compile times, and more, here:
https://developer.android.com/studio/releases/gradle-plugin#3-0-0
So how to update Android Gradle Plugin and Gradle?
OPTION 1: Manually
In your build.gradle find your gradle classpath and update version to gradle 3.0. Also, google() maven's repository should be added:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
In gradle-wrapper.properties find your distributionUrl and update to:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
OPTION 2: Through project properties
(But notice the manual configuration WILL override it)
Go to File→Project Structure→Project
One last thing, you can also choose whether to replace compile() by implementation() or by api(). By default I would suggest just to use implementation(). You can read more about the differences here:
https://developer.android.com/studio/build/dependencies
In my case it was just a typo. It was an extra sign "/" after one of rows
I ran into similar errors. when I tried to include the recyclerview dependencies from may build.gradle(Module: app) with a code like so.
//other build.gradle(Module: App) code above
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
testCompile 'junit:junit:4.12'
implementation 'com.android.support:recyclerview-v7:25.3.1'
}
So the simple way I fixed this is by changing the code to this, where I changed the implement to compile while retaining every other code and their version numbers.
//other build.gradle(Module: App) code above
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:25.3.1'
}
I hope this helps some one having similar issues.
PS: The actual error was caused by implementation 'com.android.support:recyclerview-v7:25.3.1' and changing the code to compile 'com.android.support:recyclerview-v7:25.3.1' got it fixed.
in my case i add these line and issue resolved
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
ive recently upgraded to android studio 1.2.1.1 and all of a sudden when I run my project iam getting the error "local path doesnt exist"?
this is due to the fact that the debug .apk file is not in the location the IDE is looking.
local path: C:\Users\eoin_a\AndroidStudioProjects\XmppTest\app\build\outputs\apk\app-debug.apk
ive searched myself and the apk isn't there. In fact i cant seem to find the .apk file in my project at all?
Ive tried a number of solutions Ive seen on previously asked questions but Iam getting no joy here. Ive tried syncing with gradle and clean the project with no luck.
Ive tried in cmd "gradlew clean packageDebug" to no avail.
i would try adding this to the project .iml file
<option name="APK_PATH" value="/build/apk/apk file name" />
inside tags.
but I dont have a .apk file or I cant find it.
If anyone could help me out here that would be awesome!
also withing my dependencies in gradle I have gradle plugin version 1.2.3
classpath 'com.android.tools.build:gradle:1.2.3'
EDIT:
just thought id put up my gradle.build files as they may be of use. Iam still not sure whats going on
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.eoin_a.xmpptest"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile ("org.igniterealtime.smack:smack-android:4.1.0-rc1")
{
exclude(group: 'xpp3', module: 'xpp3')
exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
}
compile ("org.igniterealtime.smack:smack-tcp:4.1.0-rc1")
{
exclude(group: 'xpp3', module: 'xpp3')
exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
}
// optional features
compile ("org.igniterealtime.smack:smack-android-extensions:4.1.0-rc1")
{
exclude(group: 'xpp3', module: 'xpp3')
exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
}
compile 'javax.annotation:jsr250-api:1.0'
apt 'com.google.dagger:dagger-compiler:2.0'
compile 'com.google.dagger:dagger:2.0'
compile 'org.glassfish:javax.annotation:10.0-b28'
testCompile 'junit:junit:4.12'
testCompile "org.assertj:assertj-core:1.7.0"
testCompile('org.robolectric:robolectric:3.0-rc2') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
}
and
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenCentral()
}
}
Could you see if you have in this way your settings:
1.In build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
2.In gradle-wrapper.properties make sure to use gradle 2.2.1
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
3.Sync project with gradle files by pressing the button to the left of the avd button
4.Try to build project again. If still having issues possibly try File > Invalidate Caches/Restart
Doing this way worked for me
I follow this https://stackoverflow.com/a/19496969/2091181
i am new to android studio and wants to include boofcv library in my project. I am using Android studio for development. I have done the following steps in order to include the library and is stuck with build.gradle configuration.
Step 1: Have downloaded per-compiled jar files from http://boofcv.org/index.php?title=Download:BoofCV
Step 2: Have updated settings.gradle as
include ':app'
include ':libs:boofcv-libs'
Step 3: My build.gradle looks like:
apply plugin: 'com.android.application'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
As the note of your project's build.gradle file will suggest:
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Remove the compile statements in that gradle file:
compile project(":libs:boofcv-libs")
And copy them to other (module's) build.gradle and make dependencies look like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.+'
compile project(":libs:boofcv-libs")
}
BoofCV is on maven central so you could just do the following too:
['calibration','feature','geo','ip','recognition','sfm','android'].each
{ String a -> compile group: 'org.boofcv', name: a, version: '0.18' }
In the next it will be even easier if you just want everything:
compile group: 'org.boofcv', name: "all", version: '0.19-SNAPSHOT'
Get latest version from this page
https://boofcv.org/index.php?title=Download
Convert maven to to gradle using this website and make sure to
change artifactId to boofcv-android:
http://sagioto.github.io/maven2gradle
so it will be something like this:
compile "org.boofcv:boofcv-android:0.27"
as mentioned in this page, to avoid library conflict add this to app.gradle:
// Remove libraries that conflict with libraries already included with Android
configurations {
all*.exclude group: "xmlpull", module: "xmlpull"
all*.exclude group: "org.apache.commons", module: "commons-compress"
}
I'm trying to use Robolectric in a project build with gradle inside the new Ide for android: Android studio, but I'm facing a strange problem, I've correctly imported all the libraries and created the "test" folder inside "src", the fact is that whenever I run the tests the ide keep saying "Class not found: "com.example.myandroidproject.test" what I'm doing wrong? i need to change something in the gradle.build? here's my directory structure:
#Aldo Borrero, finally it seems that someone has found the way to test android projects under "Android Studio" using Robolectric and Gradle.
Please, take a look at this answer Robolectric with Gradle
Update:
The guys from square have released a plugin to make Robolectric work out of the box with Gradle and Android Studio, this feature will be integrated with Robolectric in v2, meanwhile you can grab the plugin here: Gradle Android test Plugin
I tried different appraoaches to combine android studio & robolectric & espresso. I ended with this example project setup https://github.com/nenick/android-gradle-template
Here some explanation for the different approaches:
application module + espresso + robolectric
There is an example https://github.com/robolectric/deckard-gradle supported by robolectric maintainers.
This is based on the plugin https://github.com/robolectric/gradle-android-test-plugin. But this have a drawback with dependency pollution reported at https://github.com/robolectric/gradle-android-test-plugin/issues/17 which results in slow esspresso tests compile time and execution time.
build.gradle snippet which combines all
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.+'
}
}
apply plugin: 'android'
apply plugin: 'android-test'
android {
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
}
androidTest {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}
dependencies {
androidTestCompile('junit:junit:4.11')
androidTestCompile('org.robolectric:robolectric:2.3-SNAPSHOT')
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}
seperate espresso
An example is shown by https://github.com/stephanenicolas/Quality-Tools-for-Android but it is much outdated and had also some drawbacks. It will recompile
and makes android studio behave strange. It flags application module sources as (root source) of the espresso test module. That works but not intuitive.
build.gradle snippet for espresso module
dependencies {
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}
android {
sourceSets {
main {
manifest.srcFile '../AndroidSample/AndroidManifest.xml'
java.srcDirs += ['../AndroidSample/src/main/java']
resources.srcDirs = ['../AndroidSample/res']
res.srcDirs = ['../AndroidSample/res']
}
}
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
}
spereate robolectric
There exist a plugin https://github.com/novoda/gradle-android-test-plugin which enable us to put robolectric tests in a sperate package. This project
setup works for me great:
- MyProject
|- app (with espresso tests)
|- - build.gradle (app)
|- robolectric (unit tests)
|- - build.gradle (robo)
build.gradle (app + espresso) snippet
dependencies {
androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r2'
}
android {
defaultConfig {
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
}
build.gradle (robo) snippet
buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
classpath "com.novoda:gradle-android-test-plugin:0.9.8-SNAPSHOT"
}
}
android {
projectUnderTest ':AndroidSample'
}
apply plugin: 'java'
apply plugin: 'android-test'
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'com.squareup:fest-android:1.0.+')
testCompile ('org.robolectric:robolectric:2.3-SNAPSHOT')
}
There a some pitfall when you try setup this project setup, so just start with a working example: https://github.com/nenick/android-gradle-template
This is unlikely to work out of the box as src/test isn't used automatically.
You'd need to create a test task automatically that compiles this source sets, sets the right dependencies and run it.
We intend to support this in the future but right now you'd need to do this manually.
I tested all the solutions presented here and they all lack of something (version of gradle / gradle plugin not supported, library project not supported, no integration with Android studio, etc). It may not be true in the future but it is today.
The best way I found is to configure the unit tests by yourself. You will need to add a few lines of config to your build.gradle file. Explications are on the following article: http://tryge.com/2013/02/28/android-gradle-build/. Since I'm not the author, I don't think I can copy past the content here directly.
In addition to that article, if you want configure Android Studio to see the unit test folder as a source folder (autocompletion and stuff), you can apply the following little dirty hack and let the IDE think that the unit tests are located in the instrumentationTest folder. Of course, it will mess with your real instrumentation tests so it works only if you don't have any of those.
build.gradle
// the unit test source set as described in the article
sourceSets {
unitTest {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
android {
// tell Android studio that the instrumentTest source set is located in the unit test source set
sourceSets {
instrumentTest.setRoot('src/test')
}
}
dependencies {
// your unit test dependencies as described in the article
unitTestCompile files("$project.buildDir/classes/release")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'com.google.android:android:4.1.1.4'
unitTestCompile 'org.robolectric:robolectric:2.1.1'
// duplicate these dependencies in the instrumentTestCompile scope
// in order to have the integration in Android Studio (autocompletion and stuff)
instrumentTestCompile 'junit:junit:4.11'
instrumentTestCompile 'org.robolectric:robolectric:2.1.1'
}
// the rest of the config as described in the article
Tested with Android Studio 0.2.6 and android gradle plugin 0.5.
Gradle Android Unit Testing Plugin is the best option for me.
Developed by Jake Wharton, I guess it is going to be the next standard (maybe until google releases an out of the box support for Robolectric in Android Studio).
You can import the library by adding in your build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.X.+'
classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.+'
}
}
...
apply plugin: 'android-test'
...
dependencies {
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.1.+'
testCompile 'com.squareup:fest-android:1.0.+'
}
Update: This library has been deprecated since gradle plugin version 0.8
I've tested a lot of scenarios (like http://tryge.com/2013/02/28/android-gradle-build/ and http://www.peterfriese.de/android-testing-with-robolectric/) but only the solution provided by the Robolectric team worked for me. The setup uses instrumented and robolectric tests in one Android project with gradle as build system.
See http://robolectric.org/getting_started/
and the sources on
https://github.com/robolectric/deckard-gradle
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.4'
}
}
allprojects {
repositories {
mavenCentral()
}
}
apply plugin: 'android'
apply plugin: 'android-test'
android {
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
}
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 18
targetSdkVersion 18
versionCode 2
versionName "1.0.0-SNAPSHOT"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
}
androidTest {
setRoot('src/test')
}
}
}
androidTest {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}
dependencies {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
// Espresso
androidTestCompile files('lib/espresso-1.1.jar', 'lib/testrunner-1.1.jar', 'lib/testrunner-runtime-1.1.jar')
androidTestCompile 'com.google.guava:guava:14.0.1',
'com.squareup.dagger:dagger:1.1.0',
'org.hamcrest:hamcrest-integration:1.1',
'org.hamcrest:hamcrest-core:1.1',
'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:2.3-SNAPSHOT') {
exclude module: 'classworlds'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-plugin-registry'
exclude module: 'maven-profile'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'nekohtml'
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-http-shared'
exclude module: 'wagon-provider-api'
}
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
apply plugin: 'idea'
idea {
module {
testOutputDir = file('build/test-classes/debug')
}
}