How to use Gradle Kotlin DSL in Android

Issue #285

kts

settings.gradle.kts

1
include(":app")

build.gradle.kts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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

Comments