google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.35k stars 2k forks source link

JaCoCo junit coverage does not work when adding Hilt to Android project #2740

Closed ZOlbrys closed 3 years ago

ZOlbrys commented 3 years ago

I recently noticed that simply adding the Hilt dependencies to my project resulted in JaCoCo no longer picking up code coverage data from JUnit tests of my Kotlin code.

To test this out a bit I created a new app with a single Kotlin class and added JaCoCo - so far so great. After adding Hilt to the app (note: not even using Hilt yet, just adding the dependencies) and re-running JaCoCo the coverage was no longer being reported.

version info: Hilt 2.37 JaCoCo 0.87 Kotlin 1.5.20

Sample app and instructions are available here: https://github.com/ZOlbrys/HiltJacocoSampleApp

bcorso commented 3 years ago

This sounds like a dupe of #1982. Closing this one for now.

ZOlbrys commented 3 years ago

I saw that earlier but wasn't even using @AndroidEntryPoint syntax yet - the issue described here is reproduced just by adding the dependencies... so I figured it was different. Hmm.

slott commented 2 years ago

I just saw the same - damn that was a tricky one.

So just adding this

apply plugin: 'dagger.hilt.android.plugin'

caused the problem...

So this shouldn't be close as its not 100% the same as the other ?

bcorso commented 2 years ago

I think these are essentially the same underlying issue, which is that adding the Hilt Gradle plugin adds a transform to the AGP transformation pipeline, and JaCoCo isn't handling that properly. If that issue is fixed, it should fix both #1982 and this issue.

The workaround for #1982 should also apply here, which is to remove the plugin and use the suggestion (https://github.com/google/dagger/issues/1982#issuecomment-710308132) for any @AndroidEntryPoint usages. It's not great, but I think it's the best we have for now.

Morteza-Rastgoo commented 1 year ago

A year after and the problem still exists. Any updates on this?

shubhamgarg1 commented 1 year ago

I was able to resolve this issue in my project by using the hack mentioned here: https://issuetracker.google.com/issues/161300933#comment5

aljohnston112 commented 11 months ago

Same issue. Created a test project to confirm.

rkrishnan2012 commented 11 months ago

You can work around this by adding build/intermediates/classes/debug/transformDebugClassesWithAsm to your Jacoco classes set.