How to use Gradle Kotlin DSL in Android

Khoa Pham
Khoa Pham
Nov 6 · 2 min read

kts

settings.gradle.kts

include(":app")

build.gradle.kts

import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin
import org.gradle.kotlin.dsl.*
import org.jetbrains.kotlin.config.KotlinCompilerVersion
plugins {
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
}
//apply {
// from("$rootDir/tools/grgit.gradle")
// from("$rootDir/buildSrc/quality.gradle.kts")
// from("$rootDir/tools/ktlint.gradle")
// from("$rootDir/tools/detekt.gradle")
//}
android {
compileSdkVersion(28)
flavorDimensions("default")
defaultConfig {
applicationId = "com.onmyway133.myapp"
minSdkVersion(26)
targetSdkVersion(28)
// versionCode = ext.get("gitCommitCount") as? Int
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
signingConfigs {
create("release") {
keyAlias = "keyalias"
keyPassword = "keypassword"
storePassword = "storepassword"
storeFile = file("/Users/khoa/Android/Key/keystore")
}
}
buildTypes {
getByName("debug") {
signingConfig = signingConfigs.getByName("debug")
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "$project.rootDir/tools/proguard-rules-debug.pro")
}
getByName("release") {
signingConfig = signingConfigs.getByName("release")
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "$project.rootDir/tools/proguard-rules.pro")
}
}
productFlavors {
create("staging") {
} create("production") { }
}
lintOptions {
lintConfig = file("$project.rootDir/tools/lint-rules.xml")
htmlOutput = file("$project.buildDir/outputs/lint/lint.html")
xmlReport = false
htmlReport = true
}
}
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
implementation("androidx.appcompat:appcompat:1.0.2")
implementation("androidx.core:core-ktx:1.0.2")
implementation("androidx.constraintlayout:constraintlayout:1.1.3")
implementation("com.google.android.material:material:1.0.0")
testImplementation("junit:junit:4.12")
androidTestImplementation("androidx.test:runner:1.1.1")
androidTestImplementation("androidx.test.espresso:espresso-core:3.1.1")
}
tasks.getByName("check").dependsOn("lint")

tools/quality.gradle.kts

plugins {
id("findbugs")
id("pmd")
id("checkstyle")
}
tasks {
val findbugs by registering(FindBugs::class) {
ignoreFailures = false
effort = "max"
reportLevel = "low"
classes = files("$project.buildDir/intermediates/javac")
setExcludeFilter(file("$rootProject.rootDir/tools/findbugs-exclude.xml")) source = fileTree("src/main/java/")
classpath = files()
reports {
xml.isEnabled = false
html.isEnabled = true
html.destination = file("$project.buildDir/outputs/findbugs/findbugs-output.html")
}
}
val pmd by registering(Pmd::class) {
ruleSetFiles = files("${project.rootDir}/tools/pmd-rules.xml")
ignoreFailures = false
ruleSets = listOf<String>()
fileTree()
source(fileTree(baseDir = "src/main/java"))
include("**/*.kt")
exclude("**/gen/**")
reports {
xml.isEnabled = false
html.isEnabled = true
html.destination = file("$project.buildDir/outputs/pmd/pmd.html")
}
}
val checkstyle by registering(Checkstyle::class) {
description = "Check code standard"
group = "verification"
configFile = file("$project.rootDir/tools/checkstyle.xml")
source(fileTree(baseDir = "src"))
include("**/*.kt")
exclude("**/gen/**")
classpath = files()
ignoreFailures = false
}
}

Reference

About Gradle

Fantageek

Simple apps that make sense

Khoa Pham

Written by

Khoa Pham

My apps https://onmyway133.github.io/

Fantageek

Fantageek

Simple apps that make sense

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade