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