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

Refactor Gradle build logic #474

Merged
merged 2 commits into from
Mar 18, 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
1 change: 1 addition & 0 deletions build-logic/gradle.properties
70 changes: 70 additions & 0 deletions build-logic/plugins/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`kotlin-dsl`
}

group = "ch.srgssr.pillarbox.gradle"

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

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.majorVersion
}
}

dependencies {
compileOnly(libs.android.gradle.api)
compileOnly(libs.kotlinx.kover.gradle)
compileOnly(libs.kotlin.gradle.plugin)
}

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

gradlePlugin {
plugins {
register("PillarboxAndroidApplication") {
id = "ch.srgssr.pillarbox.gradle.android_application"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidApplicationPlugin"
}

register("PillarboxAndroidLibrary") {
id = "ch.srgssr.pillarbox.gradle.android_library"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryPlugin"
}

register("PillarboxAndroidLibraryCompose") {
id = "ch.srgssr.pillarbox.gradle.android_library_compose"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryComposePlugin"
}

register("PillarboxAndroidLibraryLint") {
id = "ch.srgssr.pillarbox.gradle.android_library_lint"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryLintPlugin"
}

register("PillarboxAndroidLibraryPublishing") {
id = "ch.srgssr.pillarbox.gradle.android_library_publishing"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryPublishingPlugin"
}

register("PillarboxAndroidLibraryTestedModule") {
id = "ch.srgssr.pillarbox.gradle.android_library_tested_module"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryTestedModulePlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.AppConfig
import ch.srgssr.pillarbox.gradle.internal.VersionConfig
import ch.srgssr.pillarbox.gradle.internal.configureAndroidLintModule
import ch.srgssr.pillarbox.gradle.internal.configureAndroidModule
import ch.srgssr.pillarbox.gradle.internal.configureComposeModule
import ch.srgssr.pillarbox.gradle.internal.configureKotlinModule
import com.android.build.api.dsl.ApplicationExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.extra

/**
* Custom Gradle plugin to configure an Android library module for Pillarbox.
*/
class PillarboxAndroidApplicationPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.application")
pluginManager.apply("org.jetbrains.kotlin.android")

extensions.configure<ApplicationExtension> {
configureAndroidLintModule(this)
configureAndroidModule(this)
configureComposeModule(this)

defaultConfig {
applicationId = namespace
targetSdk = AppConfig.targetSdk
versionCode = VersionConfig.versionCode()
versionName = VersionConfig.versionName()
vectorDrawables.useSupportLibrary = true
}

signingConfigs {
create("release") {
val password = System.getenv("DEMO_KEY_PASSWORD") ?: extra.properties["pillarbox.keystore.password"] as String?

storeFile = file("./demo.keystore")
storePassword = password
keyAlias = "demo"
keyPassword = password
}
}

buildTypes {
debug {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
}

release {
signingConfig = signingConfigs.getByName("release")
isMinifyEnabled = false
isDebuggable = true

proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}

configureKotlinModule()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.configureComposeModule
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

/**
* Custom Gradle plugin to configure Compose in an Android library module for Pillarbox.
*/
class PillarboxAndroidLibraryComposePlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")

extensions.configure<LibraryExtension> {
configureComposeModule(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.configureAndroidLintModule
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

/**
* Custom Gradle plugin to configure Lint in an Android library module for Pillarbox.
*/
class PillarboxAndroidLibraryLintPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")

extensions.configure<LibraryExtension> {
configureAndroidLintModule(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.configureAndroidModule
import ch.srgssr.pillarbox.gradle.internal.configureKotlinModule
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

/**
* Custom Gradle plugin to configure an Android library module for Pillarbox.
*/
class PillarboxAndroidLibraryPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("org.jetbrains.kotlin.android")

extensions.configure<LibraryExtension> {
configureAndroidModule(this)

defaultConfig {
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false

proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}

configureKotlinModule()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package ch.srgssr.pillarbox.gradle

import VersionConfig
import ch.srgssr.pillarbox.gradle.internal.VersionConfig
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -15,9 +15,9 @@ import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.register

/**
* Custom Gradle plugin to manage publication of a Pillarbox's library modules.
* Custom Gradle plugin to configure publication in an Android library module for Pillarbox.
*/
class PillarboxPublishingPlugin : Plugin<Project> {
class PillarboxAndroidLibraryPublishingPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("org.gradle.maven-publish")
Expand Down Expand Up @@ -49,9 +49,14 @@ class PillarboxPublishingPlugin : Plugin<Project> {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/SRGSSR/pillarbox-android")

credentials {
username = findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
password = findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN")
username = providers.gradleProperty("gpr.user")
.orElse(providers.environmentVariable("USERNAME"))
.get()
password = providers.gradleProperty("gpr.key")
.orElse(providers.environmentVariable("GITHUB_TOKEN"))
.get()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.withType

/**
* Custom Gradle plugin to configure a Pillarbox module for testing.
* Custom Gradle plugin to configure testing in an Android library module for Pillarbox.
*/
class PillarboxTestedModulePlugin : Plugin<Project> {
class PillarboxAndroidLibraryTestedModulePlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("org.jetbrains.kotlinx.kover")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle.internal

import org.gradle.api.JavaVersion

internal object AppConfig {
internal const val minSdk = 21
internal const val targetSdk = 34
internal const val compileSdk = 34
internal const val androidXComposeCompiler = "1.5.10"

// When changing this value, don't forget to also update the Detekt config in the root `build.gradle.kts` file
internal val javaVersion = JavaVersion.VERSION_17
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle.internal

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

internal fun Project.configureAndroidModule(extension: CommonExtension<*, *, *, *, *, *>) = with(extension) {
namespace = "ch.srgssr.pillarbox." + name.removePrefix("pillarbox-").replace('-', '.')
compileSdk = AppConfig.compileSdk

defaultConfig {
minSdk = AppConfig.minSdk
}

compileOptions {
sourceCompatibility = AppConfig.javaVersion
targetCompatibility = AppConfig.javaVersion
}

buildFeatures {
resValues = false
shaders = false
}
}

internal fun configureComposeModule(extension: CommonExtension<*, *, *, *, *, *>) = with(extension) {
buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = AppConfig.androidXComposeCompiler
}
}

internal fun Project.configureKotlinModule() {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = AppConfig.javaVersion.majorVersion
}
}
}

internal fun Project.configureAndroidLintModule(extension: CommonExtension<*, *, *, *, *, *>) = with(extension) {
lint {
abortOnError = true
checkAllWarnings = true
checkDependencies = true
sarifReport = true
sarifOutput = file("${rootProject.rootDir}/build/reports/android-lint/$name.sarif")
disable.add("LogConditional")
}
}
Loading
Loading