From f2a1f19a6a5ceb6b16cf9ebc271e97add87450e7 Mon Sep 17 00:00:00 2001 From: Norman Breau Date: Wed, 8 May 2024 10:58:45 -0300 Subject: [PATCH] API 34: Change Kotlin JVM Target default. The new default value is null. When null, it will by default to the Java Target compatibility. Updating AndroidJavaTargetCompatibility will also influence the Kotlin JVM target, unless if AndroidKotlinJVMTarget is also explicitly defined. --- framework/cdv-gradle-config-defaults.json | 2 +- templates/project/app/build.gradle | 28 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/framework/cdv-gradle-config-defaults.json b/framework/cdv-gradle-config-defaults.json index d4d84c7a8..2609c7ed8 100644 --- a/framework/cdv-gradle-config-defaults.json +++ b/framework/cdv-gradle-config-defaults.json @@ -15,5 +15,5 @@ "PACKAGE_NAMESPACE": "io.cordova.helloCordova", "JAVA_SOURCE_COMPATIBILITY": 8, "JAVA_TARGET_COMPATIBILITY": 8, - "KOTLIN_JVM_TARGET": "1.8" + "KOTLIN_JVM_TARGET": null } diff --git a/templates/project/app/build.gradle b/templates/project/app/build.gradle index ba137a716..32832c509 100644 --- a/templates/project/app/build.gradle +++ b/templates/project/app/build.gradle @@ -257,6 +257,34 @@ android { } if (cordovaConfig.IS_GRADLE_PLUGIN_KOTLIN_ENABLED) { + if (cordovaConfig.KOTLIN_JVM_TARGET == null) { + // If the value is null, fallback to JAVA_TARGET_COMPATIBILITY, + // as they generally should be equal + def javaTarget = JavaLanguageVersion.of(cordovaConfig.JAVA_TARGET_COMPATIBILITY) + println "FALLBACK: " + javaTarget + + // check if javaTarget is <= 8; if so, we need to prefix it with "1." + // Starting with 9 and later, the value can be used as is. + if (javaTarget.compareTo(JavaLanguageVersion.of(8)) <= 0) { + println "PREFIXING 1." + javaTarget = "1." + javaTarget + } + + cordovaConfig.KOTLIN_JVM_TARGET = javaTarget + } + + // Similar to above, check if kotlin target is <= 8, if so prefix it. + // This allows the user to use consistent set of values in config.xml + // Rather than having to be aware whether the "1."" prefix is needed. + // This check is only done if the value isn't already prefixed with 1. + if ( + !cordovaConfig.KOTLIN_JVM_TARGET.startsWith("1.") && + JavaLanguageVersion.of(cordovaConfig.KOTLIN_JVM_TARGET).compareTo(JavaLanguageVersion.of(8)) <= 0 + ) { + cordovaConfig.KOTLIN_JVM_TARGET = "1." + cordovaConfig.KOTLIN_JVM_TARGET + } + + println "KOTLINJVMTARGET: " + cordovaConfig.KOTLIN_JVM_TARGET kotlinOptions { jvmTarget = cordovaConfig.KOTLIN_JVM_TARGET }