KMM - Important concepts

KMP is a SDK and not a framework. Integrating and migrating are progressive forms.

  • Library is code that your application calls.
  • Framework is an application or library that is almost ready made. You just fill in some blank spots with your own code that the framework calls.
  • SDK is a bigger concept as it can include libraries, frameworks, documentation, tools, etc.

Native Code 
    It allows to interact with native environment without using bridge or channels and provide code more natively which is impossible with other cross platform.   

Smooth Interoperability 
 Any task which cannot be resolved in the common code part, it can be implemented using the expect actual model.  We have a possibility to code specific object for only one specific target and not for others. 

Connect to platform-specific APIs 
 To access platform-specific APIs that implement the required functionality, we use the Kotlin mechanism of expect and actual declarations. 
  • Common source set define an expected declaration 
  • Each platform source set iOS and Android should provide the actual declarations that corresponds to expected declarations 
Native UI 

High Performance 
compiles the code in the same specific format as the target platform so it has same performance as native.

While Setting up project 

use android() not jvm("android")
Because, when we use android() we have to create AndroidManifest.xml in androidMain folder and add this lines. 
plugins {
id("com.android.library") // this
kotlin("multiplatform")
kotlin("plugin.serialization") version "1.4.0" //org.jetbrains.kotlin.config.KotlinCompilerVersion.VERSION
}

android { // this
compileSdkVersion(29)
buildToolsVersion = buildToolsVersion
defaultConfig {
minSdkVersion(21)
targetSdkVersion(29)
}
sourceSets {
val main by getting {
java.srcDirs("src/androidMain/kotlin")
manifest.srcFile("src/androidMain/AndroidManifest.xml")
res.srcDirs("src/androidMain/res")
}
}
}
otherwise the project will not build. 


KMP - Konan Compiler 
  • Frontend: convert the Kotlin code into IR Intermediate Representation 
  • Backend: convert the Intermediate Representation into native code thanks to the Kotlin/Native infrastructure built by JetBrains. 
Expect : declare in Common module 

Actual : declare at target platform module 

Libraries used : 

  • Kotlin: v1.4.0
  • Reaktive(Kotlin multiplatform implementation of RXs): v1.1.18
  • Ktor(asynchronous Web framework for Kotlin): v1.4.0. : Networking library which allows to have back-end and client codes using always the same code and the same library. 
  • Kodein(A multiplatform Dependency Injection library): 7.1.0
  • Kotlinx.seralization : multi platform, multi format serialisation library and it creates digital code for your classes, is serialisation and deserialisation interesting without using reflection. Because passing values from multi platform library to iOS or android is not as trivial as we think. 
  • Coroutine : both in iOS and Android, in Android we use Dispatcher.Main while in iOS we use NsQueueDispatcher()  
 


Running Test in KMM
  • You test it from the common code i.e. we define it in commonTest and run them 
  • We run them on both iOS and Android running from the commonTest works 
  • commonTest works if there are no Coroutines involved Eg. runBlocking does not exist in all platforms 
  • Testing androidMain also tests commonMain folder 


















Comments

Popular Posts