From 85cc2921ecc6c851778b7dc8c085170d999603c4 Mon Sep 17 00:00:00 2001 From: Jaehwa Noh Date: Wed, 25 Dec 2024 11:59:56 +0900 Subject: [PATCH] Follow kotlin dsl without type-safe documentation on plugins https://docs.gradle.org/current/userguide/kotlin_dsl.html#ex-configuring-plugins-without-type-safe-accessors Change-Id: I194844ace17cc2bfa2c80bcc4a338bccfd050a9e --- .../kotlin/AndroidApplicationConventionPlugin.kt | 12 +++++------- .../AndroidApplicationFirebaseConventionPlugin.kt | 9 ++++----- .../AndroidApplicationJacocoConventionPlugin.kt | 4 +++- .../main/kotlin/AndroidFeatureConventionPlugin.kt | 14 ++++++++------ .../main/kotlin/AndroidLibraryConventionPlugin.kt | 13 +++++++------ .../kotlin/AndroidLibraryJacocoConventionPlugin.kt | 5 +++-- .../src/main/kotlin/AndroidLintConventionPlugin.kt | 3 ++- .../src/main/kotlin/AndroidRoomConventionPlugin.kt | 5 +++-- .../src/main/kotlin/AndroidTestConventionPlugin.kt | 8 +++----- .../src/main/kotlin/HiltConventionPlugin.kt | 6 ++++-- .../src/main/kotlin/JvmLibraryConventionPlugin.kt | 8 ++++---- 11 files changed, 46 insertions(+), 41 deletions(-) diff --git a/build-logic/convention/src/main/kotlin/AndroidApplicationConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidApplicationConventionPlugin.kt index 72be1cb740..1ab3a2ca08 100644 --- a/build-logic/convention/src/main/kotlin/AndroidApplicationConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidApplicationConventionPlugin.kt @@ -23,18 +23,17 @@ import com.google.samples.apps.nowinandroid.configureKotlinAndroid import com.google.samples.apps.nowinandroid.configurePrintApksTask import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.getByType class AndroidApplicationConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - with(pluginManager) { - apply("com.android.application") - apply("org.jetbrains.kotlin.android") - apply("nowinandroid.android.lint") - apply("com.dropbox.dependency-guard") - } + apply(plugin = "com.android.application") + apply(plugin = "org.jetbrains.kotlin.android") + apply(plugin = "nowinandroid.android.lint") + apply(plugin = "com.dropbox.dependency-guard") extensions.configure { configureKotlinAndroid(this) @@ -49,5 +48,4 @@ class AndroidApplicationConventionPlugin : Plugin { } } } - } diff --git a/build-logic/convention/src/main/kotlin/AndroidApplicationFirebaseConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidApplicationFirebaseConventionPlugin.kt index 7aacc0ffe3..be3ec03655 100644 --- a/build-logic/convention/src/main/kotlin/AndroidApplicationFirebaseConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidApplicationFirebaseConventionPlugin.kt @@ -19,6 +19,7 @@ import com.google.firebase.crashlytics.buildtools.gradle.CrashlyticsExtension import com.google.samples.apps.nowinandroid.libs import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.exclude @@ -26,11 +27,9 @@ import org.gradle.kotlin.dsl.exclude class AndroidApplicationFirebaseConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - with(pluginManager) { - apply("com.google.gms.google-services") - apply("com.google.firebase.firebase-perf") - apply("com.google.firebase.crashlytics") - } + apply(plugin = "com.google.gms.google-services") + apply(plugin = "com.google.firebase.firebase-perf") + apply(plugin = "com.google.firebase.crashlytics") dependencies { val bom = libs.findLibrary("firebase-bom").get() diff --git a/build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt index 5bd58fabeb..b0eece41da 100644 --- a/build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidApplicationJacocoConventionPlugin.kt @@ -19,12 +19,14 @@ import com.android.build.api.variant.ApplicationAndroidComponentsExtension import com.google.samples.apps.nowinandroid.configureJacoco import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.getByType class AndroidApplicationJacocoConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - pluginManager.apply("jacoco") + apply(plugin = "jacoco") + val androidExtension = extensions.getByType() androidExtension.buildTypes.configureEach { diff --git a/build-logic/convention/src/main/kotlin/AndroidFeatureConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidFeatureConventionPlugin.kt index aa30f5115b..1af5523c56 100644 --- a/build-logic/convention/src/main/kotlin/AndroidFeatureConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidFeatureConventionPlugin.kt @@ -19,17 +19,17 @@ import com.google.samples.apps.nowinandroid.configureGradleManagedDevices import com.google.samples.apps.nowinandroid.libs import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies class AndroidFeatureConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - pluginManager.apply { - apply("nowinandroid.android.library") - apply("nowinandroid.hilt") - apply("org.jetbrains.kotlin.plugin.serialization") - } + apply(plugin = "nowinandroid.android.library") + apply(plugin = "nowinandroid.hilt") + apply(plugin = "org.jetbrains.kotlin.plugin.serialization") + extensions.configure { testOptions.animationsDisabled = true configureGradleManagedDevices(this) @@ -47,7 +47,9 @@ class AndroidFeatureConventionPlugin : Plugin { "implementation"(libs.findLibrary("kotlinx.serialization.json").get()) "testImplementation"(libs.findLibrary("androidx.navigation.testing").get()) - "androidTestImplementation"(libs.findLibrary("androidx.lifecycle.runtimeTesting").get()) + "androidTestImplementation"( + libs.findLibrary("androidx.lifecycle.runtimeTesting").get(), + ) } } } diff --git a/build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt index feffa50371..3fe727410e 100644 --- a/build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidLibraryConventionPlugin.kt @@ -24,17 +24,16 @@ import com.google.samples.apps.nowinandroid.disableUnnecessaryAndroidTests import com.google.samples.apps.nowinandroid.libs import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies class AndroidLibraryConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - with(pluginManager) { - apply("com.android.library") - apply("org.jetbrains.kotlin.android") - apply("nowinandroid.android.lint") - } + apply(plugin = "com.android.library") + apply(plugin = "org.jetbrains.kotlin.android") + apply(plugin = "nowinandroid.android.lint") extensions.configure { configureKotlinAndroid(this) @@ -45,7 +44,9 @@ class AndroidLibraryConventionPlugin : Plugin { configureGradleManagedDevices(this) // The resource prefix is derived from the module name, // so resources inside ":core:module1" must be prefixed with "core_module1_" - resourcePrefix = path.split("""\W""".toRegex()).drop(1).distinct().joinToString(separator = "_").lowercase() + "_" + resourcePrefix = + path.split("""\W""".toRegex()).drop(1).distinct().joinToString(separator = "_") + .lowercase() + "_" } extensions.configure { configurePrintApksTask(this) diff --git a/build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt index 6f2ff60c5a..d249e4cbf1 100644 --- a/build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidLibraryJacocoConventionPlugin.kt @@ -15,17 +15,18 @@ */ import com.android.build.api.dsl.LibraryExtension -import com.android.build.api.variant.ApplicationAndroidComponentsExtension import com.android.build.api.variant.LibraryAndroidComponentsExtension import com.google.samples.apps.nowinandroid.configureJacoco import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.getByType class AndroidLibraryJacocoConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - pluginManager.apply("jacoco") + apply(plugin = "jacoco") + val androidExtension = extensions.getByType() androidExtension.buildTypes.configureEach { diff --git a/build-logic/convention/src/main/kotlin/AndroidLintConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidLintConventionPlugin.kt index 1a01a77254..884d6f076b 100644 --- a/build-logic/convention/src/main/kotlin/AndroidLintConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidLintConventionPlugin.kt @@ -19,6 +19,7 @@ import com.android.build.api.dsl.LibraryExtension import com.android.build.api.dsl.Lint import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure class AndroidLintConventionPlugin : Plugin { @@ -32,7 +33,7 @@ class AndroidLintConventionPlugin : Plugin { configure { lint(Lint::configure) } else -> { - pluginManager.apply("com.android.lint") + apply(plugin = "com.android.lint") configure(Lint::configure) } } diff --git a/build-logic/convention/src/main/kotlin/AndroidRoomConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidRoomConventionPlugin.kt index 5a8d648f30..6919b5e5dc 100644 --- a/build-logic/convention/src/main/kotlin/AndroidRoomConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidRoomConventionPlugin.kt @@ -19,6 +19,7 @@ import com.google.devtools.ksp.gradle.KspExtension import com.google.samples.apps.nowinandroid.libs import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure import org.gradle.kotlin.dsl.dependencies @@ -26,8 +27,8 @@ class AndroidRoomConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - pluginManager.apply("androidx.room") - pluginManager.apply("com.google.devtools.ksp") + apply(plugin = "androidx.room") + apply(plugin = "com.google.devtools.ksp") extensions.configure { arg("room.generateKotlin", "true") diff --git a/build-logic/convention/src/main/kotlin/AndroidTestConventionPlugin.kt b/build-logic/convention/src/main/kotlin/AndroidTestConventionPlugin.kt index 8a084598f8..67933f77d4 100644 --- a/build-logic/convention/src/main/kotlin/AndroidTestConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/AndroidTestConventionPlugin.kt @@ -19,15 +19,14 @@ import com.google.samples.apps.nowinandroid.configureGradleManagedDevices import com.google.samples.apps.nowinandroid.configureKotlinAndroid import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure class AndroidTestConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - with(pluginManager) { - apply("com.android.test") - apply("org.jetbrains.kotlin.android") - } + apply(plugin = "com.android.test") + apply(plugin = "org.jetbrains.kotlin.android") extensions.configure { configureKotlinAndroid(this) @@ -36,5 +35,4 @@ class AndroidTestConventionPlugin : Plugin { } } } - } diff --git a/build-logic/convention/src/main/kotlin/HiltConventionPlugin.kt b/build-logic/convention/src/main/kotlin/HiltConventionPlugin.kt index 24d8c33824..5a90ff98f3 100644 --- a/build-logic/convention/src/main/kotlin/HiltConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/HiltConventionPlugin.kt @@ -18,12 +18,14 @@ import com.android.build.gradle.api.AndroidBasePlugin import com.google.samples.apps.nowinandroid.libs import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.dependencies class HiltConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - pluginManager.apply("com.google.devtools.ksp") + apply(plugin = "com.google.devtools.ksp") + dependencies { "ksp"(libs.findLibrary("hilt.compiler").get()) } @@ -37,7 +39,7 @@ class HiltConventionPlugin : Plugin { /** Add support for Android modules, based on [AndroidBasePlugin] */ pluginManager.withPlugin("com.android.base") { - pluginManager.apply("dagger.hilt.android.plugin") + apply(plugin = "dagger.hilt.android.plugin") dependencies { "implementation"(libs.findLibrary("hilt.android").get()) } diff --git a/build-logic/convention/src/main/kotlin/JvmLibraryConventionPlugin.kt b/build-logic/convention/src/main/kotlin/JvmLibraryConventionPlugin.kt index f9ae4a1c45..a1477891db 100644 --- a/build-logic/convention/src/main/kotlin/JvmLibraryConventionPlugin.kt +++ b/build-logic/convention/src/main/kotlin/JvmLibraryConventionPlugin.kt @@ -18,15 +18,15 @@ import com.google.samples.apps.nowinandroid.configureKotlinJvm import com.google.samples.apps.nowinandroid.libs import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.dependencies class JvmLibraryConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { - with(pluginManager) { - apply("org.jetbrains.kotlin.jvm") - apply("nowinandroid.android.lint") - } + apply(plugin = "org.jetbrains.kotlin.jvm") + apply(plugin = "nowinandroid.android.lint") + configureKotlinJvm() dependencies { "testImplementation"(libs.findLibrary("kotlin.test").get())