kotlin-parcelize-darwin
Kotlin/Native compiler plugin generating Parcelable
implementations for Darwin/Apple.
Allows writing Parcelable
classes for iOS (eventually for all Darwin targets, possibly),
similary to the Android's kotlin-parcelize
plugin. Can be used together with kotlin-parcelize
plugin to write Parcelable
classes in the commonMain
source set.
It is working, but still work in progress.
Supported targets: iosX64
, iosArm64
.
Supported types:
- All Kotlin primitive types
- The
String
type Parcelable
classes- Collections:
List
,MutableList
,Set
,MutableSet
,Map
,MutableMap
of all supported types
Setup
The plugin is not published yet, but can be used when published locally.
-
Checkout the repository
-
Run
./gradlew publishToMavenLocal
-
In the root
build.gradle
:
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath "com.arkivanov.parcelize.darwin:kotlin-parcelize-darwin:0.1.0"
}
}
- In the project's
build.gradle
:
apply plugin: "kotlin-parcelize-darwin"
repositories {
mavenLocal()
}
kotlin {
ios()
sourceSets {
iosMain {
dependencies {
implementation "com.arkivanov.parcelize.darwin:kotlin-parcelize-darwin-runtime:0.1.0"
}
}
}
}
Using
The plugin works similary to the Android's kotlin-parcelize
plugin.
The kotlin-parcelize-darwin-runtime
module provides the following things:
- Parcelable interface
- @Parcelize annotation
- Some handy CoderUtils
Here is an example of some Parcelable
classes:
@Parcelize
data class User(
val name: String,
val age: Int
) : Parcelable
@Parcelize
data class UserGroup(
val users: List<User>
) : Parcelable
Encoding a Parcelable
class using NSCoder
:
import com.arkivanov.parcelize.darwin.runtime.encodeParcelable
fun encode(coder: NSCoder, user: User) {
coder.encodeParcelable(value = user, key = "user")
}
Decoding a Parcelable
class using NSCoder
:
import com.arkivanov.parcelize.darwin.runtime.decodeParcelable
fun decode(coder: NSCoder) {
val user: User? = coder.decodeParcelable(key = "user") as User?
}
Writing Parcelables in commonMain
The plugin can be used to write Parcelable
classes in the commonMain
source set:
- Define the following expectactions in the
commonMain
source set:
expect interface Parcelable
@OptIn(ExperimentalMultiplatform::class)
@OptionalExpectation
@Target(AnnotationTarget.CLASS)
expect annotation class Parcelize()
- Define the following actuals in the
androidMain
source set (only needed ifkotlin-parcelize
plugin is used):
actual typealias Parcelable = android.os.Parcelable
actual typealias Parcelize = kotlinx.parcelize.Parcelize
- Define the following actuals in the
iosMain
source set:
actual typealias Parcelable = com.arkivanov.parcelize.darwin.runtime.Parcelable
actual typealias Parcelize = com.arkivanov.parcelize.darwin.runtime.Parcelize
- Define the following actual for all other targets that do not support
Parcelize
:
actual interface Parcelable
// No need to define Parcelize, it is optional
- Make sure you have enabled the plugin as described in the Setup section
Now you should be able to use Parcelize
feature as usual in the commonMain
source set.
The code will be automatically generated for Android and iOS, without affecting all other targets.
Limitations
There is no proper IDE support at the moment. So Parcelable
classes in the iosMain
source set
are highlighted as incomplete, but the code still compiles just fine. There are no IDE errors in the commonMain
source set.
Tests as using examples
Some tests can be found here.
Author
Twitter: @arkann1985