I have been charged with integrating Dagger in our existing project and I am having a bit of a problem with the dagger annotation processor.
My environment is pretty restricted so I cannot just use jcenter() or even Google Maven to get dependencies. We have an internal Ivy repo that stores and manages all of our Dependencies.
That said, I have pulled down all the dependencies that Dagger requires but I still have an issue. Gradle sync finishes successfully and resolves the dependencies, however when I go to build I get the following error.
error: Bad service configuration file, or exception thrown while
constructing Processor object: javax.annotation.processing.Processor:
Provider dagger.internal.codegen.ComponentProcessor could not be
instantiated: java.lang.NoClassDefFoundError:
com/google/common/collect/SetMultimap
Clearly I am missing a dependency as when I specifically specify and allow jcenter() and Google Maven as repositories, which I can do for testing purposes, but for prod building this cannot be allowed, I am able to build with no exceptions.
Now a strict reading of the error tells me that Dagger cannot find com.google.comm.collect.SetMutliMap.
I have searched quite a bit trying to find this dependency and about all I can find is that this file is a part of Guava or at least some of its functions are.
One thing to note is I am using the following version of gradle:
classpath 'com.android.tools.build:gradle:3.0.0'
Here is my dagger dependency in build.gradle which is an older version:
//Dagger
compile 'com.google.dagger:dagger:2.10'
annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
Other dependencies:
//lifecycle libs
compile "android.arch.lifecycle:runtime:1.0.3"
compile "android.arch.lifecycle:extensions:1.0.0"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
compile 'de.greenrobot:eventbus:2.4.1'
compile 'org.apache.commons:commons-lang3:3.4'
compile 'commons-io:commons-io:2.4'
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:support-v4:26.1.0'
compile 'com.android.support:design:26.1.0'
compile 'com.github.ganfra:material-spinner:2.0.0'
compile 'com.nulab-inc:zxcvbn:1.1.3'
compile('com.google.android.gms:play-services-vision:10.2.1') {
exclude group: 'com.android.support'
}
compile 'org.zakariya.stickyheaders:stickyheaders:0.7.6'
compile 'com.google.code.gson:gson:2.8.0'
compile('com.google.android.gms:play-services-gcm:10.2.1') {
exclude group: 'com.android.support'
}
compile('com.google.android.gms:play-services-maps:10.2.1') {
exclude group: 'com.android.support'
}
compile('com.google.android.gms:play-services-location:10.2.1') {
exclude group: 'com.android.support'
}
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:cardview-v7:26.1.0'
compile 'com.android.support:support-annotations:26.1.0'
//retrofit dependencies
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
Due to my restricted environment, I cannot update dependencies as I would like to do, so working within what I have, does anyone have any ideas on how to resolve this?
Thanks
As it turns out com.google.common.collect is part of com.google.guava
With that find in hand I found a solution that works. For whatever reason dagger-compiler was not resolving its own dependency, which is com.google.guava.
My solution consists of excluding the guava dependency from the compiler and adding it to the annotationProcessor path. I also exclude find bugs for my case as an older version is a dependency in our test project.
annotationProcessor ('com.google.guava:guava:22.0'){
exclude group: 'com.google.code.findbugs'
}
annotationProcessor('com.google.dagger:dagger-compiler:2.13') {
exclude group: 'com.google.guava'
}
my app uses these dependencies
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.google.android.gms:play-services:7.0.0'
compile 'com.google.code.gson:gson:2.2.4'
when i imported seek arc library it uses different dependencies
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile project(':SeekArc_library')
How can i solve this problem ?
I am assuming you are using the standard gradle build tooling and Android Studio.
Below is the recommended solution for issues of clashing dependencies. Eg say you have declared an explicit dependency on com.google.guava version X but some other dependency is bringing its own internal dependency on com.google.guava version X-1.
Add the following after your dependencies clause in the build.gradle file.
configurations {
all*.exclude group: 'com.google.guava', module: 'guava-jdk5'
}
For details see https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.Configuration.html
Note: there is another approach where you may selectively exclude certain dependency from each compile clause but it is not recommended since it doesn't scale well. For completeness I'll include it here but I do not recommend it. Using the same made-up example as above
compile(group: 'com.google.guava', name: 'guava', version: 'X')
compile(group: 'com.some.other.dependency', name: 'foo', version: 'bar')
{
// exclude transitive dependency since we want to depend on version `X` declared above
exclude(group: 'com.google.guava', module: 'guava-jdk5')
}
My App and newly added Library dependencies were in conflict. I changed app compiled & build sdk to api 23 and added "useLibrary 'org.apache.http.legacy'". Now problem solved.
The top level applications library dependency will override the lower level library dependency . You don't need to override explicitly. Refer to Android build documentation
If you want to exclude the dependancies of the library you are using you can
compile 'yourLibraryName'{
exclude module: 'appcompat-v7'
exclude module: 'appcompat-v7'
}
Make sure you provide your own version though, otherwise you'll get runtime errors
I have an android project where I use espresso to define tests. It all worked well until now but after upgrading to AppCompat 23.2.1 (from AppCompat 23.0.1) the execution of the tests always crashes.
My build.gradle dependencies:
dependencies {
// Ok Config
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support:support-annotations:23.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
The project compiles and executes ok, but when I try to run a test it crashes with this error:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity
Despite the text of the error I am using a descendant theme of Theme.AppCompat, so I don't understand the error message at all.
Anyone had the same problem? It seems to be any problem with the dependencies of appcompat and espresso, but I'm unable to find it and solve my problem.
Any ideas?
Thanks!
I think that the main problem is that espresso modules use a different support library than the one used in my project, so when I try to run the test the tests crashes.
Finally I've resolved it excluding the support library of all the espresso modules, to force them to use the support library of my project. And now everything works great. Hope this could help anyone!
My gradle looks like this:
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile 'com.android.support:support-annotations:23.2.1'
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-core:2.2.2') {
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.test.espresso:espresso-intents:2.2.2') {
exclude group: 'com.android.support'
}
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"
I just imported my project which worked fine from Eclipse to Android Studio v1.2.11 and now the project is no longer working with the following error which make the app crashes at onCreate() method:
java.lang.NoClassDefFoundError: android.support.v7.app.AppCompatDelegateImplV11.
I have updated updated my app to the latest v7 library, i.e. 22.1.1. below is my gradle.build dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile('com.android.support:support-v4:19.1.0') {
force = true
}
compile('com.google.android.gms:play-services-base:7.3.0') {
force = true
}
compile 'com.google.android.gms:play-services-plus:7.3.0'
compile 'joda-time:joda-time:2.7'
compile 'com.google.http-client:google-http-client-gson:1.20.0'
compile 'com.parse.bolts:bolts-android:1.1.2'
compile('com.facebook.android:facebook-android-sdk:3.22.0#aar') {
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'bolts'
}
compile 'com.google.guava:guava:18.0'
compile project(':PullToRefreshListView')
compile project(':CircularImageView')
compile project(':CountryPicker')
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:support-v13:22.1.1'
}
configurations {
// to avoid double inclusion of support libraries
all*.exclude group: 'com.android.support', module: 'support-v4'
}
NB: I am using Theme.AppCompat.Light in my styles which I believe supports ActionBar. What I want to achieve is the Nav. drawer but the app doesn't go past the onCreate(). Any help with this exception will be highly appreciated
For anyone having similar issue, I was able to solve this and thought I should share with others. The error was due to my configuration as I was excluding the android.support in my project. Once I commented it, it just vanished.
Make sure your class dependency is in your application specific build.gradle file, not your global build.gradle file.