Welcome! After a lot of pain trying to migrate from Gradle's Groovy to Kotlin DSL on Android Studio, I developed this tool to solve most migration issues and reduce the amount of work you need in order to make things ready.
Please see this tool as a helper, a first resource like Android Studio's Java converter to Kotlin: it won't make a perfect conversion, but it helps a lot in reducing the time you would spend on repetitive tasks.
The script was written in Kotlin and depends on JVM (for File access). If you need to install Kotlin for command line access, check here (brew install kotlin
or scoop install kotlin -g
on windows with Scoop).
File mode:
$ ./gradlekotlinconverter.kts build.gradle
$ kotlinc -script gradlekotlinconverter.kts build.gradle
Clipboard mode:
$ ./gradlekotlinconverter.kts
Motivation: on my own apps, I've used apostrophes ' instead of quotation marks " since forever, so it wasn't fun when I discovered I would need to modify more than 100 lines of code to make Kotlin DSL work. Besides this, the tool also solves a few common issues that might appear, like the task clean(type: Delete)
that becomes a completely different thing.
Sometimes you just want to copy and paste. Just copy whatever you want, run the script, then paste on your IDE. The GIF showcases how simple it is:
Description | Before | After |
---|---|---|
Replace all ' with " | 'kotlin-android' | "kotlin-android" |
Replace "def " with "val " | def appcompat = "1.0.0" | val appcompat = "1.0.0" |
Convert plugins | apply plugin: "kotlin-kapt" | apply(plugin = "kotlin-kapt") |
Add ( ) to dependencies | implementation ":epoxy" | implementation(":epoxy") |
Convert Maven | maven { url "https://jitpack.io" } | maven("https://jitpack.io") |
Convert Sdk Version | compileSdkVersion 28 | compileSdkVersion(28) |
Convert Version Code | versionCode 4 | versionCode = 4 |
Convert Build Types | debuggable true | isDebuggable = true |
Convert proguardFiles | proguardFiles getDef..., "..." | setProguardFiles(listOf(getDef..., "...") |
Convert sourceCompatibility | sourceCompatibility = "1.8" | sourceCompatibility = JavaVersion.VERSION_1_8 |
Convert androidExtensions | androidExtensions { experimental = true } | androidExtensions { isExperimental = true } |
Convert include | include ":app", ":diffutils" | include(":app", ":diffutils") |
Convert signingConfigs | signingConfigs { debug { ... } } | signingConfigs { register("debug") { ... } } |
Convert buildTypes | buildTypes { debug { ... } } | buildTypes { named("debug") { ... } }) |
Convert classpath.exclude | configurations.classpath.exclude group: '...lombok' | configurations.classpath { exclude(group = "...lombok") } |
Kt dependencies (1/3) | "org.jetbrains.kotlin:kotlin-gradle-plugin:$kt_v" | kotlin("gradle-plugin", version = "$kt_v") |
Kt dependencies (2/3) | "org.jetbrains.kotlin:kotlin-stdlib:$kt_v" | kotlin("stdlib") |
Kt dependencies (3/3) | "org.jetbrains.kotlin:kotlin-reflect" | kotlin("reflect") |
Convert signingConfig | signingConfig signingConfigs.release | signingConfig = signingConfigs.getByName("release") |
Convert extras | ext.enableCrashlytics = false | extra.set("enableCrashlytics", false) |
Convert plugins | apply(...) apply(...) | plugins { id(...) id(...) } |
Convert plugin ids | id "io.gitlab.arturbosch.detekt" version "1.0" | id("io.gitlab.arturbosch.detekt") version "1.0" |
Convert ":" to " =" | testImpl(name: "junit", version: "4.12") | testImpl(name = "junit", version = "4.12") |
You can find all the details on the source code.
The script was made to convert build.gradle
in build.gradle.kts
, but it can also help in the migration if you paste something that is not in Kotlin DSL format
and you want it converted. If you paste something like a implementation '...'
and want it converted to implementation("...")
, you are free to call the script on build.gradle.kts
file and see it working as expected.
When applying on build.gradle
, the script will create, for example, build.gradle.kts
.
When applying on a file that already ends in .kts
, the script will overrite the file.
In that case, please make sure you are using git or have a backup, in case things turn out wrong.
- If you find anything, just tell me.
Found a bug? Have an idea for an improvement? Feel free to add an issue.
Copyright 2018 Bernardo Ferrari.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.