Kotlin 1.3-M2: new multiplatform projects model


#1

In Kotlin 1.3, we kept up active development and improvement of the multiplatform story. Aiming to simplify the projects structure, make code sharing more granular, and extend the platforms list by adding Kotlin/Native, we arrived at the new multiplatform projects model, which brings all of this along with other new features.

The recently released Kotlin 1.3-M2 already contains the very early access preview of the new model:

  • the new kotlin-multiplatform Gradle plugin enables setting up all target platforms in a single Gradle subproject;
  • Kotlin/Native targets now share the DSL and can co-exist in the same project with JVM and JS ones;
  • with many provided configuration presets, the notion of a target platform becomes more flexible; for example, you may define JVM, NodeJS, and Linux targets each with a single line;
  • now it is possible to share a part of code between only some of the targeted platforms rather than all of them (for instance, there can be one part for JVM, JS, and Native and another piece shared only between JVM and JS in the same project);
  • publishing MPP libraries is available out-of-the-box;
  • dependency management gets simpler: a single line in the build script is enough to use a multiplatform library across all of the target platforms;

To find out more, follow this link: https://github.com/h0tk3y/k-new-mpp-samples. The repo contains a detailed description of the new model and several project examples which demonstrate some of the advantages listed above, such as partial code sharing in the split-actuals project. Alternatively, you may create a new multiplatform project through New Project Wizard in IntelliJ IDEA: there is a new project template at the Kotlin tab, called Kotlin (Multiplatform - Web) , with JVM and JS targets specified. There’s more to come!

While it already works in many use cases, there is still a lot to improve in the tooling and IDE support. Building multiplatform projects based on the new model from within IntelliJ IDEA and importing them into Android Studio are not supported yet, as well as writing the build scripts with Gradle Kotlin DSL. You may find all the known issues with this search query: https://youtrack.jetbrains.com/issues/KT?q=%23new-multiplatform.

Nevertheless, we encourage you to try the new multiplatform projects and let us know what you think. Your feedback is more than welcome, as well as YouTrack issues for bugs you may encounter :slight_smile:
Feel free to ask any questions here and let us know if you experience any difficulties.


#6

Does this use the maven-publish Gradle plugin? How is the javadoc JAR handled by default in the case, like with Sonatype, where release to Maven central fails without it? I’ve had to do what I’ve seen other Kotlin projects do and make an empty one manually. (can delete/redirect question if this announcement post is an unacceptable place to discuss this)


#7

Yes, the default publishing configuration by the kotlin-multiplatform plugin is done with the maven-publish plugin (note: it is done only if you apply maven-publish), as it provides some features we need for publishing a multiplatform library as a connected set of artifacts/modules which can be consistently consumed by another multiplatform project, in contrast with the old maven plugin.

We do not create the Javadoc JAR by default, though, but when you need it you will be able to add it to the publication with the maven-publish plugin API, i.e. with the publishing { publications { ... } } DSL.

However, we are going to provide a default setup for the sources JARs for each target. This and some more changes in multiplatform libraries publishing are in-progress now.


#8

Is it just build system tweak or does it use the same IR for all platforms like it was announced earlier this year?


#9

At this point, the binary formats are the same as in Kotlin 1.2.x multiplatform projects, there’s no new IR as of now.


#10

Does this mean IDEA will soon have full support for Kotlin native? I feel that with a decent set of gir bindings Kotlin would be a great language for developing with GTK/GNOME etc sine Vala is slowly dying. But having to either make apps run on the JVM or pay for a decent IDE for native is a put-off.


#11

@realh69 Please stay tuned, we might have news for you some time later.


#12

Sounds promising. I’m not very active on these forums TBH, but I’ll be sure to pay extra attention to the summary emails, thanks.


#13

It is possible to publish a common only library with expect statements without any targets or actuals?

Then client projects can depend on this library and implement the actuals.


#14

Unfortunately, no, actual implementations are still required


#15

What about just having the actuals in another module like the old plugin?


#16

Can you add a sample with iOS and the new multiplatform structure? I’m trying to configure it but I’m not able to make it work. Thanks!


#17

You may have preconfigured source sets, as well as custom ones, and all of them will turn into IDE modules during the IDE import. In terms of modules, there will be a separate IDE module for common code with expect declarations, e.g. commonMain, and a different one for a platform realization, e.g. jvm6Main. You may take a look at the simplest web template with such a structure by creating a new Multiplatform (Web) project via New Project Wizard in Intellij IDEA, Kotlin section, having 1.3-M2 Kotlin plugin installed.

Moreover, platform-specific source sets may share actual declarations as it’s done in one of the provided examples.


#18

There is a plan to make an Android + iOS sample with the new model. Please stay tuned, I’ll let you know when it’s ready


#19

Hi @wild_lynx, I make it work by doing this:

fromPreset(presets.iosX64, ‘ios’) {
compilations.main.outputKinds += KotlinNativeTarget.FRAMEWORK
}

I found the KotlinNativeTarget enum by reviewing the code :frowning: that’s why we should have a sample app or document this in the great k-new-mpp-sample which is really helpful.


#20

I’m not being able to publish my Jar with maven-publish plugin and the new structure. Any help on how to do this? Many thanks!


#22

Have you read this section about the publishing? On which stage do you experience the problems?


#23

@juanchosaravia Thanks for the note about iOS frameworks, I’ve added it to the Notes on Kotlin/Native support section of k-new-mpp-samples. We are going to provide a sample project targeting iOS sometime soon as well.


#24

Thanks man! Really useful!


#25

Looks really good. Does this now also mean that I can (easily, on class by class basis) have some classes have different code for android, js and jvm and some share code between android and jvm (but not js). Is that also possible if the android variant is in reality a java library that is just designed for use on Android.