forked from igorwojda/android-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
158 lines (130 loc) · 5.09 KB
/
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import com.android.build.gradle.BaseExtension
import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id(GradlePluginId.DETEKT)
id(GradlePluginId.KTLINT_GRADLE)
id(GradlePluginId.ANDROID_JUNIT_5) apply false
id(GradlePluginId.KOTLIN_ANDROID) apply false
id(GradlePluginId.ANDROID_APPLICATION) apply false
id(GradlePluginId.ANDROID_DYNAMIC_FEATURE) apply false
id(GradlePluginId.ANDROID_LIBRARY) apply false
id(GradlePluginId.SAFE_ARGS) apply false
}
// all projects = root project + sub projects
allprojects {
repositories {
google()
jcenter()
}
// We want to apply ktlint at all project level because it also checks Gradle config files (*.kts)
apply(plugin = GradlePluginId.KTLINT_GRADLE)
// Ktlint configuration for sub-projects
ktlint {
// Version of ktlint cmd tool (Ktlint Gradle plugin is just a wrapper for this tool)
version.set("0.40.0")
verbose.set(true)
android.set(true)
// Uncomment below line and run .\gradlew ktlintCheck to see check ktlint experimental rules
// enableExperimentalRules.set(true)
reporters {
reporter(org.jlleitschuh.gradle.ktlint.reporter.ReporterType.CHECKSTYLE)
}
filter {
exclude { element -> element.file.path.contains("generated/") }
}
}
// Gradle dependency locking - lock all configurations of the app
// More: https://docs.gradle.org/current/userguide/dependency_locking.html
dependencyLocking {
lockAllConfigurations()
}
}
subprojects {
tasks.withType<Test> {
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
}
apply(plugin = GradlePluginId.DETEKT)
detekt {
config = files("$rootDir/detekt.yml")
parallel = true
// By default detekt does not check test source set and gradle specific files, so hey have to be added manually
input = files(
"$rootDir/buildSrc",
"$rootDir/build.gradle.kts",
"$rootDir/settings.gradle.kts",
"src/main/kotlin",
"src/test/kotlin"
)
}
afterEvaluate {
configureAndroid()
}
// While writing versions locks pre-release version of dependencies will be ignored
configurations.all {
resolutionStrategy.componentSelection {
// Accept the highest version matching the requested version that isn't...
all {
// detekt is using pre-release dependencies
val detektExceptions = listOf(
"io.gitlab.arturbosch.detekt",
"com.fasterxml.jackson",
"com.fasterxml.jackson.core",
"com.fasterxml.jackson"
)
if (detektExceptions.any { it == candidate.group }) {
return@all
}
// android lint is using pre-release dependencies
val androidLintExceptions = listOf("com.android.tools.build")
if (androidLintExceptions.any { it == candidate.group }) {
return@all
}
// Do reject pre-release version
val rejected = listOf("alpha", "beta", "rc", "cr", "m", "preview")
.any { Regex("(?i).*[.-]$it[.\\d-]*").matches(candidate.version) }
if (rejected) {
reject("Release candidate")
}
}
}
}
}
fun Project.configureAndroid() {
(project.extensions.findByName("android") as? BaseExtension)?.run {
sourceSets {
map { it.java.srcDir("src/${it.name}/kotlin") }
}
}
}
// JVM target applied to all Kotlin tasks across all sub-projects
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}
// Target version of the generated JVM bytecode. It is used for type resolution.
tasks.withType<Detekt> {
this.jvmTarget = "1.8"
}
/*
Mimics all static checks that run on CI.
Note that this task is intended to run locally (not on CI), because on CI we prefer to have parallel execution
and separate reports for each of the checks (multiple statuses eg. on github PR page).
*/
task("staticCheck") {
group = "verification"
afterEvaluate {
// Filter modules with "lintDebug" task (non-Android modules do not have lintDebug task)
val lintTasks = subprojects.mapNotNull { "${it.name}:lintDebug" }
// Get modules with "testDebugUnitTest" task (app module does not have it)
val testTasks = subprojects.mapNotNull { "${it.name}:testDebugUnitTest" }
.filter { it != "app:testDebugUnitTest" }
// All task dependencies
val taskDependencies =
mutableListOf("app:assembleAndroidTest", "ktlintCheck", "detekt").also {
it.addAll(lintTasks)
it.addAll(testTasks)
}
// By defining Gradle dependency all dependent tasks will run before this "empty" task
dependsOn(taskDependencies)
}
}