Convert from Groovy to Kotlin DSL for Gradle, focused on Android.
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE Added clipboard mode. Jan 10, 2019
README.md Fix broken link on Github. Jan 10, 2019
clipboard_mode.gif Add clipboard gif. Jan 10, 2019
gradlekotlinconverter.kts Fix typo Jan 11, 2019
tests.txt Improved conversion from .kts to .kts! Should work perfectly now, in … Jan 10, 2019

README.md

Gradle Kotlin DSL converter

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.

💻 Getting Started

The tool was developed using kscript. You can find how to install it here. Gradle 5 is currently incompatible with the tool that packages it, so I'm afraid you will need to install kscript before running this tool. It should be really easy, however. After that, you can run the script by calling:

File mode:
$ ./gradlekotlinconverter.kts build.gradle
$ kscript gradlekotlinconverter.kts build.gradle

Clipboard mode (still experimental):
$ ./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.

📋 Clipboard mode

Sometimes you just want to copy and paste. This is still experimental, and implementation requires the dependencies block, but it is really useful already. The GIF showcases how simple it is:

GIF

😱 Things it can do

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") { ... } })

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.

😨 Things it still can't do

  • Dealing with testInstrumentationRunner
  • To avoid bugs, all plugins should be in one block (3th law of SUPERCILEX)
  • If you find anything else, just tell me.

Issue Tracking

Found a bug? Have an idea for an improvement? Feel free to add an issue.

License

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.