I have tried almost every trick in the book.
ResolutionStrategy.force
Excluding modules
But nothing seems to work, below is my build.gradle. I'm using Gradle version 1.2.3. Can someone please throw light on what else could be wrong with my code.
The only thing I haven't tried is changing version of Gradle.
It's a very basic Espresso Test case. Thanks!
apply plugin: 'com.android.application'
android {
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.rasika.job"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
I forked android-topeka google sample and updated appcompat version to 23.1.0, same message:
Warning:Conflict with dependency
'com.android.support:support-annotations'. Resolved versions for app
(23.1.0) and test app (23.0.1) differ.
I added:
androidTestCompile 'com.android.support:support-annotations:23.1.0'
Now both resolve to 23.1.0, the warning is gone, and the app and tests still work.
I'm not sure that it's the better solution, so I'm searching for another but found your question.
Update: Read this good explanation by PaulR.
Update2: Confirmed, android-testing google sample does it.
// Testing-only dependencies
// Force usage of support annotations in the test app, since it is internally used by the runner module.
androidTestCompile 'com.android.support:support-annotations:23.0.1'
Update3: Another good response by CommonsWare.
Check your specific versions/conflicts/resolutions using:
./gradlew -q yourmodule:dependencies
Appcompat is 22.1.1 in your case but you are forcing 22.1.0.
Update4:
Dependency conflict explained at The Android Build System (Android Dev Summit 2015).
Resolving conflicts between main and test APK
When instrumentation tests are run, both the main APK and test APK
share the same classpath. Gradle build will fail if the main APK and
the test APK use the same library (e.g. Guava) but in different
versions. If gradle didn't catch that, your app could behave
differently during tests and during normal run (including crashing in
one of the cases).
To make the build succeed, just make sure both APKs use the same
version. If the error is about an indirect dependency (a library you
didn't mention in your build.gradle), just add a dependency for the
newer version to the configuration ("compile" or "androidTestCompile")
that needs it. You can also use Gradle's resolution strategy
mechanism. You can inspect the dependency tree by running ./gradlew
:app:dependencies and ./gradlew :app:androidDependencies.
I solved the conflict by adding dependency:
androidTestCompile 'com.android.support:support-annotations:23.2.0'
I had the same issue, solved by this:
// build.gradle
...
android {
...
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
...
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2') {
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.3') {
// Necessary if your app targets Marshmallow (since the test runner
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
}
solution was found here:
https://github.com/codepath/android_guides/wiki/UI-Testing-with-Espresso
UPDATE:
finally dependencies block in my build.gradle looks like this:
dependencies {
...
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
compile 'com.android.support:design:23.2.1'
...
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
exclude group: 'com.android.support'
}
androidTestCompile('com.android.support.test.espresso:espresso-intents:2.2.2') {
exclude group: 'com.android.support'
}
androidTestCompile('com.android.support.test:runner:0.5') {
exclude group: 'com.android.support'
}
androidTestCompile('com.android.support.test:rules:0.5') {
exclude group: 'com.android.support'
}
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
exclude group: 'com.android.support'
}
androidTestCompile('com.android.support:support-annotations:23.2.1') {
exclude group: 'com.android.support'
}
androidTestCompile('com.android.support.test.uiautomator:uiautomator-v18:2.1.2') {
exclude group: 'com.android.support'
}
}
This happened to me recently when adding the uiautomator. To fix this issue, you need to figure out which dependency or dependencies is using the outdated module. You can do this by wrapping each androidTestCompile dependency into a block, like so:
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2') {
transitive = false;
}
This might break some other things though, so you need to be careful. I was able to pinpoint exactly which two of the dependencies was causing this issue for me, and just adding this blocking mechanism to those.
I solved the conflict by excluding the support-annotation library from both runner and espresso-core dependencies:
androidTestCompile 'com.android.support.test:runner:0.5',{
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2'){
exclude group: 'com.android.support', module: 'support-annotations'
}
Add follow codes to your dependency block in build.gradle file
compile 'com.android.support:support-annotations:23.2.1'
testCompile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support:support-annotations:23.2.1'
Add this to your main build.gradle:
allprojects {
...
configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:23.1.1'
}
...
}
androidTestCompile change to testCompile. And remind dont't change it to compile, just need this dependencies to be compiled into our debug APK or test APK.
For me this worked fine
dependencies {
androidTestCompile 'com.android.support:support-annotations:23.1.1'
}
I solved the conflict by adding dependency:
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
...
androidTestCompile 'com.android.support:support-annotations:23.2.1'
In also got into the Problem saying
Could not resolve
com.android.support:support-annotations:23.1.0
and tried to find in other servers ,
But what resolved my problem is adding :
google-service.json
file from
https://developers.google.com/mobile/add
and copy and paste it into
YourAndroidProject/app
Then recompile it and i hope your code will fly
Use this for resolving conflict
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
I solved the conflict by adding dependency:
androidTestCompile "com.android.support:support-annotations:26.0.0-beta1"
Related
I get the following error when I add Firebase analytics in my Android Studio project:
"Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/measurement/AppMeasurementContentProvider;"
Id really be thankful if someone tells me where the additional AppmeasurementContentProvider is coming from and the corresponding "exclude" command.
Here is my build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 21
buildToolsVersion "22.0.1"
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/maven/com.google.guava/guava/pom.properties'
exclude 'META-INF/maven/com.google.guava/guava/pom.xml'
exclude 'com.google.android.gms.measurement'
}
defaultConfig {
applicationId "com.elisiumlabs.sarthi"
minSdkVersion 10
targetSdkVersion 19
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile project(':androidQuery')
compile project(':pinnedSectionListActivity')
compile project(':volley')
compile project(':facebookSDK')
compile 'com.google.code.gson:gson:2.2.4'
compile 'joda-time:joda-time:2.3'
compile 'com.android.support:gridlayout-v7:21.0.0'
compile "com.google.android.gms:play-services-location:8.3.0"
// compile "com.google.android.gms:play-services-gcm:8.3.0"
compile 'com.android.support:appcompat-v7:21.0.3'
compile('com.google.android.gms:play-services-gcm:8.3.0') {
exclude group: 'com.google.android.gms.measurement'
}
compile('com.google.firebase:firebase-core:9.0.2')
{
exclude group: 'com.google.android.gms.measurement'
}
/*compile files('libs/appcompat_v7.jar')*/
compile files('libs/commons-io-1.4.jar')
compile files('libs/commons-lang-2.4.jar')
compile files('libs/httpclient-4.3.5.jar')
/*compile files('libs/objectify-5.0.3.jar')*/
compile 'commons-codec:commons-codec:1.9'
compile files('libs/splunk-mint-4.1.jar')
testCompile 'junit:junit:4.12'
// androidTestCompile 'com.android.support:support-annotations:21.0.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
// Optional -- Hamcrest library
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
// Optional -- UI testing with Espresso
/* androidTestCompile('com.android.support.test.espresso:espresso- core:2.2.1') {
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}*/
androidTestCompile('com.android.support.test.espresso:espresso- contrib:2.2.2') {
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'javax.inject'
exclude group: 'com.android.support'
}
androidTestCompile('com.android.support.test.espresso:espresso-web:2.2.2'){
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
// Optional -- UI testing with UI Automator
androidTestCompile('com.android.support.test.uiautomator:uiautomator-v18:2.1.1'){
// Necessary if your app targets Marshmallow (since the test runner
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
}
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:23.0.1'
}
}
Configuring Your App for Multidex with Gradle
reference https://developer.android.com/studio/build/multidex.html#mdex-gradle
Modify the module-level build.gradle file configuration to include the support library and enable multidex output, as shown in the following code snippet:
android {
compileSdkVersion 21
buildToolsVersion "21.1.0"
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex. For more information, see the MultiDexApplication reference documentation.
Try to my answer....
here is the link
Android Studio Gradle Error:Execution failed for task ':app:dexDebug' using Ion koush lib
Please take the Google service as my answer and also add to your gradle some code as shown also add some code in your mainfiest file
And also true the multidexenable with comile the multidex as shown in answer
First Compile the build with
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.android.gms:play-services:+'
In Your AndroidManifest.xml add this lines android:name
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:name="android.support.multidex.MultiDexApplication"
>
And In your build.gradle also add
dexOptions {
//incremental = true;
preDexLibraries = false
javaMaxHeapSize "4g"
}
I really don't think you could mix GCM and FCM, because FCM is the new GCM. Logically, they must share a near similar api to interwork.
You should choose either FCM or GCM. From your build.gradles looks like you work with gcm. So remove the FCM code. Remove these lines:
compile('com.google.firebase:firebase-core:9.0.2')
{
exclude group: 'com.google.android.gms.measurement'
}
Read the play service setup with GCM at Setting Up Google Play Services
and FCM at Add Firebase to your Android Project.
When I try and the version levels do not match, Android Studio gives the following warning:
Please fix the version conflict either by updating the version of the google-services plugin (information about the latest version is available at https://bintray.com/android/android-tools/com.google.gms.google-services/)
Thus setting to the same version levels resolves the dependencies not working with each other
compile('com.google.android.gms:play-services-gcm:11.8.0')
compile('com.google.firebase:firebase-core:11.8.0')
I have tried to add a Google Map fragment to my android app. I add the dependency
compile 'com.google.android.gms:play-services-maps:8.1.0'
However when I try to sync the gradle build file I get the error
Warning:Conflict with dependency 'com.android.support:support-annotations'.
Resolved versions for app (22.2.0) and test app (23.0.1) differ.
On the advice of another stack overflow answer we ran the Gradle dependencies report, and found that the only package that includes the module 22.2.0 is the google play one itself.
I have tried to exclude the module but this is to no avail.
Has anyone solved this problem? Any help would be much appreciated.
I attach the relevant part of the build.gradle file
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
//./compile 'com.android.support:appcompat-v7:19.1.0'
androidTestCompile ('com.android.support.test:runner:0.4') {
exclude module: 'support annotations'
}
// Set this dependency to use JUnit 4 rules
androidTestCompile ('com.android.support.test:rules:0.4') {
exclude module: 'support annotations'
}
// Set this dependency to build and run Espresso tests
androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.1') {
exclude module: 'support annotations'
}
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
androidTestCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services-maps:8.1.0'
}
You are not excluding the support annotations module from your testing libraries because of a small typo. Change "support annotations" to "support-annotations" in your exclude statements.
exclude module: 'support-annotations'
Huh! Faced this issue in the morning and now seeing this question.
I resolved by adding the following additional dependency:
androidTestCompile 'com.android.support:support-annotations:22.+'
I was not sure of the correct resolving version thereby kept with wildcard '+', if you are sure replace the wildcard with the right one.
I'm trying to integrate espresso into my application for ui testing. Here are my dependencies in Gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:design:22.2.1'
compile 'com.github.bumptech.glide:okhttp-integration:1.3.1#aar'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'de.hdodenhof:circleimageview:1.3.0'
compile 'com.android.support:cardview-v7:21.+'
compile 'com.android.support:recyclerview-v7:21.+'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2'
compile 'com.android.support:support-annotations:22.2.0'
androidTestCompile 'com.android.support.test:runner:0.3'
compile project(':common')
compile project(':service')
}
So all my espresso dependencies are included. However when I try to build I get this error:
Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (22.2.1) and test app (22.2.0) differ.
Has anyone encountered this? I've found it reported here however there's no resolution. Does anyone have a fix for this?
New version of espresso-contrib 2.2.2 library has now dependency on com.android.support:appcompat-v7:23.1.1 resulting into conflict when using different version of appcompat-v7 in our compile time dependency like below:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
}
To avoid conflict when we exclude appcompat-v7 dependency from espresso-contrib like below it breaks again due to some value dependencies on design support lib.
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'support-v13'
exclude module: 'recyclerview-v7'
exclude module: 'appcompat-v7'
}
Error:
Error:(69) Error retrieving parent for item: No resource found that matches the given name 'TextAppearance.AppCompat.Display1'.
So, the solution to above problem is to exclude 'design-support' lib depedency from espresso-contrib like below:
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'support-v13'
exclude module: 'recyclerview-v7'
exclude module: 'design'
}
That solves the conflict problem!
For more detailed version of the answer you could check my other answer
So after a lot of digging around, I found I needed to change the dependency for the support annotations.
So I needed to change
compile 'com.android.support:support-annotations:22.2.0'
to
androidTestCompile 'com.android.support:support-annotations:22.+'
Latest versions of androidTest dependencies depend on appropriate version of support-annotations lib. In my case it is:
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
androidTestCompile 'org.mockito:mockito-core:2.0.31-beta'
Also, as a workaround you can add the next code in your build.gradle, android{} section:
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:23.0.1'
}
}
In the Jake Wharton U2020 application it is solved in the next way
Add to you gradle.build file
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:23.0.1'
}
}
I had to combine the following versions for L release after receiving a similar dependency conflict between project and test app:
android {
useLibrary 'org.apache.http.legacy'
compileSdkVersion 23
buildToolsVersion '23.0.1'
defaultConfig {
minSdkVersion 14
targetSdkVersion 23
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}
useLibrary was needed since we use org.apache.http imports, see https://github.com/bitstadium/HockeySDK-Android/issues/80
The problem is in this file:
android-sdk\extras\android\m2repository\com\android\support\test\runner\0.3\runner-0.3.pom
here:
<dependency>
<groupId>com.android.support</groupId>
<artifactId>support-annotations</artifactId>
<version>22.2.0</version>
<scope>compile</scope>
</dependency>
if you set 22.2.1 instead 22.2.0 it will work
As stated in the google documentation:
https://sites.google.com/a/android.com/tools/tech-docs/new-build-system/user-guide#TOC-Resolving-conflicts-between-main-and-test-APK
The way to resolve is to explicitly set the support library in androidTestCompile to the version you are using in the project.
if for example you are using support library version 25.0.1 just add
androidTestCompile 'com.android.support:support-annotations:25.0.1'
in your build.gradle configuration
just change compile com.android.support:support-annotations:22.2.0 to 23.0.1
if want to use 2.2.1 version
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/