First Impression of Jetpack Compose, QoL Issues
Jetpack Compose is marketed by Google as
Android’s modern toolkit for building native UI
And it’s slated to be the future of Android, replacing xml layouts. But is it really ready yet? I‘ve heard about Compose for a long time now and am excited to try a way to write layouts in Kotlin rather than xml, something that wouldn’t have been as elegant to do in Java (I’ve tried), so I gave Compose a try, and here are the immediate quality of life issues I had with using it in Android Studio.
1. Function should start with a lowercase letter
Immediately you can see lint warning on every single Composable function. Typically functions are supposed to start with a lower case letter however Composable functions break that norm and seem like they should start with an uppercase letter.
There isn’t a good solution. You can either live with seeing a warning on every single Composable function in your project, disable the inspection, or change the rules of the inspection, but whatever you do applies to all functions. You can’t create a different rule just for Composable functions. I found this issue in JetBrain’s issue tracker, it’s over a year old and hasn’t been worked on. Consider giving it an upvote to try to get it more attention.
2. Latest version of Kotlin not supported
Android Studio templates have always been kinda bad, so I checked build.gradle and tried to update the dependencies and language version. And to my surprise, the latest version of Kotlin is not supported.
BUILD FAILED
e: This version (1.0.5) of the Compose Compiler requires Kotlin version 1.5.31 but you appear to be using Kotlin version 1.6.10 which is not known to be compatible.
I can only imagine this is a possible consequence of Jetpack Compose being not just a library, but also a Kotlin compiler plugin. Kotlin 1.5.31 despite being a few versions old is only 3 months older than Kotlin 1.6.10. Still, it concerns me that I can’t use the latest version of Kotlin. I could understand not being able to use a much older version of Kotlin, if there was a minimum version requirement, but it doesn’t make sense to me that I can’t use the latest. I gave 1.5.21 a try and got the same error. It appears that each version of Compose forces you to use a specific Kotlin version, no lower and no higher.
This is a huge pain point. If for any reason at all you have some other Kotlin version requirement then you’re going to be screwed.
3. Type `TypeVariable(T)` has no method getValue
I have a long history in Android, but coming into Jetpack Compose I feel like a complete beginner again. My immediate goal is to make a counter on the screen that increments once per second. But we don’t have a modifiable TextView, just a Text function with no return. This is a big paradigm shift.
Anyways, looking at tutorials, I can use something like
var counterState = mutableStateOf(0)
@Composable
fun Counter() {
val counter by remember { counterState }
Text(text = "Count: $counter")
}
This allows me to reactively update the text by updating the value of counterState. BUT, there’s an error and build failure.
As a complete beginner, this confused me for a while. I did exactly what various tutorials and stack overflows showed, checked, double checked, nothing different. On further reading I found out the cause, missing imports that you have to manually add.
// Can auto-import
import androidx.compose.runtime.remember// You have to manually add these two
import androidx.compose.runtime.getValue // Needed for val and var
import androidx.compose.runtime.setValue // Only needed for var
This is a pain akin to needing to write boilerplate in every file. Every time you need state and want to use the delegate pattern you’re going to need to go to the top of your file and manually add these imports. I’ve written my own delegates before, and never needed three imports to use them or had this kind of problem before.
And I can imagine the annoyance already if I have a val by remember, add both imports, then later import optimization removes the unused setValue import, then later try adding a var by remember and encounter the same error and find I need to add the setValue import again.
In Conclusion,
My Day 1 impression after hearing about Compose for more than a couple years now was pretty bad. Hopefully these issues can be fixed. The strict Kotlin version requirement is a major issue in my opinion, the other two are annoyances but won’t stop you from using Compose if you want.
I’ve gotten the same feeling using it that I had in the past hearing about it, which is that I’m worried the scope of Jetpack Compose is too big. It isn’t just a way to write xml layouts in Kotlin, which alone I would’ve been happy with. It’s so much more, so much that it even requires a compiler plugin! The latest version of Jetpack Compose may have passed 1.0.0, indicating that it should be ready for use, but other than messing around, so far I don’t think I’ll be using it my projects soon.