30

Is there any option to exclude Dagger2 classes from test coverage report in Android Studio

2
  • You mean classes generated by dagger-compiler apt or your modules and components? Connected or unit tests? Commented Mar 8, 2017 at 16:45
  • classes generated by dagger Commented Mar 8, 2017 at 20:04

4 Answers 4

25

JaCoCo excludes

If you're using JaCoCo, for example using android instrumentation connected tests, you need to configure the excludes (or includes), which, according to the documentation is...

A list of class files to exclude from the report. May use wildcard characters (* and ?). When not specified nothing will be excluded.

Which means you need to match the generated dagger class names. The following rules cover virtually any class generated by dagger-compiler, without matching any of non-generated classes (unless you name your class the same as dagger does...):

excludes = [
    '**/*_MembersInjector.class',
    '**/Dagger*Component.class', // covers component implementations
    '**/Dagger*Component$Builder.class', // covers component builders
    '**/*Module_*Factory.class'
]

You can check your generated dagger classes in app/build/generated/source/apt directory after running a build, to see if there are any additional generated classes that you want to match with excludes.

This excludes array is a configuration property of jacoco plugin. Now, where to put this excludes array depends on whether you define your own tasks based on the jacoco plugin, or use a 'higher level plugin' that does this for you. For example using this plugin (you can see the plugin source to see where the excludes are actually applied):

jacocoAndroidUnitTestReport {
  excludes +=  [
        '**/*_MembersInjector.class',
        '**/Dagger*Component.class',
        '**/Dagger*Component$Builder.class',
        '**/*Module_*Factory.class'
    ]
}

Connected tests

If you're running android connected test coverage by setting testCoverageEnabled true in your buildType, unfortunately there is no idiomatic way to declare excludes, since the android gradle plugin doesn't provide such options, and the predefined jacoco report task has the excludes hardcoded. In this case, you have to script your own task with excludes.


IntelliJ test runner

If you're using the IntelliJ test runner, whether the coverage is done by IntelliJ or JaCoCo, you need to put the includes for a test configuration.

  1. Open the Edit Configurations window:

Edit Configurations

  1. Choose your test config and define includes (classes or whole packages). In this case I included the whole com.google.android.gms package, just as an example:

Test coverage includes

To exclude dagger generated files, the quickest way is to put all the dagger dependencies in one root package, and include all the other packages in the test configuration.

1
  • What is $Builder in '*/DaggerComponent$Builder.class' ? Commented Jun 13, 2022 at 12:18
11

Exclude files from AndroidStudio index

After many days I found solution: exclusion files from IDE index also exclude them from IDE's code coverage report.
So we need create new File-Type for all codegen files (or only Dagger/Hilt files), and exclude this File-Type from index.

To achieve this you need:

1. Create new File-Type 'Codegen' for codegen-files

Go to Preferences -> File Types, and add new file-type Codegen: create new file-type

Add this templates (templates may change in future):

*_*Factory.java
*_ComponentTreeDeps.java
*_Factory.java
*_GeneratedInjector.java
*_HiltComponents.java
*_HiltModules.java
*_HiltModules_BindsModule.java
*_HiltModules_KeyModule.java
*_MembersInjector.java
*_ProvideFactory.java
*_SingletonC.java
*_TestComponentDataSupplier.java
BR.java
BuildConfig.java
DataBinderMapperImpl.java
Hilt_*.java
_test_*.java

2. Exclude 'Codegen' from index

Open Go to File, choose tab Files and then click Filter and uncheck Codegen file-type. uncheck codegen

That's all!

After this when you will run tests with code coverage (with IntelliJ IDEA runner), matched Codegen-files will be excluded also from code coverage report in IDE.


Note: With this approach you can't get coverage from CLI. If you need get coverage from CLI - use Jacoco.

2
  • 1
    That worked for me! Thanks for the hint!
    – inazaruk
    Commented Nov 9, 2021 at 6:33
  • 2
    sweet :) I just added _.java cause I only write in kotlin :D Commented Feb 24, 2022 at 23:48
6

More recent versions of Dagger generate additional files with slightly different patterns. I had success with the following excludes when using Dagger 2.15

'**/*_MembersInjector.class',
'**/Dagger*Component*.class',
'**/Dagger*Subcomponent*.class',
'**/*Subcomponent$Builder.class',
'**/*Module_*Factory.class',
0

If you are using Kover library then you can use annotatedBy exclusions options. Take a look more about it here .

kover {
reports {
    filters {
        excludes {
            annotatedBy("dagger.internal.DaggerGenerated")
            annotatedBy("javax.annotations.Generated")
        }
    }
}
}

Dagger uses @DaggerGenerated annotation for the generated classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.