pajato0
pajato0

Reputation: 3676

Android (Kotlin) code coverage anomalies

Code coverage (Jacoco) in Android connected tests is a very useful way to determine what methods/functions need some TLC. Now that I am switching to Kotlin over Java I have discovered some anomalies that I cannot explain, as this screen shot illustrates: enter image description here

The methods starting with _$... are internal to Kotlin or Android, I strongly suspect. My questions are: 1) does anyone have any insight into why these methods are included in the Jacoco code coverage report, and 2) is there a way to exclude them?

Upvotes: 0

Views: 760

Answers (2)

pajato0
pajato0

Reputation: 3676

In large part due to the answer from @triad, I was able to come up with a solution that is a work-around to what appears to be a Kotlin bug. The work-around was documented in the Antonio Leiva post referenced by @triad. In a nutshell the solution was to turn off caching in MainActivity. The complete solution is up on GitHub.

What I do not completely understand is the cost of this solution so I will hold off checking this answer in the hopes that a better solution is provided.

Upvotes: 0

triad
triad

Reputation: 21567

Those methods are added when using the synthetic properties via Kotlin Android Extensions. Each Kotlin Activity using synthetic properties will have those methods added.

Kotlin Android Extensions is a plugin for the Kotlin compiler, and it does two things:

Adds a hidden caching function and a field inside each Kotlin Activity. The method is pretty small so it doesn't increase the size of APK much.

Replaces each synthetic property call with a function call.

Explanation on the official docs:

https://kotlinlang.org/docs/tutorials/android-plugin.html#under-the-hood

This articles does a pretty good job going into detail:

https://antonioleiva.com/kotlin-android-extensions/

Upvotes: 1

Related Questions