From 836361d9e73988c0dbc3bbfceb53f9ff9670425a Mon Sep 17 00:00:00 2001 From: Vishal Kumar Date: Sat, 23 Nov 2024 08:16:54 -0800 Subject: [PATCH] Fix issue with kotlin-kapt plugin during project build process. --- README.md | 4 ++-- build.gradle.kts | 4 ---- gradle/libs.versions.toml | 11 ++++++---- resourcemanager/build.gradle.kts | 20 ++++++++++--------- .../src/main/kotlin/ResourceManagerPlugin.kt | 17 ++++++++++++++-- .../manager/MigrationManager.kt | 2 ++ .../manager/ResourceManager.kt | 2 +- 7 files changed, 38 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 212734a..460d98b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ plugins { id("com.android.application") version "8.0.1" apply false id("org.jetbrains.kotlin.android") version "1.8.21" apply false ... - id("dev.randos.resourcemanager") version "0.0.6" apply false // Add ResourceManager plugin + id("dev.randos.resourcemanager") version "0.0.7" apply false // Add ResourceManager plugin } ``` - If your project uses the buildscript block. @@ -25,7 +25,7 @@ buildScripts { classpath "com.android.tools.build:gradle:8.0.1" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.21" ... - classpath 'dev.randos:resourcemanager:0.0.6' // Add ResourceManager plugin + classpath 'dev.randos:resourcemanager:0.0.7' // Add ResourceManager plugin } } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 9e429cc..c54cce8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,8 +2,4 @@ plugins { alias(libs.plugins.jetbrains.kotlin.jvm) apply false alias(libs.plugins.ktlint) apply false -} - -ext { - set("agp", "7.0.0") } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d194cf6..35f90d1 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,15 +1,18 @@ [versions] -jetbrainsKotlinJvm = "1.9.0" +kotlin = "1.9.22" ktlint = "12.1.1" junit = "4.12" mockk = "1.13.13" +agp = "7.0.0" +gradlePluginPublish = "1.3.0" [libraries] junit = { group = "junit", name = "junit", version.ref = "junit" } mockk = {group = "io.mockk", name = "mockk", version.ref = "mockk"} +android-tools = {group = "com.android.tools.build", name = "gradle", version.ref = "agp" } +kotlin-gradle-plugin = {group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin"} [plugins] -jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "jetbrainsKotlinJvm" } +jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" } - - +gradle-plugin-publish = { id = "com.gradle.plugin-publish", version.ref = "gradlePluginPublish" } \ No newline at end of file diff --git a/resourcemanager/build.gradle.kts b/resourcemanager/build.gradle.kts index d26ccab..9fb3385 100644 --- a/resourcemanager/build.gradle.kts +++ b/resourcemanager/build.gradle.kts @@ -1,25 +1,27 @@ import org.jlleitschuh.gradle.ktlint.reporter.ReporterType plugins { + `java-gradle-plugin` `kotlin-dsl` - kotlin("jvm") - id("java-gradle-plugin") - id("com.gradle.plugin-publish") version "1.3.0" - id("maven-publish") - id("org.jlleitschuh.gradle.ktlint") + `maven-publish` + alias(libs.plugins.gradle.plugin.publish) + alias(libs.plugins.jetbrains.kotlin.jvm) + alias(libs.plugins.ktlint) jacoco } apply(from = "../gradle/jacoco.gradle.kts") val agp: String by project +val kotlinVersion: String by project val pluginGroup = "dev.randos" -val pluginVersion = "0.0.6" +val pluginVersion = "0.0.7" val pluginName = "resourcemanager" dependencies { - implementation(gradleApi()) - implementation(localGroovy()) - compileOnly("com.android.tools.build:gradle:$agp") + compileOnly(gradleApi()) + compileOnly(localGroovy()) + compileOnly(libs.android.tools) + compileOnly(libs.kotlin.gradle.plugin) testImplementation(libs.junit) testImplementation(libs.mockk) testImplementation(gradleTestKit()) diff --git a/resourcemanager/src/main/kotlin/ResourceManagerPlugin.kt b/resourcemanager/src/main/kotlin/ResourceManagerPlugin.kt index ca00edb..42516f7 100644 --- a/resourcemanager/src/main/kotlin/ResourceManagerPlugin.kt +++ b/resourcemanager/src/main/kotlin/ResourceManagerPlugin.kt @@ -4,6 +4,10 @@ import dev.randos.resourcemanager.manager.ModuleManager import dev.randos.resourcemanager.manager.ResourceManager import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.tasks.compile.JavaCompile +import org.gradle.kotlin.dsl.withType +import org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import task.ResourceManagerMigrationTask import java.io.File @@ -38,10 +42,19 @@ class ResourceManagerPlugin : Plugin { } // Ensure that the "generateResourceManager" task runs before any compile task. - project.tasks.matching { it.name.startsWith("compile") } - .configureEach { + project.tasks.withType().configureEach { + dependsOn(generateResourceManagerTask) + } + project.tasks.withType().configureEach { + dependsOn(generateResourceManagerTask) + } + + // Specifically handle kapt stubs generation in case if consuming project has kotlin-kapt plugin applied. + project.pluginManager.withPlugin("kotlin-kapt") { + project.tasks.withType().configureEach { dependsOn(generateResourceManagerTask) } + } // Add the generated file to the Kotlin source sets so that it can be used as part of the build. project.extensions.getByType(BaseExtension::class.java) diff --git a/resourcemanager/src/main/kotlin/dev/randos/resourcemanager/manager/MigrationManager.kt b/resourcemanager/src/main/kotlin/dev/randos/resourcemanager/manager/MigrationManager.kt index 8af0228..f8a0a6d 100644 --- a/resourcemanager/src/main/kotlin/dev/randos/resourcemanager/manager/MigrationManager.kt +++ b/resourcemanager/src/main/kotlin/dev/randos/resourcemanager/manager/MigrationManager.kt @@ -162,6 +162,8 @@ internal class MigrationManager( if (matchResultImport?.groups?.size == 2) { currentResourceImportStatement = matchResultImport.groups[0]?.value.orEmpty() currentResourceImportStatementIndex = index + // Since we may be including a new line, increment index. + index++ } var matchResultResources: Sequence diff --git a/resourcemanager/src/main/kotlin/dev/randos/resourcemanager/manager/ResourceManager.kt b/resourcemanager/src/main/kotlin/dev/randos/resourcemanager/manager/ResourceManager.kt index 662e275..aeb08c3 100644 --- a/resourcemanager/src/main/kotlin/dev/randos/resourcemanager/manager/ResourceManager.kt +++ b/resourcemanager/src/main/kotlin/dev/randos/resourcemanager/manager/ResourceManager.kt @@ -76,7 +76,7 @@ internal class ResourceManager( list.add( Resource( type = ResourceType.DRAWABLES, - moduleDetails = ModuleDetails(module, namespace, File(resFile, "drawable")) + moduleDetails = ModuleDetails(module, namespace, File(resFile, drawableDirectory.name)) ) ) }