Share
Embed
Introducing Kotlin Multiplatform in an existing project | Kotlin Budapest Meetup -ย December
After discovering a new interesting technology or framework, you will probably start asking yourself how to integrate it into an existing project. Thatโs because, the possibility to start with a blank canvas is rare (not impossible, but rare).
This is also the case for Kotlin Multiplatform, and even though it is still in alpha, you can already start to use it in production applications.
In this talk, we will understand which part of the code can be a starting point for sharing, how to consume the shared code and how to structure an existing project to have an as smooth as possible integration.
Marco Gomiero
December 14, 2021
Transcript
80 slides
-
Kotlin Budapest Meetup - @marcoGomier Marco Gomiero ๐จ๐ป Android Engineer
@ TIER ๐ด ๐ฉ๐ช ๐ฎ๐น โจ Google Developer Expert for Kotlin > Twitter: @marcoGomier โจ > Github: prof18 โจ > Website: marcogomiero.com -
Kotlin Budapest Meetup - @marcoGomier Kotlin Multiplatform โข Not about
compiling all code for all platforms โข Share as much [NO UI] code as possible -
Kotlin Budapest Meetup - @marcoGomier Common Kotlin Kotlin/JVM Kotlin/JS Kotlin/Native
Java Android Browser NodeJS Android NDK iOS macOS watchOS tvOS Linux Windows -
Kotlin Budapest Meetup - @marcoGomier Common Kotlin Kotlin/JVM Kotlin/JS Kotlin/Native
Java Android Browser NodeJS Android NDK iOS macOS watchOS tvOS Linux Windows Mobile App -
Kotlin Budapest Meetup - @marcoGomier Kotlin Budapest Meetup - @marcoGomier
. โโโ kmm-project โโโ androidApp โโโ iosApp โโโ shared Same Repository -
Kotlin Budapest Meetup - @marcoGomier // settings.gradle.kts include(":shared") // build.gradle.kts
implementation(project(":shared")) Android -
Kotlin Budapest Meetup - @marcoGomier CocoaPods integration https://kotlinlang.org/docs/reference/native/cocoapods.html Pod ::
Spec.new do |spec| spec.name = 'shared' spec.version = '1.0-SNAPSHOT' spec.homepage = 'Link to a Kotlin/Native module homepage' spec.source = { :git => "Not Published", :tag = > "Cocoapods/ #{ spec.name} #{ spec.authors = '' spec.license = '' spec.summary = 'Some description for a Kotlin/Native module' spec.static_framework = true spec.vendored_frameworks = "build/cocoapods/framework/shared.framework" spec.libraries = "c ++ " spec.module_name = " # { spec.name}_umbrella" spec.pod_target_xcconfig = { 'KOTLIN_TARGET[sdk=iphonesimulator*]' => 'ios_x64', 'KOTLIN_TARGET[sdk=iphoneos*]' => 'ios_arm', 'KOTLIN_TARGET[sdk=watchsimulator*]' => 'watchos_x86', 'KOTLIN_TARGET[sdk=watchos*]' => 'watchos_arm', 'KOTLIN_TARGET[sdk=appletvsimulator*]' = > 'tvos_x64', 'KOTLIN_TARGET[sdk=appletvos*]' => 'tvos_arm64', 'KOTLIN_TARGET[sdk=macosx*]' = > 'macos_x64' } spec.script_phases = [ { :name = > 'Build shared', :execution_position = > :before_compile, :shell_path => '/bin/sh', :script => < <- SCRIPT set -ev REPO_ROOT="$PODS_TARGET_SRCROOT" "$REPO_ROOT/ .. /gradlew" -p "$REPO_ROOT" :shared:syncFramework \ -Pkotlin.native.cocoapods.target=$KOTLIN_TARGET \ -Pkotlin.native.cocoapods.configuration=$CONFIGURATION \ -Pkotlin.native.cocoapods.cflags="$OTHER_CFLAGS" \ -Pkotlin.native.cocoapods.paths.headers="$HEADER_SEARCH_PATHS" \ -Pkotlin.native.cocoapods.paths.frameworks="$FRAMEWORK_SEARCH_PATHS" SCRIPT } ] end -
Kotlin Budapest Meetup - @marcoGomier iOS Project: Podfile pod 'shared',
:path => ' .. /shared' -
Kotlin Budapest Meetup - @marcoGomier Photo by Ashkan Forouzani on
Unsplash ๐ shared androidApp iosApp Gradle Module Framework Same Repository -
Kotlin Budapest Meetup - @marcoGomier Photo by Erwan Hesry on
Unsplash ๐ก Create a library! -
Kotlin Budapest Meetup - @marcoGomier Photo by Erwan Hesry on
Unsplash A little piece of tech stack to start with -
Kotlin Budapest Meetup - @marcoGomier โข Boring code to write
multiple times โข Code/feature that centralises the source of truth (i.e. a field is nullable or not) โข Code/feature that can be gradually extracted Where to start? -
Kotlin Budapest Meetup - @marcoGomier โข DTOs โข Common Models
โข Utility methods, aka `object Utils {}` โข Analytics โข . . . Where to start? -
Kotlin Budapest Meetup - @marcoGomier Common Kotlin Android App iOs
App .aar Framework โจ Maven โจ Cocoa Repo -
Kotlin Budapest Meetup - @marcoGomier Common Kotlin Android App iOs
App .aar Framework โจ Maven โจ Cocoa Repo Android App Repository KMP Repository iOs App Repository -
Kotlin Budapest Meetup - @marcoGomier plugins { //.. . id("maven-publish")
} group = "com.prof18.hn.foundation" version = "1.0" publishing { repositories { maven{ credentials { username = "username" password = "pwd" } url = url("https: // mymavenrepo.it") } } } Setup a Maven repository to share the artifacts: build.gradle.kts -
Kotlin Budapest Meetup - @marcoGomier Publish the artifacts ./gradlew publish
./gradlew publishToMavenLocal -
Kotlin Budapest Meetup - @marcoGomier XCFramework Official Support from Kotlin
1.5.30 https://kotlinlang.org/docs/whatsnew1530.html#support-for-xcframeworks -
XCFramework prior to Kotlin 1.5.30 https://github.com/prof18/kmp-xcframework-sample/blob/pre-kotlin-1.5.30/build.gradle.kts#L86 register("buildReleaseXCFramework", Exec :: class.java)
{ description = "Create a Release XCFramework" dependsOn("link${libName}ReleaseFrameworkIosArm64") dependsOn("link${libName}ReleaseFrameworkIosX64") val arm64FrameworkPath = "$rootDir/build/bin/iosArm64/${libName}ReleaseFramework/${libName}.framework" val arm64DebugSymbolsPath = "$rootDir/build/bin/iosArm64/${libName}ReleaseFramework/${libName}.framework.dSYM" val x64FrameworkPath = "$rootDir/build/bin/iosX64/${libName}ReleaseFramework/${libName}.framework" val x64DebugSymbolsPath = "$rootDir/build/bin/iosX64/${libName}ReleaseFramework/${libName}.framework.dSYM" val xcFrameworkDest = File("$rootDir/ .. /kmp-xcframework-dest/$libName.xcframework") executable = "xcodebuild" args(mutableListOf<String>().apply { add("-create-xcframework") add("-output") add(xcFrameworkDest.path) // Real Device add("-framework") add(arm64FrameworkPath) add("-debug-symbols") add(arm64DebugSymbolsPath) // Simulator add("-framework") add(x64FrameworkPath) add("-debug-symbols") add(x64DebugSymbolsPath) }) doFirst { xcFrameworkDest.deleteRecursively() } } -
Kotlin Budapest Meetup - @marcoGomier XCFramework prior to Kotlin 1.5.30
https: / / www.marcogomiero.com/posts/2021/build-xcframework-kmp/ -
Kotlin Budapest Meetup - @marcoGomier XCFramework: build.gradle.kts https://github.com/prof18/shared-hn-android-ios-backend/blob/main/hn-foundation/build.gradle.kts import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
val libName = "LibraryName" kotlin { val xcFramework = XCFramework(libName) ios { binaries.framework(libName) { xcFramework.add(this) } } ... } -
Kotlin Budapest Meetup - @marcoGomier XCFramework: build.gradle.kts https://github.com/prof18/shared-hn-android-ios-backend/blob/main/hn-foundation/build.gradle.kts import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
val libName = "LibraryName" kotlin { val xcFramework = XCFramework(libName) ios { binaries.framework(libName) { xcFramework.add(this) } } ... } -
Kotlin Budapest Meetup - @marcoGomier XCFramework: build.gradle.kts https://github.com/prof18/shared-hn-android-ios-backend/blob/main/hn-foundation/build.gradle.kts import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
val libName = "LibraryName" kotlin { val xcFramework = XCFramework(libName) ios { binaries.framework(libName) { xcFramework.add(this) } } ... } -
Kotlin Budapest Meetup - @marcoGomier XCFramework: build.gradle.kts https://github.com/prof18/shared-hn-android-ios-backend/blob/main/hn-foundation/build.gradle.kts import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XCFramework
val libName = "LibraryName" kotlin { val xcFramework = XCFramework(libName) ios { binaries.framework(libName) { xcFramework.add(this) } } ... } -
Kotlin Budapest Meetup - @marcoGomier . โโโ build โโโ XCFrameworks
โโโ debug โ โโโ LibraryName.xcframework โโโ release โโโ LibraryName.xcframework XCFramework -
Kotlin Budapest Meetup - @marcoGomier HNFoundation.podspec https://github.com/prof18/hn-foundation-cocoa-xcframework/blob/main/HNFoundation.podspec Pod :: Spec.new
do |s| # โโโ Spec Metadata โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ # # # These will help people to find your library, and whilst it # can feel like a chore to fill in it's definitely to your advantage. The # summary should be tweet-length, and the description more in depth. # s.name = "HNFoundation" s.version = "2.0.0" s.summary = "HNFoundation KMP library" s.homepage = "https: // github.com/prof18/hn-foundation-cocoa-xcframework" s.license = "Apache" s.author = { "Marco Gomiero" = > "[email protected]" } s.vendored_frameworks = 'HNFoundation.xcframework' s.source = { :git => "[email protected]:prof18/hn-foundation-cocoa-xcframework", :tag => " #{ s.version}" } s.exclude_files = "Classes/Exclude" end -
Kotlin Budapest Meetup - @marcoGomier iOs Project: Podfile # For
develop releases: pod 'HNFoundation', :git => โ[email protected]:prof18/hn-foundation-cocoa-xcframework.git", :branch = > 'develop' -
Kotlin Budapest Meetup - @marcoGomier iOs Project: Podfile # For
develop releases: pod 'HNFoundation', :git => โ[email protected]:prof18/hn-foundation-cocoa-xcframework.git", :branch = > 'develop' # For stable releases pod 'HNFoundation', :git => โ[email protected]:prof18/hn-foundation-cocoa-xcframework.git", :tag => โ2.0.0' -
register("publishDevFramework") { description = "Publish iOs framework to the Cocoa
Repo" doFirst { project.exec { workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "checkout", "develop").standardOutput } } dependsOn("assemble${libName}DebugXCFramework") doLast { copy { from("$buildDir/XCFrameworks/debug") into("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") } .. .. https://github.com/prof18/shared-hn-android-ios-backend/blob/main/hn-foundation/build.gradle.kts#L110 Publish Dev Framework Task -
Kotlin Budapest Meetup - @marcoGomier workingDir = File("$rootDir/ .. /
.. /hn-foundation-cocoa-xcframework") commandLine("git", "checkout", "develop").standardOutput } } dependsOn("assemble${libName}DebugXCFramework") doLast { copy { from("$buildDir/XCFrameworks/debug") into("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") } val dir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework/$libName.podspec") val tempFile = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework/$libName.podspec.new") val reader = dir.bufferedReader() val writer = tempFile.bufferedWriter() var currentLine: String? while (reader.readLine().also { currLine -> currentLine = currLine } != null) { if (currentLine ?. startsWith("s.version") == true) { writer.write("s.version = \"${libVersionName}\"" + System.lineSeparator()) -
Kotlin Budapest Meetup - @marcoGomier into("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework")
} val dir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework/$libName.podspec") val tempFile = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework/$libName.podspec.new") val reader = dir.bufferedReader() val writer = tempFile.bufferedWriter() var currentLine: String? while (reader.readLine().also { currLine -> currentLine = currLine } != null) { if (currentLine ?. startsWith("s.version") == true) { writer.write("s.version = \"${libVersionName}\"" + System.lineSeparator()) } else { writer.write(currentLine + System.lineSeparator()) } } writer.close() reader.close() val successful = tempFile.renameTo(dir) if (successful) { project.exec { workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") -
Kotlin Budapest Meetup - @marcoGomier if (successful) { project.exec {
workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "add", ".").standardOutput } val dateFormatter = SimpleDateFormat("dd/MM/yyyy - HH:mm", Locale.getDefault()) project.exec { workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") commandLine( "git", "commit", "-m", "\"New dev release: ${libVersionName}-${dateFormatter.format(Date())}\"" ).standardOutput } project.exec { workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "push", "origin", "develop").standardOutput } } -
https://github.com/prof18/shared-hn-android-ios-backend/blob/main/hn-foundation/build.gradle.kts#L177 Publish Release Framework Task register("publishFramework") { description = "Publish
iOs framework to the Cocoa Repo" doFirst { project.exec { workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "checkout", "main").standardOutput } } dependsOn("assemble${libName}ReleaseXCFramework") doLast { copy { from("$buildDir/XCFrameworks/release") into("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") } .. .. -
Kotlin Budapest Meetup - @marcoGomier project.exec { workingDir = File("$rootDir/
.. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "checkout", "main").standardOutput } } dependsOn("assemble${libName}ReleaseXCFramework") doLast { copy { from("$buildDir/XCFrameworks/release") into("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") } val dir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework/$libName.podspec") val tempFile = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework/$libName.podspec.new") val reader = dir.bufferedReader() val writer = tempFile.bufferedWriter() var currentLine: String? while (reader.readLine().also { currLine -> currentLine = currLine } != null) { if (currentLine ?. startsWith("s.version") == true) { -
Kotlin Budapest Meetup - @marcoGomier project.exec { workingDir = File("$rootDir/
.. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "add", ".").standardOutput } project.exec { workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "commit", "-m", "\"New release: ${libVersionName}\"").standardOu } project.exec { workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "tag", libVersionName).standardOutput } project.exec { workingDir = File("$rootDir/ .. / .. /hn-foundation-cocoa-xcframework") commandLine("git", "push", "origin", "main", " -- tags").standardOutput } } } } -
Kotlin Budapest Meetup - @marcoGomier โข Kotlin/Native different from the
JVM โข One thread -> no problems, you can use mutable objects โข Many thread - > only immutable objects Concurrency https://kotlinlang.org/docs/mobile/concurrency-overview.html -
Kotlin Budapest Meetup - @marcoGomier ๐ฅถ Freeze the object to
share it between threads -
Kotlin Budapest Meetup - @marcoGomier override fun deserialize(jsonString: String): NewsDTO
{ val newsDTO: NewsDTO = json.decodeFromString(jsonString) newsDTO.freeze() return newsDTO } -
Kotlin Budapest Meetup - @marcoGomier Faced [and resolved] difficulties โข
How to organise the project without a monorepo โข How to effectively distribute the iOS framework -
Kotlin Budapest Meetup - @marcoGomier Start little then go bigger
โข Validate the process with โlittleโ effort โข Then you can go bigger and share more โfeaturesโ โจ -
Bibliography / Useful Links โข https: / / kotlinlang.org/lp/mobile/ โข
https: / / www.marcogomiero.com/posts/2020/my-2cents-cross-platform/ โข https: / / giansegato.com/essays/a-technical-framework-for-early-stage-startups/ โข https: / / giansegato.com/essays/the-ebb-and-the-flow-of-product-development/ โข https: / / www.marcogomiero.com/posts/2021/build-xcframework-kmp/ โข https: / / www.marcogomiero.com/posts/2021/kmp-xcframework-official-support/ โข https: / / www.marcogomiero.com/posts/2020/kotlin-serialization-alamofire/ โข https: / / www.marcogomiero.com/posts/2021/kmp-existing-project/ โข https: / / www.youtube.com/watch?v=oxQ6e1VeH4M โข https: / / dev.to/kpgalligan/series/3739 โข https: / / blog.jetbrains.com/kotlin/2021/08/try-the-new-kotlin-native-memory-manager- development-preview/ -
Marco Gomiero Kotlin Budapest Meetup Thank you! > Twitter: @marcoGomier
โจ > Github: prof18 โจ > Website: marcogomiero.com