From 0530f8d5d52944d3eba7f72cc0cb52c01a731217 Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Thu, 22 Feb 2024 09:59:18 +0100 Subject: [PATCH] =?UTF-8?q?[=F0=9F=90=98=20gradle-plugin]=20Use=20com.andr?= =?UTF-8?q?oid.lint=20Gradle=20rules=20(#5639)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use com.android.lint * use version catalog --- build-logic/build.gradle.kts | 1 + gradle/libraries.toml | 2 ++ .../build.gradle.kts | 4 ++++ .../apollo3/gradle/api/androidProject.kt | 8 +++---- .../gradle/internal/AndroidPluginFacade.kt | 22 ++++++------------- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index ceddca79ab6..2b1fdaed596 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -33,6 +33,7 @@ dependencies { implementation(libs.poet.kotlin) implementation(libs.intellij.plugin) implementation(libs.intellij.changelog) + implementation(libs.androidx.lint.gradle.plugin) // We want the KSP plugin to use the version from the classpath and not force a newer version // of the Gradle plugin diff --git a/gradle/libraries.toml b/gradle/libraries.toml index 6de61e56d52..247ab418fdb 100644 --- a/gradle/libraries.toml +++ b/gradle/libraries.toml @@ -52,6 +52,8 @@ androidx-activity = "androidx.activity:activity-ktx:1.7.2" androidx-annotation = { group = "androidx.annotation", name = "annotation", version = "1.6.0" } androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version = "1.6.1" } androidx-espresso-idlingresource = { group = "androidx.test.espresso", name = "espresso-idling-resource", version = "3.5.1" } +androidx-lint-rules = "androidx.lint:lint-gradle:1.0.0-alpha01" +androidx-lint-gradle-plugin = "com.android.lint:com.android.lint.gradle.plugin:8.2.2" androidx-profileinstaller = "androidx.profileinstaller:profileinstaller:1.3.1" androidx-sqlite = { group = "androidx.sqlite", name = "sqlite", version.ref = "androidx-sqlite" } androidx-sqlite-framework = { group = "androidx.sqlite", name = "sqlite-framework", version.ref = "androidx-sqlite" } diff --git a/libraries/apollo-gradle-plugin-external/build.gradle.kts b/libraries/apollo-gradle-plugin-external/build.gradle.kts index 0234f2f3ac7..98e7912803f 100644 --- a/libraries/apollo-gradle-plugin-external/build.gradle.kts +++ b/libraries/apollo-gradle-plugin-external/build.gradle.kts @@ -3,6 +3,7 @@ plugins { id("java-gradle-plugin") id("com.gradleup.gr8") // Only used for removeGradleApiFromApi() id("org.jetbrains.kotlin.plugin.serialization") + id("com.android.lint") } apolloLibrary( @@ -21,6 +22,9 @@ dependencies { implementation(libs.asm) implementation(libs.kotlinx.serialization.json) } +dependencies { + lintChecks(libs.androidx.lint.rules) +} gradlePlugin { website.set("https://github.com/apollographql/apollo-kotlin") diff --git a/libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/api/androidProject.kt b/libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/api/androidProject.kt index d109399bbf7..1a8c5944798 100644 --- a/libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/api/androidProject.kt +++ b/libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/api/androidProject.kt @@ -14,18 +14,18 @@ import org.gradle.api.Project object AndroidProject { fun onEachVariant(project: Project, withTestVariants: Boolean = false, block: (BaseVariant) -> Unit) { - project.applicationVariants?.all { + project.applicationVariants?.configureEach { block(it) } - project.libraryVariants?.all { + project.libraryVariants?.configureEach { block(it) } if (withTestVariants) { - project.testVariants?.all { + project.testVariants?.configureEach { block(it) } - project.unitTestVariants?.all { + project.unitTestVariants?.configureEach { block(it) } } diff --git a/libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/AndroidPluginFacade.kt b/libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/AndroidPluginFacade.kt index 6589c3fcd89..1e536a0aec8 100644 --- a/libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/AndroidPluginFacade.kt +++ b/libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/AndroidPluginFacade.kt @@ -16,7 +16,6 @@ import org.gradle.api.Task import org.gradle.api.file.Directory import org.gradle.api.provider.Provider import org.gradle.api.tasks.TaskProvider -import java.io.File import java.lang.reflect.Method import java.lang.reflect.ParameterizedType @@ -26,13 +25,13 @@ private fun Project.getVariants(): NamedDomainObjectContainer { val extension = project.androidExtensionOrThrow when (extension) { is LibraryExtension -> { - extension.libraryVariants.all { variant -> + extension.libraryVariants.configureEach { variant -> container.add(variant) } } is AppExtension -> { - extension.applicationVariants.all { variant -> + extension.applicationVariants.configureEach { variant -> container.add(variant) } } @@ -42,10 +41,10 @@ private fun Project.getVariants(): NamedDomainObjectContainer { @Suppress("USELESS_IS_CHECK", "KotlinRedundantDiagnosticSuppress") if (extension is TestedExtension) { - extension.testVariants.all { variant -> + extension.testVariants.configureEach { variant -> container.add(variant) } - extension.unitTestVariants.all { variant -> + extension.unitTestVariants.configureEach { variant -> container.add(variant) } } @@ -63,17 +62,10 @@ fun connectToAndroidSourceSet( kotlinSourceSet.srcDir(outputDir) } - project.getVariants().all { + project.getVariants().configureEach { if (it.sourceSets.any { it.name == sourceSetName }) { if (kotlinSourceSet == null) { - try { - // AGP 7.0.0+: do things lazily - it.javaClass.getMethod("registerJavaGeneratingTask", TaskProvider::class.java, Array::class.java) - .invoke(it, taskProvider, arrayOf(outputDir.get().asFile)) - } catch (e: Exception) { - // Older AGP: do things eagerly - it.registerJavaGeneratingTask(taskProvider.get(), outputDir.get().asFile) - } + it.registerJavaGeneratingTask(taskProvider, outputDir.get().asFile) } else { // The kotlinSourceSet carries task dependencies, calling srcDir() above is enough // to setup task dependencies @@ -155,7 +147,7 @@ fun connectToAndroidVariant(project: Project, variant: Any, outputDir: Provider< fun connectToAllAndroidVariants(project: Project, outputDir: Provider, taskProvider: TaskProvider) { if (lazyRegisterJavaGeneratingTask != null) { - project.getVariants().all { + project.getVariants().configureEach { lazyRegisterJavaGeneratingTask.invoke(it, taskProvider, listOf(outputDir.get().asFile)) } } else {