From b6f9e1740c6643da7111f71fc5769b193e1b64e8 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 9 May 2024 11:36:34 -0700 Subject: [PATCH] Move Java/Kotlin compat to the convention plugin. This was apparently only ever in the gradle scripts for projects that used Kotlin. I don't know if it's necessary for Java projects, but it's likely not harmful? For Kotlin projects these versions all need to match. It's actually possible for us to bump this to Java 11, by the look of things (see the Now In Android link below). For now I'm keeping things the way they are. https://github.com/android/nowinandroid/blob/7f2ce57c67c68aa921de9231525a4a47ef431fe8/build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/KotlinAndroid.kt#L63 --- build-logic/build.gradle.kts | 5 ++++ .../AndroidApplicationConventionPlugin.kt | 4 +++ .../buildlogic/KotlinConventionPlugin.kt | 27 +++++++++++++++++++ .../ndk/samples/buildlogic/Versions.kt | 3 +++ gradle/libs.versions.toml | 1 + 5 files changed, 40 insertions(+) create mode 100644 build-logic/src/main/java/com/android/ndk/samples/buildlogic/KotlinConventionPlugin.kt diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index 60bfea6c8..63f87cc55 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -19,6 +19,7 @@ tasks.withType().configureEach { dependencies { compileOnly(libs.android.gradlePlugin) + compileOnly(libs.kotlin.gradlePlugin) } gradlePlugin { @@ -27,5 +28,9 @@ gradlePlugin { id = "ndksamples.android.application" implementationClass = "com.android.ndk.samples.buildlogic.AndroidApplicationConventionPlugin" } + register("kotlinAndroid") { + id = "ndksamples.android.kotlin" + implementationClass = "com.android.ndk.samples.buildlogic.KotlinConventionPlugin" + } } } diff --git a/build-logic/src/main/java/com/android/ndk/samples/buildlogic/AndroidApplicationConventionPlugin.kt b/build-logic/src/main/java/com/android/ndk/samples/buildlogic/AndroidApplicationConventionPlugin.kt index 8997bfc2d..23f521823 100644 --- a/build-logic/src/main/java/com/android/ndk/samples/buildlogic/AndroidApplicationConventionPlugin.kt +++ b/build-logic/src/main/java/com/android/ndk/samples/buildlogic/AndroidApplicationConventionPlugin.kt @@ -20,6 +20,10 @@ class AndroidApplicationConventionPlugin : Plugin { minSdk = Versions.MIN_SDK targetSdk = Versions.TARGET_SDK } + compileOptions { + sourceCompatibility = Versions.JAVA + targetCompatibility = Versions.JAVA + } } } } diff --git a/build-logic/src/main/java/com/android/ndk/samples/buildlogic/KotlinConventionPlugin.kt b/build-logic/src/main/java/com/android/ndk/samples/buildlogic/KotlinConventionPlugin.kt new file mode 100644 index 000000000..4b031da8c --- /dev/null +++ b/build-logic/src/main/java/com/android/ndk/samples/buildlogic/KotlinConventionPlugin.kt @@ -0,0 +1,27 @@ +package com.android.ndk.samples.buildlogic + +import com.android.build.api.dsl.ApplicationExtension + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.configure +import org.gradle.kotlin.dsl.withType +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile + +class KotlinConventionPlugin : Plugin { + override fun apply(target: Project) { + with(target) { + with(pluginManager) { + apply("org.jetbrains.kotlin.android") + } + + extensions.configure { + tasks.withType().configureEach { + kotlinOptions { + jvmTarget = Versions.JAVA.toString() + } + } + } + } + } +} diff --git a/build-logic/src/main/java/com/android/ndk/samples/buildlogic/Versions.kt b/build-logic/src/main/java/com/android/ndk/samples/buildlogic/Versions.kt index 068504756..3d4898a4a 100644 --- a/build-logic/src/main/java/com/android/ndk/samples/buildlogic/Versions.kt +++ b/build-logic/src/main/java/com/android/ndk/samples/buildlogic/Versions.kt @@ -1,8 +1,11 @@ package com.android.ndk.samples.buildlogic +import org.gradle.api.JavaVersion + object Versions { const val COMPILE_SDK = 34 const val TARGET_SDK = 34 const val MIN_SDK = 21 const val NDK = "26.3.11579264" // r26d + val JAVA = JavaVersion.VERSION_1_8 } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 736618e5a..0c12d7450 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -14,6 +14,7 @@ ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitV espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" } material = { group = "com.google.android.material", name = "material", version.ref = "material" } +kotlin-gradlePlugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlin" } # build-logic dependencies android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" }