For a more comprehensive tool, check out GradleKotlinConverter, which does the same replacements and a lot more.
Turn your build.gradle
into build.gradle.kts
.
This script uses a list of regexes to turn single quotes into double ones, dependencies, etc... It's not perfect but it can save you time. Just don't take the output as gospel.
# if not done yet, install kscript:
curl -s "https://get.sdkman.io" | bash # install sdkman
source ~/.bash_profile # add sdkman to PATH
sdk install kotlin # install Kotlin
sdk install kscript
# run ktsify
./ktsify.kts path/to/project
# your build.gradle and settings.gradle file will be converted to build.gradle.kts and settings.gradle.kts
# backups are kept under .old suffixes
Extra properties are very dynamic and hard to translate to kotlin by nature. This script uses groovy.util.Eval.x() to escape them and get their value as Any
.
You can also tweak the heuristics that try to detect the extra properties:
fun String.isExtra() = when {
startsWith("dep") -> true
startsWith("androidConfig") -> true
else -> false
}
In all cases, you might need to cast the value to your required type
Groovy | Kotlin | |
Strings | 'a string' |
"a string" |
Conventions |
java { targetCompatibility JavaVersion.VERSION_1_7 } |
withConvention(JavaPluginConvention::class) { targetCompatibility = JavaVersion.VERSION_1_7 } |
Extensions |
android { compileSdkVersion 28 } |
extensions.findByType(BaseExtension::class.java)!!.apply { compileSdkVersion(28) } |
Tasks |
compileKotlin { kotlinOptions { jvmTarget = "1.8" } } |
tasks.withType<KotlinCompile> { kotlinOptions { jvmTarget = "1.8" } } |