Skip to content

Commit 0be0daf

Browse files
committed
introduce Kotlin convention plugins for java and android libraries
This pulls compiler configurations into a single location, except for friend paths. Also introduces the Kotlin bom platform dependency to ensure a single Kotlin version.
1 parent fd48bf4 commit 0be0daf

File tree

57 files changed

+169
-202
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+169
-202
lines changed

benchmarks/performance-poetry/complex-poetry/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
22
id("com.android.application")
3-
kotlin("android")
3+
`kotlin-android`
44
id("kotlin-parcelize")
55
}
66
android {

build.gradle.kts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import org.jetbrains.dokka.gradle.DokkaTask
2-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
32
import org.jlleitschuh.gradle.ktlint.KtlintExtension
43
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
54

@@ -31,9 +30,6 @@ plugins {
3130
`dependency-guard`
3231
}
3332

34-
// See https://stackoverflow.com/questions/25324880/detect-ide-environment-with-gradle
35-
val isRunningFromIde get() = project.properties["android.injected.invoked.from.ide"] == "true"
36-
3733
subprojects {
3834

3935
apply(plugin = "org.jlleitschuh.gradle.ktlint")
@@ -45,21 +41,6 @@ subprojects {
4541
}
4642
}
4743

48-
tasks.withType<KotlinCompile> {
49-
kotlinOptions {
50-
// Allow warnings when running from IDE, makes it easier to experiment.
51-
if (!isRunningFromIde) {
52-
allWarningsAsErrors = true
53-
}
54-
55-
jvmTarget = "1.8"
56-
57-
// Don't panic, all this does is allow us to use the @OptIn meta-annotation.
58-
// to define our own experiments.
59-
freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
60-
}
61-
}
62-
6344
// Configuration documentation: https://github.com/JLLeitschuh/ktlint-gradle#configuration
6445
configure<KtlintExtension> {
6546
// Prints the name of failed rules.
@@ -73,35 +54,6 @@ subprojects {
7354

7455
apply(from = rootProject.file(".buildscript/binary-validation.gradle"))
7556

76-
// Require explicit public modifiers and types for actual library modules, not samples.
77-
allprojects.filterNot {
78-
it.path.startsWith(":samples") ||
79-
it.path.startsWith(":benchmarks")
80-
}
81-
.forEach {
82-
it.tasks.withType<KotlinCompile>().configureEach {
83-
// Tests and benchmarks aren't part of the public API, don't turn explicit API mode on for
84-
// them.
85-
if (!name.contains("test", ignoreCase = true) &&
86-
!name.contains("jmh", ignoreCase = true)
87-
) {
88-
kotlinOptions {
89-
// TODO this should be moved to `kotlin { explicitApi() }` once that's working for android
90-
// projects, see https://youtrack.jetbrains.com/issue/KT-37652.
91-
@Suppress("SuspiciousCollectionReassignment")
92-
freeCompilerArgs += "-Xexplicit-api=strict"
93-
94-
// Make sure our module names don't conflict with those from pre-workflow1
95-
// releases, so that old and new META-INF/ entries don't stomp each other.
96-
// (This is only an issue for apps that are still migrating from workflow to
97-
// workflow1, and so need to import two versions of the library.)
98-
// https://blog.jetbrains.com/kotlin/2015/09/kotlin-m13-is-out/
99-
moduleName = "wf1-${it.name}"
100-
}
101-
}
102-
}
103-
}
104-
10557
// This plugin needs to be applied to the root projects for the dokkaGfmCollector task we use to
10658
// generate the documentation site.
10759
apply(plugin = "org.jetbrains.dokka")

buildSrc/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ repositories {
1010
}
1111

1212
dependencies {
13+
14+
implementation(platform(libs.kotlin.bom))
15+
1316
compileOnly(gradleApi())
1417

1518
implementation(libs.android.gradle.plugin)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.squareup.workflow1.buildsrc
2+
3+
import org.gradle.api.Project
4+
import org.gradle.kotlin.dsl.dependencies
5+
import org.gradle.kotlin.dsl.kotlin
6+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
7+
8+
// See https://stackoverflow.com/questions/25324880/detect-ide-environment-with-gradle
9+
val Project.isRunningFromIde
10+
get() = properties["android.injected.invoked.from.ide"] == "true"
11+
12+
@Suppress("SuspiciousCollectionReassignment")
13+
fun Project.kotlinCommonSettings(
14+
compileTask: KotlinCompile
15+
) {
16+
17+
// force the same Kotlin version everywhere, including transitive dependencies
18+
dependencies {
19+
"implementation"(platform(kotlin("bom")))
20+
}
21+
22+
compileTask.kotlinOptions {
23+
24+
// Allow warnings when running from IDE, makes it easier to experiment.
25+
if (!isRunningFromIde) {
26+
allWarningsAsErrors = true
27+
}
28+
29+
// Don't panic, all this does is allow us to use the @OptIn meta-annotation.
30+
// to define our own experiments.
31+
freeCompilerArgs += "-opt-in=kotlin.RequiresOptIn"
32+
33+
// Make sure our module names don't conflict with those from pre-workflow1
34+
// releases, so that old and new META-INF/ entries don't stomp each other.
35+
// (This is only an issue for apps that are still migrating from workflow to
36+
// workflow1, and so need to import two versions of the library.)
37+
// https://blog.jetbrains.com/kotlin/2015/09/kotlin-m13-is-out/
38+
moduleName = "wf1-${project.name}"
39+
}
40+
41+
maybeEnableExplicitApi(compileTask)
42+
}
43+
44+
private fun Project.maybeEnableExplicitApi(compileTask: KotlinCompile) {
45+
when {
46+
path.startsWith(":samples") -> return
47+
path.startsWith(":benchmarks") -> return
48+
compileTask.name.contains("test", ignoreCase = true) -> return
49+
compileTask.name.contains("jmh", ignoreCase = true) -> return
50+
else -> compileTask.kotlinOptions {
51+
// TODO this should be moved to `kotlin { explicitApi() }` once that's working for android
52+
// projects, see https://youtrack.jetbrains.com/issue/KT-37652.
53+
freeCompilerArgs += "-Xexplicit-api=strict"
54+
}
55+
}
56+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import com.squareup.workflow1.buildsrc.kotlinCommonSettings
2+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3+
4+
plugins {
5+
kotlin("android")
6+
}
7+
8+
extensions.getByType(JavaPluginExtension::class).apply {
9+
sourceCompatibility = JavaVersion.VERSION_1_8
10+
targetCompatibility = JavaVersion.VERSION_1_8
11+
}
12+
13+
tasks.withType<KotlinCompile> {
14+
kotlinOptions {
15+
jvmTarget = "1.8"
16+
}
17+
18+
project.kotlinCommonSettings(this)
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import com.squareup.workflow1.buildsrc.kotlinCommonSettings
2+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3+
4+
plugins {
5+
kotlin("jvm")
6+
}
7+
8+
extensions.getByType(JavaPluginExtension::class).apply {
9+
sourceCompatibility = JavaVersion.VERSION_1_8
10+
targetCompatibility = JavaVersion.VERSION_1_8
11+
}
12+
13+
tasks.withType<KotlinCompile> {
14+
kotlinOptions {
15+
jvmTarget = "1.8"
16+
}
17+
18+
project.kotlinCommonSettings(this)
19+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ jmh-gradle-plugin = "me.champeau.gradle:jmh-gradle-plugin:0.5.3"
169169

170170
junit = { module = "junit:junit", version.ref = "jUnit" }
171171

172+
kotlin-bom = { module = "org.jetbrains.kotlin:kotlin-bom", version.ref = "kotlin" }
172173
kotlin-common = { module = "org.jetbrains.kotlin:kotlin-stdlib-common", version.ref = "kotlin" }
173174
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
174175
kotlin-jdk6 = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }

internal-testing-utils/build.gradle.kts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
plugins {
2-
kotlin("jvm")
2+
`kotlin-jvm`
33
}
44

55
apply(from = rootProject.file(".buildscript/configure-maven-publish.gradle"))
66

7-
java {
8-
sourceCompatibility = JavaVersion.VERSION_1_8
9-
targetCompatibility = JavaVersion.VERSION_1_8
10-
}
11-
127
dependencies {
138
implementation(libs.kotlin.jdk8)
149

internal-testing-utils/dependencies/runtimeClasspath.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
org.jetbrains.kotlin:kotlin-bom:1.6.10
12
org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10
23
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10
34
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10

samples/compose-samples/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
22
id("com.android.application")
3-
kotlin("android")
3+
`kotlin-android`
44
}
55

66
apply(from = rootProject.file(".buildscript/android-sample-app.gradle"))

0 commit comments

Comments
 (0)