Kotlin 1.3-M2: new multiplatform projects model

Is this solution possible in the last version? Last time I’ve checked I could not depend on multiplaftorm module from simple koltin module.

How can I use kapt in kotlin-multiplatform project ? from kapt document and example, I figure out following steps:

include “:kapt” in settings.gradle
apply plugin ‘kotlin-kapt’ in build.gradle
add deps in sourceSets.commonMain

but i got this error: Could not find method kapt() for arguments [project ‘:xxxx’]

@darksnake Yes, it should work. Here’s a minimal sample:

jmv-depends-on-mpp.zip (53.0 KB)

If it doesn’t work for you, please let us know what goes wrong, or report an issue at https://kotl.in/issue.

@zzmingo You should add the Kapt dependencies at the top level in a dependencies { ... } block, not in the kotlin { sourceSets { ... } } DSL. Also, at this point, Kapt is expected to work with Android targets in multiplatform projects, but not with JVM ones which are created with presets.jvm since it does not compile Java. If you need Kapt for a non-Android project, you can try to use presets.jvmWithJava, but please note that this preset is going to be replaced with another form of Java support, as @wild_lynx described above.

UPD: make sure that the top-level dependencies { ... } where you add a Kapt is placed after the kotlin { ... } block where the Android target is defined.

It seems to be working with IDEA, but gradle gives me this:

Could not resolve project :kmath-core.
     Required by:
         project :kmath-jmh
Cannot choose between the following variants of project :kmath-core:
  - jvmRuntimeElements
  - runtimeElements
All of them match the consumer attributes:
  - Variant 'jvmRuntimeElements':
      - Found org.gradle.usage 'java-runtime-jars' but wasn't required.
      - Required org.jetbrains.kotlin.localToProject ':kmath-jmh' but no value provided.
      - Required org.jetbrains.kotlin.platform.type 'jvm' and found compatible value 'jvm'.
  - Variant 'runtimeElements':
      - Found org.gradle.usage 'java-runtime-jars' but wasn't required.
      - Required org.jetbrains.kotlin.localToProject ':kmath-jmh' but no value provided.
      - Required org.jetbrains.kotlin.platform.type 'jvm' but no value provided.

@darksnake Which plugins do you apply in the :kmath-core project? It looks like the ambiguity is caused by a configuration runtimeElements created by the java plugin. This may happen if you apply one of the java and kotlin plugins along with kotlin-multiplatform.

You are correct! Thank you very much for suggestion I found that I forgot to remove jmh plugin from core. Now it works properly. Great news indeed!

The only problem left is that for some reason IDEA generates gradle root project configuration for each module. It does not affect build, but is a bit annoying.

It’s working now by adding into top-level deps.
As you said, seems it’s only working with java or jvmWithJava module, can I use it in a platform-common module?

@zzmingo Note that, as most annotation processors work by generating Java, using Kapt for other platforms than JVM doesn’t make much sense (at least unless you somehow compile the generated Java sources for those platforms as well, which is not possible out-of-the-box). So I’m just not quite sure I understand how exactly you want to use Kapt on common sources. Could you please describe the overall structure of your project?

@h0tk3y So far, the project structure is very simple and it created by IDEA kotlin plugin with “Kotlin (Mobile Shared Library)”.
image

The module kapt contains compiler sources and aim to analysis sources of commonMain to generating some extensions of kotlin data class.

For example, decalare a class in commonMain named model.kt

@GeneratedMyExtension
data class Model(var name: String)

I hope there is a extension file auto generated, look like this:

fun Model.displayName() = "My name is $name"

Do you mean, IDEA adds some configuration, specific to a root project only (like settings.gradle file), every time you add a new multiplatform module into an existing project?

Something like that. I had normal root configuration and idea root gradle configurations for each of modules, so I had several separate trees in gradle sync window. I solved the problem by removing those phantom roots manually, they do not reappear on sync. I don’t remember how I added modules in the first place. Maybe it was the problem of module wizard in IDEA. I will report back if the problem reappears.

When using new mpp with version 1.3.0-rc-190 and coroutine 1.0.0, I got this fail to compile iOS:

warning: skipping ....gradle/caches/modules-2/files-2.1/org.jetbrains.kotlinx/
kotlinx-coroutines-core-native_debug_ios_arm64/1.0.0/7ca0a37e5dca2e6edf4e605f87c8ea5e92d3ef77/
kotlinx-coroutines-core-native.klib. 
The compiler versions don't match. Expected '[0.9.3]', found '1.0.1-release-4583'

I am afraid I can’t fix it by myself.

Please, update the versions to the compatible ones: 1.0.1 for kotlinx.coroutines and 1.3.0 for Kotlin. Did it help?

I have very bizarre behavior for MPP project with several modules (let’s call them m1 and m2). When I import in m2-common some classes from file in m1-common everything is working fine as long as I use separate imports for each class. But if I use star import (which is done automatically by IDE), gradle build fails with Unresolved reference.

I searched the tracker and found a lot of different errors which are mostly fixed in 1.3.20. Is this a known problem?

I didn’t recognize it as a known issue from the first glance, so can you please either share build.gradle configurations for the project and its modules here or in a new YT issue?
The only guess so far is that there are same-named packages in both modules and there is an ambiguity in the mapping of platform-specific source sets between m1 and m2 modules

The project is here. And the build fails in MetaFormat class.The package name is not the same. In the library module it is hep.dataforge.meta and in io module it is hep.dataforge.meta.io. I will open a ticket later.

is it possible to use plugins in presets or ir it can be done some other way? for example shadow(fat jar) for jvm or kotlin-frontend for js? does anyone have working example?

I’m working on a multi-platform project, currently Android and JS (client), and am trying to copy additional files as part of the Gradle build.


kotlin {
  targets {
    fromPreset(presets.android, 'android') {
      compilations.all {
        tasks[compileKotlinTaskName].dependsOn syncAndroidAssets
      }
    }

    fromPreset(presets.js, 'js') {
      compilations.all {
        tasks[compileKotlinTaskName].dependsOn syncJsAssets, syncJsHtml
      }
    }
  }

  sourceSets {
    // ....
  }

Where “sync[Name]” are my custom tasks.The JS target works great, but the Android target fails with

Task with name ‘compileDebugKotlinAndroid’ not found in project

Is this just not working, or should be done in a different way?

tasks.compileKotlinNative.dependsOn compileWindowsResources

works for Kotlin/Native.

Maybe for you case something like this will work:

tasks.compileKotlinAndroid.dependsOn syncAndroidAssets