Is there any option to exclude Dagger2 classes from test coverage report in Android Studio
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.
- Open the Edit Configurations window:
- 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:
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.
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
:
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.
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.
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',
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.
© 2022 - 2024 — McMap. All rights reserved.