1

Struggling to use Dagger 2 for UI testing powered by Espresso. I'm trying to generate a dedicated test @Component under androidTest directory but getting this error:

Error:Bad service configuration file, or exception thrown while constructing
Processor object: javax.annotation.processing.Processor: 
Provider dagger.android.processor.AndroidProcessor could not be instantiated:
java.lang.NoClassDefFoundError: com/google/common/collect/SetMultimap

Here's how dependencies look like:

androidTestCompile "com.google.dagger:dagger:2.11",
androidTestCompile "com.google.dagger:dagger-android:2.11"
androidTestCompile  "com.google.dagger:dagger-android-support:2.11"
androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:2.11"
androidTestAnnotationProcessor "com.google.dagger:dagger-android-processor:2.11"

Have anyone faced with this and has a clue how to solve it?

Thanks.

Ivan
  • 1,446
  • 2
  • 11
  • 25
  • so you're using dagger only in your test code and not in production? – lelloman Aug 02 '17 at 11:42
  • @lelloman I use it for production as well, but in order to use it in UI tests I need expose Dagger2 dependencies for `androidTest` directory I should specify them not as `compile` and `annotationProcessor` dependencies but as `androidTestCompile` and `androidTestAnnotationProcessor`. I can do Gradle Sync but when it comes to building Android Studio shows the error mentioned above. – Ivan Aug 02 '17 at 13:09
  • I don't understand why you need to define the dependencies also for androidTest – lelloman Aug 02 '17 at 13:15
  • @lelloman otherwise all Dagger 2 stuff won't be visible in my test classes. Initially, I didn't have that and Android Studio was complaining about `@Component`, `@Provides` etc. – Ivan Aug 02 '17 at 13:33
  • BTW here's an example I've found, https://github.com/maydin/DaggerTesting/blob/master/app/build.gradle and the guy also uses `andtoidTestComplie` and `androidTestAnnotationOrocessor` stuff in his `build.gradle` – Ivan Aug 02 '17 at 13:35
  • I've just tried to include only compile and I can see @Component in the androidTest, without declaring the dependency again – lelloman Aug 02 '17 at 13:40
  • @lelloman wow, that's interesting. Can you see `DaggerMyComponent` generated from `MyComponent` or whatever it's called? – Ivan Aug 02 '17 at 13:56
  • yes also that one. as far as I know, the dependencies defined with compile will also be included in androidTest, but the dependencies defined only in androidTest won't be part of the app. but I might be wrong eh – lelloman Aug 02 '17 at 13:58
  • @lelloman it sounds very true to me, but the problem here is that I don't want to have `DaggerTestComponent` inside of my main project. That's actually how I was using it before. But feels like to use compiler and annotation processor upon `androidTest` dir I must list these dependencies in `build.gradle` as I'm doing now :( – Ivan Aug 02 '17 at 14:08
  • @lelloman thanks for being so proactive with my issue, appreciate it – Ivan Aug 02 '17 at 14:09
  • 1
    np, one thing I don't understand, what happens if you do this: define dagger dependencies only with compile and annotationProcessor, no androidTest dependencies. clean your project and rebuild. are you sure you cant use @Component and other dagger dependenies in androidTest folder? – lelloman Aug 02 '17 at 14:15
  • @lelloman don't know what's sort of voodoo is that, but after removing these dependencies and building project again I haven't got any missing classes complaints. Though, I didn't get any output from `MyTestComonent`. Feels like it's being just skipped. – Ivan Aug 02 '17 at 14:40
  • 1
    well so one problem is solved :) this other problem seems a new one – lelloman Aug 02 '17 at 15:35
  • Seems, it's required to specify at least annotation processor for tests https://stackoverflow.com/a/36231516/907912 – Ivan Aug 02 '17 at 16:53

1 Answers1

0

Found a solution, basically, the culprit was lying in build.gradle just a few lines below:

configurations {
   ...

   androidTestCompile.exclude group: 'com.google.guava', module: 'guava'

   ...
}

This line was excluding some useful stuff from Dagger library and preventing it from being compilable.

Ivan
  • 1,446
  • 2
  • 11
  • 25