Hello everyone :wave: , hope you are all doing wel...
# gradle
d
Hello everyone 👋 , hope you are all doing well I have a question about
kotlin-gradle-plugin
I am developing a library that uses
kotlin-gradle-plugin
and in there I use toolchains like so:
Copy code
kotlinExtension.jvmToolchain {
                languageVersion.set(JavaLanguageVersion.of(8))
            }
            kotlinOptions.jvmTarget = "1.8"
kotlin gradle plugin version is
1.8.10
Also if it is maybe relevant, I have an Android project and there android kotlin is applied like so
Copy code
id("org.jetbrains.kotlin.android") version "1.8.10" apply false
When I publish my library and utilize it in the client application, I get Kotlin incompability issues
Copy code
.gradle/caches/........ -> Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.
Why would this matter? Why would Kotlin version used in the library matter, isn't it just compiled to java 8 binary in this case? In the client app (I didn't work on it and am not allowed to change it) I can see some legacy code like
Copy code
id("org.jetbrains.kotlin.android") version "1.6.21" apply false
I just want the library to be compatible with this client app without a need to change a lot of things in the client app. In library development as far as I understood I should have all the libraries be up to date. Only target/source compatability should be lower in order to support more client apps. I have been trying to fix this without success. The only way was updating the app to the newest versions and for some reason I don't understand it needs android
compileOptions
updated to Java 17 I have been stuck on this for a couple of days now. Can someone tell me how to fix this or at least lead me to some useful resources?
t
Gradle uses own Kotlin compiler version to compile build logic/scripts. This compiler only could only use dependencies compiled with Kotlin language version = Kotlin compiler version + 1. For example, Kotlin compiler 1.5.x could read only LV 1.6, but not LV 1.7. So you need to carefully pickup which dependencies are you using.
blob thinking upside down 1
m
👀 1
👍 1
❤️ 1
t
@Miguel Oliveira I’m not the OP but this article is gold, explained so much thing I didn’t know, that’s a lot for the link!
👌 2
d
Thank you a lot guys. Article is very good and is just what I was searching for. The only things is that even after spending a day on it, I wasn't able to implement everything properly. Will let you know about the progress tomorrow 😃
@Miguel Oliveira, @Tgo1014 After following the article you guys pointed out everything works fine. There is just one thing I had to remove from the provided code:
Copy code
kotlin {
   compilerOptions {
      freeCompilerArgs.add(libs.versions.jdkTarget.map { // <5>
	        "-Xjdk-release=${it.toJdkTarget()}"
	    })
   }
}
Adding
-Xjdk-release="1.8"
to compiler arguments of
KotlinJvmCompilerOptions
(compilerOptions) causes an error
Copy code
e: JDK_HOME path is not specified in compiler configuration
Do you maybe know why that happens. Note: Works even if I don't specify this line but I am unsure why adding this would cause any issues. Do you maybe know?
m
I don't, I was assuming that setting the jvmTarget would have the same result as that. Maybe there is some difference between 1.8 (you are using) and 1.9 (article) in the way you add the compiler args.
p
Hi @Djuro I have the same "JDK_HOME" error and this thread is the only it's reference on the whole internet 😄 Have you managed to fix it?
I've noticied that if i set jdk home explicitly
Copy code
freeCompilerArgs.add("-Xjdk-release=11")
freeCompilerArgs.add("-jdk-home=/Library/Java/JavaVirtualMachines/zulu-21.jdk/Contents/Home")
Then there is a message > w: The '-jdk-home' option is ignored because '-no-jdk' is specified I've noticied no-jdk is set by kotlin android plugin https://github.com/JetBrains/kotlin/blame/bfbd03d046b426abb34ca0bc9bf1d8f5ec466b38[…]otlin/org/jetbrains/kotlin/gradle/plugin/KotlinAndroidPlugin.kt Is it intended? So
-Xjdk-release
is not possible for android projects?
Yes, -no-jdk is inteded for android projects. https://youtrack.jetbrains.com/issue/KT-54317/It-is-impossible-to-use-no-jdk-kotlin-comp[…]id-project-while-trying-to-configure-it-using-allProjects I guess there is no way to use
-Xjdk-release
for android projects unless you use old and outdated java to build desired api version for android projects 😭
d
@plastiv I understood what I was doing more or less by following this article. I recommend you so the same and try to implement it the same way and see where you might be making a mistake
m
e: JDK_HOME path is not specified in compiler configuration
I believe this is because
jdk-release
doesn’t make sense for Android (Android doesn’t use
rt.jar
). There is a specific message for the Java compiler but looks like the error is less explicit for Kotlin. See: • https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:[…]leUtils.kt;l=410;drc=d59e14a47b09934c8c510174fceab9a290fe072dhttps://issuetracker.google.com/issues/278800528
241 Views