A project aiming to generate KMP declarations from several library versions

Overview

kotlin-ketchup

A project aiming to generate KMP declarations from several library versions

LICENSE

Apache 2.0. See ./LICENSE in this repository

See the ./NOTICE for more details on the third-party sources or libraries that we use

Why

Assume one does a library or a plugin that has to be compatible with several host versions. Think about an IntelliJ plugin (or maybe an Android App?)

There are may ways to set up the project. One is to have a dedicated src/kotlin source roots per different versions. The shared code can still be in the src/main/kotlin source set. That scenario usually have a parameter somewhere in Gradle to specify what actual version of the platform to use.

More formally, it may look like

project
   \
     src
     + kotlin   <-- here comes the common part
     + v2021.1  <-- a specific code to include for 2021.1 version
     + v2021.2
     + v2021.3

Something similar we may have with resource folders, tests, ect.

Problem

This approach has problems

  • every time there are un-included source folders
  • refactorings are tricky to implement as un-included folders has to be processed
  • the code in common part may assume a class declaration from the version part

These problems are not showstoppers from one hand. From the other hand, this projects aims to hack the solution with the help of Kotlin Multiplatform

Solution

We try to generate necessary expect/actual declarations with Kotlin Multiplatform and allow having all source folders included as if there were several target directories.

The main trick in the generation is that it should only include symbols which are available in the intersection of platform libraries.

To achieve that we use ASM library to read the bytecode of the dependencies from the classpath. We are going to use KotlinPoet to generate the necessary declarations as Kotlin code.

Next is the Gradle part. This is a Gradle plugin. It intersects the classpath and attaches the generated Kotlin code to the common (with actuals) and to each of version-specific targets. On the other hand, it could be a dedicated sub-project, which can be generated once and be ready for use.

Downsides

Parsing JVM bytecode with ASM and kotlinx-metadata requires an additional code to map its object into Kotlin code declarations back.

We have to have a duality - one approach uses ASM and JVM to recover Java declarations, the second uses Kotlin own metadata to generate the proper declarations.

Kotlin compiler parameters or package-wide nullability options has to be included and supported as well. That is harder to implement and requires effective testing.

Generated declarations would miss original javadocs/dokka annotations. It could make using the libraries harder as well. An alternative could be to parse sources instead of the compiled bytecode to enrich the generated code with annotations.

Alternative approaches

At that point it seems more practical to implement the logic as a Kotlin compiler plugin. It is hard to say how easy or if that could be easier to implement.

You might also like...
sample project that shows you how you can use Ktor to creat a server for real Project.

Ktor-Sample This is a sample project that shows you how you can use Ktor to creat a server for real Project. What is done Save data to database (Get a

Basic-Android-Project - A Basic Android Project with proper structure and all necessary dependencies

Basic-Android-Project A Basic Android Project with proper structure and all nece

It is a project that contains lessons and examples about Kotlin programming language. 🇰
It is a project that contains lessons and examples about Kotlin programming language. 🇰

Kotlin Tutorials What is Kotlin? I added the platforms it supports and great resources. You can access the article from the link below: https://medium

This is a first kotlin project
This is a first kotlin project

SmallPocket This is a first kotlin app, help user to save links easily, and can export to Evernote as weekly. Steps: copy link anywhere open SmallPock

Example of Android project showing integration with Kotlin and Dagger 2

kotlin-dagger-example This project demonstrate how to setup an Android Project with Kotlin and Dagger 2. It's based on Dagger 2 example ##Known issues

Showcase project of Functional Reactive Programming on Android, using RxJava.

FunctionalAndroidReference FunctionalAndroidReference is a showcase project of Functional Reactive Programming on Android, using RxJava. It's a compan

A simple store project that includes a list of products, search on products, details of the product, and review submission.
A simple store project that includes a list of products, search on products, details of the product, and review submission.

AdidasTest A simple store project that includes a list of products, search on products, details of the product, and review submission. Summary Technol

Mobile client for official Nextcloud News App written as Kotlin Multiplatform Project
Mobile client for official Nextcloud News App written as Kotlin Multiplatform Project

Newsout Android and iOS mobile client for Nextcloud news App. The Android client is already available to download in the Play Store. F-Droid and Apple

Releases(2.1)
Owner
Eugene Petrenko
Pushing boundaries. Making software to make the world better. Working at JetBrains.
Eugene Petrenko
A clone of hn.premii.com implemented in KMP with Web (React), iOS (Swift UI), Android and Desktop (Compose) UIs

An clone of hn.premii.com implemented in Kotlin Multiplatform with shared Android/Desktop Compose UI, SwiftUI on iOS and React for Web This example su

Tarek Belkahia 7 Feb 5, 2022
A project that helps us generate the test project to test the Gradle plugin.

Ktlint Gradle Provides the function to generate a Gradle project for us to test your Gradle plugin Latest plugin version: [1.0.0] Table of content How

Jack Chen 5 Jul 20, 2022
Github User App for searching Github Users and get several information from it.

GithubUserApp Github User App for searching Github Users and get several information from it. This code implement with Coroutines, Retrofit, Architect

Wahyu Hendiarto W. 0 Apr 16, 2022
A Kotlin library used to analyse discrete Markov chains, in order to generate plausible sequences

Markov Markov is a Kotlin library used to analyse discrete Markov chains, in order to generate plausible sequences. Using This project is still under

Xavier F. Gouchet 0 Nov 14, 2021
A ksp library to automatically generate navigation functions for jetpack compose.

Compose/Navigation/Generator ⚠️ This library is still under development and not considered stable! Content Introduction Usage Example: Single destinat

Steffen Eckardt 4 Sep 13, 2022
Used to generate the template code of GetX framework

Language: English | 中文简体 statement some fast code snippet prompt come from getx-snippets-intelliJ Description install Plugin effect Take a look at the

小呆呆666 242 Dec 30, 2022
FizzBuzzKotlin - A function fizzBuzz to generate a list of string based on the input number

FizzBuzzKotlin write a function fizzBuzz to generate a list of string based on t

gson 0 Feb 12, 2022
Ktorm KSP extension to help generate boilerplate code.

Ktorm KSP extension to help generate boilerplate code. It can automatically generate Table objects through entity classes, while making entities defined by data classes easier to use, and supports custom extension code generation logic.

KTORM.ORG 24 Dec 30, 2022
🎲 Kotlin Symbol Processor to auto-generate extensive sealed classes and interfaces for Android and Kotlin.

SealedX ?? Kotlin Symbol Processor to auto-generate extensive sealed classes and interfaces for Android and Kotlin. Why SealedX? SealedX generates ext

Jaewoong Eum 236 Nov 30, 2022
KotlinScript that generate Reel from a given image, text and audio

ReelScriot KotlinScript that generate Reel from a given image, text and audio 80f4ea39-a7da-4f21-b0ff-7a17836a1cd0.mp4 6691b51d-d7a3-4915-ae41-8bec400

Chetan Gupta 2 Dec 6, 2022