From b5cf65e0601235cfa950dfc3557124daa3ef58ff Mon Sep 17 00:00:00 2001 From: originx Date: Thu, 26 Oct 2017 08:40:26 +0200 Subject: [PATCH 1/4] Add support for Android build tools 3.0 and ktlin tasks for multidimension builds. new google repo is added since this is now a maven repo where google hosts all of its tools from now on. --- build.gradle.kts | 3 +- .../jlleitschuh/gradle/ktlint/KtlintPlugin.kt | 29 +++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 3a9ae9bf..c7f785b3 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,12 +13,13 @@ version = "2.2.2-SNAPSHOT" repositories { jcenter() + google() } dependencies { compileOnly(gradleApi()) compileOnly(kotlin("gradle-plugin", "1.1.1")) - compileOnly("com.android.tools.build:gradle:2.3.3") + compileOnly("com.android.tools.build:gradle:3.0.0-rc2") /* * Do not depend upon the gradle script kotlin plugin API. IE: gradleScriptKotlinApi() * It's currently in flux and has binary breaking changes in gradle 4.0 diff --git a/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt b/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt index 0a259d40..62fc328e 100644 --- a/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt +++ b/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt @@ -1,6 +1,6 @@ package org.jlleitschuh.gradle.ktlint -import com.android.build.gradle.BaseExtension +import com.android.build.gradle.* import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.Task @@ -11,6 +11,7 @@ import org.gradle.api.internal.HasConvention import org.gradle.api.plugins.Convention import org.gradle.api.plugins.JavaPluginConvention import org.gradle.api.tasks.JavaExec +import org.gradle.api.tasks.StopExecutionException import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet import kotlin.reflect.KClass @@ -63,28 +64,38 @@ open class KtlintPlugin : Plugin { } } - private fun addKtLintTasksToAndroidKotlinPlugin(target: Project, - extension: KtlintExtension) { + private fun addKtLintTasksToAndroidKotlinPlugin(target: Project, extension: KtlintExtension) { target.pluginManager.withPlugin("kotlin-android") { target.afterEvaluate { val ktLintConfig = createConfiguration(target, extension) - - target.extensions.findByType(BaseExtension::class.java)?.sourceSets?.forEach { - val kotlinSourceDir = it.java.sourceFiles - val runArgs = it.java.srcDirs.map { "${it.path}/**/*.kt" }.toMutableSet() + + val variantManager = when { + target.plugins.hasPlugin(AppPlugin::class.java) -> target.plugins.findPlugin(AppPlugin::class.java)?.variantManager + target.plugins.hasPlugin(LibraryPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager + target.plugins.hasPlugin(InstantAppPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager + target.plugins.hasPlugin(InstantAppPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager + target.plugins.hasPlugin(FeaturePlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager + target.plugins.hasPlugin(TestPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager + else -> throw StopExecutionException("Must be applied with 'android' or 'android-library' plugin.") + } + + variantManager?.variantScopes?.forEach { + val kotlinSourceDir = target.files(it.variantData.javaSources) + val runArgs = it.variantData.javaSources.map { "${it.dir.path}/**/*.kt" }.toMutableSet() addAdditionalRunArgs(extension, runArgs) - val checkTask = createCheckTask(target, extension, it.name, ktLintConfig, kotlinSourceDir, runArgs) + val checkTask = createCheckTask(target, extension, it.fullVariantName, ktLintConfig, kotlinSourceDir, runArgs) addKtlintCheckTaskToProjectMetaCheckTask(target, checkTask) setCheckTaskDependsOnKtlintCheckTask(target, checkTask) - val ktlintSourceSetFormatTask = createFormatTask(target, it.name, ktLintConfig, kotlinSourceDir, runArgs) + val ktlintSourceSetFormatTask = createFormatTask(target, it.fullVariantName, ktLintConfig, kotlinSourceDir, runArgs) addKtlintFormatTaskToProjectMetaFormatTask(target, ktlintSourceSetFormatTask) } } } } + private fun createConfiguration(target: Project, extension: KtlintExtension) = target.configurations.maybeCreate("ktlint").apply { target.dependencies.add(this.name, From efb187909271a7a02372dfde639a7c8b3e2c8826 Mon Sep 17 00:00:00 2001 From: originx Date: Thu, 26 Oct 2017 08:40:26 +0200 Subject: [PATCH 2/4] Add support for Android build tools 3.0 and ktlin tasks for multidimension builds. new google repo is added since this is now a maven repo where google hosts all of its tools from now on. --- CHANGELOG.md | 2 +- build.gradle.kts | 2 +- .../kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt | 7 +++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b61e8ea2..d49bb0c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [unreleased] ### Fixed - Fixed plugin doesn't apply custom reporter for ktlint versions >0.10.x (#28) - +- Bumped android tools versions to 3.0 and now support multidimension projects >0.10.x (#29) ## [2.2.1] - 2017-10-06 ### Fixed - Fixed report output is always opened since task is created diff --git a/build.gradle.kts b/build.gradle.kts index c7f785b3..c0f1cd21 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,7 +19,7 @@ repositories { dependencies { compileOnly(gradleApi()) compileOnly(kotlin("gradle-plugin", "1.1.1")) - compileOnly("com.android.tools.build:gradle:3.0.0-rc2") + compileOnly("com.android.tools.build:gradle:3.0.0") /* * Do not depend upon the gradle script kotlin plugin API. IE: gradleScriptKotlinApi() * It's currently in flux and has binary breaking changes in gradle 4.0 diff --git a/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt b/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt index 62fc328e..38e4dd68 100644 --- a/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt +++ b/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt @@ -72,10 +72,9 @@ open class KtlintPlugin : Plugin { val variantManager = when { target.plugins.hasPlugin(AppPlugin::class.java) -> target.plugins.findPlugin(AppPlugin::class.java)?.variantManager target.plugins.hasPlugin(LibraryPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager - target.plugins.hasPlugin(InstantAppPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager - target.plugins.hasPlugin(InstantAppPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager - target.plugins.hasPlugin(FeaturePlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager - target.plugins.hasPlugin(TestPlugin::class.java) -> target.plugins.findPlugin(LibraryPlugin::class.java)?.variantManager + target.plugins.hasPlugin(InstantAppPlugin::class.java) -> target.plugins.findPlugin(InstantAppPlugin::class.java)?.variantManager + target.plugins.hasPlugin(FeaturePlugin::class.java) -> target.plugins.findPlugin(FeaturePlugin::class.java)?.variantManager + target.plugins.hasPlugin(TestPlugin::class.java) -> target.plugins.findPlugin(TestPlugin::class.java)?.variantManager else -> throw StopExecutionException("Must be applied with 'android' or 'android-library' plugin.") } From 7ddc8b03b562446d64edebf1b4d7eec3f0bdbb09 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Tue, 14 Nov 2017 19:58:26 +0100 Subject: [PATCH 3/4] Fix wildcard import. Signed-off-by: Yahor Berdnikau --- .../kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt index a58854f5..20f50806 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt @@ -1,6 +1,10 @@ package org.jlleitschuh.gradle.ktlint -import com.android.build.gradle.* +import com.android.build.gradle.AppPlugin +import com.android.build.gradle.FeaturePlugin +import com.android.build.gradle.InstantAppPlugin +import com.android.build.gradle.LibraryPlugin +import com.android.build.gradle.TestPlugin import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.Task From d26f377041144ac55b188d5dcc475da472ebb1a8 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Tue, 14 Nov 2017 20:07:15 +0100 Subject: [PATCH 4/4] Fix failing checkstyle. Signed-off-by: Yahor Berdnikau --- CHANGELOG.md | 2 +- .../main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 322105f4..e804e2dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - Define a different output file for each sourceSet ### Fixed -- Fixed plugin doesn't apply custom reporter for ktlint versions >0.10.x (#28) + - Fixed plugin doesn't apply custom reporter for ktlint versions >0.10.x (#28) ## [2.2.1] - 2017-10-06 ### Fixed diff --git a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt index 20f50806..25a46f5d 100644 --- a/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt +++ b/plugin/src/main/kotlin/org/jlleitschuh/gradle/ktlint/KtlintPlugin.kt @@ -98,7 +98,6 @@ open class KtlintPlugin : Plugin { } } - private fun createConfiguration(target: Project, extension: KtlintExtension) = target.configurations.maybeCreate("ktlint").apply { target.dependencies.add(this.name,