Introduction
I'm working on a small timekeeping app for Android, mostly for learning purposes. A few days ago I decided to convert the Gradle build scripts (originally written in Groovy) into Kotlin. Since I'm new both to Gradle and Kotlin, it was not trivial, but after consulting a dozen web resources, I finally managed to get things working.
The main resource I used is the official Groovy to Kotlin migration guide, but I also had to dig through GitHub to find how to convert certain Groovy constructs not directly mentioned in the guide.
The root script
The root build.gradle
script:
buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
... converted to build.gradle.kts
. Note I also added a ktlint plugin to the project and added ktlint configuration block to the script, turning on the debug and verbose switches:
plugins {
id("org.jlleitschuh.gradle.ktlint") version "9.2.1"
}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
val kotlinVersion = "1.3.61"
extra["kotlinVersion"] = kotlinVersion
classpath("com.android.tools.build:gradle:3.5.3")
classpath(kotlin("gradle-plugin", version = kotlinVersion))
}
}
allprojects {
repositories {
google()
jcenter()
}
}
tasks.register<Delete>("clean") {
delete(rootProject.buildDir)
ktlint {
debug.set(true)
verbose.set(true)
}
Settings script
The root settings.gradle
script:
include ':app'
rootProject.name='TimeSlack'
... converted to settings.gradle.kts
script:
include(":app")
rootProject.name = "TimeSlack"
The module script
The module build.gradle
script:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "net.igorbrejc.timeslack"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
implementation group: 'com.google.guava', name: 'guava', version: '28.2-android'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.6.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
... converted to build.gradle.kts
:
plugins {
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
id("org.jlleitschuh.gradle.ktlint")
}
android {
compileSdkVersion(29)
buildToolsVersion("29.0.3")
defaultConfig {
applicationId = "net.igorbrejc.timeslack"
minSdkVersion(15)
targetSdkVersion(29)
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro")
}
}
}
dependencies {
val kotlinVersion: String? by extra
implementation(
fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(kotlin("stdlib-jdk7", version = kotlinVersion))
implementation("androidx.appcompat:appcompat:1.1.0")
implementation("androidx.core:core-ktx:1.2.0")
implementation("androidx.constraintlayout:constraintlayout:1.1.3")
implementation("com.google.android.material:material:1.1.0")
implementation(
group = "com.google.guava", name = "guava", version = "28.2-android")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.6.0")
androidTestImplementation("androidx.test.ext:junit:1.1.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0")
}
// tells all test tasks to use Gradle's built-in JUnit 5 support
tasks.withType<Test> {
useJUnitPlatform()
// tells the test runner to display results of all tests,
// not just failed ones
testLogging {
events("passed", "skipped", "failed")
}
}
Note we added an instruction for all test tasks to use Gradle's built-in JUnit 5 support instead of the obsolete JUnit Gradle plug-in.