https://kotlinlang.org logo
#android
Title
# android
j

jme

04/20/2020, 12:23 AM
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

emmano

04/20/2020, 1:02 AM
This definitely sounds interesting.
j

jw

04/20/2020, 1:02 AM
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

jme

04/20/2020, 1:14 AM
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

jw

04/20/2020, 12:21 PM
Extensions won't work. You can't handle static functions or overloading functions based on return type only.
j

jme

04/20/2020, 9:05 PM
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

jw

04/21/2020, 12:49 AM
I don't recall off-hand
j

jme

04/21/2020, 1:13 AM
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.