Possible duplicate of this question, although the solution did not help me.
I hate copy/paste all source code but seems in gradle there is no way :( since this is the 3rd day that gradle staffing me around I put my codes here and my apology for putting so much code...
I have three projects in my main project. Therefore my settings.gradle looks like this:
include ':booking-sdk'
include ':booking-app-lib'
include ':booking-app'
and my main build.gradle (in root of project) looks like this:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.1.1'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
maven { url 'https://zendesk.artifactoryonline.com/zendesk/repo' }
}
}
ext {
ANDROID_SUPPORT = 'com.android.support:support-v4:20.0.0'
CRASHLYTICS = 'com.crashlytics.android:crashlytics:1.+'
androidConfiguration = {
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
packagingOptions {
exclude 'values/com_crashlytics_export_strings.xml'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'LICENSE.txt'
}
lintOptions {
abortOnError false
absolutePaths false
lintConfig file("lint.xml")
}
sourceSets {
main {
manifest.srcFile 'src/main/AndroidManifest.xml'
java.srcDirs = ['src/main/java']
resources.srcDirs = ['src/main/resources']
aidl.srcDirs = ['src/main/java']
renderscript.srcDirs = ['src/main/java']
res.srcDirs = ['src/main/res']
assets.srcDirs = ['src/main/assets']
java {
exclude 'com/booking_1/passenger/LibraryConfigurationConstants.java'
exclude 'com/booking_2/passenger/ProductFlavorConstants.java'
}
}
}
signingConfigs {
debug {
storeFile file("$rootProject.projectDir/debug.keystore")
storePassword "android"
keyAlias "androiddebugkey"
keyPassword "android"
}
release {
storeFile file("$rootProject.projectDir/booking-androd-prod.keystore")
storePassword System.getenv("PASSWORD")
keyAlias System.getenv("ALIAS")
keyPassword System.getenv("PASSWORD")
}
}
}
}
For doing Unit test by Robolectric and Instrument test I am following Decard-Gradle project that I tested and works successfully.
booking-sdk is the project that holds business logic of app. It's structure is like this:
-booking-sdk
-/build
-/src
- booking_1 (flavour 1)
- booking_2 (flavour 2)
- main
- test
- build.gradle
I have same package name under /src/main/java and /src/test/java. Finally, build.gradle of booking-sdk looks like this:
apply plugin: 'com.android.library'
android androidConfiguration
android {
publishNonDefault true
productFlavors {
booking_1 { }
booking_2 { }
}
}
dependencies {
repositories {
mavenCentral()
}
compile ANDROID_SUPPORT
compile CRASHLYTICS
compile 'com.google.code.gson:gson:2.3'
compile 'com.squareup.retrofit:retrofit:1.7.0'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.squareup.okhttp:okhttp:2.1.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0'
compile 'com.squareup.picasso:picasso:2.4.0'
compile 'com.squareup:otto:1.3.5'
compile 'org.apache.commons:commons-lang3:3.3.2'
compile files('src/main/libs/GeoPIP4J.jar')
// Robolectric
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
testCompile 'org.hamcrest:hamcrest-core:1.1'
testCompile 'org.hamcrest:hamcrest-library:1.1'
testCompile 'org.hamcrest:hamcrest-integration:1.1'
// TODO: requires special build of robolectric right now. working on this...
androidTestCompile('org.robolectric:robolectric:2.4') {
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'
}
}
When I run clean command the result is:
./gradlew :booking-sdk:clean
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for booking_1DebugUnitTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for booking_1DebugUnitTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for booking_1ReleaseUnitTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for booking_1ReleaseUnitTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for booking_1DebugUnitTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for booking_1DebugUnitTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency commons-logging:commons-logging:1.1.1 is ignored for booking_1ReleaseUnitTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.3 is ignored for booking_1ReleaseUnitTest as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
:booking-sdk:clean
BUILD SUCCESSFUL
Total time: 4.83 secs
Error happens when I run ./gradlew :passenger-sdk:check code to see unit test result. I'm getting above outputs first then a lot of errors like this:
...
:booking-sdk:compileBookingDebugUnitTestJava
/Users/admin/Desktop/android/booking/booking-sdk/src/test/java/com/booking/passenger/db/dao/BookingDAOTest.java:3: error: package com.booking.passenger.db.providers does not exist
import com.booking.passenger.db.providers.BookingContentProvider;
It is basically saying all my imports do not exist while they do exists and I have no error in my code file and all imports recognised by class.
Any Idea would be appreciated. Thanks.
OK, Geadle is f*king horrible although is powerful tool.
For your reference if you fall in same trouble this is my experience. Since I'm not sure my findings are absolutely right please correct me if I'm wrong.
I found that it is not possible to put your test folder under library projects. I wanted to have src/test/java for every project libraries and run my test cases like ./gradlew :booking-sdk:test. However, by moving this package to main application (in my case booking-app) and also moving related files from build.gradle of booking-sdk to build.gradle of booking-app I could see test case results.
I found exclude modules from dependencies don't work. The solution that I found is using configuration.
Like this:
configurations {
all*.exclude module: 'classworlds'
all*.exclude module: 'commons-logging'
all*.exclude module: 'httpclient'
all*.exclude module: 'maven-artifact'
all*.exclude module: 'maven-artifact-manager'
all*.exclude module: 'maven-error-diagnostics'
all*.exclude module: 'maven-model'
all*.exclude module: 'maven-project'
all*.exclude module: 'maven-settings'
all*.exclude module: 'plexus-container-default'
all*.exclude module: 'plexus-interpolation'
all*.exclude module: 'plexus-utils'
all*.exclude module: 'wagon-file'
all*.exclude module: 'wagon-http-lightweight'
all*.exclude module: 'wagon-provider-api'
}
Hope it helps.
Related
I'm trying to add the following dependency but it is ignore. I can't understand how to resolve it please help me Thank you.
Dependency
compile 'com.google.apis:google-api-services-drive:v2-rev170-1.20.0'
Waring for ignoring dependency
Warning:Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for debug as it may be conflicting with the internal version
provided by Android.
In case of problem, please repackage it with jarjar to change the class packages Warning:Dependency
org.apache.httpcomponents:httpclient:4.0.1 is ignored for release as
it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
You can exclude dependencies in the build.gradle file of your module.
compile('com.google.apis:google-api-services-drive:v2-rev170-1.20.0') {
exclude module: 'httpclient' //by artifact name
exclude group: 'org.apache.httpcomponents' //by group
exclude group: 'org.apache.httpcomponents', module: 'httpclient' //by both name and group
}
Exclude module httpclient from all configurations. Add this code in the build.gradle file:
configurations {
compile.exclude group: "org.apache.httpcomponents", module: "httpclient"
}
Use the following code to exclude the conflicting modules from the google APIs library.
compile 'com.google.apis:google-api-services-drive:v2-rev170-1.20.0' {
exclude module: 'httpcore'
exclude module: 'httpclient'
}
I'm new to Android Studio so do not know how gradle works. Maybe it is a silly question.
Whenever I tried to import the GitHub project as module in my project and try to build the project, I always get this error:
Could not find property 'GROUP' on project build.gradle
I know Group need to recognize the project name. I tried to find the answer for this question but there is not much reference available, so I am posting the question here.
Recently I tried to import Fresco as module in project and got the same error.
Question is
Why am I getting this error?
How does the gradle build project?
Here is my build.gradle:
apply plugin: 'com.android.library'
apply plugin: 'maven'
project.group = GROUP
version = VERSION_NAME
dependencies {
compile "com.android.support:support-v4:${SUPPORT_V4_VERSION}"
provided "com.google.code.findbugs:jsr305:${JSR_305_VERSION}"
provided "javax.annotation:javax.annotation-api:${ANNOTATION_API_VERSION}"
compile project(':fbcore')
testCompile "junit:junit:${JUNIT_VERSION}"
testCompile "org.mockito:mockito-core:${MOCKITO_CORE_VERSION}"
testCompile("org.robolectric:robolectric:${ROBOLECTRIC_VERSION}") {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
}
apply from: rootProject.file('release.gradle')
android {
buildToolsVersion rootProject.ext.buildToolsVersion
compileSdkVersion rootProject.ext.compileSdkVersion
packagingOptions {
exclude 'NOTICE'
exclude 'LICENSE'
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
}
}
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
artifacts.add('archives', sourcesJar)
Replace GROUP and VERSION_NAME with actual values, for example:
project.group = 'the.group.of.my.module'
version = '1.0'
EDIT: you can write value project.group anything. but it should unique and identify your module name. in my case i wrote project.group = 'com.facebook.fresco'
Hope it will help future Visitors and save their time.
Credited goes to #Henry
I would like to generate a lint-results.xml file when building my project in Android studio. I need to be able to do this in order to use the following: https://github.com/KeepSafe/android-resource-remover
I have also tried building the project from the command line but I get the following error:
FAILURE: Build failed with an exception.
What went wrong:
Task 'lint' not found in root project 'client-android'. Some candidates are: 'init'.
Try:
Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Here are my gradle files:
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
apply plugin: 'idea'
apply plugin: 'bugsnag'
android {
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
}
lintOptions {
// set to true to turn off analysis progress reporting by lint
quiet true
// if true, stop the gradle build if errors are found
abortOnError false
// if true, only report errors
ignoreWarnings true
}
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.mango.android"
minSdkVersion 14
targetSdkVersion 21
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
androidTest {
setRoot('src/test')
}
}
}
robolectric {
include '**/*Test.class'
}
dependencies {
repositories {
mavenCentral()
}
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.bugsnag:bugsnag-android:+'
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:+'
compile files('libs/android-sqlite-asset-helper.jar')
compile('com.google.inject:guice:4.0-beta:no_aop') { exclude module: 'asm' }
compile('org.roboguice:roboguice:2.0') { exclude module: 'asm' }
compile 'com.github.nirhart:parallaxscroll:1.0' // For parallax views
androidTestCompile 'junit:junit:4.11'
androidTestCompile files('libs/android-sqlite-asset-helper.jar')
androidTestCompile('org.robolectric:robolectric:2.4') {
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'
}
androidTestCompile 'com.google.android:android:4.1.1.4'
androidTestCompile 'org.mockito:mockito-core:1.9.5'
compile 'com.google.android.gms:play-services:6.5.87'
}
idea {
module {
testOutputDir = file('build/test-classes')
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.bugsnag:bugsnag-android-gradle-plugin:+'
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'org.robolectric:robolectric-gradle-plugin:0.14.0'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
This is how you can export Lint XML in Android Studio 1.3:
At top, click "Analyze"
Select "Inspect Code"
Click OK.
An Inspection panel should open at the bottom (if not, you should be able to click on "Inspection" at the bottom left to open this).
Select "Android Lint" from the list so that it is highlighted.
Click the icon with the green up arrow (second from bottom left) to Export.
Select XML format, and choose the folder you want to save the results to.
I'm not sure if Android Studio has always included Lint, or if Android Studio began including Lint at some later date. You may wish to go to Help -> About to check which version you are running. If you are running an older version, go to Help -> "Check for Updates" to update Android Studio.
Simply try this (on Windows 10) in your project folder cmd line:
./gradlew lintRelease
My build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.stanfy.spoon:spoon-gradle-plugin:0.10.0'
}
}
apply plugin: 'android-sdk-manager'
apply plugin: 'android'
apply plugin: 'spoon'
apply plugin: 'robolectric'
android {
compileSdkVersion 19
buildToolsVersion '20.0.0'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
testInstrumentationRunner 'com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner'
}
buildTypes {
debug {
}
release {
}
}
productFlavors {
first {
//just version codes and packages
}
second {
//just version codes and packages
}
third {
//just version codes and packages
}
}
}
spoon {
debug = true
}
dependencies {
compile project(':app:libs:facebookSDK')
compile 'com.google.android.gms:play-services:5.0.89'
compile 'com.android.support:support-v4:20.0.0+'
compile 'com.google.code.gson:gson:2.2.4'
compile fileTree(dir: 'libs', include: '*.jar')
androidTestCompile fileTree(dir: 'libsTest', include: '*.jar')
androidTestCompile 'com.squareup.spoon:spoon-client:1.1.0'
androidTestCompile 'junit:junit:4.+'
androidTestCompile 'org.robolectric:robolectric:2.3'
}
libsTest: espresso-contrib-1.1-bundled.jar
Now, my error is following:
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/SelfDescribing;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
More detailed log available here:
http://pastebin.com/Yye3cd1c
How do I fix this issue?
UPD:My question now is basically how do I hunt those duplicated parts?
My question now is basically how do I hunt those duplicated parts?
You do this using the log. The log you pasted says:
com.android.dex.DexException: Multiple dex files define
This is most probably due to the conflicting libraries. The log continues with:
Lorg/hamcrest/SelfDescribing;
Here is the conflicting library, hamcrest.
After adding dependencies to your project, if there are common sub-libraries used by your libraries (or as your libraries), this error occurs. So it seems like hamcrest is being used by not only one of your libraries.
We'll learn about this conflict by inspecting dependencies. Of course an efficient inspection needs to be both intuitive and rational at the same time.
Let's start with hamcrest itself. (I'll assume you haven't heard of hamcrest before.)
Let's take a look at Hamcrest project page. Hamcrest defines itself as library of matchers for building test expressions. And the definition says Typical scenarios include testing frameworks, mocking libraries... This is illuminating because you have some dependencies about testing like JUnit, Espresso and Robolectric.
Now we should continue with JUnit dependencies. Seems like JUnit makes use of hamcrest-core. Here is our first hamcrest as a sub-dependency.
Let's continue with Espresso, as you have espresso-contrib-1.1-bundled.jar in libsTest folder.
When we check out espresso-contrib project dependencies, we can see that Espresso makes use of hamcrest heavily.
We probably get the conflicting library in your project, the last step is excluding hamcrest-core from one of our dependencies while adding this dependency. You can achieve this with:
androidTestCompile('junit:junit:4.+') {
exclude module: 'hamcrest-core'
}
I had a similar issue a while ago and if I remember correctly, I fixed it by adding an exclude statement as follows:
androidTestCompile('junit:junit:4.+') {
exclude module: 'hamcrest-core'
}
Inspired from the deckard-gradle sample project, I suggest trying something like this:
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:2.3') {
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'
}
One trick to hunt those errors is using Android Studio "Navigate > Class..." option.
Given you have this error:
Lorg/hamcrest/SelfDescribing;
then you can search "org.hamcrest.SelfDescribing" so you can have an idea of the .jars using it.
I've been trying to do it for a few days, with no result. I need to set up Robolectric in Android Studio (0.8.9, latest version).
I followed different tutorials Android Unit and Integration testing, Roboelectric installation for Unit testing, Android Gradle app with Roboelectric, How to run Roboelectric JUnit tests but always got some kind of error.
So I created module specially for testing:
Kedzoh (Project) build.gradle:
// Top-level build file where you can add configuration options common to all sub- projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
}
}
allprojects {
repositories {
jcenter()
}
}
app build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
applicationId 'com.dev.kedzoh'
minSdkVersion 9
targetSdkVersion 19
versionCode 14
versionName '1.6.7'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.google.android.gms:play-services:4.2.42'
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:19.+'
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'
}
kedzoh-tests build.gradle:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'
testCompile 'junit:junit:4.+'
testCompile 'org.robolectric:robolectric:2.2'
}
At this point I cannot import Robolectric classes, it gives me error. When I add apply plugin: 'robolectric' to kedzoh-tests build.gradle, it asks for 'android' plugin. After I add it, it complains there is no Manifest and fails to build.
I'm not sure that this is the right configuration, since it never worked really. Could anyone give some advice how to set Robolectric in Android Studio, please?
EDIT:
I tried the answer below, but still stuck with "Class not found" error:
There already different project templates for robolectric. Did you try https://github.com/nenick/android-gradle-template ?
I think you maybe making your life more difficult and complicated by creating your test module outside of the main 'app' module. Most of the tutorials I have seen have an androidTest folder in the app module that can contain your Robolectric tests. All of the older tutorials based around Eclipse I have seen, seem to instruct us to create test modules separate to the main project.
If I were using Robolectric I would setup the project like I have created for you https://github.com/marcthomas2013/Kedzoh. I would use the androidTest folder to put my tests and configure the gradle files as follows.
Note that the dependencies for the tests are included using the androidTestCompile declaration and application dependencies are declared using just compile.
Another item in this gradle file is the robolectric section where it is including and excluding classes in the androidTest folder. Anything that isn't in the espresso folder will be run using the gradlew test target and everything including the espresso folder will be run when calling gradlew connectedAndroidTest target.
app gradle file
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
android {
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
}
compileSdkVersion 19
buildToolsVersion "19.1.0"
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 {
androidTest {
setRoot('src/androidTest')
}
}
}
robolectric {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}
dependencies {
repositories {
mavenCentral()
}
compile 'com.sothree.slidinguppanel:library:2.0.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
// Espresso
androidTestCompile files('libs/espresso-1.1.jar')
androidTestCompile files('libs/testrunner-1.1.jar')
androidTestCompile files('libs/testrunner-runtime-1.1.jar')
androidTestCompile 'com.google.guava:guava:14.0.1'
androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('org.robolectric:robolectric:2.3') {
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'
}
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
project build file
Here we include the robolectric class path so that we can use the robolectric gradle commands in the config file above.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
Can you explain what errors that you are having as there could be many possible errors based on how you are working, also is there a particular reason for creating a separate test module.
The project that I have provided is based on the deck-gradle setup (https://github.com/robolectric/deckard-gradle) there are a number of tips in here about JUnit conflicts etc. that you could be having trouble with.
Once you have got this far in order to be able to debug the unit test in the application you will have to manually tweak the classpath when launching the test (Not Pretty at all!), based on the information in this article https://github.com/codepath/android_guides/wiki/Robolectric-Installation-for-Unit-Testing
You will need to run your unit test, wait for it to fail, copy the -classpath parameter and it's value (It will be quite long and starts and finishes with ") and copy this into your VM Options in your run configuration, then at the end of that classpath add a ; or a : depending on your OS path delimeter and add the path to your test classes which will be something like <ABSOLUTE_PATH_TO_PROJECT>/build/test-classes this will add your test classes to the class path and then Android Studio should be able to run them.
This is also assuming that the steps to run the gradle testClasses has been done from the article http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/