Dagger not generating components for /test class

后端 未结 8 816
醉梦人生
醉梦人生 2020-12-04 20:57

I am following the guide here: https://github.com/ecgreb/dagger-2-testing-demo

I have the following setup in my app/src/main (the injection and @Provides code omitte

相关标签:
8条回答
  • 2020-12-04 21:41

    For Android Studio 3 and dagger 2.13 the already mentioned annotation processors are needed:

    testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.13'
    

    But also do not forgot to do this for the instrumented test under androidTest:

    androidTestAnnotationProcessor'com.google.dagger:dagger-compiler:2.13'
    

    You might get the impression that this alone does not work, because the DaggerXYZ classes are not generated. After hours I found out that the test source generation is only triggered when the tests are executed. If you start a test or androidTest from Android Studio the source generation should be triggered.

    If you need this earlier trigger gradle manually:

    gradlew <moduledirectory>:compile<Flavor>DebugAndroidTestSources
    gradlew <moduledirectory>:compile<Flavor>DebugTestSources
    

    Replace Debug if you run a test in a different build type.

    Note:

    If you are using multiDexEnable = true you might get an error:

    Test running failed: Instrumentation run failed due to 'java.lang.IncompatibleClassChangeError'

    Use a different runner in this case:

    android {
    
      defaultConfig {
        multiDexEnabled true
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
    
    0 讨论(0)
  • 2020-12-04 21:42

    I ran ./gradlew build from the command line and got information about a missing Provides method that Android Studio was not telling me about.

    0 讨论(0)
提交回复
热议问题