Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modularization. Convention Plugins #54

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 3 additions & 41 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.jetbrains.kotlin.android)
alias(libs.plugins.jetbrains.kotlin.compose.compiler)
alias(libs.plugins.whatthecodec.android.application)
alias(libs.plugins.whatthecodec.android.compose)
alias(libs.plugins.whatthecodec.android.hilt)
alias(libs.plugins.detekt)
alias(libs.plugins.google.ksp)
alias(libs.plugins.google.hilt)
}

android {
namespace = "com.javernaut.whatthecodec"
compileSdk = 34
defaultConfig {
minSdk = 24
targetSdk = 34

versionCode = 4100
versionName = "4.1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand All @@ -31,9 +21,6 @@ android {
signingConfig = signingConfigs.getByName("debug")
}
}
buildFeatures {
compose = true
}
bundle {
language {
enableSplit = true
Expand All @@ -45,10 +32,6 @@ android {
enableSplit = true
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
signingConfigs {
create("google") {
val alias = rootProject.ext["signing.google.alias"] as? String
Expand Down Expand Up @@ -118,38 +101,19 @@ android {
}
}

kotlin {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_1_8)
freeCompilerArgs.add("-Xcontext-receivers")
}
}

detekt {
config.setFrom("$projectDir/../config/detekt/detekt.yml")
buildUponDefaultConfig = true
}

hilt {
enableAggregatingTask = true
}

dependencies {
implementation(project(":features:settings:api"))
implementation(project(":features:settings:data"))
implementation(project(":features:settings:ui"))
implementation(project(":features:home:localization"))

ksp(libs.dagger.compiler)
ksp(libs.dagger.hilt.compiler)
implementation(libs.dagger.hilt.android)

implementation(libs.bundles.kotlinx.coroutines)

implementation(platform(libs.androidx.compose.bom))
implementation(libs.bundles.androidx.compose)
debugImplementation(libs.androidx.ui.test.manifest)

implementation(libs.bundles.androidx.lifecycle)
implementation(libs.androidx.palette)
implementation(libs.androidx.savedstate)
Expand All @@ -161,8 +125,6 @@ dependencies {

testImplementation(libs.mockk.unit)

androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
androidTestImplementation(libs.androidx.test.core)
androidTestImplementation(libs.androidx.test.runner)
androidTestImplementation(libs.androidx.test.rules)
Expand Down
64 changes: 64 additions & 0 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
`kotlin-dsl`
}

group = "com.javernaut.whatthecodec.buildlogic"

// Configure the build-logic plugins to target JDK 17
// This matches the JDK used to build the project, and is not related to what is running on device.
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}

dependencies {
compileOnly(libs.android.gradlePlugin)
compileOnly(libs.android.tools.common)
compileOnly(libs.compose.gradlePlugin)
compileOnly(libs.kotlin.gradlePlugin)
compileOnly(libs.ksp.gradlePlugin)
compileOnly(libs.hilt.gradlePlugin)
// Accessing the Version Catalog in convention plugins according to
// https://github.com/gradle/gradle/issues/15383#issuecomment-779893192
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

tasks {
validatePlugins {
enableStricterValidation = true
failOnWarning = true
}
}

gradlePlugin {
plugins {
register("androidApplication") {
id = "whatthecodec.android.application"
implementationClass = "AndroidApplicationConventionPlugin"
}
register("androidCompose") {
id = "whatthecodec.android.compose"
implementationClass = "AndroidComposeConventionPlugin"
}
register("androidLibrary") {
id = "whatthecodec.android.library"
implementationClass = "AndroidLibraryConventionPlugin"
}
register("androidHilt") {
id = "whatthecodec.android.hilt"
implementationClass = "AndroidHiltConventionPlugin"
}
register("jvmLibrary") {
id = "whatthecodec.jvm.library"
implementationClass = "JvmLibraryConventionPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import com.android.build.api.dsl.ApplicationExtension
import com.javernaut.whatthecodec.buildlogic.Versions
import com.javernaut.whatthecodec.buildlogic.configureKotlinAndroid
import com.javernaut.whatthecodec.buildlogic.extension.apply
import com.javernaut.whatthecodec.buildlogic.extension.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

class AndroidApplicationConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply(libs.plugins.android.application)
apply(libs.plugins.jetbrains.kotlin.android)
}

extensions.configure<ApplicationExtension> {
configureKotlinAndroid(this)
defaultConfig.targetSdk = Versions.targetSdk
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.dsl.LibraryExtension
import com.javernaut.whatthecodec.buildlogic.configureAndroidCompose
import com.javernaut.whatthecodec.buildlogic.extension.apply
import com.javernaut.whatthecodec.buildlogic.extension.hasPlugin
import com.javernaut.whatthecodec.buildlogic.extension.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

class AndroidComposeConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply(libs.plugins.jetbrains.kotlin.compose)

when {
hasPlugin(libs.plugins.android.application) ->
configure<ApplicationExtension> {
configureAndroidCompose(this)
}

hasPlugin(libs.plugins.android.library) ->
configure<LibraryExtension> {
configureAndroidCompose(this)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import com.javernaut.whatthecodec.buildlogic.extension.apply
import com.javernaut.whatthecodec.buildlogic.extension.implementation
import com.javernaut.whatthecodec.buildlogic.extension.ksp
import com.javernaut.whatthecodec.buildlogic.extension.libs
import dagger.hilt.android.plugin.HiltExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies

class AndroidHiltConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply(libs.plugins.google.ksp)
apply(libs.plugins.google.hilt)
}

dependencies {
implementation(libs.dagger.hilt.android)
ksp(libs.dagger.hilt.compiler)
}

extensions.configure<HiltExtension> {
enableAggregatingTask = true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import com.android.build.gradle.LibraryExtension
import com.javernaut.whatthecodec.buildlogic.Versions
import com.javernaut.whatthecodec.buildlogic.configureKotlinAndroid
import com.javernaut.whatthecodec.buildlogic.extension.apply
import com.javernaut.whatthecodec.buildlogic.extension.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

class AndroidLibraryConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply(libs.plugins.android.library)
apply(libs.plugins.jetbrains.kotlin.android)
}

extensions.configure<LibraryExtension> {
configureKotlinAndroid(this)
defaultConfig.targetSdk = Versions.targetSdk
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import com.javernaut.whatthecodec.buildlogic.configureKotlinJvm
import com.javernaut.whatthecodec.buildlogic.extension.apply
import com.javernaut.whatthecodec.buildlogic.extension.libs
import org.gradle.api.Plugin
import org.gradle.api.Project

class JvmLibraryConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply(libs.plugins.jetbrains.kotlin.jvm)
}
configureKotlinJvm()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.javernaut.whatthecodec.buildlogic

import com.android.build.api.dsl.CommonExtension
import com.javernaut.whatthecodec.buildlogic.extension.androidTestImplementation
import com.javernaut.whatthecodec.buildlogic.extension.debugImplementation
import com.javernaut.whatthecodec.buildlogic.extension.implementation
import com.javernaut.whatthecodec.buildlogic.extension.libs
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 {
implementation(platform(libs.androidx.compose.bom))
implementation(libs.bundles.androidx.compose)

debugImplementation(libs.androidx.ui.test.manifest)

androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
}
}

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

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
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

/**
* Configure base Kotlin with Android options
*/
internal fun Project.configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
commonExtension.apply {
compileSdk = Versions.compileSdk

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

compileOptions {
sourceCompatibility = Versions.javaSourceCompatibility
targetCompatibility = Versions.javaTargetCompatibility
}
@Suppress("UnstableApiUsage")
testOptions.animationsDisabled = true
}

configureKotlin<KotlinAndroidProjectExtension>()
}

/**
* Configure base Kotlin options for JVM (non-Android)
*/
internal fun Project.configureKotlinJvm() {
extensions.configure<JavaPluginExtension> {
sourceCompatibility = Versions.javaSourceCompatibility
targetCompatibility = Versions.javaTargetCompatibility
}

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 {
jvmTarget = Versions.kotlinJvmTarget
allWarningsAsErrors = warningsAsErrors.toBoolean()
freeCompilerArgs.add("-Xcontext-receivers")
}
}
Loading