Kotlin 1.3-M2: new multiplatform projects model

Then just specify the corresponding dependencies in the second project’s source sets, like it’s described here: logkat-metadata goes to the common part and logkat-jvm to the jvm one. Does it work for you?

Somehow I completely missed that, my apologies. I tested it out with the method you suggested, and it worked without a hitch. However, I did run into something that you guys might want to address-- the metadata publication does not inherit the root project’s groupId or version by default.

— that sounds like something went wrong there. I failed to reproduce it from scratch: having group and version specified in the common build.gradle like this:

group 'com.example'
version '0.0.1'

— I got the following set of artifacts for a lib called foo: com.example:foo-metadata:0.0.1, com.example:foo-jvm:0.0.1 — isn’t it the same for you? Can you share an example project or at least its configuration then?

Is there anything built in to run applications like the gradle application plugin does? I got an jvm, common, js web app that just needs to start the backend for things to work but I’m finding myself having to manually define the startup behavior with more detail than probably necessary. How would you go about building a fat jar for example?

Turns out that target.jvmWithJava works correctly with the application plugin. jvmWithJava is not mentioned in the main page docs. Also it seems that the js compilation doesn’t generate kotlin.js files that would normally be created with the kotlin2js plugin. I’m using kotlin 1.2.71

target.jvmWithJava isn’t supported actually and going to be removed in the future.
Also, please use the latest available Kotlin 1.3.* version (which is currently 1.3.0-rc-190) — as the topic name says, the new model is introduced in Kotlin 1.3. There is a bug at the documentation site, though, which recommends 1.2.71 version, but it’s going to be fixed soon.

As to JS configuration, please make sure it is configured properly since it indeed requires some additional set up for the proper files generation (which is general for JS part and not specific for the multiplatform, though). To get the idea, have a look at this and this examples.

@piacenti As an alternative to the unsupported jvmWithJava preset mentioned by @wild_lynx, you can also create a separate Kotlin/JVM module (with no kotlin-multiplatform plugin, just kotlin) that depends on the multiplatform module, then just apply the application plugin in that JVM module. The same, I think, should work for building a fat JAR, as the JVM dependencies of the multiplatform module will be available in the JVM module as well.

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)”.

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

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/
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.