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

stantronic

02/25/2020, 3:02 PM
Hallo. Curious about how many people use any form of kotlin reflection in Android apps? Cant find any resources on how much this is encouraged / discouraged.
w

wasyl

02/25/2020, 3:11 PM
Do you have any specific use case in mind? I don’t think I use any reflection at this time, but e.g. Retrofit does use Java reflection to create an implementation of the API interface (https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/HttpServiceMethod.java) but it also caches some stuff (https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit2/Retrofit.java#L192-L193) so that it doesn’t use Reflection often.
I think the overall sentiment is that reflection is slow and shouldn’t be used often, but also not slow enough not to be used ever 😉 *I don’t have sources for that statement though
☝️ 1
a

Adam Powell

02/25/2020, 3:25 PM
Avoid it. The kotlin-reflect library is large in terms of apk size and reflection is indeed slow. This is compounded on Android since the system expects to be able to kill -9 your app at any time and restart it any time it's needed, so one-time reflection that is cached still hurts your ability to become interactive quickly or launch work in the background like handling broadcasts or jobs.
If you can do it statically, it's generally worth that approach instead, even if that means code generation
a

Alowaniak

02/25/2020, 4:28 PM
Reflection can also break easier IMO I guess ideally tests would catch that for you, but that's not always airtight in my experience
s

stantronic

02/25/2020, 5:52 PM
Thanks guys. Current use case is diffing various data classes against each other to see which fields are different - idea is to present a timeline of records, and highlight, in each record those fields which have changed from the previous record. Wrote a solution in js in 2 mins which I hoped would run on a server, but now I am being asked to provide a solution client-side.
h

Holger Steinhauer [Mod]

02/26/2020, 10:26 AM
If you change your state in only one place you can keep track of the changes. And yes, it sounds like Redux for a reason 😉