Will Google convert LiveData to Kotlin?
LiveData
is an observable data holder class. It’s part of the androidx.lifecycle library in Android Jetpack and was released in 2018. Google has promoted the usage of LiveData since, and it has become a widely adopted and valuable component. However, despite Kotlin adoption already being underway in 2018, Google wrote LiveData in Java, and to this day it is still written in Java. The perplexing part is that Google was the primary driver of Kotlin adoption but still wrote this library in Java. This has been to the detriment of many Android developers and Android apps…
LiveData’s nullability problem
Null-safety feels like a basic necessity for a modern programming language and is one of the simplest and best advantages of Kotlin over Java. Unfortunately, even if your entire app is written in Kotlin, LiveData and its partner MutableLiveData do not respect null vs non-null types.
For example:
// Despite code being in Kotlin and using a non-nullable Boolean type:
MutableLiveData<Boolean>(null) // This unfortunately compiles.
mutableLiveData.value = null // This unfortunately compiles.
liveData.observe(this) { value /* Boolean! */->
// The value is platform type Boolean! rather than Boolean,
// meaning it may unfortunately be null.
}
This problem wouldn’t exist and could be fixed with a Kotlin conversion.
Developers committed to LiveData are faced with either littering their code with null checks (ugly & grows complexity), trusting themselves and their fellow developers to never put or allow a null value to be put into LiveData (good luck developers are untrustworthy idiots; be prepared for app crashes), or writting null-safe sugar coats (best, but defeats the value of an industry standard solution).
Something changed
Five years later, on March 8th, 2023, Version 2.6.0 of androidx.lifecycle was released. LiveData was not converted to Kotlin, but a large amount of other classes were.
That’s 18 classes converted to Kotlin! Judging from going through the entire version history notes, it’s the first time Google’s done this. So what will happen in future releases. Will Google convert LiveData to Kotlin?