From bfec6e0db7a5fc161dd76644f5688b6318313684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rton=20Braun?= Date: Fri, 10 May 2024 13:02:01 +0200 Subject: [PATCH] Set up library convention plugin --- .gitignore | 2 +- build-logic/convention/build.gradle.kts | 32 +++++++++++++ .../kotlin/AndroidLibraryConventionPlugin.kt | 47 +++++++++++++++++++ build-logic/settings.gradle.kts | 14 ++++++ gradle/libs.versions.toml | 9 ++++ requirektx-bundle/build.gradle.kts | 33 ++----------- requirektx-intent/build.gradle.kts | 32 ++----------- requirektx-navigation/build.gradle.kts | 32 ++----------- requirektx-work/build.gradle.kts | 35 ++------------ settings.gradle.kts | 1 + 10 files changed, 116 insertions(+), 121 deletions(-) create mode 100644 build-logic/convention/build.gradle.kts create mode 100644 build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt create mode 100644 build-logic/settings.gradle.kts diff --git a/.gitignore b/.gitignore index 10cfdbf..727c48c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,7 @@ /local.properties /.idea .DS_Store -/build +build/ /captures .externalNativeBuild .cxx diff --git a/build-logic/convention/build.gradle.kts b/build-logic/convention/build.gradle.kts new file mode 100644 index 0000000..69f041b --- /dev/null +++ b/build-logic/convention/build.gradle.kts @@ -0,0 +1,32 @@ +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +plugins { + `kotlin-dsl` +} + +//group = "com.google.samples.apps.nowinandroid.buildlogic" + +java { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} +tasks.withType().configureEach { + kotlinOptions { + jvmTarget = JavaVersion.VERSION_17.toString() + } +} + +dependencies { + compileOnly(libs.android.gradlePlugin) + compileOnly(libs.android.tools.common) + compileOnly(libs.kotlin.gradlePlugin) +} + +gradlePlugin { + plugins { + register("androidLibrary") { + id = "co.zsmb.requirektx.library" + implementationClass = "AndroidLibraryConventionPlugin" + } + } +} \ No newline at end of file diff --git a/build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt new file mode 100644 index 0000000..78a5fc2 --- /dev/null +++ b/build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt @@ -0,0 +1,47 @@ +import com.android.build.gradle.LibraryExtension +import org.gradle.api.JavaVersion +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.configure +import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi +import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension + +class AndroidLibraryConventionPlugin : Plugin { + override fun apply(target: Project) { + with(target) { + with(pluginManager) { + apply("com.android.library") + apply("org.jetbrains.kotlin.multiplatform") + } + + extensions.configure { + androidTarget { + publishLibraryVariants("release") + compilations.all { + kotlinOptions { + jvmTarget = "17" + } + } + } + + explicitApi() + + @OptIn(ExperimentalKotlinGradlePluginApi::class) + compilerOptions { + freeCompilerArgs.add("-progressive") + } + } + + extensions.configure { + compileSdk = 34 + defaultConfig { + minSdk = 21 + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 + } + } + } + } +} diff --git a/build-logic/settings.gradle.kts b/build-logic/settings.gradle.kts new file mode 100644 index 0000000..2907fbf --- /dev/null +++ b/build-logic/settings.gradle.kts @@ -0,0 +1,14 @@ +dependencyResolutionManagement { + repositories { + google() + mavenCentral() + } + versionCatalogs { + create("libs") { + from(files("../gradle/libs.versions.toml")) + } + } +} + +rootProject.name = "build-logic" +include(":convention") diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6875de7..2d52b4d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -11,6 +11,9 @@ navigation-runtime = "2.7.7" robolectric = "4.12.1" work-runtime = "2.9.0" +# TODO +androidTools = "31.3.0" + [libraries] appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" } constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" } @@ -25,7 +28,13 @@ navigation-runtime = { module = "androidx.navigation:navigation-runtime", versio robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" } work-runtime = { module = "androidx.work:work-runtime", version.ref = "work-runtime" } +# TODO STOPSHIP fix these +android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" } +android-tools-common = { group = "com.android.tools", name = "common", version.ref = "androidTools" } +kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" } + [plugins] androidApplication = { id = "com.android.application", version.ref = "agp" } androidLibrary = { id = "com.android.library", version.ref = "agp" } kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } +requireKtxLibrary = { id = "co.zsmb.requirektx.library", version = "unspecified" } diff --git a/requirektx-bundle/build.gradle.kts b/requirektx-bundle/build.gradle.kts index 26bfdf4..e16c119 100644 --- a/requirektx-bundle/build.gradle.kts +++ b/requirektx-bundle/build.gradle.kts @@ -1,18 +1,10 @@ plugins { - alias(libs.plugins.kotlinMultiplatform) - alias(libs.plugins.androidLibrary) + alias(libs.plugins.requireKtxLibrary) } -kotlin { - androidTarget { - publishLibraryVariants("release") - compilations.all { - kotlinOptions { - jvmTarget = "17" - } - } - } +android.namespace = "co.zsmb.requirektx.bundle" +kotlin { sourceSets { commonMain.dependencies {} commonTest.dependencies { @@ -25,23 +17,4 @@ kotlin { } } } - - explicitApi() - compilerOptions { - freeCompilerArgs.add("-progressive") - } -} - -android { - namespace = "co.zsmb.requirektx.bundle" - compileSdk = 34 - - defaultConfig { - minSdk = 21 - } - - compileOptions { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - } } diff --git a/requirektx-intent/build.gradle.kts b/requirektx-intent/build.gradle.kts index 13e4b00..c945401 100644 --- a/requirektx-intent/build.gradle.kts +++ b/requirektx-intent/build.gradle.kts @@ -1,18 +1,10 @@ plugins { - alias(libs.plugins.kotlinMultiplatform) - alias(libs.plugins.androidLibrary) + alias(libs.plugins.requireKtxLibrary) } -kotlin { - androidTarget { - publishLibraryVariants("release") - compilations.all { - kotlinOptions { - jvmTarget = "17" - } - } - } +android.namespace = "co.zsmb.requirektx.intent" +kotlin { sourceSets { commonMain.dependencies { api(project(":requirektx-bundle")) @@ -27,22 +19,4 @@ kotlin { } } } - - explicitApi() - compilerOptions { - freeCompilerArgs.add("-progressive") - } -} - -android { - namespace = "co.zsmb.requirektx.intent" - compileSdk = 34 - - defaultConfig { - minSdk = 21 - } - compileOptions { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - } } diff --git a/requirektx-navigation/build.gradle.kts b/requirektx-navigation/build.gradle.kts index 5ecbd8f..5c8f949 100644 --- a/requirektx-navigation/build.gradle.kts +++ b/requirektx-navigation/build.gradle.kts @@ -1,18 +1,10 @@ plugins { - alias(libs.plugins.kotlinMultiplatform) - alias(libs.plugins.androidLibrary) + alias(libs.plugins.requireKtxLibrary) } -kotlin { - androidTarget { - publishLibraryVariants("release") - compilations.all { - kotlinOptions { - jvmTarget = "17" - } - } - } +android.namespace = "co.zsmb.requirektx.navigation" +kotlin { sourceSets { commonMain.dependencies { api(project(":requirektx-bundle")) @@ -30,22 +22,4 @@ kotlin { } } } - - explicitApi() - compilerOptions { - freeCompilerArgs.add("-progressive") - } -} - -android { - namespace = "co.zsmb.requirektx.navigation" - compileSdk = 34 - - defaultConfig { - minSdk = 21 - } - compileOptions { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - } } diff --git a/requirektx-work/build.gradle.kts b/requirektx-work/build.gradle.kts index e31742e..57b91d6 100644 --- a/requirektx-work/build.gradle.kts +++ b/requirektx-work/build.gradle.kts @@ -1,20 +1,11 @@ plugins { - alias(libs.plugins.kotlinMultiplatform) - alias(libs.plugins.androidLibrary) + alias(libs.plugins.requireKtxLibrary) } -kotlin { - androidTarget { - publishLibraryVariants("release") - compilations.all { - kotlinOptions { - jvmTarget = "17" - } - } - } +android.namespace = "co.zsmb.requirektx.work" +kotlin { sourceSets { - commonMain.dependencies {} commonTest.dependencies { implementation(libs.kotlin.test) } @@ -31,28 +22,8 @@ kotlin { } } } - - explicitApi() - compilerOptions { - freeCompilerArgs.add("-progressive") - } } tasks.withType { useJUnitPlatform() } - -android { - namespace = "co.zsmb.requirektx.work" - compileSdk = 34 - - defaultConfig { - minSdk = 21 - testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" - consumerProguardFile("consumer-rules.pro") - } - compileOptions { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - } -} diff --git a/settings.gradle.kts b/settings.gradle.kts index 1f30134..3668d92 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,4 +1,5 @@ pluginManagement { + includeBuild("build-logic") repositories { google() mavenCentral()