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

Dealing with KSP in Common Code

Dealing with KSP in Common Code

How to use KSP (experimental) with Kotlin Multiplatform for both iOS and Android

Anna Zharkova

July 27, 2021
Tweet

More Decks by Anna Zharkova

Other Decks in Programming

Transcript

  1. Dealing With KSP in Common Code for Android and iOS

    Anna Zharkova Lead Mobile developer
  2. Introduction • Mobile developer since 2013 • Lead Mobile developer

    in «Usetech software» . • Native mobile development, iOS and Android (Swift/Objective-C, Kotlin/Java) cross-platform mobile development (Xamarin, Kotlin multiplatform) • Leading mobile team. Mentorship • Public speaking (Mobius Moscow 2020, AppsLive 2020) • Tutor in Otus (iOS advanced) • Writing articles about mobile development (especially iOS and KMM ) • Women Techmaker Ambassador since 2021 • Kotlin GDE (2021) 2
  3. Goals. Today discuss: • Kotlin Symbol Processing, how it works

    • KSP in practice. Simplify Dependency Injection • Kotlin Multiplatform and its conception. Using KSP with common code • KSP with iOS. Supporting unsupported 3
  4. Kotlin Symbol Processing 4 First experimental release in 2020 Announced

    by Google in 10 Feb 2021 Compatible with Kotlin (since 1.4.30) Good for multiplatform
  5. Annotation processing. Purpose 6 • Analyze code • Generate common

    code for some purposes • Simplify abstractions and etc
  6. Annotation processing. Basis 7 Register annotation processing List unprocessed source

    files with annotation Run processors Any Processors for them? Compile
  7. KAPT (Kotlin annotation processing tool)1 8 Kotlin KAPT Data for

    APT Annotation processor (kotlin wrapper) Java bytecode Annotation processor
  8. KAPT (Kotlin annotation processing tool) 2/3 9 Kotlin KAPT AST

    for JAVA stub Javac Java classes JSR 269
  9. KAPT vs KSP 10 • KAPT generates Java code •

    Kotlin compiler has to generate Java stubs before the compilation. • More time • More code
  10. Kotlin symbol processing. How it works 15 KSP uses reflection

    to get info about types and components.
  11. Kotlin symbol processing. How it works 16 1. Processors read

    and analyze source programs and resources. 2. Processors generate code or other forms of output. 3. Generated code is compiled together with sources.
  12. Kotlin poet 17 Kotlin and Java API for generating .kt

    source files https://square.github.io/kotlinpoet/
  13. Kotlin Symbol Processing. Limitations 19 • Processors cannot modify existed

    code • Generates new files • Strong dependency on JVM at moment • Experimental feature (not recommended to use in production) • Not enough documentation for different cases
  14. Ready-made solutions 20 Koin Compiler playground (my sample was inspired

    with it) https://github.com/InsertKoinIO/koin-compiler- playground
  15. Manual dependency solution 22 Android part for Multiplatform-DI solution https://github.com/anioutkazharkova/kmm-di-sample

    https://github.com/anioutkazharkova/di-multiplatform-lib Mulitplatform DI core (KMM) Android Native app
  16. DI Solution 23 DIContainer -> Resolver Scopes define the conditions

    DIContainer Resolver Scope Scope Scope Resolve Reference<T> -> T? <T> ReferenceReg<T> MainScopeHolder
  17. Manual dependency solution 24 What we get in start https://github.com/anioutkazharkova/kmm-di-

    sample https://github.com/anioutkazharkova/di- multiplatform-lib
  18. KSP DI Solution 26 Mulitplatform DI core (KMM) Android Native

    app Mulitplatform DI core (KMM) KSP Compiler KSP Annotations
  19. KSP DI Solution 30 DI Code Symbol Processor Scanners (Visitors)

    Code generator Generated_data.kt Found data Search request Found and processed data (tree)
  20. Kotlin Multiplatform 44 • SDK, not framework • JVM for

    Android and LLVM for iOS • Total sharing (almost)
  21. Dealing with KSP in multiplatform 47 KSP (JVM library) Common

    Main JVM main/Android main Shared module
  22. Dealing with KSP in multiplatform 48 KSP (JVM library) Common

    Main JVM main/Android main Shared module
  23. KSP and Kotlin/Native problem 49 • KSP supports Kotlin JS/JVM

    • Kotlin/Native will be supported in future (official plans)
  24. KSP and Kotlin/Native problem 51 • KSP supports Kotlin JS/JVM

    • Kotlin/Native will be supported in future (official plans) • Bunch of info about unsupported targets L
  25. Dealing with KSP in multiplatform 52 • KSP could be

    used in multiplatform project • To use library with multiplatform it should be multiplatform
  26. Multiplatform – DI - KSP 55 Mulitplatform DI core (KMM)

    Android Native app iOS Native app Shared module KMM :ksp-di-shared Mulitplatform DI core (KMM)
  27. KSP and Kotlin/Native problem 57 • KSP supports Kotlin JS/JVM

    • Kotlin/Native will be supported in future (official plans)
  28. KSP and iOS setup 62 Multiplatform code should be wrapped

    into library with source set prior Multiplatform Library Generated common code iOS Native app
  29. Setup using generated data 63 Add it to source set

    for all platforms in shared module
  30. Kotlin symbol processing. Main challenges 65 • Work with multiplatform

    • Kotlin/Native • Generated files are not used by default • Experimental feature • Not to much documentation for purposes
  31. Kotlin compile plugin vs KSP 66 • Analyze Abstract Syntax

    Tree • Modify existing code • Could be used in Kotlin/Native
  32. Kotlin symbol processing. Future and prospects 67 • Make the

    IDE aware of the generated code. • Support Kotlin Native.
  33. Summary. 68 • KSP is faster than KAPT, better performance

    • KSP is experimental, but still be developed. • Not recommended for production at moment, but could be used for interesting cases • Multiplatform at moment, but doesn’t support Kotlin/Native • Will support Kotlin/Native and multiple round in future