Difference between Kotlin plugins - kotlin

What are the difference between this three Kotlin plugin and what 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

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.

Related

How to get a recent Kotlin version in a multi-project Gradle setup with Kotlin DSL

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)

Kotlin multiplatform library exporting dependencies for JVM

I’m creating a multiplatform for jvm and iOS on kotlin using Gradle.
For the jvm, even if I define my dependencies as implementation, they are included in the library generated .pom and with runtime scope.
By using implementation, I was expecting that these dependencies are not passed to the library consumer.
But, when I use this library on my other jvm project, Gradle is importing the library-specific version. Not the one that I set in my application dependencies.
in this case, I'm doing a downgrade. The library is using the dependency version 1.4.1, and on the application I want to use version 1.4.0.1.
By using implementation, I was expecting that these dependencies are not passed to the library consumer.
If you expect the consumers to provide those transitive dependencies themselves, you should use compileOnly instead of implementation.
The difference between api and implementation is that transitive dependencies declared with api will be visible and usable by the application by just depending on your library (seen at compile time AND runtime). With implementation, the transitive dependency will still be here at runtime but will not be visible on the compile classpath of the consuming application, so you cannot use the transitive dependency's declarations in the application's code.
Have a look at the table here:
https://docs.gradle.org/current/userguide/java_library_plugin.html
If you stick with implementation, you can still force a version for the transitive dependency in Gradle by using strictly or by excluding it and redeclaring it yourself. See the doc:
https://docs.gradle.org/current/userguide/dependency_downgrade_and_exclude.html

Can you use a Gradle plugin as a dependency to another Gradle plugin?

If I have one Gradle plugin that wants to read and modify the properties of another gradle plugin, I would need the source of the other plugin as a dependency, right? This way I could do project.pluginManager.getPlugin(PluginClass.class) and manipulate it's properties. Is it possible to include a gradle plugin as a dependency?
My specific use case is, I wish to make a plugin to detect if kotlin multiplatform plugin is applied and then generate some source files and add them as sources for the various compilations depending on the targets defined.

Kotlin Multiplatform project include cocoapod dependency

Trying to setup cocoapods for my Kotlin MPP:
plugins {
kotlin("multiplatform") version "1.5.10"
kotlin("native.cocoapods") version "1.5.10"
id("com.android.library")
id("maven-publish")
}
Syncing the project ends up in this error:
Execution failed for task ':podspec'.
> this and base files have different roots: C:\Users\user\AppData\Local\Temp\wrap6957loc\gradlew and G:\Workspace\tracking-lib.
I am on windows and always will be. The project should build on Windows and Mac. So I assume basic inclusion of that plugin should work?
What can I do to fix the paths?
This looks like a bug. The plugin should be disabled, but the build should be working fine. Please consider reporting it to https://kotl.in/issue The cocoapods plugin strongly relies on pre-installed components, such as Xcode CLI Tools, cocoapods and cocoapods-generate. All of them are described here. The iOS targets of the Kotlin MPP project themselves can't be built on Windows, this is a restriction of the compiler. See a full list of targets and their availability here.

option to create a kotlin multiplatform project not present in idea 2019.3

i want to create a kotlin mp project in intellij idea 2019.3 Community, but the options i see in new project window for kotlin are just jvm|idea and js|idea.
In several articles there are options like mobile shared library | gradle, present even for older versions oj IJIdea.
I already have kotlin in another project, as part of a maven project
any hints?
Make sure you have latest Kotlin plugin installed and Gradle bundled plugin enabled.
See these steps about creating Kotlin MPP project.
gradle plugin was enabled, but still something smelled, so after some time spent enabling/disabling plugins, i decided to enable android support plugin - then after restart idea complained that gradle-java plugin is not enabled, after enabling that (from the error tab link, not the plugin management window in preferences) - the options to create various gradle projects appeared, including the ones for kotlin/multiplatform