1kotlin-reflect 2============== 3 4To generate source code from any [`KType`][k-type], including information that's not accessible to 5the builtin reflection APIs, KotlinPoet depends on [kotlin-reflect][kotlin-reflect]. `kotlin-reflect` 6can read the metadata of your classes and access this extra information. KotlinPoet can for an 7example, read the type parameters and their [variance][variance] from a generic `KType` and 8generate appropriate source code. 9 10`kotlin-reflect` is a relatively big dependency though and in some cases it is desirable to remove 11it from the final executable to save some space and/or simplify the proguard/R8 setup (for example 12for a Gradle plugin that generates Kotlin code). It is possible to do so and still use most of the 13KotlinPoet APIs: 14 15```kotlin 16dependencies { 17 implementation("com.squareup:kotlinpoet:<version>") { 18 exclude(module = "kotlin-reflect") 19 } 20} 21``` 22 23The main APIs that require `kotlin-reflect` are [`KType.asTypeName()`][as-type-name] and 24[`typeNameOf<T>()`][type-name-of]. If you're calling one of these without `kotlin-reflect` in the 25classpath and the type is generic or has annotations you will get a crash. 26 27You can replace it with code that passes type parameters or annotations explicitly and doesn't 28need `kotlin-reflect`. For example: 29 30```kotlin 31// Replace 32// kotlin-reflect needed 33val typeName = typeNameOf<List<Int?>>() 34 35// With 36// kotlin-reflect not needed 37val typeName = 38 List::class.asClassName().parameterizedBy(Int::class.asClassName().copy(nullable = true)) 39``` 40 41 [k-type]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-type/ 42 [kotlin-reflect]: https://kotlinlang.org/docs/reflection.html#jvm-dependency 43 [variance]: https://kotlinlang.org/docs/generics.html#variance 44 [as-type-name]: https://square.github.io/kotlinpoet/1.x/kotlinpoet/kotlinpoet/com.squareup.kotlinpoet/as-type-name.html 45 [type-name-of]: https://square.github.io/kotlinpoet/1.x/kotlinpoet/kotlinpoet/com.squareup.kotlinpoet/type-name-of.html 46