Skip to content

Commit

Permalink
refactor: Rework buildSrc
Browse files Browse the repository at this point in the history
Co-authored-by: AntsyLich <[email protected]>
  • Loading branch information
null2264 and AntsyLich committed Dec 25, 2024
1 parent b201e41 commit c09c404
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 86 deletions.
22 changes: 3 additions & 19 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import java.time.format.DateTimeFormatter
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(androidx.plugins.application)
alias(kotlinx.plugins.android)
alias(kotlinx.plugins.compose.compiler)
id("yokai.android.application")
id("yokai.android.application.compose")
alias(kotlinx.plugins.serialization)
alias(kotlinx.plugins.parcelize)
alias(libs.plugins.aboutlibraries)
Expand Down Expand Up @@ -122,7 +121,6 @@ android {

buildFeatures {
viewBinding = true
compose = true
// If you're here because there's not BuildConfig, build the app first, it'll generate it for you
buildConfig = true

Expand Down Expand Up @@ -284,33 +282,19 @@ tasks {
// "-opt-in=kotlin.Experimental",
"-opt-in=kotlin.RequiresOptIn",
"-opt-in=kotlin.ExperimentalStdlibApi",
"-opt-in=coil3.annotation.ExperimentalCoilApi",
"-opt-in=androidx.compose.foundation.layout.ExperimentalLayoutApi",
"-opt-in=androidx.compose.material3.ExperimentalMaterial3Api",
"-opt-in=androidx.compose.ui.ExperimentalComposeUiApi",
"-opt-in=androidx.compose.foundation.ExperimentalFoundationApi",
"-opt-in=androidx.compose.animation.ExperimentalAnimationApi",
// "-opt-in=androidx.compose.animation.graphics.ExperimentalAnimationGraphicsApi",
"-opt-in=coil3.annotation.ExperimentalCoilApi",
// "-opt-in=com.google.accompanist.permissions.ExperimentalPermissionsApi",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-opt-in=kotlinx.coroutines.FlowPreview",
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
"-opt-in=kotlinx.coroutines.InternalCoroutinesApi",
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
)

if (project.findProperty("tachiyomi.enableComposeCompilerMetrics") == "true") {
compilerOptions.freeCompilerArgs.addAll(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" +
(project.layout.buildDirectory.asFile.orNull?.absolutePath ?: "/tmp/yokai") + "/compose_metrics",
)
compilerOptions.freeCompilerArgs.addAll(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" +
(project.layout.buildDirectory.asFile.orNull?.absolutePath ?: "/tmp/yokai") + "/compose_metrics",
)
}
}

// Duplicating Hebrew string assets due to some locale code issues on different devices
Expand Down
46 changes: 0 additions & 46 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import java.util.*
plugins {
alias(libs.plugins.kotlinter)
alias(libs.plugins.gradle.versions)
alias(androidx.plugins.application) apply false
alias(androidx.plugins.library) apply false
alias(kotlinx.plugins.android) apply false
alias(kotlinx.plugins.compose.compiler) apply false
alias(kotlinx.plugins.multiplatform) apply false
alias(kotlinx.plugins.parcelize) apply false
alias(kotlinx.plugins.serialization) apply false
alias(libs.plugins.aboutlibraries) apply false
alias(libs.plugins.firebase.crashlytics) apply false
Expand All @@ -22,46 +16,6 @@ plugins {
alias(libs.plugins.sqldelight) apply false
}

subprojects {
tasks.withType<KotlinCompile> {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
}

plugins.withType<BasePlugin> {
configure<BaseExtension> {
compileSdkVersion(AndroidConfig.compileSdk)
ndkVersion = AndroidConfig.ndk

defaultConfig {
minSdk = AndroidConfig.minSdk
targetSdk = AndroidConfig.targetSdk
ndk {
version = AndroidConfig.ndk
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
isCoreLibraryDesugaringEnabled = true
}

dependencies {
add("coreLibraryDesugaring", libs.desugar)
}
}
}
}

tasks.named("dependencyUpdates", com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask::class.java).configure {
rejectVersionIf {
val stableKeyword = listOf("RELEASE", "FINAL", "GA").any { candidate.version.uppercase(Locale.ROOT).contains(it) }
Expand Down
17 changes: 16 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
plugins {
`kotlin-dsl`
}

dependencies {
implementation(androidx.gradle)
implementation(kotlinx.gradle)
implementation(kotlinx.compose.compiler.gradle)
implementation(gradleApi())

implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
implementation(files(androidx.javaClass.superclass.protectionDomain.codeSource.location))
implementation(files(compose.javaClass.superclass.protectionDomain.codeSource.location))
implementation(files(kotlinx.javaClass.superclass.protectionDomain.codeSource.location))
}

repositories {
gradlePluginPortal()
mavenCentral()
}
google()
}
17 changes: 17 additions & 0 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
create("androidx") {
from(files("../gradle/androidx.versions.toml"))
}
create("compose") {
from(files("../gradle/compose.versions.toml"))
}
create("kotlinx") {
from(files("../gradle/kotlinx.versions.toml"))
}
}
}

rootProject.name = "yokai-buildSrc"
11 changes: 7 additions & 4 deletions buildSrc/src/main/kotlin/AndroidConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import org.gradle.api.JavaVersion as GradleJavaVersion

object AndroidConfig {
const val compileSdk = 35
const val minSdk = 23
const val targetSdk = 35
const val ndk = "27.2.12479018"
const val COMPILE_SDK = 35
const val MIN_SDK = 23
const val TARGET_SDK = 35
const val NDK = "27.2.12479018"
val JavaVersion = GradleJavaVersion.VERSION_17
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import yokai.build.configureCompose

plugins {
id("com.android.application")
kotlin("android")
}

android {
configureCompose(this)
}
15 changes: 15 additions & 0 deletions buildSrc/src/main/kotlin/yokai.android.application.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import yokai.build.configureAndroid
import yokai.build.configureTest

plugins {
id("com.android.application")
kotlin("android")
}

android {
defaultConfig {
targetSdk = AndroidConfig.TARGET_SDK
}
configureAndroid(this)
configureTest()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import yokai.build.configureCompose

plugins {
id("com.android.library")
}

android {
configureCompose(this)
}
11 changes: 11 additions & 0 deletions buildSrc/src/main/kotlin/yokai.android.library.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import yokai.build.configureAndroid
import yokai.build.configureTest

plugins {
id("com.android.library")
}

android {
configureAndroid(this)
configureTest()
}
92 changes: 92 additions & 0 deletions buildSrc/src/main/kotlin/yokai/build/ProjectExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,98 @@
package yokai.build

import com.android.build.api.dsl.CommonExtension
import org.gradle.accessors.dm.LibrariesForAndroidx
import org.gradle.accessors.dm.LibrariesForCompose
import org.gradle.accessors.dm.LibrariesForKotlinx
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.Project
import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeFeatureFlag
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.io.File

val Project.androidx get() = the<LibrariesForAndroidx>()
val Project.compose get() = the<LibrariesForCompose>()
val Project.kotlinx get() = the<LibrariesForKotlinx>()
val Project.libs get() = the<LibrariesForLibs>()

val Project.generatedBuildDir: File get() = project.layout.buildDirectory.asFile.get().resolve("generated/yokai")

internal fun Project.configureAndroid(commonExtension: CommonExtension<*, *, *, *, *, *>) {
commonExtension.apply {
compileSdk = AndroidConfig.COMPILE_SDK
defaultConfig {
minSdk = AndroidConfig.MIN_SDK
ndk {
version = AndroidConfig.NDK
}
}
compileOptions {
sourceCompatibility = AndroidConfig.JavaVersion
targetCompatibility = AndroidConfig.JavaVersion
isCoreLibraryDesugaringEnabled = true
}
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = AndroidConfig.JavaVersion.toString()
// freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
// freeCompilerArgs += "-Xcontext-receivers"
// Treat all Kotlin warnings as errors (disabled by default)
// Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
// val warningsAsErrors: String? by project
// allWarningsAsErrors = warningsAsErrors.toBoolean()
}
}
dependencies {
"coreLibraryDesugaring"(libs.desugar)
}
}

internal fun Project.configureCompose(commonExtension: CommonExtension<*, *, *, *, *, *>) {
pluginManager.apply(kotlinx.plugins.compose.compiler.get().pluginId)

commonExtension.apply {
buildFeatures {
compose = true
}

dependencies {
"implementation"(platform(compose.bom))
}
}

extensions.configure<ComposeCompilerGradlePluginExtension> {
featureFlags.set(setOf(ComposeFeatureFlag.OptimizeNonSkippingGroups))

val enableMetrics = project.providers.gradleProperty("enableComposeCompilerMetrics").orNull.toBoolean()
val enableReports = project.providers.gradleProperty("enableComposeCompilerReports").orNull.toBoolean()

val rootBuildDir = rootProject.layout.buildDirectory.asFile.get()
val relativePath = projectDir.relativeTo(rootDir)

if (enableMetrics) {
rootBuildDir.resolve("compose-metrics").resolve(relativePath).let(metricsDestination::set)
}

if (enableReports) {
rootBuildDir.resolve("compose-reports").resolve(relativePath).let(reportsDestination::set)
}
}
}

internal fun Project.configureTest() {
tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
}
}
4 changes: 2 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(androidx.plugins.library)
alias(kotlinx.plugins.multiplatform)
id("yokai.android.library")
kotlin("multiplatform")
alias(kotlinx.plugins.serialization)
}

Expand Down
4 changes: 2 additions & 2 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
alias(kotlinx.plugins.multiplatform)
id("yokai.android.library")
kotlin("multiplatform")
alias(kotlinx.plugins.serialization)
alias(androidx.plugins.library)
alias(libs.plugins.sqldelight)
}

Expand Down
4 changes: 2 additions & 2 deletions domain/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
alias(kotlinx.plugins.multiplatform)
id("yokai.android.library")
kotlin("multiplatform")
alias(kotlinx.plugins.serialization)
alias(androidx.plugins.library)
}

kotlin {
Expand Down
2 changes: 2 additions & 0 deletions gradle/androidx.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ agp = "8.7.3"
lifecycle = "2.8.7"

[libraries]
gradle = { module = "com.android.tools.build:gradle", version.ref = "agp" }

activity = { module = "androidx.activity:activity-ktx", version.ref = "activity" }
activity-compose = { module = "androidx.activity:activity-compose", version.ref = "activity" }
annotation = { module = "androidx.annotation:annotation", version = "1.9.1" }
Expand Down
5 changes: 4 additions & 1 deletion gradle/kotlinx.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ serialization = "1.7.3"
xml_serialization = "0.90.3"

[libraries]
gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
compose-compiler-gradle = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" }

coroutines-bom = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-bom", version = "1.10.1" }
coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android" }
coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core" }
Expand All @@ -26,4 +29,4 @@ android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" }
parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize" }
4 changes: 2 additions & 2 deletions i18n/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import yokai.build.generatedBuildDir
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(kotlinx.plugins.multiplatform)
alias(androidx.plugins.library)
id("yokai.android.library")
kotlin("multiplatform")
alias(libs.plugins.moko)
}

Expand Down
Loading

0 comments on commit c09c404

Please sign in to comment.