How to avoid partial coverage with lateinit fields

2019-02-25 20:09发布

问题:

I have an Android service written in Kotlin which I inject using Guice. It has lateinit fields which cannot be null, but they must be lateinit because I cannot use constructor injection.

Something around these lines:

class VibrationService : Service() {
    @Inject
    private lateinit var pm: PowerManager
    private lateinit var wakeLock: WakeLock

    override fun onCreate() {
        AlarmApplication.guice().injectMembers(this)
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "VibrationService")
        wakeLock.acquire()
    }
}

Now when I create JaCoCo reports, all lines where any of the lateinit fields are accessed are marked as partially covered. I think Kotlin compiler adds some checks to the bytecode to make sure that fields are initialized before they are accessed.

Is there any way to disable these checks? I want my 100% coverage:-)