Virtual reflection of Kotlin all target
us English | cn 简体中文
- Set all classes in certain packages to support virtual reflection
- Mark certain classes that can or cannot be virtually reflected through annotations
Step 1.Your app dir, build.gradle.kts add:
- If it is a single platform, add it to build.gradle.kts in the app module directory
plugins {
...
id("com.google.devtools.ksp") version "1.7.10-1.0.6"//this, The left 1.7.10 corresponds to your the Kotlin version,more version: https://github.com/google/ksp/releases
}
//The fourth step of configuring ksp to generate directory reference links: https://github.com/ltttttttttttt/Buff/blob/main/README.md
dependencies {
...
implementation("io.github.ltttttttttttt:VirtualReflection-lib:$version")//this, such as 1.2.1
ksp("io.github.ltttttttttttt:VirtualReflection:$version")//this, such as 1.2.1
}
- If it is multi-platform, add it to build.gradle.kts in the common module directory
plugins {
...
id("com.google.devtools.ksp") version "1.7.10-1.0.6"
}
...
val commonMain by getting {
//Configure the ksp generation directory
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
dependencies {
...
api("io.github.ltttttttttttt:VirtualReflection-lib:$version")//this, such as 1.2.1
}
}
...
dependencies {
add("kspCommonMainMetadata", "io.github.ltttttttttttt:VirtualReflection:$version")//this, such as 1.2.1
}
Step 2.Use VirtualReflection
Configure packages that require virtual reflection, Your app dir, build.gradle.kts -> android(or kotlin) add:
ksp {
//Configure multiple paths separated by spaces
arg("packageListWithVirtualReflection", "com.lt.virtual_reflection.bean/*your package*/")
//Configure the generated function name, which defaults to newInstance
//arg("functionNameWithVirtualReflection", xxx)
}
use
//Constructing Objects Using Non parametric Constructors
KClass.newInstance()
//Constructing Objects Using Parametric Constructors
KClass.newInstance(parameters...)
//By string method
VirtualReflectionUtil.newInstance("MainActivity")
//Additional configuration of classes or files that can be virtually reflected
@ReflectionObject
//Configure the constructor within the path to not support virtual reflection
@NotReflectionObjectConstructor