Android espresso-contrib gradle build failing - android-gradle-plugin

I'm trying to learn android espresso.. I followed some basic tutorials and it was working fine. But now I want to do some tests on the android navigation drawer. For that I need to use gradle dependency androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2' but it's causing conflict with other dependencies. My gradle file :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "my.com.myapp_android"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
jcenter()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
//material design
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
//zxing
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
compile 'com.google.zxing:core:3.2.1'
//Testing
// Optional -- Mockito framework
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support:support-annotations:23.3.0'
androidTestCompile 'com.android.support.test:runner:0.5'
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.2'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
// Optional -- UI testing with UI Automator
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
//inMarketSDK
//compile group: 'com.inmarket', name: 'm2msdk', version: '2.29', ext: 'aar'
}
Error is something like this:
Error:Conflict with dependency 'com.android.support:support-v4'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
followed this : link for espresso install
I also tried to exclude annotation dependency :
androidTestCompile ('com.android.support.test.espresso:espresso-core: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'
}
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'
}

TL;DR;
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'.
Root cause:
This is because the design support lib has dependency on
appcompat-v7.
So,when we exclude 'appcompat-v7' module from
espresso-contrib dependencies(like above) , the design support lib downloaded as
part of transitive dependency of espresso-contrib lib couldn't find
the compatible version of appcompat-v7 lib(23.1.1) it is using
internally in its resources files and thus gives out the above error.
So, the solution to above problem is to exclude 'design-support' lib dependency 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!
LONGER VERSION (in case someone is interested):
To found out the reasons of various conflict issues we face when using `espresso-contrib' library i have created sample app to find out the root cause.
Step 1:Using Espresso-Contrib Lib version 2.2.1
Created App to use 'espresso-contrib' lib version 2.2.1 by adding following lines in app/build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
}
Note: In this case,I am not importing any other support library components like
appcompat-v7,recyclerview-v7,etc.
The dependency graph for the above setup looks like below:
As can be seen that espresso-contrib 2.2.1lib has transitive dependencies on version 23.0.1 of
support-v4,recyclerview-v7,support-annotations ,etc.
As i am not defining dependencies for recyclerview-v7,support-annotations in my project the above setup would work just fine.
But when we define those as compile dependencies [like below] in our project we get version conflict issues as stated in your question.
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
To avoid those conflicts we add below line to our espresso-contrib lib:
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'support-v13'
exclude module: 'recyclerview-v7'
}
This makes sure those dependencies aren't downloaded as part of espresso-contrib transitive dependencies.
Everything runs fine with above setup.No issues!
Step 2: Using Espresso-Contrib lib version 2.2.2
Changed App's build.gradle to use 'espresso-contrib' lib version 2.2.2 by changing the previous build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
testCompile 'junit:junit:4.12'
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'
}
}
But when i build project using above setup..build fails with error posted in question..
Error:
Error:Conflict with dependency 'com.android.support:appcompat-v7'. Resolved versions for app (23.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
So, looking at error i added one more line to above build.gradle:
exclude module: 'appcompat-v7' (inside androidTestCompile block of espresso-contrib)
But that doesn't resolves the conflict issue and i get value dependencies error posted in comments.
So i check for dependency graph of my app again:
As can be seen now that espresso-contrib 2.2.2 lib has now transitive dependency on com.android.support:design:23.1.1 causing the above conflict.
So, we need to add below line inside androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.2') block:
exclude module: 'design'
This resolves the conflict issue in lib version 2.2.2!

For some reason, it has also started to happen to me out of the blue.
The only thing that solved it was to invalidate caches and restart

do below
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1'){
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'recyclerview-v7'
}

Related

Declare a bundled plugin as a dependency in IntelliJ IDEA Plugin

Non bundled plugins are stored in the repository and can be referenced by groupId:artifactId:versionId but the documentation only says this ambiguous thing about declaring dependencies on bundled plugins:
locate the plugin’s main JAR file containing META-INF/plugin.xml
descriptor with tag (or if not specified).
Okay. Locate it and do what exactly?
My solution was to copy the template for a gradle IntelliJ plugin and copy this example which led me to declare the dependency by plugin name with no version, groupId. In my case I wanted to use classes from the built-in maven plugin so I added this to the plugins = [....]. The plugin will automatically grab the version of that plugin that is in your IntelliJ.
build.gradle
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.4.26'
}
group 'com.whatever'
version '1.0-SNAPSHOT'
id 'MyPlugin'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
runtime files('maven.jar', 'libs/gson-2.2.4.jar')
runtime fileTree(dir: 'libs', include: '*.jar')
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2020.2.2'
plugins = ['maven']
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}

Could not resolve com.android.support:design:26.1.0

I have been having this problem for quite a while, that my com.android.support:design:26.1.0 fails me and I have no clue what to change anymore.
These are my files for module gradle.
Build.gradle (app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.xxx.yyy"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.google.android.gms:play-services-vision:9.4.0'
// compile 'com.android.support:design:26.1.0'
}
//apply plugin: 'com.google.gms.google-services'
The project gradle file
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter({ url "http://jcenter.bintray.com/" })
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// classpath 'com.google.gms:google-services:3.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter({ url "http://jcenter.bintray.com/" })
maven { url "https://maven.google.com" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Error:Unable to resolve dependency for
':app#releaseUnitTest/compileClasspath': Could not resolve
com.android.support:design:26.1.0.
Error:Unable to resolve dependency
for ':app#release/compileClasspath': Could not resolve
com.android.support:design:26.1.0.
Error:Unable to resolve dependency
for ':app#debugUnitTest/compileClasspath': Could not resolve
com.android.support:design:26.1.0.
Error:Unable to resolve dependency
for ':app#debugAndroidTest/compileClasspath': Could not resolve
com.android.support:design:26.1.0.
Error:Unable to resolve dependency
for ':app#debug/compileClasspath': Could not resolve
com.android.support:design:26.1.0.
And only the 'Snackbar' function does not work but rest of it seems fine.
When I change the design to 25.1.0 version instead, it works but it clashes with other 26.1.0 ones so it's not recommended by the Android Studio. Anyone..?
I have tried putting the
compile 'com.android.support:design:26.1.0'
instead of implement, it does not work. When I tried to change the whole thing to either 25.4.0 or any other 25 version, a lot of other things doesnt compile as well. I have unchecked the Offline work, tried removing / adding maven directory, un-commenting a few more of dependencies, changing all to compile instead of implement but just gives me more errors.
Yes I have updated all my SDKs to the newest but I have a feeling that Gradle just cannot download certain dependencies from the websites that's why I cannot download any more of the other dependencies. (Although my off-line work is unchecked)
Also, whatever that are uncommented in the code above, it means I have tried inputting those lines but it did not solve the issue or rather created more issues.
Here is the way i found it please try the below
Go to File->Settings->Build, Execution, Deployment->Gradle->Uncheck Offline work option.
Try this once ,instead of implementing compile the design support gradle code
compile 'com.android.support:design:26.1.0'
and remove this
implementation 'com.android.support:design:26.1.0'
Problem that I realized in the end was that when I was setting up the android proxy, I have only set up 1 proxy which was for HTTP and did not realize that HTTPS proxy was not set up.
So after fixing that issue by setting HTTPS proxy, there was no more problems.

Unity 5.6 custom gradle configuration with Fabric

I would like to configure Fabric dependencies for Unity Android build using gradle. I'm now exporting the project and using Android Studio to get rid of the errors and then prepare a custom working "mainTemplate.gradle" so I can build directly from Unity 5.6.
Here are the configured dependencies as Unity suggested:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// other dependencies
compile project(':answers')
compile project(':beta')
compile project(':crashlytics')
compile project(':crashlytics-wrapper')
compile project(':fabric')
compile project(':fabric-init')
}
Each of the Fabric folders is treated as a library that has its own gradle config.
Here are the errors I'm getting (due to a file used in same namespace of two "libraries"):
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lio/fabric/unity/crashlytics/android/BuildConfig;
Uncaught translation error: java.lang.IllegalArgumentException: already added: Lio/fabric/unity/android/BuildConfig;
I tried adding the following but it did not work:
android {
dexOptions {
preDexLibraries = false
}
I also tried without success:
task androidReleaseJar(type: Jar, dependsOn: assembleRelease) {
from "$buildDir/intermediates/classes/release/"
exclude '**/BuildConfig.class'
}
Here is how I solved this issue:
By default each fabric folder is treated as a project however only "fabric" needs to be handled as one since it contains a "res" folder and an "AndroidManifest.xml" file with required meta data values. So I just kept only "fabric" as a project and changed the other dependencies to be handled as simple *.jar files.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'fabric-init/libs', include: ['*.jar'])
compile fileTree(dir: 'crashlytics-wrapper/libs', include: ['*.jar'])
compile fileTree(dir: 'crashlytics/libs', include: ['*.jar'])
compile fileTree(dir: 'beta/libs', include: ['*.jar'])
compile fileTree(dir: 'answers/libs', include: ['*.jar'])
compile project(':fabric')
}
and in Settings.gradle I keep only one project reference:
//include 'answers'
//include 'beta'
//include 'crashlytics'
//include 'crashlytics-wrapper'
include 'fabric'
//include 'fabric-init'
You can disable generation of BuildConfig java class by changes in only one file (without Fabric modifications). Place this at end of Plugins/Android/mainTemplate.gradle for all your problem projects:
['crashlytics', 'crashlytics-wrapper', 'fabric', 'fabric-init'].each { name ->
project(":$name").tasks.whenTaskAdded { task ->
if (task.name == 'generateDebugBuildConfig' || task.name == 'generateReleaseBuildConfig' ) {
task.enabled = false
}
}
}

How to add dependency sources to to IntelliJ

I have an IntelliJ project with a gradle build file that includes several projects from a maven central repository. One such dependency is Geb.
When I navigate my classes, I sometimes come across a Geb class that looks interesting. I select "Go to declaration" and get a sad "Cannot find declaration to go to".
Obviously this is because IntelliJ has not loaded the Geb source files. But how do I get it to do that without including Geb as a source in my project? I DO NOT want Geb to be compiled into my project from source because I'm already including it as a dependency in my gradle build file.
Adding it as a module dependency does not work. This is like adding more sources.
I suppose I can grab the repo and build the jars and then include those. Is that really necessary?
Adding the IDEA plugin to the gradle file doesn't work.
Relevant part of the gradle script:
apply plugin: 'groovy'
apply plugin: 'idea'
dependencies {
// need to depend on geb-spock
testCompile "org.gebish:geb-spock:0.13.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile "org.apache.commons:commons-lang3:3.4"
testCompile "io.github.bonigarcia:webdrivermanager:1.5.0"
testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
}
idea {
module {
downloadJavadoc = true // defaults to false
downloadSources = true
}
}
This is a complete build script that downloads all the dependencies with sources:
apply plugin: 'java'
apply plugin: 'idea'
idea {
module {
downloadJavadoc = true // defaults to false
downloadSources = true
}
}
repositories {
mavenCentral()
}
dependencies {
// need to depend on geb-spock
testCompile "org.gebish:geb-spock:0.13.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile "org.apache.commons:commons-lang3:3.4"
testCompile "io.github.bonigarcia:webdrivermanager:1.5.0"
testRuntime "org.seleniumhq.selenium:selenium-support:2.53.1"
}
task wrapper(type: Wrapper) {
gradleVersion = '3.3'
}
The dependency shows in the list:
And I am able to browse the source code, you can see the comments are there:
One of the possible explanation might be that in your repository list is a repo, such as mavenLocal or a caching Artifactory, that doesn't have the sources dependency.
The ordering of the repositories matters, so if mavenLocal is first and the sources are not available there, I believe they will not get downloaded. A possible fix would be to remove the dependency from mavenLocal and re-download it, change order of dependencies or if it is the parent script, exempt your subproject when adding the repositories:
configure(allprojects - project(':my-subproj')) {
repositories {
...
}
}
I don't think there is any way you can prevent that from the subproject's build script though. It must be done in the parent.

Intellij-idea gradle sub-module keeps forgetting root project dependency

I have two gradle projects defined like so
apiclient (root)
| src\
| build.gradle
|--- android (module)
| src\
| build.gradle
These projects are not on maven, yet, and the module apiclient.android depends on the root module (apiclient).
I keep adding apiclient using the project structure... -> dependency dialog and the reference keeps getting removed from the .iml file whenever I click refresh in the gradle tool window.
how can I keep the child module from forgetting the parent module dependency?
edit: as requested the build.gradle files.
apiclient build.gradle
group 'emby.apiclient'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.google.guava:guava:18.0'
compile 'org.java-websocket:Java-WebSocket:1.3.0'
}
apiclient.android build.gradle
group 'emby.apiclient'
version '1.0-SNAPSHOT'
apply plugin: 'java'
sourceCompatibility = 1.7
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.codehaus.groovy:groovy-all:2.3.11'
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.google.code.gson:gson:2.5'
compile 'com.mcxiaoke.volley:library:1.0.16'
compile 'com.squareup.okhttp:okhttp:2.1.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.1.0'
compile 'emby.apiclient:apiclient:1.0-SNAPSHOT'
}
The last line compile 'emby.apiclient:apiclient:1.0-SNAPSHOT' is underlined red in the gradle project window. I've tried compile project 'apiclient' as well and the outcome is unchanged.
settings.gradle
rootProject.name = 'apiclient'
include 'android'
findProject(':android')?.name = 'emby.apiclient.android'
In the /android build.gradle file, you need to add compile project(":apiclient") to your dependency list. I found out the hard way that IntelliJ's "Do you want to add a dependency to classpath" dialog only adds it to the .iml file, which isn't run as part of the build.