加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
build.gradle.kts 4.29 KB
一键复制 编辑 原始数据 按行查看 历史
import com.android.build.gradle.BaseExtension
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
plugins {
id(GradlePluginId.DETEKT)
id(GradlePluginId.KTLINT_GRADLE)
id(GradlePluginId.GRADLE_VERSION_PLUGIN)
id(GradlePluginId.KOTLIN_JVM) apply false
id(GradlePluginId.KOTLIN_ANDROID) apply false
id(GradlePluginId.KOTLIN_ANDROID_EXTENSIONS) 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 build gradle files
apply(plugin = GradlePluginId.KTLINT_GRADLE)
// Ktlint configuration for sub-projects
ktlint {
version.set(CoreVersion.KTLINT)
verbose.set(true)
android.set(true)
// Uncomment below line and run .\gradlew ktlintCheck to see check ktlint experimental rules
// enableExperimentalRules.set(true)
reporters {
reporter(ReporterType.CHECKSTYLE)
}
filter {
exclude { element -> element.file.path.contains("generated/") }
}
}
}
subprojects {
tasks.withType<Test> {
maxParallelForks = (Runtime.getRuntime().availableProcessors() / 2).takeIf { it > 0 } ?: 1
}
apply(plugin = GradlePluginId.DETEKT)
detekt {
config = files("${project.rootDir}/detekt.yml")
parallel = true
}
afterEvaluate {
configureAndroid()
}
}
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()
}
tasks {
// Gradle versions plugin configuration
"dependencyUpdates"(DependencyUpdatesTask::class) {
resolutionStrategy {
componentSelection {
all {
// Do not show pre-release version of library in generated dependency report
val rejected = listOf("alpha", "beta", "rc", "cr", "m", "preview")
.map { qualifier -> Regex("(?i).*[.-]$qualifier[.\\d-]*") }
.any { it.matches(candidate.version) }
if (rejected) {
reject("Release candidate")
}
// kAndroid newest version is 0.8.8 (jcenter), however maven repository contains version 0.8.7 and
// plugin fails to recognize it correctly
if (candidate.group == "com.pawegio.kandroid") {
reject("version ${candidate.version} is broken for ${candidate.group}'")
}
}
}
}
}
}
task("staticCheck") {
description =
"""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 check (multiple statuses eg. on github PR page).
""".trimMargin()
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)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化