Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bridging the gap between Kotlin and Swift

Aydar
January 10, 2022

Bridging the gap between Kotlin and Swift

Tooling and technologies around Kotlin Multiplatform always leave Apple platforms a little overboard. After all, Kotlin is a part of Android development, not in iOS. I will tell you what we do to fix it. You will learn about the Kotlin Multiplatform Mobile plugin for AppCode, what it was created for, and what it can do at the moment. I will show the latest developments in the Kotlin Multiplatform, and will tell you how they work from the inside.

Aydar

January 10, 2022
Tweet

More Decks by Aydar

Other Decks in Programming

Transcript

  1. Shared.framework ⌨ New code ⏳ Build project 7 Problems of

    iOS development KMM plugin for Android Studio
  2. class MyClass { fun hello() = "Hello from Kotlin!" }

    class MyClass { fun hello() = "Hello from Kotlin!" } 8 let a = MyClass() Problems of iOS development KMM plugin for Android Studio
  3. 9 / / . . . _ _ attribute __

    ((swift_name("KotlinDouble"))) @interface KNFDouble : KNFNumber - (instancetype)initWithDouble:(double)value; + (instancetype)numberWithDouble:(double)value; @end; _ _ attribute __ ((swift_name("KotlinBoolean"))) @interface KNFBoolean : KNFNumber - (instancetype)initWithBool:(BOOL)value; + (instancetype)numberWithBool:(BOOL)value; @end; _ _ attribute __ ((objc_subclassing_restricted)) _ _ attribute __ ((swift_name("MyClass"))) @interface KNFMyClass : KNFBase - (instancetype)init _ _ attribute __ ((swift_name("init()"))) __ attribute __ ((objc_designated_initializer)); + (instancetype)new __ attribute _ _ ((availability(swift, unavailable, message="use object initializers instead"))); - (NSString *)hello __ attribute _ _ ((swift_name("hello()"))); @end; Problems of iOS development KMM plugin for Android Studio
  4. 11 • Lack of Swift and Obj-C support in Android

    Studio • Necessity to rebuild the framework every time you change the interface • Complicated relationships between declaration in Kotlin and usage in Swift Problems of iOS development KMM plugin for Android Studio
  5. 12

  6. KMM plugin for AppCode Develop Kotlin Multiplatform projects in AppCode

    Highlighting, autocompletion for Kotlin Run | Debug iOS and Android apps 14 Go to declaration, refactorings and fi nd usages work for Kotlin, Obj-C and Swift
  7. What is a symbol Symbol • Name • Location •

    Attributes • Relationships with other symbols • … 17 Internal representation of the declaration of anything in the code. Symbol tables - the IDE's knowledge of classes, functions, variables, etc.
  8. class Person { let name = "John" let surname =

    "Doe" let age = 42 // Very important function func greet() { print("Hi \(name)!") } } class Person let name: String let surname: String let age: Int func greet() -> Void Symbol table 18 What is a symbol
  9. Obj-C - Swift Cross Resolve Obj-C class AnotherObject: NSObject @interface

    AnotherObject : NSObject //... @end Naming Rules 20
  10. MyShared.framework MyFramework.h MyFramework IDE How it works in Xcode val

    helloString = "Hello from Kotlin!" @property (readonly) NSString *helloString; objc var helloString: String { get } Kotlin compiler ⏳ Build project 23 Symbols translation
  11. Kotlin compiler 24 Kotlin frontend Kotlin/Native backend Kotlin 
 Code

    IR MyShared.framework MyFramework.h MyFramework Symbols translation
  12. IDE How it works in KMM for AppCode val helloString

    = "Hello from Kotlin!" var helloString: String { get } Internal Compiler Structures @property (readonly) NSString *helloString; objc New 26 Symbols translation
  13. Advantages of the new approach 🚀 Updates on the fl

    y 🎯 Go to Declaration, refactorings and etc. 😅 Tolerates mistakes 27 Symbols translation
  14. Hooray! 
 Now you don't have to rebuild the framework

    after each Kotlin code change! 28 print(a.hello) SourceKit
  15. 30 SourceKit val helloString = "Hello!" import MyFramework Getting annotations

    from SourceKit Problem MyFramework ⁉ @property (readonly) NSString *helloString; MyFramework.h objc build
  16. 31 SourceKit val helloString = "Hello!" print(a.helloString) Getting annotations from

    SourceKit Problem MyFramework @property (readonly) NSString *helloString; MyFramework.h objc val hello = "Hello!" print(a.hello) ⁉
  17. 32 📁 MyFramework.framework/ 📁 Headers/ MyFramework.h 📁 Modules/ module.modulemap Info.plist

    MyFramework SourceKit Getting annotations from SourceKit Solution
  18. 33 📁 MyFramework.framework/ 📁 Headers/ MyFramework.h 📁 Modules/ module.modulemap SourceKit

    After each Kotlin code change /iosApp/build/tmp/SourceKitFrameworkStubs/MyFramework.framework Getting annotations from SourceKit Solution
  19. Optimizations 34 • Generate right before the opening of a

    Swift fi le • Ignore changes in comments and function bodies / / This is very important fun doImportantThings(): Int { val a = 1 val b = 2 return a + b } Getting annotations from SourceKit
  20. 38 Project model Gradle-only approach Kotlin shared Android app iOS

    app Gradle Gradle Xcode project Gradle Source of truth for iOS apple { iosApp { productName = "CocoaConferences" sceneDelegateClass = "SceneDelegate" launchStoryboard = "LaunchScreen" dependencies { implementation(project(":shared")) } } } iosApp/build.gradle.kts : ( )
  21. 39 Project model Hybrid approach Kotlin shared Android app iOS

    app Gradle Gradle Xcode project kotlin { ios() cocoapods { summary = "Kotlin CocoaPods iOS Library" homepage = "https: / / jetbrains.com" podfile = project.file(" .. /xcode/Podfile") ios.deploymentTarget = "14.7" } } ioisMain/build.gradle.kts : + UNDER DEVELOPMENT
 COMING SOON
  22. 40 Cross-resolve between Kotlin and Swift already exists, and you

    can try it right now in KMM plugin for AppCode Conclusions
  23. Thank you! 43 What we talked about: • Kotlin Multiplatform

    intro • Current problems of the iOS world • KMM plugin for AppCode • How Kotlin-Swift cross-resolve works • Pleasing SourceKit • Project model • Conclusions @appcode @aydarmukh