Automatically Generating Type-Safe Builders for Data Classes

This is the first example I found when googling “kotlin kapt tutorial”. It looks decent, at least I didn’t see any obvious mistakes when I looked through it quickly:

If you want to see a real code example of kapt being used I suggest looking at moshi:

Also should also look into kotlin poet. It’s a library that can be used to easily generate kotlin code, handle imports and stuf

Last lib you might also want to look into is kotlinx.metadata. You will need this to read kotlins internal @Metadata annotation which contains information about kotlin constructs that java doesn’t know about, eg nullability, sealed/data classes, etc. Kapt is a wrapper around a java api so you don’t get that information for free. Square also has some easy to use wrapper for this. I think it’s part of kotlinpoet but I’m not 100% sure. It’s been a while since I last used any of this stuff.

1 Like