Skip to content

Commit f97df24

Browse files
committed
[Infra] Update how we declare kotlin compiler options
Kotlin compiler options should be specified using the kotlin block instead of the `kotlinOptions` within `android`. See https://youtrack.jetbrains.com/issue/KT-27301/Expose-compiler-flags-via-Gradle-lazy-properties and https://b.corp.google.com/issues/247544167. This applies to how the JVM target is declared. Additionally, there's a better way to declare free compiler args, in our case to forche the explicit api flag to be "strict".
1 parent 60e3bcb commit f97df24

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

firebase-ai/firebase-ai.gradle.kts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
@file:Suppress("UnstableApiUsage")
1818

19-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
19+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
20+
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
2021

2122
plugins {
2223
id("firebase-library")
@@ -55,7 +56,6 @@ android {
5556
sourceCompatibility = JavaVersion.VERSION_1_8
5657
targetCompatibility = JavaVersion.VERSION_1_8
5758
}
58-
kotlinOptions { jvmTarget = "1.8" }
5959
testOptions {
6060
targetSdk = targetSdkVersion
6161
unitTests {
@@ -73,17 +73,17 @@ android {
7373
}
7474
}
7575

76+
kotlin { compilerOptions { jvmTarget = JvmTarget.JVM_1_8 } }
77+
7678
// Enable Kotlin "Explicit API Mode". This causes the Kotlin compiler to fail if any
7779
// classes, methods, or properties have implicit `public` visibility. This check helps
7880
// avoid accidentally leaking elements into the public API, requiring that any public
7981
// element be explicitly declared as `public`.
8082
// https://github.com/Kotlin/KEEP/blob/master/proposals/explicit-api-mode.md
8183
// https://chao2zhang.medium.com/explicit-api-mode-for-kotlin-on-android-b8264fdd76d1
82-
tasks.withType<KotlinCompile>().all {
84+
tasks.withType<KotlinJvmCompile>().configureEach {
8385
if (!name.contains("test", ignoreCase = true)) {
84-
if (!kotlinOptions.freeCompilerArgs.contains("-Xexplicit-api=strict")) {
85-
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
86-
}
86+
compilerOptions.freeCompilerArgs.add("-Xexplicit-api=strict")
8787
}
8888
}
8989

firebase-functions/firebase-functions.gradle.kts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2-
31
// Copyright 2022 Google LLC
42
//
53
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -14,6 +12,9 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
1412
// See the License for the specific language governing permissions and
1513
// limitations under the License.
1614

15+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
16+
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
17+
1718
plugins {
1819
id("firebase-library")
1920
id("kotlin-android")
@@ -49,25 +50,24 @@ android {
4950
sourceCompatibility = JavaVersion.VERSION_1_8
5051
targetCompatibility = JavaVersion.VERSION_1_8
5152
}
52-
kotlinOptions { jvmTarget = "1.8" }
5353
testOptions {
5454
targetSdk = targetSdkVersion
5555
unitTests { isIncludeAndroidResources = true }
5656
}
5757
lint { targetSdk = targetSdkVersion }
5858
}
5959

60+
kotlin { compilerOptions { jvmTarget = JvmTarget.JVM_1_8 } }
61+
6062
// Enable Kotlin "Explicit API Mode". This causes the Kotlin compiler to fail if any
6163
// classes, methods, or properties have implicit `public` visibility. This check helps
6264
// avoid accidentally leaking elements into the public API, requiring that any public
6365
// element be explicitly declared as `public`.
6466
// https://github.com/Kotlin/KEEP/blob/master/proposals/explicit-api-mode.md
6567
// https://chao2zhang.medium.com/explicit-api-mode-for-kotlin-on-android-b8264fdd76d1
66-
tasks.withType<KotlinCompile>().all {
68+
tasks.withType<KotlinJvmCompile>().configureEach {
6769
if (!name.contains("test", ignoreCase = true)) {
68-
if (!kotlinOptions.freeCompilerArgs.contains("-Xexplicit-api=strict")) {
69-
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
70-
}
70+
compilerOptions.freeCompilerArgs.add("-Xexplicit-api=strict")
7171
}
7272
}
7373

firebase-sessions/firebase-sessions.gradle.kts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
@file:Suppress("UnstableApiUsage")
1818

19+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
20+
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
21+
1922
plugins {
2023
id("firebase-library")
2124
id("firebase-vendor")
@@ -49,7 +52,6 @@ android {
4952
sourceCompatibility = JavaVersion.VERSION_1_8
5053
targetCompatibility = JavaVersion.VERSION_1_8
5154
}
52-
kotlinOptions { jvmTarget = "1.8" }
5355
testOptions {
5456
targetSdk = targetSdkVersion
5557
unitTests { isIncludeAndroidResources = true }
@@ -60,8 +62,12 @@ android {
6062
}
6163
}
6264

63-
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs::class.java).configureEach {
64-
kotlinOptions.jvmTarget = "1.8"
65+
kotlin { compilerOptions { jvmTarget = JvmTarget.JVM_1_8 } }
66+
67+
tasks.withType<KotlinJvmCompile>().configureEach {
68+
if (!name.contains("test", ignoreCase = true)) {
69+
compilerOptions.freeCompilerArgs.add("-Xexplicit-api=strict")
70+
}
6571
}
6672

6773
dependencies {

0 commit comments

Comments
 (0)