I've been trying to get an environment for working with Kotlin (multiplatform) for a little too long now and would like some advice. I seem to get stuck on which version of JDK do I need, should I download Android Studio for the Android SDK or is there a cleaner way than getting the full IDE, which version of IntelliJ to use and which version of the Kotlin plugin is compatible.
I would like to use Kotlin 1.3.
My aim is to develop a common library with no platform-specific code; though I would like to test it on Android, Linux, WebAssembly.
Lubuntu 18.04.1
Intel x64
I'd suggest using the Kotlin Multiplatform Plugin which just came out. Its setup is very simple:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.2.71'
}
repositories {
mavenCentral()
}
kotlin {
targets {
fromPreset(presets.jvm, 'jvm')
fromPreset(presets.js, 'js')
fromPreset(presets.mingwX64, 'mingw')
}
sourceSets { /* ... */ }
}
It comes with built-in presets for all supported platforms but you can also set them up individually. I've worked with the "old way" of doing multiplatform development and using this plugin will just make it infinitely easier to deal with. You can read more about setting it up here.
You should target Java 6 code if you're planning to use the library in Android and not having any headaches. Note that you need to specify that explicitly to the Kotlin compiler. Only setting the IntelliJ IDEA project to target Java 6 won't do.
The JDK version doesn't really matter since you can tell the compiler to generate code for any previous Java version.
IntelliJ IDEA is probably the best IDE for Kotlin since it's made by JetBrains, the same guys that are behind Kotlin itself. Get the latest version.
You don't need any extra plugin, Kotlin is supported out of the box by IntelliJ IDEA (i.e. Kotlin plugin is included in the standard version).
Related
I am running into a compile error when trying to compile a project with Kotlin and Compose Multiplatform, I think due to the Compose 1.1.0 compiler extension not being compatible with Kotlin 1.6.21. Even though I set my Kotlin plugin version to 1.6.10:
plugins {
idea
kotlin("jvm") version "1.6.10"
id("org.jetbrains.compose") version "1.1.0"
}
I get this error:
Kotlin: kotlinc-jvm 1.6.21-release-334 (JRE 15+36-1562)
Kotlin: [Internal Error] java.lang.IllegalStateException: The provided plugin androidx.compose.compiler.plugins.kotlin.ComposeComponentRegistrar is not compatible with this version of compiler
I can't figure out how to set the version of the Kotlin compiler that is used. From the log above the error, I can see it's using version 1.6.21-release-334, rather than some 1.6.10 version.
An alternate possible solution would be to change the version of the Compose Kotlin Compliler plugin to a more recent alpha or beta version that supports Kotlin 1.6.21, but I can't figure out how to do that either. In an Android project, you would use:
android {
composeOptions {
kotlinCompilerExtensionVersion = "1.2.0-beta03"
}
}
However, I would rather solve this by setting the Kotlin compiler so I won't run into the issue again whenever the latest version of Kotlin outpaces the compatibility of the Compose Multiplatform Kotlin Compiler Plugin.
The main idea I was thinking about is that there are some other places in the existing project where the old version 1.6.21 was used or some other setups that require that version or maybe an old cache. So basically need to clean the project, even delete the .idea directory of the project, and check other places where the old version can be set. After that reopen the project, do cleanup, sync and rebuild it.
Setup
I setup a Gradle multi-project project, including the Kotlin DSL as described on in the Gradle documentation. My Gradle version is 7.4.1, which is the recent version at the time of writing.
In this part of the documentation we are told to use convention plugins for subproject configuration and we should not use subprojects or allprojects DSL.
That is the reason I ended up having
plugins {
`kotlin-dsl`
}
in my buildSrc/build.gradle.kts, which is all working as expected.
Problem
By using kotlin-dsl I'm wired to gradle-kotlin-dsl-7.4.1.jar (Gradle version, I mentioned earlier). This jar seems to specify all Kotlin related versions along the project; i.e. I cannot do this to specify my Kotlin version
plugins {
`kotlin-dsl`
kotlin("jvm") version "1.6.10"
// ...
}
Because two different Kotlin versions will appear
$ ./gradlew build
> Configure project :buildSrc
WARNING: Unsupported Kotlin plugin version.
The `embedded-kotlin` and `kotlin-dsl` plugins rely on features of Kotlin `1.5.31` that might work differently than in the requested version `1.6.10`.
That behaviour and the warning are described here.
Questions
Am I able to use a more recent version of Kotlin in this setup, if so: how?
Out of curiosity: where do I see the Kotlin version tracked, that is baked into Gradle? I could not find any notice in the last hand full of release notes
atm there is no way to update the embedded version of the Kotlin the kotlin-dsl plugin relies on (github.com/gradle/gradle/issues/16345)
I think your best bet would be the gradle github
(github.com/gradle/gradle/issues/19308)
I need to provide maintenance support for the "legacy" mobile App, while also providing enhanced feature support to the "next major version" of this mobile App. To do it efficiently, I need to be able to switch between 1.3.x and 1.4.x of the Kotlin compiler. Everything that I am reading, implies that Kotlin only moves forward, so I hesitate to upgrade my version of Kotlin in Android Studio to get 1.4.x plugin installed without the confidence that I can switch back to 1.3.x.
It looks like a simple change in the Gradle build scripts should work, but it is not clear that changing the version changes all of the toolset, or simply tries to be backward compatible from a future-looking configuration.
You can update the Android Studio plugin safely - it should be backward compatible with the older versions of Kotlin.
If you want to change the compiler version (for using new language features), you should update the version in the Gradle script - you can change it whenever you want, the older versions of Kotlin libs and compiler are still available in the repositories.
I recommend you to always update Android Studio plugin to the latest version, so you have full Kotlin support in your IDE for all versions of the Kotlin compiler.
i understand that i can use Kotlin Plugin comes with IntelliJ but i can also install Kotlin standalone compiler. Is there any pros/cons using standalone vs IntelliJ own integrated?
I'd say that:
Any real Kotlin project (including projects in IntelliJ) should use a build system such as Gradle or Maven.
Enabling Kotlin support in a Gradle/Maven project will automatically download the correct compiler (and switch it when you update the Kotlin version in the config file) and not care about whether you have a stand-alone version installed.
Any other Kotlin tool will likely be integrated with them as well.
So the standalone compiler is pretty much only useful when you want to try something quickly outside any project, but then https://play.kotlinlang.org/ or https://try.kotlinlang.org/ can work as well; and again let you switch between Kotlin versions simpler than a manually installed compiler.
Running Kotlin scripts may be the only case where I would use the stand-alone compiler.
Not much, but having own install have few advantages, but probably not needed by most people on their machines:
You don't need intellij, so you can use that compiler in other IDE or just for other applications
You can use different version of compiler than the one from plugin.
But in most cases integrated one is all you need.
Using Gradle 4.4.1 with the Kotlin-DSL, I want to configure the experimental Kotlin coroutines in my Kotlin 1.2.20 project. This proves to be surprisingly difficult.
The official Kotlin documentation itself only mentions Gradle with the Groovy DSL instead of Kotlin.
The Kotlin-DSL project has an example for coroutines that does not work. Probably, it’s for an older Gradle version (the Kotlin version in that script is 1.1.51, which is suspicious in itself).
PS: Even IntelliJ’s configure Kotlin writes incorrect Gradle code. Is downgrading the only option?
Addendum: The build script was very red in IntelliJ IDEA. The IDE kept adding import statements all the time. It seems that these futile attempts were what made the script non-functional. I disabled ‘Add unambiguos imports on the fly’ temporarily, and even though the script remained mostly red, Gradle could execute it. And afterwards, the error notices withing IntelliJ were gone as well.
Assuming you have the kotlin plugin applied to the project:
import org.jetbrains.kotlin.gradle.dsl.Coroutines
plugins {
kotlin("jvm")
}
kotlin { // type is KotlinJvmProjectExtension
experimental.coroutines = Coroutines.ENABLE
}
This works for me with Gradle 4.4.1 and 4.5-rc-1, and I should also work for versions before that. If you are bringing in the Kotlin plugin by using buildscript instead of plugins, you may not have the generated accessors and will have to use extensions.getByType(KotlinJvmProjectExtension::class.java).apply {} (or something like that).