Difference between Kotlin plugins
Asked Answered
S

1

9

What are the differences between these three Kotlin plugins and what do they actually do?

plugins {
    id 'kotlin-android'
    id 'org.jetbrains.kotlin.android'
    id "org.jetbrains.kotlin.jvm" version "1.6.20"
}

The third one seems to be the recommended way specially when using Kotlin Coroutines

Superadd answered 11/6, 2022 at 15:2 Comment(0)
B
17

These plugins provide integration with different other Gradle plugins. They both setup compiling Kotlin for the JVM, but aim to interoperate with different other tools.

  • org.jetbrains.kotlin.android or kotlin-android

    This plugin offers integration of Kotlin with the Android Gradle plugin, which should also be applied to the project. The Kotlin compilations are set up to be included in the builds of Android variants (e.g. debug, release, testDebug etc.)

    The IDs kotlin-android and org.jetbrains.kotlin.android designate the same Gradle plugin. The only difference is that the "full" ID org.jetbrains.kotlin.android can be used for resolving the plugin from the Gradle Plugin Portal, while the shorter ID kotlin-android can only be used for applying the plugin if you already have it on the build classpath (i.e. it's added elsewhere).

  • org.jetbrains.kotlin.jvm (also has a shorter alias kotlin)

    This is the plugin for building Kotlin projects that target JVM without Android support.

    The plugin offers integration with the Gradle java plugin (as well as java-library or application). The project that applies this plugin can also use Java sources. The Kotlin compilations are wired with the java plugin's source sets (main and test by default)

Normally you should only apply one of these plugins, depending on whether you target Android or "standard" JVM. If you need to target both platforms, you should use the Kotlin Multiplatform plugin by ID org.jetbrains.kotlin.multiplatform, which adds the DSL to setup the targets in the project. Those might include jvm() and android(), as well as other targets: JS, WASM, Kotlin/Native.

Beth answered 11/6, 2022 at 15:47 Comment(9)
Planning to migrate this Android project to KMM soon, is org.jetbrains.kotlin.android the one should I use as I want to omit the classpath? I am trying to use org.jetbrains.kotlin.jvm and you are correct as it gives me error upon syncing The 'java' plugin has been applied, but it is not compatible with the Android plugins.Superadd
While you only have Android, you can use org.jetbrains.kotlin.android. Then at some point you will have to migrate to org.jetbrains.kotlin.multiplatform – either at the point when you add more target platforms or at some time before that. In the latter case, you can setup Kotlin Multiplatform to build just the single Android target for the time being.Beth
Or it's already okay to use org.jetbrains.kotlin.multiplatformSuperadd
Thanks a lot! This was such a great answer!Superadd
Sorry I just want to follow up as I am getting error with kapt after changing to multiplatform. An exception occurred applying plugin request [id: 'org.jetbrains.kotlin.kapt', version: '1.6.20-M1'] > Failed to apply plugin 'org.jetbrains.kotlin.kapt'. > Extension with name 'kotlin' does not exist. Currently registered extension names: [ext, base, defaultArtifacts, sourceSets, reporting, java, javaToolchains, buildOutputs, android, androidComponents, play, googleServices, ksp, kapt]. Using id 'org.jetbrains.kotlin.kapt' version "${kotlin_version}"Superadd
We can't set org.jetbrains.kotlin.multiplatform in Android build.gradle right? We can only set it at shared module build.gradle after setting up KMM?Superadd
The above issue is due to order of plugins, kotlin android should be placed before kapt and other kotlin related plugins.Superadd
It is possible to use the Multiplatform plugin in an Android application module, but a lot of setup guides recommend having a separate, shared Multiplatform library module just to make the project structure cleaner. So it's up to you. As for the Kapt plugin – yes, you have to apply it after the other Kotlin plugin.Beth
how is it that the google's nowinandroid project uses the kotlin.jvm instead of the kotlin-android or kotlin-multiplatform plugin?Mab

© 2022 - 2024 — McMap. All rights reserved.