Skip to content

Instantly share code, notes, and snippets.

@alwarren
Last active April 24, 2019 15:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alwarren/28f292e59d241cea4c816eefff18d681 to your computer and use it in GitHub Desktop.
Save alwarren/28f292e59d241cea4c816eefff18d681 to your computer and use it in GitHub Desktop.
Documents the process of converting a new Android project to Kotlin Gradle DSL.

Conversion from Gradle to Kotlin DSL

Tips

  • IMPORTANT: Things can break badly if you don't do things in the correct order. Make backups or if you're using version control, create branches that you can recover from.
  • Do changes step-wise. That is, for all gradle files change single quotes, then do syntax changes, then do the plugins block.
  • Sync Gradle often. Make each type of change for files then do a Gradle sync.
  • When in doubt after a sync, rebuild the project.

Initial Setup

  1. Create a new project. (if converting an existing project, be sure you have a backup)
  2. Update Gradle Wrapper (as needed)
  3. Update classpath or dependency versions (as needed)
  4. Set Java and Kotlin to 1.8

Important

Steps 5-7 must be made before renaming any build scripts.

Prepare Gradle files

  1. Remove all ext. variables and replace their usages with values. See the examples below or the docs for alternatives. (Kotlin Gradle doesn't know about ext values)
  2. Replace all single quotes with double quotes. ( don't forget settings.gradle)
  3. Convert spaces to assignments or function calls.

Some examples

In settings.gradle:

  • include is a function call.

In the root build.gradle:

  • Remove ext.kotlin_version = "1.3.21" and replace the instance in class path with a value
  • classpath is a function call.
  • In the clean task delete is a function call.

In the app build.gradle:

  • Replace any instances of $kotlin_version with a value.
  • Anything that ends with Version is a function call.
  • In buildTypes, proguardFiles is a function call
  • Everything in the dependencies block is a function call.
  • Everything else is an assignment.
  • If you're not sure, try to make it an assignment. If that doesn't work, make it a function call.

Verify Steps 5-7

Examine all gradle files and make sure you haven't missed anything.

Add the .kts extension and refactor.

IMPORTANT: Do all renaming and refactoring one file at a time. Do not try to make all modifications at the same time.

Tips

  • When you rename a file, you'll get a problems window. If you're sure you want to proceed, click Continue.
  • Each file that you rename may require further refactoring. You're likely to see some compile errors in the editor.
  • When you see a compile error in the editor, you'll need to refactor that section of code.

Note

Once you rename the file, when syncing Gradle, you may see a message bar that look like this:

There are new script dependencies available.

Choose Apply dependencies to apply your changes or Enable auto-reload to turn on auto-reload.

Rename and Refactor Root Build File

The root build file should almost ready to rename. However, task clean will need refactoring.

Note

Refactoring task clean is version specific.

For Gradle version 5.3.1, we used this:

tasks{
    val clean by registering(Delete::class) {
        delete(buildDir)
    }
}

For Gradle version 4.10.1, we used this:

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

Rename and Refactor App Build File

Note

There are a number of changes necessary after renaming the file. The recommended approach is to rename the build file and then work through each error one-by-one.

In general, these need to be changed:

  1. Plugins Block
  2. Build Types
  3. Dependencies (as needed)

1) Plugins Block

The Plugins block uses some Kotlin methods instead of id methods.

plugins {
    id("com.android.application")
    kotlin("android")
    kotlin("android.extensions")
    kotlin("kapt")
}

2) Build Types

The build types block uses a lambda.

buildTypes {
    getByName("release") {
        isMinifyEnabled = false
        proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
    }
}

Note: minifyEnabled is renamed isMinifyEnabled.

3) Dependencies

If you have a fileTree in your dependencies, use the following:

implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

Takeaways and Warnings

  • These step, tips, and notes were created after numerous attempts. There is limited documentation and limited resources available online.
  • There are breaking version changes. Many of the examples, videos, and tutorials do not mention which versions they used.
  • If you try to convert an existing app, you MUST maintain backups before, during, and after conversion. In come cases, broken builds may be completely unrecoverable.

Development Environment:

  • Gradle 5.3.1
  • Android Gradle Plugin 3.3.2
  • Kotlin 1.3.30
  • Android Studio 3.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment