Kmp4free - A Gradle Plugin that allows seamless switching between Kotlin JVM and the Kotlin Multiplatform Plugins

Last update: Jun 17, 2022

🆓 kmp4free

LICENSE Latest Snapshot CI

Allows you to toggle between Kotlin JVM Plugin -> Kotlin Multiplatform with a Gradle Property kmp4free=true.

This Gradle Plugin was built to support gradual adoption of Kotlin Multiplatform. It's called kmp4free because you are able to take a normal Kotlin JVM module, and with a single line change, enable or disable the Kotlin Multiplatform Plugin.

There is always a risk of adding something new, especially with a large project. The introduction of a technology that is not at a stable version yet can block the adoption of a new technology. The goal of this plugin is to reduce risk to a single line change.

Plugin Installation

Add the Snapshot Repo in your project's settings.gradle.kts

pluginManagement {
    repositories {
        // ...
        maven { url = "https://s01.oss.sonatype.org/content/repositories/snapshots/" }
    }
}

Add the Plugin on your project's build.gradle.kts

plugins {
    id("com.handstandsam.kmp4free") version "${latest_version}"
}

Usage

Just replace kotlin("jvm") with id("com.handstandsam.kmp4free") in the plugins block of your module's build.gradle.kts file.

Enabling Multiplatform

You can set kmp4free=true in your gradle.properties or send it in as a command-line parameter to gradle with -Pkmp4free=true.

This enables property the Kotlin Multiplatform Plugin, along with the additional changes required to support seamless switching between the Kotlin JVM Plugin.

Any code in the main sourceSet needs to be commonMain compatible when multiplatform is enabled, otherwise the build will fail about not being able to resolve JVM dependencies. This isn't a bad thing though, as you will then be able to identify what you need to change to make your code multiplatform compatible.

SourceSet Mapping

  • src/main ➡️ src/commonMain
  • src/test ➡️ src/jvmTest

Configuration Mapping

  • implementation ➡️ commonMainImplementation
  • api ➡️ commonMainApi
  • testImplementation ➡️ jvmTestImplementation
  • testApi ➡️ jvmTestApi

When tests are already written with JVM Libraries like JUnit and Google's Truth library, it would be a lot of work to migrate those tests over to commonTest, so these tests will only run on the JVM with kmp4free. Additionally, this task alias means that your scripts that run Gradle Tasks do not have to update either when the plugin is enabled, since the test task will be available.

Task Aliasing

  • :module:test ➡️ :module:jvmTest

Disabling Multiplatform

This uses the Kotlin JVM Plugin, and bypasses Kotlin Multiplatform entirely.

SourceSet Mapping

  • src/commonMain ➡️ src/main
  • src/jvmMain ➡️ src/main
  • src/commonTest ➡️ src/test
  • src/jvmTest ➡️ src/test

Configuration Mapping

  • commonMainImplementation ➡️ implementation
  • commonMainApi ➡️ api
  • commonTestImplementation ➡️ testImplementation
  • commonTestApi ➡️ testApi
  • jvmTestImplementation ➡️ testImplementation
  • jvmTestApi ➡️ testApi

You can set the following properties in your gradle.properties

Related Content

Droidcon SF Presentation: Embracing commonMain for Android Development

FAQ

Why do I need to use maybeCreate()?

You may typically see source sets defined like val commonMain by getting. Because with kmp4free we toggle the multiplatform plugin on/off, that Source Set will be sometimes be already there, and other times may not be. Therefore we use maybeCreate("commonMain") to avoid the error.

What Configurations are Mapped?

  • api
  • implementation
  • compileOnly
  • runtimeOnly
  • apiDependenciesMetadata
  • implementationDependenciesMetadata
  • compileOnlyDependenciesMetadata
  • runtimeOnlyDependenciesMetadata

Can I use kmp4free on an Android Library?

No. kmp4free is a step in the journey of migrating a kotlin("jvm") module to kotlin("multiplatform") with a commonMain SourceSet without adding any plugin changes when disabled.

The reason why you can't is because a kotlin("jvm") module means you cannot have resources in the res directory, an AndroidManifest.xml and not use Android APIs. You may find that your Android Library doesn't have any of those. If that's true, just convert it to a kotlin("jvm") module, and then you can use the plugin!

See Issue #6 for more discussion on this topic.

How do I configure my Source Sets?

You are able to use jvm or multiplatform style configuration. Note: will want to use the maybeCreate("commonMain") call instead of val commonMain by getting when accessing Source Sets.

That is because sometimes the configuration is there when enabled, but off when disabled. When you are ready, you can migrate away from the kmp4free plugin and start using the standard kotlin("jvm") project with just a jvm() target.

How do I configure my iOS and JS Targets?

You can only apply your configuration IF the kotlin("multiplatform") plugin has been added. That is because this plugin allows it to be on & off.

Look up the KotlinMultiplatformExtension, and if not null (Checked by the ?. operator), you can supply your configuration like this:

import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension

project.extensions.findByType(KotlinMultiplatformExtension::class.java)?.apply {
    ios()
}

You can find more information in Issue #2. I'm thinking this would be a lambda in the plugin configuration block for each target.

GitHub

https://github.com/handstandsam/kmp4free
You might also like...

Free plugins for kraken using the community api.

Community Plugins Free plugins for kraken using the community api. Wiki Make sure you read the Wiki for guides and information on how to use the commu

May 9, 2022

Gradle Plugin to enable auto-completion and symbol resolution for all Kotlin/Native platforms.

Gradle Plugin to enable auto-completion and symbol resolution for all Kotlin/Native platforms.

CompleteKotlin Gradle Plugin to enable auto-completion and symbol resolution for all Kotlin/Native platforms. What this plugin provides This zero-conf

Jun 23, 2022

Klinker is a gradle plugin making it possible to link kotlin native executables with custom linkers and options.

Klinker is a gradle plugin making it possible to link kotlin native executables with custom linkers and options. It does this by creating a static library for kotlin compilation, then generates a c+kotlin wrapper that calls into kotlin to start the app, finally using the specified compiler to compile and link the c code and kotlin library into a binary.

Apr 14, 2022

Android Gradle Plugin -- Auto Check big image and compress image in building.

McImage I will continue to update, please rest assured to use 中文文档 Android优雅的打包时自动化获取全部res资源 McImage is a Non-invasive plugin for compress all res in

Jun 15, 2022

Gradle Plugin for publishing artifacts to Sonatype and Nexus

Introduction Due to Sonatype's strict validation rules, the publishing requirement must be satisfied by every artifact which wants to be published to

May 28, 2022

Gradle plugin that parses version updates and assigns them to groups of people.

Notifier Gradle Plugin This gradle plugin serves the need of automating how dependencies are handles in a project. More specifically, it functions usi

Oct 15, 2021

A Gradle plugin enforcing pre-commit and commit-msg Git hooks configuration

A Gradle plugin enforcing pre-commit and commit-msg Git hooks configuration. Conventional-commits-ready.

Jun 8, 2022

Gradle plugin to check, if rest-controllers are used by clients and clients have suitable rest-interfaces

Verify-Feign Gradle Plugin This plugin will help you to verify all rest-clients and -controllers in a multimodule spring project. It will check, if al

May 11, 2022

The core Gradle plugin and associated logic used for Slack's Android app

slack-gradle-plugin This repository contains the core Gradle plugin and associated logic used for Slack's Android app. This repo is effectively read-o

Jun 13, 2022
Comments
  • 1. Find a way to optionally apply add multiplatform config.

    Tried to figure out an elegant solution to https://github.com/handstandsam/kmp4free/issues/2

    It didn't make sense for kmp4free to be opinionated about what multiplatform targets you should use or support. This method allows you to have full control over that, and then it is outside the scope of this plugin.

    Reviewed by handstandsam at 2022-06-02 18:49
  • 2. Unable to resolve plugin

    Hi,

    I can't seem to be able to resolve this plugin. I've included the custom maven repo as shown in the readme but I get is the following exception when adding the plugin itself to the plugins {} block.

    * Exception is:
    java.lang.NoClassDefFoundError: org/gradle/configurationcache/extensions/CharSequenceExtensionsKt
    	at com.handstandsam.kmp4free.internal.Kmp4FreeSourceSetMagic.extendConfigurationsAndSourceSets(Kmp4FreeSourceSetMagic.kt:65)
    	at com.handstandsam.kmp4free.internal.Kmp4FreeMagic.enableKotlinJvm(Kmp4FreeMagic.kt:48)
    	at com.handstandsam.kmp4free.Kmp4FreePlugin.apply(Kmp4FreePlugin.kt:25)
    
    Reviewed by ThanosFisherman at 2022-06-16 22:22
  • 3. Should we use withJava() for the jvm {} multiplatform target?

    Do we need withJava() since we are already mapping sourcesets? Not sure. Creating this issue to track this thought.

    https://kotlinlang.org/docs/multiplatform-configure-compilations.html#use-java-sources-in-jvm-compilations

    val multiplatformExtension =
        target.extensions.getByType(KotlinMultiplatformExtension::class.java)
    multiplatformExtension.apply {
        // Always enable JVM
        jvm {
            // Do we really need withJava()?
            // https://kotlinlang.org/docs/multiplatform-configure-compilations.html#use-java-sources-in-jvm-compilations
            //withJava()
        }
    }
    

    For now, we'll go with just jvm() unless we can see why we'd need withJava()

    Reviewed by handstandsam at 2022-06-02 18:42
Kotlin Gradle plugins for conveniently setting up Kotlin projects (JVM/MPP), publishing, Dokka, etc

Arrow Gradle config Add basic config to a Kotlin Multiplatform project In an Arrow KMP project, just add to the plugin block: plugins { kotlin("mu

Jun 7, 2022
Gradle plugin to ease Kotlin IR plugin development and usage in multimodule gradle projects

Gradle plugin to ease Kotlin IR plugin development and usage in multimodule gradle projects. Former: kotlin-ir-plugin-adapter-gradle

Mar 8, 2022
Gradle Plugin that allows you to decompile bytecode compiled with Jetpack Compose Compiler Plugin into Java and check it

decomposer Gradle Plugin that allows you to decompile bytecode compiled with Jetpack Compose Compiler Plugin into Java and check it How to use Run bui

Jun 8, 2022
A Gradle plugin to help analyse the dependency between modules and run tasks only on modules impacted by specific set of changes.
A Gradle plugin to help analyse the dependency between modules and run tasks only on modules impacted by specific set of changes.

Change Tracker Plugin A Gradle plugin to help analyse the dependency between modules and run tasks only on modules impacted by specific set of changes

Apr 25, 2022
How to write Gradle plugins - answers to common questions and alternative implementation solutions
How to write Gradle plugins - answers to common questions and alternative implementation solutions

Gradle Plugins: Why? How? There is some misunderstanding and confusion about the concept of Plugins in Gradle. This is unfortunate, as it is one of th

Jun 3, 2022
Gradle plugins for easy start into kotlin

Starter gradle plugins for kotlin The aim The aim of this project is to create a

Mar 5, 2022
Gradle Plugin to automatically upgrade your gradle project dependencies and send a GitHub pull request with the changes
Gradle Plugin to automatically upgrade your gradle project dependencies and send a GitHub pull request with the changes

Gradle Plugin to automatically upgrade your gradle project dependencies and send a GitHub pull request with the changes

May 16, 2022
Gradle plugin that generates a Swift Package Manager manifest and an XCFramework to distribute a Kotlin Multiplatform library for Apple platforms.

Multiplatform Swift Package This is a Gradle plugin for Kotlin Multiplatform projects that generates an XCFramework for your native Apple targets and

Jun 22, 2022
A Gradle plugin to ease testing in Kotlin Multiplatform projects.

Multiplatform Testing Plugin A Gradle plugin for easy testing of Kotlin Multiplatform projects in your CI pipeline. Support for testing android() targ

Dec 28, 2021
Useful and Fun plugins to use for Aliucord!

♥ Aliucord Plugins ♥ What do I need to use plugins ? To obtain Aliucord plugins (Plugins for Discord) make sure you have Aliucord installed! If you do

May 28, 2022