Skip to content

Commit

Permalink
Migrate build files to kotlin DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
romansavka-mq committed Jul 23, 2024
1 parent 428a872 commit a8ecd6e
Show file tree
Hide file tree
Showing 45 changed files with 582 additions and 415 deletions.
File renamed without changes.
File renamed without changes.
22 changes: 0 additions & 22 deletions build.gradle

This file was deleted.

9 changes: 9 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.compose) apply false
alias(libs.plugins.kotlin.kapt) apply false
alias(libs.plugins.hilt) apply false
id("deps-management.convention") apply true
}
34 changes: 34 additions & 0 deletions gradle/build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
`kotlin-dsl`
}

group = "com.miquido.buildlogic"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}

dependencies {
implementation(libs.android.gradlePlugin)
implementation(libs.android.tools.common)
implementation(libs.kotlin.gradlePlugin)
implementation(libs.kotlin.compose.gradlePlugin)
implementation(libs.deps.update.scanner.gradlePlugin)
implementation(libs.deps.catalog.updater.gradlePlugin)
}

tasks {
validatePlugins {
enableStricterValidation = true
failOnWarning = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import com.miquido.configureAndroidCompose

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.plugin.compose")
}

android {
configureAndroidCompose(this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import com.miquido.configureAndroid
import com.miquido.configureKotlinAndroid
import com.miquido.projectTargetSdk

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("lint.convention")
id("ktlint.convention")
id("detekt.convention")
}

android {
defaultConfig.targetSdk = projectTargetSdk

configureAndroid(this)
configureKotlinAndroid()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import com.miquido.configureAndroidCompose

plugins {
id("com.android.library")
id("org.jetbrains.kotlin.plugin.compose")
}

android {
configureAndroidCompose(this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import com.miquido.configureAndroid
import com.miquido.configureKotlinAndroid
import com.miquido.disableUnnecessaryAndroidTests
import com.miquido.projectTargetSdk

plugins {
id("com.android.library")
id("org.jetbrains.kotlin.android")
id("lint.convention")
id("ktlint.convention")
id("detekt.convention")
}

android {
configureAndroid(this)
configureKotlinAndroid()

testOptions.targetSdk = projectTargetSdk

androidComponents {
disableUnnecessaryAndroidTests(this)
}
// The resource prefix is derived from the module name,
// so resources inside ":core:module1" must be prefixed with "core_module1_"
resourcePrefix = path
.split("""\W""".toRegex())
.drop(1)
.distinct()
.joinToString(separator = "_")
.lowercase() + "_"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.miquido

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project

internal fun Project.configureAndroid(
commonExtension: CommonExtension<*, *, *, *, *, *>
) = with(commonExtension) {
compileSdk = projectCompileSdk

defaultConfig {
minSdk = projectMinSdk
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

@Suppress("UnstableApiUsage")
testOptions {
animationsDisabled = true
unitTests {
isIncludeAndroidResources = true
all {
it.maxParallelForks = Runtime.getRuntime().availableProcessors() - 1
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.miquido

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.assign
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension

/**
* Configure Compose-specific options
*/
internal fun Project.configureAndroidCompose(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
commonExtension.apply {
buildFeatures {
compose = true
}

dependencies {
add("implementation", libs.findLibrary("androidx-compose-foundation").get())
add("implementation", libs.findLibrary("androidx-compose-runtime").get())
add("implementation", libs.findLibrary("androidx-compose-ui").get())
}
}

extensions.configure<ComposeCompilerGradlePluginExtension> {
enableStrongSkippingMode = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.miquido

import com.android.build.api.variant.LibraryAndroidComponentsExtension
import org.gradle.api.Project

/**
* Disable unnecessary Android instrumented tests for the [project] if there is no `androidTest` folder.
* Otherwise, these projects would be compiled, packaged, installed and ran only to end-up with the following message:
*
* > Starting 0 tests on AVD
*
* Note: this could be improved by checking other potential sourceSets based on buildTypes and flavors.
*/
internal fun Project.disableUnnecessaryAndroidTests(
components: LibraryAndroidComponentsExtension,
) = components.beforeVariants {
it.enableAndroidTest = it.enableAndroidTest
&& projectDir.resolve("src/androidTest").exists()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.miquido

import org.gradle.api.Project
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.kotlin.dsl.assign
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.provideDelegate
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension

internal fun Project.configureKotlinAndroid() {
configureKotlin<KotlinAndroidProjectExtension>()
}

internal fun Project.configureKotlinJvm() {
configureKotlin<KotlinJvmProjectExtension>()
}

/**
* Configure base Kotlin options
*/
private inline fun <reified T : KotlinTopLevelExtension> Project.configureKotlin() = configure<T> {
// Treat all Kotlin warnings as errors (disabled by default)
// Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
val warningsAsErrors: String? by project
when (this) {
is KotlinAndroidProjectExtension -> compilerOptions
is KotlinJvmProjectExtension -> compilerOptions
else -> TODO("Unsupported project extension $this ${T::class}")
}.apply {
kotlinExtension.jvmToolchain {
languageVersion = JavaLanguageVersion.of(11)
}
allWarningsAsErrors = warningsAsErrors.toBoolean()
freeCompilerArgs.add(
// Enable experimental coroutines APIs, including Flow
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.miquido

import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.getByType

val Project.libs
get(): VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.miquido

const val projectVersionName = "1.0.0-beta"
const val projectCompileSdk = 34
const val projectMinSdk = 23
const val projectTargetSdk = 34
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
import nl.littlerobots.vcu.plugin.VersionCatalogUpdateExtension

plugins {
id("com.github.ben-manes.versions")
id("nl.littlerobots.version-catalog-update")
}

tasks.withType<DependencyUpdatesTask> {
rejectVersionIf {
isNonStable(candidate.version)
}
}

extensions.configure<VersionCatalogUpdateExtension> {
sortByKey = false
keep {
keepUnusedLibraries = true
}
}

private fun isNonStable(version: String): Boolean {
val stableKeyword = listOf("RELEASE", "FINAL", "GA").any { version.uppercase().contains(it) }
val regex = "^[0-9,.v-]+(-r)?$".toRegex()
val isStable = stableKeyword || regex.matches(version)
return isStable.not()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import com.miquido.libs

val detektConfiguration: Configuration by configurations.creating

dependencies {
detektConfiguration(libs.findLibrary("detekt").get())
detektConfiguration(libs.findLibrary("detekt-rules-libraries").get())
}

tasks.register("detekt", JavaExec::class) {
group = "verification"
description = "Check Kotlin code style."
classpath = detektConfiguration
mainClass.set("io.gitlab.arturbosch.detekt.cli.Main")
inputs.files(fileTree("src").matching {
include("**/*.kt")
exclude("test/**/*.kt", "androidTest/**/*.kt")
})
outputs.file("build/reports/detekt-report.xml")
outputs.cacheIf { true }

args = listOf(
"--input", "src",
"--excludes", "src/test/**,src/androidTest/**",
"--config", "$rootDir/.codeStyleConfig/detekt-config.yml",
"--report", "xml:build/reports/detekt-report.xml,html:build/reports/detekt-report.html"
)

maxHeapSize = "256m"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import com.miquido.configureKotlinJvm

plugins {
id("org.jetbrains.kotlin.jvm")
id("lint.convention")
id("ktlint.convention")
id("detekt.convention")
}

configureKotlinJvm()
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import com.miquido.libs

val ktlintConfiguration: Configuration by configurations.creating

dependencies {
ktlintConfiguration(libs.findLibrary("ktlint").get())
}

tasks.register("ktlint", JavaExec::class) {
group = "verification"
description = "Check Kotlin code style."
classpath = ktlintConfiguration
mainClass.set("com.pinterest.ktlint.Main")
inputs.files(fileTree("src").matching {
include("**/*.kt")
exclude("test/**/*.kt", "androidTest/**/*.kt")
})
outputs.file("build/reports/ktlint-report.xml")
outputs.cacheIf { true }

args = listOf(
"src/**/*.kt", "!src/test/**/*.kt", "!src/androidTest/**/*.kt",
"--editorconfig=${rootDir}/.codeStyleConfig/.editorconfig",
"--reporter=checkstyle,output=build/reports/ktlint-report.xml",
"--reporter=plain"
)

maxHeapSize = "256m"
}
Loading

0 comments on commit a8ecd6e

Please sign in to comment.