I'm trying to upgrade Kotlin to 1.7.0 and I'm no l...
# gradle
m
I'm trying to upgrade Kotlin to 1.7.0 and I'm no longer able to get my
buildSrc
project to configure. It's breaking on applying the
kotlin-dsl
plugin.
Copy code
An exception occurred applying plugin request [id: 'org.gradle.kotlin.kotlin-dsl', version: '2.3.3']
> Failed to apply plugin class 'org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper'.
   > Could not create an instance of type org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension.
      > Companion
Gradle version: 7.3.3 Kotlin version: 1.7.0 kotlin-dsl version: 2.2 and 2.3.3
Some of the stack trace
Copy code
Caused by: java.lang.NoSuchFieldError: Companion
        at org.jetbrains.kotlin.gradle.dsl.ToolchainSupport$Companion.createToolchain$kotlin_gradle_plugin(ToolchainDsl.kt:33)
        at org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension.<init>(KotlinProjectExtension.kt:69)
        at org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension.<init>(KotlinProjectExtension.kt:109)
        at org.jetbrains.kotlin.gradle.dsl.KotlinSingleTargetExtension.<init>(KotlinProjectExtension.kt:118)
        at org.jetbrains.kotlin.gradle.dsl.KotlinSingleJavaTargetExtension.<init>(KotlinProjectExtension.kt:124)
        at org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension.<init>(KotlinProjectExtension.kt:128)
        at org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension_Decorated.<init>(Unknown Source)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at org.gradle.internal.instantiation.generator.AsmBackedClassGenerator$InvokeConstructorStrategy.newInstance(AsmBackedClassGenerator.java:2070)
        at org.gradle.internal.instantiation.generator.AbstractClassGenerator$GeneratedClassImpl$GeneratedConstructorImpl.newInstance(AbstractClassGenerator.java:488)
        at org.gradle.internal.instantiation.generator.DependencyInjectingInstantiator.doCreate(DependencyInjectingInstantiator.java:64)
        ... 311 more
m
You can't really use 1.7.0 in
buildSrc
because Gradle uses something else (1.5 most likely). Your best bet is to use
Copy code
plugins {
  `embedded-kotlin`
}
or
Copy code
plugins {
  `kotlin-dsl`
}
Oh wait, looks like this is what you're doing already 🤔
Can you build your
buildSrc
project separately?
Copy code
./gradlew -p buildSrc build
m
no I also tried adding the
embedded-kotlin
plugin but still no luck. When I did revert back to 1.6.21 the warning about incompatible kotlin versions went away, but I still cannot build with 1.7.0.
m
buildSrc
should know nothing about 1.6 vs 1.7
``kotlin-dsl`` will choose the correct version of Kotlin to use based on your Gradle version
If you have a manually defined Kotlin version in
buildSrc
, that's most likely the cause of your error
m
I have not defined in in
buildSrc
but I do use
Copy code
kotlin("plugin.serialization") version "1.6.21"
in the plugins.
m
I guess that's in a convention plugin somewhere?
m
Yes
m
Copy code
buildSrc/src/main/kotlin/my.convention.gradle.kts
or so?
m
Does the version of dependencies for the actual source code impact what is used when loading the plugins?
m
Define "actual" 😅
m
Actually everything in the
buildSrc
are
.kt
files no
.kts
files
m
m
Actual being what is declared in the dependency section of the gradle file but not the buildscript dependencies. So the dependencies that the Kotlin source code links with.
m
I mean you have multiple gradle files and buildscript there so it's hard to tell
What I'd do is move from
kotlin-dsl
to
embedded-kotlin
and rewrite the convention plugins as regular
.kt
files, it might be easier to see where the problem is
Plus you'd save some build time
m
It was the dependencies, when I downgraded them all to ones that use an older version of kotlin the error goes away.
Thanks for all your help.
m
Sure thing! Glad you found a solution!
m
This is the same problem discussed earlier in this channel. https://kotlinlang.slack.com/archives/C19FD9681/p1656593570021959
There seems to be no way to upgrade to Kotlin 1.7 currently when using the standard project layout created according to the Gradle docs, because the kotlin version used by the project and by the build get conflated.
m
@mikehearn do you have a reproducer? I'm pretty sure you can use 1.7 in your main build while still using the embedded Kotlin version for your `buildSrc`/`convention plugins`
m
In the thread above a whole lot of people worked with me on it, and we got nowhere. It never worked. It did result in a PR to the gradle docs, but, the underlying issue remains. There's no obvious way to specify a version of Kotlin for your project but not the build script - the attempts I made all failed with different errors 😞
mkrussel seems to reach the same point as me - he downgrades to older kotlin again
m
How do I reproduce ? Follow the steps at https://docs.gradle.org/current/samples/sample_building_kotlin_applications_multi_project.html and bump the kotlin version?
Copy code
mbonnin:~/git/demo$ ./gradlew :app:run            

> Task :app:run
Hello World! 1.7.0

BUILD SUCCESSFUL in 484ms
m
The issue is caused by common plugins.
The thread above ends with sort of concluding that there's some plugin that is causing conflicts because it depends on kotlin, but there's no easy way to figure out which one. So ... we wait for Gradle to upgrade Kotlin to 1.7 and hope the issue goes away.
Or something like that. Don't quite recall. It seems like a common problem though.
m
In that case yes but it's an issue from a specific plugin, not a general issue with 1.7
m
Right Kotlin itself is fine, but lots of projects are stuck due to this issue. I shared the build.gradle.kts before and even disabled some plugins but it didn't help, the only ones left are all common (like dokka). Could try again soon and see if there are many new plugin versions that fix it but for now we gave up. It's too hard to upgrade Kotlin with Gradle and when things break, the errors don't help 😞
m
FWIW, we shadow
kotlin-stdlib
in our plugins to avoid exactly that issue. I wrote about it there: https://blog.mbonnin.net/use-latest-kotlin-in-your-gradle-plugins
If you have a specific plugin that doesn't work, it's worth opening an issue there as it's fixable/workaroundable
I know that situation is far from ideal but getting stuck on older versions of Kotlin isn't ideal either...
m
Problem was, I couldn't figure out which was doing it. Disabling more and more plugins is one way but then that would just break the build, as parts do depend on those plugins.
m
Can you share all your plugins with versions?
m
https://gist.github.com/mikehearn/4c2a5ccfb3d222fa28805cf12b2c53e0 NB: The powerassert and arrow analysis plugins are/were known to not work on 1.7 but even with them disabled, there was no difference. Generally all the obviously optional ones were commented out and disabled, but no dice.
👀 1
m
Perfect 👌
You can see the dependencies with
./gradlew -p buildSrc dependencies
, that should show which one is pulling Kotlin 1.7
In that specific case,
kotlin-reflect
is pulling
kotlin-stdlib:1.7.0
You can either: • remove kotlin-reflect from your dependencies • or workaround by disabling the metadata version check:
Copy code
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class.java) {
    kotlinOptions {
        freeCompilerArgs = freeCompilerArgs + "-Xskip-metadata-version-check"
    }
}
m
Ah, this rings a bell. Refreshing my memory from the other thread, I wrote this:
OK - partial success! After removing that reflect line and the three plugins, I can get a build with Kotlin 1.7, woop! Thanks! Unfortunately, re-enabling Dokka and upgrading it to Dokka 1.7.0 brings back the same metadata version mismatch error as before.
So it seemed at the time we have to choose between Dokka and Kotlin 1.7, and Dokka is more important. Perhaps there's been a new release since that fixes this.
This was only two weeks ago though.
m
Yea, just tried with dokka and for some reason 1.7.10 still pulls the stdlib 😞
So somewhat looks like my fix didn't work 🤔
m
I should commit the removed reflect dep anyway though, thx for the reminder. I think I just got frustrated and rolled everything back as by that point the build was pretty hacked up.
👍 1
m
Something's weird with the dokka 1.7.10 release 🤔 I don't have the issue if I build locally
Or maybe something's weird with my local setup. Investingating, I'll update this thread
m
Thanks!
m
Fun stuff: the dokka:1.7.10 from
mavenCentral()
isn't the same as the dokka:1.7.10 from
gradlePluginPortal()
m
Oof 😞
i
Hi! Yeah, sorry to hear Dokka is getting in the way. You can have a look at the issue, there's some workarounds https://github.com/Kotlin/dokka/issues/2546
m
Thanks. I think we'll wait. 1.7 is a nice upgrade but not urgent. Once it's all sorted out, then we'll try again
m
For Dokka, you can use dokka 1.7.10 and do the following:
Copy code
repositories {
    mavenCentral()
    gradlePluginPortal {
        content {
            // Might not even be needed since mavenCentral() should take precedence
            excludeGroup("org.jetbrains.dokka")
        }
    }
}
That + removing the kotlin-reflect line should make your project compile
I filed this issue on Arrow but this is only warnings. I think it shouldn't prevent you to build
If you ever have another issue, please report them, the sooner we get them fixed, the sooner the ecosystem can move forward
m
Thanks!
I'll try that soon.
🙏 1
👍 1
m
In other news, Gradle 7.5 is out 🎉 : https://docs.gradle.org/7.5/release-notes.html
i
Updating to it also solves the problem, right?
m
Yep, most likely. Gradle 7.5 (with embedded Kotlin 1.6) can read the Kotlin 1.7 metadata
Because there's a 1 version "forward compatibility"
760 Views