As a Friday project I've prototyped a Gradle Plugi...
# android
j
As a Friday project I've prototyped a Gradle Plugin which generates a typed equivalent to
R.java
using Kotlin's experimental Inline Classes, giving us better compile-time type safety over resource referencing. For example:
R.color.red
is of type
Int
, whereas
K.color.red
is of type
ColorRes
. Is this something that people would be interested in? There's more work to be done to make it production-ready, and it feels like this should be somewhere more central (like AGP), but you can find more examples and the code in this Github repo
e
This definitely sounds interesting.
j
We investigated this for AGP. That is the easy part. The hard part is that you need to need to rewrite the bytecode of the
android.jar
and all libraries where they accept a resource int to have an overload which accepts the new inline classes. This also means synthesizing the Kotlin metadata annotation, to be clear.
👍 3
j
That was the next thing I was thinking of looking in to, so thanks for the heads up! An idea I had around this instead of going the bytecode route, is to have something which is able to build compatibility modules with libraries and perhaps
android.jar
- essentially a collection of extension functions which act as overloads, using the inline types. This would make using them a bit more tedious, as it would be another module to include, and of course be backwards incompatible, but would be much cleaner and lower effort. The API signature
.txt
files include everything needed to generate these extension
j
Extensions won't work. You can't handle static functions or overloading functions based on return type only.
j
Yeah, makes sense. What was the reasoning behind not adding it to AGP? Was there anything blocking it other than being hard to implement with needing to synthesize Kotlin Metadata?
j
I don't recall off-hand
j
No problem, thanks for answering my questions! I'm going to dig a bit deeper in to bytecode rewriting and see where I end up.