- 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.
- Create a new project. (if converting an existing project, be sure you have a backup)
- Update Gradle Wrapper (as needed)
- Update classpath or dependency versions (as needed)
- Set Java and Kotlin to 1.8
Steps 5-7 must be made before renaming any build scripts.
- 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) - Replace all single quotes with double quotes. ( don't forget settings.gradle)
- Convert spaces to assignments or function calls.
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.
Examine all gradle files and make sure you haven't missed anything.
IMPORTANT: Do all renaming and refactoring one file at a time. Do not try to make all modifications at the same time.
- 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.
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.
The root build file should almost ready to rename. However,
task clean
will need refactoring.
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)
}
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:
- Plugins Block
- Build Types
- Dependencies (as needed)
The Plugins block uses some Kotlin methods instead of id methods.
plugins {
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
}
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
.
If you have a fileTree in your dependencies, use the following:
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
- 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.
- Gradle 5.3.1
- Android Gradle Plugin 3.3.2
- Kotlin 1.3.30
- Android Studio 3.3