From 44f0e54196b234e4bcfc56b53aa4b1c3762ab6c6 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 01:12:17 +0200 Subject: [PATCH 01/16] Migration to Kotlin 2.0.0 and Compose compiler plugin --- build.gradle | 2 +- gradle/libs.versions.toml | 11 +++-- ...droidApplicationComposeConventionPlugin.kt | 1 + .../AndroidLibraryComposeConventionPlugin.kt | 1 + .../android/buildlogic/AndroidCompose.kt | 40 ------------------- .../android/buildlogic/KotlinAndroid.kt | 38 ++++++++---------- renovate.json | 5 +-- 7 files changed, 27 insertions(+), 71 deletions(-) diff --git a/build.gradle b/build.gradle index 864064c..838d320 100644 --- a/build.gradle +++ b/build.gradle @@ -31,7 +31,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'org.jetbrains.kotlin.android' version '1.9.20' apply false + id 'org.jetbrains.kotlin.android' version '2.0.0' apply false id 'io.github.gradle-nexus.publish-plugin' version '1.3.0' } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7667804..9cc3843 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,7 +6,6 @@ androidxActivity = "1.9.0" androidxAnnotation = "1.8.0" androidxAppCompat = "1.7.0" androidxComposeBom = "2024.06.00" # https://developer.android.com/jetpack/compose/bom/bom-mapping -androidxComposeCompiler = "1.5.14" # https://developer.android.com/jetpack/androidx/releases/compose-kotlin androidxComposeRuntimeTracing = "1.0.0-beta01" androidxCore = "1.13.1" androidxCoreSplashscreen = "1.0.1" @@ -34,10 +33,10 @@ hilt = "2.51.1" hiltExt = "1.2.0" jacoco = "0.8.7" junit4 = "4.13.2" -kotlin = "1.9.24" -kotlinxCoroutines = "1.8.0" -kotlinxDatetime = "0.5.0" -kotlinxSerializationJson = "1.6.3" +kotlin = "2.0.0" +kotlinxCoroutines = "1.9.0-RC" +kotlinxDatetime = "0.6.0" +kotlinxSerializationJson = "1.7.0" kotlinCompose = "1.6.11" ksp = "1.9.24-1.0.20" lint = "31.5.0" @@ -102,7 +101,6 @@ androidx-activity-compose = { group = "androidx.activity", name = "activity-comp androidx-annotation = { group = "androidx.annotation", name = "annotation", version.ref = "androidxAnnotation" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidxAppCompat" } androidx-benchmark-macro = { group = "androidx.benchmark", name = "benchmark-macro-junit4", version.ref = "androidxMacroBenchmark" } -androidx-compose-compiler = { group = "androidx.compose.compiler", name = "compiler", version.ref = "androidxComposeCompiler" } androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "androidxComposeBom" } androidx-compose-foundation = { group = "androidx.compose.foundation", name = "foundation" } androidx-compose-foundation-layout = { group = "androidx.compose.foundation", name = "foundation-layout" } @@ -273,6 +271,7 @@ kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", versi kotlin-parcelize = { id = "org.jetbrains.kotlin.plugin.parcelize", version.ref = "kotlin" } kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" } kotlin-compose = { id = "org.jetbrains.compose", version.ref = "kotlinCompose" } +compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" } protobuf = { id = "com.google.protobuf", version.ref = "protobufPlugin" } secrets = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secrets" } publish = { id = "com.gradle.plugin-publish", version.ref = "publishPlugin" } diff --git a/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt b/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt index d66357e..6bba67b 100644 --- a/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt @@ -43,6 +43,7 @@ class AndroidApplicationComposeConventionPlugin : Plugin { with(pluginManager) { apply("no.nordicsemi.android.gradle.application") apply("no.nordicsemi.android.gradle.kotlin") + apply("org.jetbrains.kotlin.plugin.compose") } val extension = extensions.getByType() diff --git a/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt b/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt index 788ee27..6b61ccb 100644 --- a/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt @@ -43,6 +43,7 @@ class AndroidLibraryComposeConventionPlugin : Plugin { with(pluginManager) { apply("no.nordicsemi.android.gradle.library") apply("no.nordicsemi.android.gradle.kotlin") + apply("org.jetbrains.kotlin.plugin.compose") } val extension = extensions.getByType() diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt index 15561dd..bd49754 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt @@ -36,8 +36,6 @@ package no.nordicsemi.android.buildlogic import com.android.build.api.dsl.CommonExtension import org.gradle.api.Project import org.gradle.kotlin.dsl.dependencies -import org.gradle.kotlin.dsl.withType -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile /** * Configure Compose-specific options @@ -50,48 +48,10 @@ internal fun Project.configureAndroidCompose( compose = true } - composeOptions { - kotlinCompilerExtensionVersion = libs.findVersion("androidxComposeCompiler").get().toString() - } - dependencies { val bom = libs.findLibrary("androidx-compose-bom").get() add("implementation", platform(bom)) add("androidTestImplementation", platform(bom)) } } - - tasks.withType().configureEach { - kotlinOptions { - freeCompilerArgs = freeCompilerArgs + buildComposeMetricsParameters() - } - } -} - -private fun Project.buildComposeMetricsParameters(): List { - val metricParameters = mutableListOf() - val enableMetricsProvider = project.providers.gradleProperty("enableComposeCompilerMetrics") - val relativePath = projectDir.relativeTo(rootDir) - - val enableMetrics = (enableMetricsProvider.orNull == "true") - if (enableMetrics) { - val buildDir = rootProject.layout.buildDirectory.asFile.get() - val metricsFolder = buildDir.resolve("compose-metrics").resolve(relativePath) - metricParameters.add("-P") - metricParameters.add( - "plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" + metricsFolder.absolutePath - ) - } - - val enableReportsProvider = project.providers.gradleProperty("enableComposeCompilerReports") - val enableReports = (enableReportsProvider.orNull == "true") - if (enableReports) { - val buildDir = rootProject.layout.buildDirectory.asFile.get() - val reportsFolder = buildDir.resolve("compose-reports").resolve(relativePath) - metricParameters.add("-P") - metricParameters.add( - "plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" + reportsFolder.absolutePath - ) - } - return metricParameters.toList() } diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt index a2d6729..c879d64 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt @@ -34,9 +34,10 @@ package no.nordicsemi.android.buildlogic import com.android.build.api.dsl.CommonExtension import org.gradle.api.JavaVersion import org.gradle.api.Project -import org.gradle.api.plugins.ExtensionAware import org.gradle.kotlin.dsl.provideDelegate -import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.dsl.KotlinVersion +import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile /** * Configure base Kotlin with Android options @@ -50,26 +51,21 @@ internal fun Project.configureKotlinAndroid( targetCompatibility = JavaVersion.VERSION_17 } - kotlinOptions { - // 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() + tasks.named("compileKotlin", KotlinJvmCompile::class.java) { + compilerOptions { + // 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.set(warningsAsErrors.toBoolean()) - freeCompilerArgs = freeCompilerArgs + listOf( - "-opt-in=kotlin.RequiresOptIn", - // Enable experimental coroutines APIs, including Flow - "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi", - "-opt-in=kotlinx.coroutines.FlowPreview", - "-opt-in=kotlin.Experimental", - ) - - // Set JVM target to 1.17 - jvmTarget = JavaVersion.VERSION_17.toString() + languageVersion.set(KotlinVersion.KOTLIN_2_0) + apiVersion.set(KotlinVersion.KOTLIN_2_0) + jvmTarget.set(JvmTarget.JVM_17) + optIn.add("kotlin.RequiresOptIn") + optIn.add("kotlinx.coroutines.ExperimentalCoroutinesApi") + optIn.add("kotlinx.coroutines.FlowPreview") + optIn.add("kotlin.Experimental") + } } } } - -fun CommonExtension<*, *, *, *, *, *>.kotlinOptions(block: KotlinJvmOptions.() -> Unit) { - (this as ExtensionAware).extensions.configure("kotlinOptions", block) -} diff --git a/renovate.json b/renovate.json index 527d26f..9dd1fb0 100644 --- a/renovate.json +++ b/renovate.json @@ -10,15 +10,14 @@ "packageRules": [ { "matchPackagePatterns": [ - "androidx.compose.compiler:compiler", "com.google.devtools.ksp", - "org.jetbrains.kotlin.*", - "^androidx.compose.compiler" + "org.jetbrains.kotlin.*" ], "groupName": "kotlin" }, { "matchPackageNames": [ + "org.slf4j:slf4j-api", "org.slf4j:slf4j-simple" ], "enabled": false From 5da3c8dc7f10a9da83f4a323c3984277cf693c4a Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 01:12:38 +0200 Subject: [PATCH 02/16] Gradle wrapper 8.8 --- gradle/wrapper/gradle-wrapper.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index a80b22c..a441313 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME From 058b962322426af2ef952ff638470e648196d76b Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 01:12:51 +0200 Subject: [PATCH 03/16] Unused files removed --- fastlane/README.md | 48 --------------------------------------------- fastlane/report.xml | 18 ----------------- 2 files changed, 66 deletions(-) delete mode 100644 fastlane/README.md delete mode 100644 fastlane/report.xml diff --git a/fastlane/README.md b/fastlane/README.md deleted file mode 100644 index 0dbe80b..0000000 --- a/fastlane/README.md +++ /dev/null @@ -1,48 +0,0 @@ -fastlane documentation ----- - -# Installation - -Make sure you have the latest version of the Xcode command line tools installed: - -```sh -xcode-select --install -``` - -For _fastlane_ installation instructions, see [Installing _fastlane_](https://docs.fastlane.tools/#installing-fastlane) - -# Available Actions - -## Android - -### android deployPlugins - -```sh -[bundle exec] fastlane android deployPlugins -``` - -Deploy plugins to Gradle Portal. - -### android deployVersionCatalog - -```sh -[bundle exec] fastlane android deployVersionCatalog -``` - -Deploy version catalog to Nexus. - -### android deployLocal - -```sh -[bundle exec] fastlane android deployLocal -``` - -Deploy libraries to Maven locally on the PC. - ----- - -This README.md is auto-generated and will be re-generated every time [_fastlane_](https://fastlane.tools) is run. - -More information about _fastlane_ can be found on [fastlane.tools](https://fastlane.tools). - -The documentation of _fastlane_ can be found on [docs.fastlane.tools](https://docs.fastlane.tools). diff --git a/fastlane/report.xml b/fastlane/report.xml deleted file mode 100644 index 70f926a..0000000 --- a/fastlane/report.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - - - From 832c6033bd33ae59a04814a25a4e071e261f7edb Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 01:13:10 +0200 Subject: [PATCH 04/16] Unused libraries removed from toml --- gradle/libs.versions.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 9cc3843..3a8059e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -89,11 +89,7 @@ firebaseCrashlyticsPlugins = "2.9.9" kmmIosPublishPlugin = "2.0.3" [libraries] -accompanist-flowlayout = { group = "com.google.accompanist", name = "accompanist-flowlayout", version.ref = "accompanist" } -accompanist-swiperefresh = { group = "com.google.accompanist", name = "accompanist-swiperefresh", version.ref = "accompanist" } accompanist-systemuicontroller = { group = "com.google.accompanist", name = "accompanist-systemuicontroller", version.ref = "accompanist" } -accompanist-pager = { group = "com.google.accompanist", name = "accompanist-pager", version.ref = "accompanist" } -accompanist-pagerindicators = { group = "com.google.accompanist", name = "accompanist-pager-indicators", version.ref = "accompanist" } accompanist-placeholder = { group = "com.google.accompanist", name = "accompanist-placeholder-material", version.ref = "accompanist" } accompanist-permissions = { group = "com.google.accompanist", name = "accompanist-permissions", version.ref = "accompanist" } android-desugarJdkLibs = { group = "com.android.tools", name = "desugar_jdk_libs", version.ref = "androidDesugarJdkLibs" } From 4f827ec9c11d1eb57f1e7117e4382522a219dd15 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 10:00:41 +0200 Subject: [PATCH 05/16] Moving common dependencies to one place --- .../main/kotlin/AndroidApplicationComposeConventionPlugin.kt | 3 --- .../src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt | 3 --- .../kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt | 3 +++ 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt b/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt index 6bba67b..50d66f9 100644 --- a/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt @@ -53,9 +53,6 @@ class AndroidApplicationComposeConventionPlugin : Plugin { dependencies { // Add Material 3 Compose add("implementation", libs.findLibrary("androidx.compose.material3").get()) - // Add UI Tooling and Previews - add("debugImplementation", libs.findLibrary("androidx.compose.ui.tooling").get()) - add("implementation", libs.findLibrary("androidx.compose.ui.tooling.preview").get()) } } } diff --git a/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt b/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt index 6b61ccb..84ed1d8 100644 --- a/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt @@ -53,9 +53,6 @@ class AndroidLibraryComposeConventionPlugin : Plugin { dependencies { // Add Material 3 Compose add("implementation", libs.findLibrary("androidx.compose.material3").get()) - // Add UI Tooling and Previews - add("debugImplementation", libs.findLibrary("androidx.compose.ui.tooling").get()) - add("implementation", libs.findLibrary("androidx.compose.ui.tooling.preview").get()) } } } diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt index bd49754..be438e8 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt @@ -52,6 +52,9 @@ internal fun Project.configureAndroidCompose( val bom = libs.findLibrary("androidx-compose-bom").get() add("implementation", platform(bom)) add("androidTestImplementation", platform(bom)) + // Add UI Tooling and Previews + add("implementation", libs.findLibrary("androidx.compose.ui.tooling.preview").get()) + add("debugImplementation", libs.findLibrary("androidx.compose.ui.tooling").get()) } } } From 9d0da4976f962338066e805841e98a4fec2dd7b7 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 10:02:05 +0200 Subject: [PATCH 06/16] Adding .kotlin to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 089363e..b8402a4 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,9 @@ out/ .gradle/ build/ +# Kotlin +.kotlin + # Signing files .signing/ From 070b34c684d5e68c7319259dfe1a221f1d2c2a42 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 10:34:06 +0200 Subject: [PATCH 07/16] Enabling strong skipping mode for Compose --- gradle/libs.versions.toml | 1 + plugins/build.gradle.kts | 1 + .../no/nordicsemi/android/buildlogic/AndroidCompose.kt | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3a8059e..4fb7fed 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -243,6 +243,7 @@ nordic-blek-uiscanner = { group = "no.nordicsemi.android.kotlin.ble", name = "ui # Dependencies of the included build-logic android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" } kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" } +compose-gradlePlugin = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" } [plugins] nordic-application = { id = "no.nordicsemi.android.gradle.application", version.ref = "nordicPlugins" } diff --git a/plugins/build.gradle.kts b/plugins/build.gradle.kts index bf3b794..a7111c7 100644 --- a/plugins/build.gradle.kts +++ b/plugins/build.gradle.kts @@ -57,6 +57,7 @@ kotlin { dependencies { compileOnly(libs.android.gradlePlugin) compileOnly(libs.kotlin.gradlePlugin) + compileOnly(libs.compose.gradlePlugin) } gradlePlugin { diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt index be438e8..e66cacd 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt @@ -36,6 +36,8 @@ package no.nordicsemi.android.buildlogic import com.android.build.api.dsl.CommonExtension import org.gradle.api.Project import org.gradle.kotlin.dsl.dependencies +import org.gradle.kotlin.dsl.configure +import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension /** * Configure Compose-specific options @@ -57,4 +59,10 @@ internal fun Project.configureAndroidCompose( add("debugImplementation", libs.findLibrary("androidx.compose.ui.tooling").get()) } } + + extensions.configure { + // Enable strong skipping mode for Compose + // https://github.com/JetBrains/kotlin/blob/master/plugins/compose/design/strong-skipping.md + enableStrongSkippingMode.set(true) + } } From d2e2bcb1cda106f396d403d24f6b57219af8f24d Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 15:49:44 +0200 Subject: [PATCH 08/16] Bug fix: wrong class used for Kotlin compiler options --- .../android/buildlogic/KotlinAndroid.kt | 59 ++++++++++++++----- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt index c879d64..992a44b 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt @@ -34,10 +34,13 @@ package no.nordicsemi.android.buildlogic import com.android.build.api.dsl.CommonExtension import org.gradle.api.JavaVersion import org.gradle.api.Project +import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.provideDelegate import org.jetbrains.kotlin.gradle.dsl.JvmTarget +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.KotlinVersion -import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile /** * Configure base Kotlin with Android options @@ -51,21 +54,47 @@ internal fun Project.configureKotlinAndroid( targetCompatibility = JavaVersion.VERSION_17 } - tasks.named("compileKotlin", KotlinJvmCompile::class.java) { - compilerOptions { - // 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.set(warningsAsErrors.toBoolean()) + configureKotlin() + } +} - languageVersion.set(KotlinVersion.KOTLIN_2_0) - apiVersion.set(KotlinVersion.KOTLIN_2_0) - jvmTarget.set(JvmTarget.JVM_17) - optIn.add("kotlin.RequiresOptIn") - optIn.add("kotlinx.coroutines.ExperimentalCoroutinesApi") - optIn.add("kotlinx.coroutines.FlowPreview") - optIn.add("kotlin.Experimental") - } +/** + * Configure base Kotlin with Android options + */ +internal fun Project.configureKotlinJvm( + commonExtension: CommonExtension<*, *, *, *, *, *>, +) { + commonExtension.apply { + compileOptions { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } + + configureKotlin() + } +} + +/** + * Configure base Kotlin options. + */ +private inline fun Project.configureKotlin() = configure { + // 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 { + allWarningsAsErrors.set(warningsAsErrors.toBoolean()) + + languageVersion.set(KotlinVersion.KOTLIN_2_0) + apiVersion.set(KotlinVersion.KOTLIN_2_0) + jvmTarget.set(JvmTarget.JVM_17) + optIn.add("kotlin.RequiresOptIn") + optIn.add("kotlinx.coroutines.ExperimentalCoroutinesApi") + optIn.add("kotlinx.coroutines.FlowPreview") + optIn.add("kotlin.Experimental") } } From d8c2e97486d79c21bcde75a9f442680b3f7f5566 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 15:50:09 +0200 Subject: [PATCH 09/16] Unused imports removed --- plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt b/plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt index bd25253..c1dfb30 100644 --- a/plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt @@ -31,14 +31,10 @@ import com.android.build.api.dsl.ApplicationExtension import com.android.build.gradle.LibraryExtension -import no.nordicsemi.android.buildlogic.configureAndroidCompose import no.nordicsemi.android.buildlogic.configureKotlinAndroid import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.VersionCatalogsExtension -import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.findByType -import org.gradle.kotlin.dsl.getByType class AndroidKotlinConventionPlugin : Plugin { override fun apply(target: Project) { From a0c78062c8fcb0cfd9db80c5dc2ca0050da7ac55 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 16:23:58 +0200 Subject: [PATCH 10/16] Unused version removed from toml --- gradle/libs.versions.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 4fb7fed..7a44d17 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -31,7 +31,6 @@ coil = "2.6.0" firebaseBom = "33.1.0" hilt = "2.51.1" hiltExt = "1.2.0" -jacoco = "0.8.7" junit4 = "4.13.2" kotlin = "2.0.0" kotlinxCoroutines = "1.9.0-RC" From d5afe2c8bce7957932dfa233ccbba5e40f6e070e Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 16:24:37 +0200 Subject: [PATCH 11/16] Renaming publications and repository --- plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt | 3 ++- plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt b/plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt index 5c63454..a1c1cc2 100644 --- a/plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt +++ b/plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt @@ -77,6 +77,7 @@ class AndroidNexusRepositoryPlugin : Plugin { publishing { repositories { maven { + name = "sonatype" setUrl("https://oss.sonatype.org/service/local/staging/deploy/maven2/") credentials { username = System.getenv("OSSR_USERNAME") @@ -85,7 +86,7 @@ class AndroidNexusRepositoryPlugin : Plugin { } } publications { - val publication = create("mavenPublication", MavenPublication::class.java) { + val publication = create("androidLibrary", MavenPublication::class.java) { // Set publication properties. with (nexusPluginExt) { artifactId = POM_ARTIFACT_ID diff --git a/plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt b/plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt index f741a52..498f07a 100644 --- a/plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt +++ b/plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt @@ -73,6 +73,7 @@ class KotlinNexusRepositoryPlugin : Plugin { publishing { repositories { maven { + name = "sonatype" setUrl("https://oss.sonatype.org/service/local/staging/deploy/maven2/") credentials { username = System.getenv("OSSR_USERNAME") @@ -81,7 +82,7 @@ class KotlinNexusRepositoryPlugin : Plugin { } } publications { - val publication = create("mavenPublication", MavenPublication::class.java) { + val publication = create("kotlinLibrary", MavenPublication::class.java) { // Set publication properties. with (nexusPluginExt) { artifactId = POM_ARTIFACT_ID From e4f145ce1109cd3afe9ea6eaea98e0fc8261677b Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 21:53:41 +0200 Subject: [PATCH 12/16] Breaking: Renaming plugins ids --- README.md | 44 +++++++++++++------ gradle/libs.versions.toml | 18 ++++---- plugins/build.gradle.kts | 40 ++++++++--------- ...droidApplicationComposeConventionPlugin.kt | 4 +- .../kotlin/AndroidFeatureConventionPlugin.kt | 4 +- .../kotlin/AndroidHiltConventionPlugin.kt | 2 +- .../AndroidLibraryComposeConventionPlugin.kt | 4 +- .../kotlin/AndroidNexusRepositoryPlugin.kt | 2 +- ...yPlugin.kt => JvmNexusRepositoryPlugin.kt} | 4 +- .../android/buildlogic/KotlinAndroid.kt | 2 +- 10 files changed, 71 insertions(+), 53 deletions(-) rename plugins/src/main/kotlin/{KotlinNexusRepositoryPlugin.kt => JvmNexusRepositoryPlugin.kt} (97%) diff --git a/README.md b/README.md index 03f99fd..ace9685 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,43 @@ # Nordic Gradle Plugins for Android The repo contains plugins which are shared between Nordic's Android applications. -[Now in Android app](https://github.com/android/nowinandroid) from Google has been an inspiration for writing Nordic's dedicated plugins. +[Now in Android app](https://github.com/android/nowinandroid) from Google has been an inspiration +for writing Nordic's dedicated plugins. ## Plugins -List of plugins currently available in the repository: +List of plugins currently available in the repository. -1. [no.nordicsemi.android.application](plugins/src/main/kotlin/AndroidApplicationConventionPlugin.kt) -2. [no.nordicsemi.android.application.compose](plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt) -3. [no.nordicsemi.android.library](plugins/src/main/kotlin/AndroidLibraryConventionPlugin.kt) -4. [no.nordicsemi.android.library.compose](plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt) -5. [no.nordicsemi.android.feature](plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt) -6. [no.nordicsemi.android.kotlin](plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt) -7. [no.nordicsemi.android.hilt](plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt) -8. [no.nordicsemi.android.gradle.nexus](plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt) -9. [no.nordicsemi.kotlin.gradle.nexus](plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt) +### Android plugins + +1. [nno.nordicsemi.android.plugin.application](plugins/src/main/kotlin/AndroidApplicationConventionPlugin.kt) +2. [nno.nordicsemi.android.plugin.application.compose](plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt) +3. [nno.nordicsemi.android.plugin.library](plugins/src/main/kotlin/AndroidLibraryConventionPlugin.kt) +4. [nno.nordicsemi.android.plugin.library.compose](plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt) +5. [nno.nordicsemi.android.plugin.feature](plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt) +6. [nno.nordicsemi.android.plugin.hilt](plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt) +7. [nno.nordicsemi.android.plugin.kotlin](plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt) +8. [nno.nordicsemi.android.plugin.nexus](plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt) + +### JVM plugins + +1. [no.nordicsemi.jvm.plugin.nexus](plugins/src/main/kotlin/JvmNexusRepositoryPlugin.kt) +2. [no.nordicsemi.jvm.plugin.kotlin](plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt) Plugins are released to Nexus repository and are available by their ids and version number. ## Version catalog + The repository also contains Gradle Version Catalog which consumes [toml](gradle/libs.versions.toml) file. -The file is automatically used by gradle to create libs reference in gradle.kts files because it is located in gradle directory. -No additional set up is required. +The file is automatically used by gradle to create libs reference in gradle.kts files because it +is located in gradle directory. No additional set up is required. + + + + + + + + + + diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7a44d17..d73f71a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -245,15 +245,15 @@ kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-pl compose-gradlePlugin = { module = "org.jetbrains.kotlin:compose-compiler-gradle-plugin", version.ref = "kotlin" } [plugins] -nordic-application = { id = "no.nordicsemi.android.gradle.application", version.ref = "nordicPlugins" } -nordic-application-compose = { id = "no.nordicsemi.android.gradle.application.compose", version.ref = "nordicPlugins" } -nordic-library = { id = "no.nordicsemi.android.gradle.library", version.ref = "nordicPlugins" } -nordic-library-compose = { id = "no.nordicsemi.android.gradle.library.compose", version.ref = "nordicPlugins" } -nordic-feature = { id = "no.nordicsemi.android.gradle.feature", version.ref = "nordicPlugins" } -nordic-hilt = { id = "no.nordicsemi.android.gradle.hilt", version.ref = "nordicPlugins" } -nordic-kotlin = { id = "no.nordicsemi.android.gradle.kotlin", version.ref = "nordicPlugins" } -nordic-nexus-android = { id = "no.nordicsemi.android.gradle.nexus", version.ref = "nordicPlugins" } -nordic-nexus-kotlin = { id = "no.nordicsemi.kotlin.gradle.nexus", version.ref = "nordicPlugins" } +nordic-application = { id = "no.nordicsemi.android.plugin.application", version.ref = "nordicPlugins" } +nordic-application-compose = { id = "no.nordicsemi.android.plugin.application.compose", version.ref = "nordicPlugins" } +nordic-library = { id = "no.nordicsemi.android.plugin.library", version.ref = "nordicPlugins" } +nordic-library-compose = { id = "no.nordicsemi.android.plugin.library.compose", version.ref = "nordicPlugins" } +nordic-feature = { id = "no.nordicsemi.android.plugin.feature", version.ref = "nordicPlugins" } +nordic-hilt = { id = "no.nordicsemi.android.plugin.hilt", version.ref = "nordicPlugins" } +nordic-kotlin-android = { id = "no.nordicsemi.android.plugin.kotlin", version.ref = "nordicPlugins" } +nordic-nexus-android = { id = "no.nordicsemi.android.plugin.nexus", version.ref = "nordicPlugins" } +nordic-nexus-kotlin = { id = "no.nordicsemi.jvm.plugin.nexus", version.ref = "nordicPlugins" } android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" } android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" } android-test = { id = "com.android.test", version.ref = "androidGradlePlugin" } diff --git a/plugins/build.gradle.kts b/plugins/build.gradle.kts index a7111c7..97e317c 100644 --- a/plugins/build.gradle.kts +++ b/plugins/build.gradle.kts @@ -68,67 +68,67 @@ gradlePlugin { gradlePlugin { plugins { register("android.application.compose") { - id = "no.nordicsemi.android.gradle.application.compose" + id = "no.nordicsemi.android.plugin.application.compose" displayName = "Application with Compose" description = "Application plugin extension with Compose and Material3 dependencies. This plugin includes 'application' plugin." implementationClass = "AndroidApplicationComposeConventionPlugin" tags.set(listOf("nordicsemi", "Android", "application", "compose")) } register("android.application") { - id = "no.nordicsemi.android.gradle.application" + id = "no.nordicsemi.android.plugin.application" displayName = "Standalone Application configuration" - description = "Application plugin extension" + description = "Application plugin extension." implementationClass = "AndroidApplicationConventionPlugin" tags.set(listOf("nordicsemi", "Android", "application")) } register("android.library.compose") { - id = "no.nordicsemi.android.gradle.library.compose" + id = "no.nordicsemi.android.plugin.library.compose" displayName = "Library with Compose" description = "Library plugin extension with Compose and Material3 dependencies. This plugin extends 'library' plugin." implementationClass = "AndroidLibraryComposeConventionPlugin" tags.set(listOf("nordicsemi", "Android", "library", "compose")) } register("android.library") { - id = "no.nordicsemi.android.gradle.library" + id = "no.nordicsemi.android.plugin.library" displayName = "Standalone library configuration" - description = "Library plugin extension" + description = "Library plugin extension." implementationClass = "AndroidLibraryConventionPlugin" tags.set(listOf("nordicsemi", "Android", "library")) } register("android.feature") { - id = "no.nordicsemi.android.gradle.feature" + id = "no.nordicsemi.android.plugin.feature" displayName = "Feature plugin" description = "UI feature plugin with Hilt & Compose. This plugin extends 'library.compose' and 'hilt' plugins and adds Compose navigation." implementationClass = "AndroidFeatureConventionPlugin" tags.set(listOf("nordicsemi", "Android", "feature")) } register("android.hilt") { - id = "no.nordicsemi.android.gradle.hilt" + id = "no.nordicsemi.android.plugin.hilt" displayName = "Hilt plugin" description = "Plugin enabling Hilt" implementationClass = "AndroidHiltConventionPlugin" tags.set(listOf("nordicsemi", "Android", "hilt")) } register("android.kotlin") { - id = "no.nordicsemi.android.gradle.kotlin" - displayName = "Kotlin plugin" - description = "Plugin enabling Kotlin" + id = "no.nordicsemi.android.plugin.kotlin" + displayName = "Kotlin plugin for Android modules" + description = "Plugin enabling Kotlin for Android modules." implementationClass = "AndroidKotlinConventionPlugin" tags.set(listOf("nordicsemi", "Android", "kotlin")) } register("android.nexus") { - id = "no.nordicsemi.android.gradle.nexus" - displayName = "Nexus plugin" - description = "Plugin creating a task for publishing to Nexus repository." + id = "no.nordicsemi.android.plugin.nexus" + displayName = "Nexus plugin for Android projects" + description = "Plugin creating a task for publishing Android libraries to Nexus repository." implementationClass = "AndroidNexusRepositoryPlugin" tags.set(listOf("nordicsemi", "Android", "nexus", "publish")) } - register("kotlin.nexus") { - id = "no.nordicsemi.kotlin.gradle.nexus" - displayName = "Nexus plugin" - description = "Plugin creating a task for publishing Kotlin library to Nexus repository." - implementationClass = "KotlinNexusRepositoryPlugin" - tags.set(listOf("nordicsemi", "kotlin", "nexus", "publish")) + register("jvm.nexus") { + id = "no.nordicsemi.jvm.plugin.nexus" + displayName = "Nexus plugin for JVM projects" + description = "Plugin creating a task for publishing JVM libraries to Nexus repository." + implementationClass = "JvmNexusRepositoryPlugin" + tags.set(listOf("nordicsemi", "jvm", "kotlin", "nexus", "publish")) } } } diff --git a/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt b/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt index 50d66f9..4ca12cb 100644 --- a/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt @@ -41,8 +41,8 @@ class AndroidApplicationComposeConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { with(pluginManager) { - apply("no.nordicsemi.android.gradle.application") - apply("no.nordicsemi.android.gradle.kotlin") + apply("no.nordicsemi.android.plugin.application") + apply("no.nordicsemi.android.plugin.kotlin") apply("org.jetbrains.kotlin.plugin.compose") } diff --git a/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt b/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt index 2c66255..ff32b0f 100644 --- a/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt @@ -39,8 +39,8 @@ class AndroidFeatureConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { pluginManager.apply { - apply("no.nordicsemi.android.gradle.library.compose") - apply("no.nordicsemi.android.gradle.hilt") + apply("no.nordicsemi.android.plugin.library.compose") + apply("no.nordicsemi.android.plugin.hilt") } val libs = extensions.getByType().named("libs") diff --git a/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt b/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt index 4e8767c..7d72dcf 100644 --- a/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt @@ -39,7 +39,7 @@ class AndroidHiltConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { with(pluginManager) { - apply("no.nordicsemi.android.gradle.kotlin") + apply("no.nordicsemi.android.plugin.kotlin") apply("org.jetbrains.kotlin.kapt") apply("dagger.hilt.android.plugin") } diff --git a/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt b/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt index 84ed1d8..c7e2064 100644 --- a/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt @@ -41,8 +41,8 @@ class AndroidLibraryComposeConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { with(pluginManager) { - apply("no.nordicsemi.android.gradle.library") - apply("no.nordicsemi.android.gradle.kotlin") + apply("no.nordicsemi.android.plugin.library") + apply("no.nordicsemi.android.plugin.kotlin") apply("org.jetbrains.kotlin.plugin.compose") } diff --git a/plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt b/plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt index a1c1cc2..93181da 100644 --- a/plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt +++ b/plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt @@ -86,7 +86,7 @@ class AndroidNexusRepositoryPlugin : Plugin { } } publications { - val publication = create("androidLibrary", MavenPublication::class.java) { + val publication = create("library", MavenPublication::class.java) { // Set publication properties. with (nexusPluginExt) { artifactId = POM_ARTIFACT_ID diff --git a/plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt b/plugins/src/main/kotlin/JvmNexusRepositoryPlugin.kt similarity index 97% rename from plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt rename to plugins/src/main/kotlin/JvmNexusRepositoryPlugin.kt index 498f07a..4574ade 100644 --- a/plugins/src/main/kotlin/KotlinNexusRepositoryPlugin.kt +++ b/plugins/src/main/kotlin/JvmNexusRepositoryPlugin.kt @@ -43,7 +43,7 @@ import org.gradle.kotlin.dsl.get import org.gradle.kotlin.dsl.getByType import org.gradle.plugins.signing.SigningExtension -class KotlinNexusRepositoryPlugin : Plugin { +class JvmNexusRepositoryPlugin : Plugin { override fun apply(target: Project) { with(target) { @@ -82,7 +82,7 @@ class KotlinNexusRepositoryPlugin : Plugin { } } publications { - val publication = create("kotlinLibrary", MavenPublication::class.java) { + val publication = create("library", MavenPublication::class.java) { // Set publication properties. with (nexusPluginExt) { artifactId = POM_ARTIFACT_ID diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt index 992a44b..b2be81d 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt @@ -43,7 +43,7 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension import org.jetbrains.kotlin.gradle.dsl.KotlinVersion /** - * Configure base Kotlin with Android options + * Configure base Kotlin with Android options. */ internal fun Project.configureKotlinAndroid( commonExtension: CommonExtension<*, *, *, *, *, *>, From a363f853b4620fba12ab7bc78be499ebcb34ac14 Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 21:54:50 +0200 Subject: [PATCH 13/16] New: JVM Kotlin convention plugin --- gradle/libs.versions.toml | 1 + plugins/build.gradle.kts | 7 +++++++ .../main/kotlin/JvmKotlinConventionPlugin.kt | 16 ++++++++++++++ .../android/buildlogic/KotlinAndroid.kt | 21 +++++++++---------- 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d73f71a..95ec861 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -252,6 +252,7 @@ nordic-library-compose = { id = "no.nordicsemi.android.plugin.library.compose", nordic-feature = { id = "no.nordicsemi.android.plugin.feature", version.ref = "nordicPlugins" } nordic-hilt = { id = "no.nordicsemi.android.plugin.hilt", version.ref = "nordicPlugins" } nordic-kotlin-android = { id = "no.nordicsemi.android.plugin.kotlin", version.ref = "nordicPlugins" } +nordic-kotlin-jvm = { id = "no.nordicsemi.jvm.plugin.kotlin", version.ref = "nordicPlugins" } nordic-nexus-android = { id = "no.nordicsemi.android.plugin.nexus", version.ref = "nordicPlugins" } nordic-nexus-kotlin = { id = "no.nordicsemi.jvm.plugin.nexus", version.ref = "nordicPlugins" } android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" } diff --git a/plugins/build.gradle.kts b/plugins/build.gradle.kts index 97e317c..21c0127 100644 --- a/plugins/build.gradle.kts +++ b/plugins/build.gradle.kts @@ -116,6 +116,13 @@ gradlePlugin { implementationClass = "AndroidKotlinConventionPlugin" tags.set(listOf("nordicsemi", "Android", "kotlin")) } + register("jvm.kotlin") { + id = "no.nordicsemi.jvm.plugin.kotlin" + displayName = "Kotlin plugin for JVM projects" + description = "Plugin enabling Kotlin for JVM modules." + implementationClass = "JvmKotlinConventionPlugin" + tags.set(listOf("nordicsemi", "jvm", "kotlin")) + } register("android.nexus") { id = "no.nordicsemi.android.plugin.nexus" displayName = "Nexus plugin for Android projects" diff --git a/plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt b/plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt new file mode 100644 index 0000000..c06c91e --- /dev/null +++ b/plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt @@ -0,0 +1,16 @@ + +import no.nordicsemi.android.buildlogic.configureKotlinJvm +import org.gradle.api.Plugin +import org.gradle.api.Project + +class JvmKotlinConventionPlugin : Plugin { + override fun apply(target: Project) { + with(target) { + with(pluginManager) { + apply("org.jetbrains.kotlin.jvm") + } + + configureKotlinJvm() + } + } +} \ No newline at end of file diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt index b2be81d..7e4b535 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt @@ -34,6 +34,7 @@ package no.nordicsemi.android.buildlogic import com.android.build.api.dsl.CommonExtension import org.gradle.api.JavaVersion import org.gradle.api.Project +import org.gradle.api.plugins.JavaPluginExtension import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.provideDelegate import org.jetbrains.kotlin.gradle.dsl.JvmTarget @@ -59,19 +60,17 @@ internal fun Project.configureKotlinAndroid( } /** - * Configure base Kotlin with Android options + * Configure base Kotlin options for JVM (non-Android) */ -internal fun Project.configureKotlinJvm( - commonExtension: CommonExtension<*, *, *, *, *, *>, -) { - commonExtension.apply { - compileOptions { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 - } - - configureKotlin() +internal fun Project.configureKotlinJvm() { + extensions.configure { + // Up to Java 11 APIs are available through desugaring + // https://developer.android.com/studio/write/java11-minimal-support-table + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 } + + configureKotlin() } /** From 47080cdc6a616ff5333064c32b957706e84e8c8d Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 22:49:28 +0200 Subject: [PATCH 14/16] Clean up --- ...droidApplicationComposeConventionPlugin.kt | 3 +- .../AndroidApplicationConventionPlugin.kt | 5 - .../kotlin/AndroidFeatureConventionPlugin.kt | 2 +- .../kotlin/AndroidHiltConventionPlugin.kt | 2 +- .../AndroidLibraryComposeConventionPlugin.kt | 3 +- .../kotlin/AndroidLibraryConventionPlugin.kt | 16 --- .../main/kotlin/JvmKotlinConventionPlugin.kt | 30 +++++ .../kotlin/no/nordicsemi/android/AppConsts.kt | 2 +- .../nordicsemi/android/buildlogic/Jacoco.kt | 103 ---------------- .../android/buildlogic/KotlinAndroid.kt | 3 - .../android/buildlogic/PrintTestApks.kt | 115 ------------------ .../android/buildlogic/ProjectExtensions.kt | 35 ++++-- 12 files changed, 60 insertions(+), 259 deletions(-) delete mode 100644 plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/Jacoco.kt delete mode 100644 plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/PrintTestApks.kt diff --git a/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt b/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt index 4ca12cb..0c040cc 100644 --- a/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt @@ -31,9 +31,9 @@ import com.android.build.api.dsl.ApplicationExtension import no.nordicsemi.android.buildlogic.configureAndroidCompose +import no.nordicsemi.android.buildlogic.libs import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.getByType @@ -49,7 +49,6 @@ class AndroidApplicationComposeConventionPlugin : Plugin { val extension = extensions.getByType() configureAndroidCompose(extension) - val libs = extensions.getByType().named("libs") dependencies { // Add Material 3 Compose add("implementation", libs.findLibrary("androidx.compose.material3").get()) diff --git a/plugins/src/main/kotlin/AndroidApplicationConventionPlugin.kt b/plugins/src/main/kotlin/AndroidApplicationConventionPlugin.kt index 3e6cebd..4e95909 100644 --- a/plugins/src/main/kotlin/AndroidApplicationConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidApplicationConventionPlugin.kt @@ -30,9 +30,7 @@ */ import com.android.build.api.dsl.ApplicationExtension -import com.android.build.api.variant.ApplicationAndroidComponentsExtension import no.nordicsemi.android.AppConst -import no.nordicsemi.android.buildlogic.configurePrintApksTask import no.nordicsemi.android.buildlogic.getVersionCodeFromTags import no.nordicsemi.android.buildlogic.getVersionNameFromTags import org.gradle.api.Plugin @@ -92,9 +90,6 @@ class AndroidApplicationConventionPlugin : Plugin { } } } - extensions.configure { - configurePrintApksTask(this) - } } } } \ No newline at end of file diff --git a/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt b/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt index ff32b0f..66adacc 100644 --- a/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt @@ -29,6 +29,7 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import no.nordicsemi.android.buildlogic.libs import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.VersionCatalogsExtension @@ -43,7 +44,6 @@ class AndroidFeatureConventionPlugin : Plugin { apply("no.nordicsemi.android.plugin.hilt") } - val libs = extensions.getByType().named("libs") dependencies { add("implementation", libs.findLibrary("androidx.hilt.navigation.compose").get()) // The above library depends on the following libraries, but to keep them in the diff --git a/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt b/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt index 7d72dcf..5e6b7a5 100644 --- a/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt @@ -29,6 +29,7 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import no.nordicsemi.android.buildlogic.libs import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.VersionCatalogsExtension @@ -44,7 +45,6 @@ class AndroidHiltConventionPlugin : Plugin { apply("dagger.hilt.android.plugin") } - val libs = extensions.getByType().named("libs") dependencies { add("implementation", libs.findLibrary("hilt.android").get()) add("kapt", libs.findLibrary("hilt.compiler").get()) diff --git a/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt b/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt index c7e2064..042bc39 100644 --- a/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt @@ -31,9 +31,9 @@ import com.android.build.gradle.LibraryExtension import no.nordicsemi.android.buildlogic.configureAndroidCompose +import no.nordicsemi.android.buildlogic.libs import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.getByType @@ -49,7 +49,6 @@ class AndroidLibraryComposeConventionPlugin : Plugin { val extension = extensions.getByType() configureAndroidCompose(extension) - val libs = extensions.getByType().named("libs") dependencies { // Add Material 3 Compose add("implementation", libs.findLibrary("androidx.compose.material3").get()) diff --git a/plugins/src/main/kotlin/AndroidLibraryConventionPlugin.kt b/plugins/src/main/kotlin/AndroidLibraryConventionPlugin.kt index 8b8edc4..cd1fe0a 100644 --- a/plugins/src/main/kotlin/AndroidLibraryConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidLibraryConventionPlugin.kt @@ -29,17 +29,13 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import com.android.build.api.variant.LibraryAndroidComponentsExtension import com.android.build.gradle.LibraryExtension import no.nordicsemi.android.AppConst -import no.nordicsemi.android.buildlogic.configurePrintApksTask import no.nordicsemi.android.buildlogic.getVersionCodeFromTags import no.nordicsemi.android.buildlogic.getVersionNameFromTags import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.kotlin.dsl.configure -import org.gradle.kotlin.dsl.getByType class AndroidLibraryConventionPlugin : Plugin { override fun apply(target: Project) { @@ -82,18 +78,6 @@ class AndroidLibraryConventionPlugin : Plugin { } } } - - extensions.configure { - configurePrintApksTask(this) - } - val libs = extensions.getByType().named("libs") - configurations.configureEach { - resolutionStrategy { - force(libs.findLibrary("junit4").get()) - // Temporary workaround for https://issuetracker.google.com/174733673 - force("org.objenesis:objenesis:2.6") - } - } } } } \ No newline at end of file diff --git a/plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt b/plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt index c06c91e..7fa8f28 100644 --- a/plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt +++ b/plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt @@ -1,3 +1,33 @@ +/* + * Copyright (c) 2022, Nordic Semiconductor + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ import no.nordicsemi.android.buildlogic.configureKotlinJvm import org.gradle.api.Plugin diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/AppConsts.kt b/plugins/src/main/kotlin/no/nordicsemi/android/AppConsts.kt index 3de5703..c27b3e0 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/AppConsts.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/AppConsts.kt @@ -4,4 +4,4 @@ object AppConst { const val MIN_SDK = 21 const val COMPILE_SDK = 34 const val TARGET_SDK = 34 -} +} \ No newline at end of file diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/Jacoco.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/Jacoco.kt deleted file mode 100644 index 097bc00..0000000 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/Jacoco.kt +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2022, Nordic Semiconductor - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be - * used to endorse or promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -package no.nordicsemi.android.buildlogic - -import com.android.build.api.variant.AndroidComponentsExtension -import org.gradle.api.Project -import org.gradle.api.tasks.testing.Test -import org.gradle.kotlin.dsl.configure -import org.gradle.kotlin.dsl.register -import org.gradle.kotlin.dsl.withType -import org.gradle.testing.jacoco.plugins.JacocoPluginExtension -import org.gradle.testing.jacoco.plugins.JacocoTaskExtension -import org.gradle.testing.jacoco.tasks.JacocoReport -import java.util.Locale - -private val coverageExclusions = listOf( - // Android - "**/R.class", - "**/R\$*.class", - "**/BuildConfig.*", - "**/Manifest*.*" -) - -private fun String.capitalize() = replaceFirstChar { - if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() -} - -internal fun Project.configureJacoco( - androidComponentsExtension: AndroidComponentsExtension<*, *, *>, -) { - configure { - toolVersion = libs.findVersion("jacoco").get().toString() - } - - val jacocoTestReport = tasks.create("jacocoTestReport") - - androidComponentsExtension.onVariants { variant -> - val testTaskName = "test${variant.name.capitalize()}UnitTest" - - val reportTask = tasks.register("jacoco${testTaskName.capitalize()}Report", JacocoReport::class) { - dependsOn(testTaskName) - - reports { - xml.required.set(true) - html.required.set(true) - } - - val buildDir = rootProject.layout.buildDirectory.asFile.get() - classDirectories.setFrom( - fileTree("$buildDir/tmp/kotlin-classes/${variant.name}") { - exclude(coverageExclusions) - } - ) - - sourceDirectories.setFrom(files("$projectDir/src/main/java", "$projectDir/src/main/kotlin")) - executionData.setFrom(file("$buildDir/jacoco/$testTaskName.exec")) - } - - jacocoTestReport.dependsOn(reportTask) - } - - tasks.withType().configureEach { - configure { - // Required for JaCoCo + Robolectric - // https://github.com/robolectric/robolectric/issues/2230 - // TODO: Consider removing if not we don't add Robolectric - isIncludeNoLocationClasses = true - - // Required for JDK 11 with the above - // https://github.com/gradle/gradle/issues/5184#issuecomment-391982009 - excludes = listOf("jdk.internal.*") - } - } -} diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt index 7e4b535..e3d69b5 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/KotlinAndroid.kt @@ -64,8 +64,6 @@ internal fun Project.configureKotlinAndroid( */ internal fun Project.configureKotlinJvm() { extensions.configure { - // Up to Java 11 APIs are available through desugaring - // https://developer.android.com/studio/write/java11-minimal-support-table sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } @@ -94,6 +92,5 @@ private inline fun Project.configureKotlin optIn.add("kotlin.RequiresOptIn") optIn.add("kotlinx.coroutines.ExperimentalCoroutinesApi") optIn.add("kotlinx.coroutines.FlowPreview") - optIn.add("kotlin.Experimental") } } diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/PrintTestApks.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/PrintTestApks.kt deleted file mode 100644 index b6f482f..0000000 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/PrintTestApks.kt +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Copyright (c) 2022, Nordic Semiconductor - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be - * used to endorse or promote products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A - * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY - * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, - * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -@file:Suppress("UnstableApiUsage") - -package no.nordicsemi.android.buildlogic - -import com.android.build.api.artifact.SingleArtifact -import com.android.build.api.variant.AndroidComponentsExtension -import com.android.build.api.variant.BuiltArtifactsLoader -import com.android.build.api.variant.HasAndroidTest -import org.gradle.api.DefaultTask -import org.gradle.api.Project -import org.gradle.api.file.Directory -import org.gradle.api.file.DirectoryProperty -import org.gradle.api.provider.ListProperty -import org.gradle.api.provider.Property -import org.gradle.api.tasks.Input -import org.gradle.api.tasks.InputDirectory -import org.gradle.api.tasks.InputFiles -import org.gradle.api.tasks.Internal -import org.gradle.api.tasks.TaskAction -import java.io.File - -internal fun Project.configurePrintApksTask(extension: AndroidComponentsExtension<*, *, *>) { - extension.onVariants { variant -> - if (variant is HasAndroidTest) { - val loader = variant.artifacts.getBuiltArtifactsLoader() - val artifact = variant.androidTest?.artifacts?.get(SingleArtifact.APK) - val javaSources = variant.androidTest?.sources?.java?.all - val kotlinSources = variant.androidTest?.sources?.kotlin?.all - - val testSources = if (javaSources != null && kotlinSources != null) { - javaSources.zip(kotlinSources) { javaDirs, kotlinDirs -> - javaDirs + kotlinDirs - } - } else javaSources ?: kotlinSources - - if (artifact != null && testSources != null) { - tasks.register( - "${variant.name}PrintTestApk", - PrintApkLocationTask::class.java - ) { - apkFolder.set(artifact) - builtArtifactsLoader.set(loader) - variantName.set(variant.name) - sources.set(testSources) - } - } - } - } -} - -internal abstract class PrintApkLocationTask : DefaultTask() { - @get:InputDirectory - abstract val apkFolder: DirectoryProperty - - @get:InputFiles - abstract val sources: ListProperty - - @get:Internal - abstract val builtArtifactsLoader: Property - - @get:Input - abstract val variantName: Property - - @TaskAction - fun taskAction() { - val hasFiles = sources.orNull?.any { directory -> - directory.asFileTree.files.any { - it.isFile && it.parentFile.path.contains("build${File.separator}generated").not() - } - } ?: throw RuntimeException("Cannot check androidTest sources") - - // Don't print APK location if there are no androidTest source files - if (!hasFiles) { - return - } - - val builtArtifacts = builtArtifactsLoader.get().load(apkFolder.get()) - ?: throw RuntimeException("Cannot load APKs") - if (builtArtifacts.elements.size != 1) - throw RuntimeException("Expected one APK !") - val apk = File(builtArtifacts.elements.single().outputFile).toPath() - println(apk) - } -} \ No newline at end of file diff --git a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/ProjectExtensions.kt b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/ProjectExtensions.kt index c74e5b9..c7ba641 100644 --- a/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/ProjectExtensions.kt +++ b/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/ProjectExtensions.kt @@ -1,17 +1,32 @@ /* - * Copyright 2023 The Android Open Source Project + * Copyright (c) 2022, Nordic Semiconductor + * All rights reserved. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: * - * https://www.apache.org/licenses/LICENSE-2.0 + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its contributors may be + * used to endorse or promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, + * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package no.nordicsemi.android.buildlogic From 6445297ef3131694f774f88f127ad5a2cd6254cb Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 23:28:15 +0200 Subject: [PATCH 15/16] Readme updated --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index ace9685..a1afda5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Nordic Gradle Plugins for Android The repo contains plugins which are shared between Nordic's Android applications. -[Now in Android app](https://github.com/android/nowinandroid) from Google has been an inspiration +[Now in Android app](https://github.com/android/nowinandroid) from Google has been an inspiration for writing Nordic's dedicated plugins. ## Plugins @@ -10,34 +10,79 @@ List of plugins currently available in the repository. ### Android plugins -1. [nno.nordicsemi.android.plugin.application](plugins/src/main/kotlin/AndroidApplicationConventionPlugin.kt) -2. [nno.nordicsemi.android.plugin.application.compose](plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt) -3. [nno.nordicsemi.android.plugin.library](plugins/src/main/kotlin/AndroidLibraryConventionPlugin.kt) -4. [nno.nordicsemi.android.plugin.library.compose](plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt) -5. [nno.nordicsemi.android.plugin.feature](plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt) -6. [nno.nordicsemi.android.plugin.hilt](plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt) -7. [nno.nordicsemi.android.plugin.kotlin](plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt) -8. [nno.nordicsemi.android.plugin.nexus](plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt) +1. [no.nordicsemi.android.plugin.application](plugins/src/main/kotlin/AndroidApplicationConventionPlugin.kt) -### JVM plugins + This plugin does the following: + * applies `com.android.application`, + * defines `compileSdk`, `minSdk` and `targetSdk` to values set in + [`AppConsts`](https://github.com/NordicSemiconductor/Android-Gradle-Plugins/blob/kotlin2/plugins/src/main/kotlin/no/nordicsemi/android/AppConsts.kt), + * sets `buildConfig`, + * configures _debug_ and _release_ build types, + * creates app signing configuration using `../keystore` file which is generated by GitHub actions during build. -1. [no.nordicsemi.jvm.plugin.nexus](plugins/src/main/kotlin/JvmNexusRepositoryPlugin.kt) -2. [no.nordicsemi.jvm.plugin.kotlin](plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt) +2. [no.nordicsemi.android.plugin.application.compose](plugins/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt) -Plugins are released to Nexus repository and are available by their ids and version number. + This plugin applies `no.nordicsemi.android.plugin.application` and adds Compose and Material3 dependency. -## Version catalog + Since version 2.1 [`enableStrongSkippingMode`](https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-compiler.html#enablestrongskippingmode) is + [enabled](https://github.com/NordicSemiconductor/Android-Gradle-Plugins/blob/6ee70d9f2fb2c2c8474067845eac05308740afa8/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt#L66). -The repository also contains Gradle Version Catalog which consumes [toml](gradle/libs.versions.toml) file. -The file is automatically used by gradle to create libs reference in gradle.kts files because it -is located in gradle directory. No additional set up is required. +3. [no.nordicsemi.android.plugin.library](plugins/src/main/kotlin/AndroidLibraryConventionPlugin.kt) + + This plugin does the following: + * applies `com.android.library`, + * defines `compileSdk` and `minSdk` to values set in + [`AppConsts`](https://github.com/NordicSemiconductor/Android-Gradle-Plugins/blob/kotlin2/plugins/src/main/kotlin/no/nordicsemi/android/AppConsts.kt), + * sets `buildConfig`, + * configures _debug_ and _release_ build types. + +4. [no.nordicsemi.android.plugin.library.compose](plugins/src/main/kotlin/AndroidLibraryComposeConventionPlugin.kt) + + This plugin applies `no.nordicsemi.android.plugin.library` and adds Compose and Material3 dependency. + + Since version 2.1 [`enableStrongSkippingMode`](https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-compiler.html#enablestrongskippingmode) is + [enabled](https://github.com/NordicSemiconductor/Android-Gradle-Plugins/blob/6ee70d9f2fb2c2c8474067845eac05308740afa8/plugins/src/main/kotlin/no/nordicsemi/android/buildlogic/AndroidCompose.kt#L66). + +5. [no.nordicsemi.android.plugin.kotlin](plugins/src/main/kotlin/AndroidNexusRepositoryPlugin.kt) + + This plugin applies `org.jetbrains.kotlin.android` and configures Kotlin compiler. +7. [no.nordicsemi.android.plugin.hilt](plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt) + This plugin applies `no.nordicsemi.android.plugin.kotlin` and `org.jetbrains.kotlin.kapt` and + configures [Hilt](https://dagger.dev/hilt/) for the module. +9. [no.nordicsemi.android.plugin.feature](plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt) + This plugin applies: + * `no.nordicsemi.android.plugin.library.compose` + * `no.nordicsemi.android.plugin.hilt` + * `no.nordicsemi.android.plugin.kotlin` + and adds dependency to `androidx.hilt.navigation.compose`. +10. [no.nordicsemi.android.plugin.nexus](plugins/src/main/kotlin/AndroidKotlinConventionPlugin.kt) +Creates `publish` and `releaseStagingRepositories` tasks using `maven-publish`. +### JVM plugins + +1. [no.nordicsemi.jvm.plugin.kotlin](plugins/src/main/kotlin/JvmKotlinConventionPlugin.kt) (since 2.1) + + This plugin applies `org.jetbrains.kotlin.jvm` and configures Kotlin compiler. + +2. [no.nordicsemi.jvm.plugin.nexus](plugins/src/main/kotlin/JvmNexusRepositoryPlugin.kt) (since 2.0) + + Creates `publish` and `releaseStagingRepositories` tasks using `maven-publish`. +Plugins are released to [Gradle Plugins](https://plugins.gradle.org/search?term=no.nordicsemi) repository +and are available by their ids and version number. +> [!Note] +> Version 2.1 altered ids of the plugins by replacing `gradle` with `plugin`. + +## Version catalog + +The repository also contains Gradle Version Catalog which consumes [toml](gradle/libs.versions.toml) file. +The file is automatically used by gradle to create libs reference in gradle.kts files because it +is located in gradle directory. No additional set up is required. \ No newline at end of file From 3c50b39b2d1b89a2f29aee573782ceee7ebfdc6e Mon Sep 17 00:00:00 2001 From: Aleksander Nowakowski Date: Thu, 20 Jun 2024 23:29:00 +0200 Subject: [PATCH 16/16] Imports organized --- plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt | 2 -- plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt | 2 -- 2 files changed, 4 deletions(-) diff --git a/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt b/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt index 66adacc..469d401 100644 --- a/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidFeatureConventionPlugin.kt @@ -32,9 +32,7 @@ import no.nordicsemi.android.buildlogic.libs import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.kotlin.dsl.dependencies -import org.gradle.kotlin.dsl.getByType class AndroidFeatureConventionPlugin : Plugin { override fun apply(target: Project) { diff --git a/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt b/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt index 5e6b7a5..49b7b86 100644 --- a/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt +++ b/plugins/src/main/kotlin/AndroidHiltConventionPlugin.kt @@ -32,9 +32,7 @@ import no.nordicsemi.android.buildlogic.libs import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.artifacts.VersionCatalogsExtension import org.gradle.kotlin.dsl.dependencies -import org.gradle.kotlin.dsl.getByType class AndroidHiltConventionPlugin : Plugin { override fun apply(target: Project) {