Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/voize-gmbh/flutter-kmp

Combine Flutter with Kotlin Multiplatform (KMP)
https://github.com/voize-gmbh/flutter-kmp

flutter kmp kotlin kotlin-multiplatform

Last synced: 3 months ago
JSON representation

Combine Flutter with Kotlin Multiplatform (KMP)

Awesome Lists containing this project

README

        

# flutter-kmp

## Installation

Add the KSP gradle plugin to your multiplatform project's build.gradle.kts file, if you have subprojects, add it to the subproject's build.gradle.kts file.

```kotlin
// android/shared/build.gradle.kts

plugins {
// from gradlePluginPortal()
id("com.google.devtools.ksp") version "1.9.23-1.0.20"
}
```

Then add the `flutter-kmp` to the commonMain source set dependencies. Also add the generated common source set to the commonMain source set:

```kotlin
// android/shared/build.gradle.kts

val commonMain by getting {
kotlin.srcDir("build/generated/ksp/metadata/commonMain/kotlin")
dependencies {
// ...
implementation("de.voize:flutter-kmp:")
}
}
```

Add `flutter-kmp-stubs` as `compileOnly` dependency to the androidMain source set:

```kotlin
// android/shared/build.gradle.kts

val androidMain by getting {
dependencies {
// ...
compileOnly("de.voize:flutter-kmp-stubs:")
}
}
```

Then add `flutter-kmp-ksp` to the KSP configurations:

```kotlin
// android/shared/build.gradle.kts

dependencies {
add("kspCommonMainMetadata", "de.voize:flutter-kmp-ksp:")
add("kspAndroid", "de.voize:flutter-kmp-ksp:")
add("kspIosX64", "de.voize:flutter-kmp-ksp:")
add("kspIosArm64", "de.voize:flutter-kmp-ksp:")
// (if needed) add("kspIosSimulatorArm64", "de.voize:flutter-kmp-ksp:")
}
```

Configure the KSP task dependencies and copy the generated Dart files to your flutter plugin:

```kotlin
// android/shared/build.gradle.kts

tasks.withType>().configureEach {
if(name != "kspCommonMainKotlinMetadata") {
dependsOn("kspCommonMainKotlinMetadata")
} else {
finalizedBy("copyGeneratedDartFiles")
}
}

tasks.register("copyGeneratedDartFiles") {
dependsOn("kspCommonMainKotlinMetadata")
from("build/generated/ksp/metadata/commonMain/resources/flutterkmp")
into("path/to/flutter/lib/generated")
}
```

Register the generated Android module classes (ending with `...Android.kt`) in your Flutter plugin Android class:

```kotlin
class MyFlutterPlugin: FlutterPlugin {
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
MyFlutterModuleAndroid()
// ...
}

...
}
```

The generated Dart code uses the `json_serializable` package.
Add the following dependencies to your Flutter plugin:

```
flutter pub add json_annotation dev:build_runner dev:json_serializable
```

Then after generating Dart data classes make sure to the following command to generate the serialization code:

```
dart run build_runner build --delete-conflicting-outputs
```