Slide 1

Slide 1 text

Dealing With KSP in Common Code for Android and iOS Anna Zharkova Lead Mobile developer

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Kotlin Symbol Processing. Purpose 5 • Build lightweight Kotlin compiler plugins • Create annotation processors

Slide 6

Slide 6 text

Annotation processing. Purpose 6 • Analyze code • Generate common code for some purposes • Simplify abstractions and etc

Slide 7

Slide 7 text

Annotation processing. Basis 7 Register annotation processing List unprocessed source files with annotation Run processors Any Processors for them? Compile

Slide 8

Slide 8 text

KAPT (Kotlin annotation processing tool)1 8 Kotlin KAPT Data for APT Annotation processor (kotlin wrapper) Java bytecode Annotation processor

Slide 9

Slide 9 text

KAPT (Kotlin annotation processing tool) 2/3 9 Kotlin KAPT AST for JAVA stub Javac Java classes JSR 269

Slide 10

Slide 10 text

KAPT vs KSP 10 • KAPT generates Java code • Kotlin compiler has to generate Java stubs before the compilation. • More time • More code

Slide 11

Slide 11 text

Why KSP better 11 PURE FASTER COMPATIBILITY Read Kotlin AST No stubs Support Java code

Slide 12

Slide 12 text

Kotlin symbol processing. How it works 12 Annotation Processor -> Symbol processor

Slide 13

Slide 13 text

Kotlin symbol processing. How it works 13 Provider should be declared as resource

Slide 14

Slide 14 text

Kotlin symbol processing. How it works 14 Resolver – to get data for specific case

Slide 15

Slide 15 text

Kotlin symbol processing. How it works 15 KSP uses reflection to get info about types and components.

Slide 16

Slide 16 text

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.

Slide 17

Slide 17 text

Kotlin poet 17 Kotlin and Java API for generating .kt source files https://square.github.io/kotlinpoet/

Slide 18

Slide 18 text

Create our own annotation processing 18

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Ready-made solutions 20 Koin Compiler playground (my sample was inspired with it) https://github.com/InsertKoinIO/koin-compiler- playground

Slide 21

Slide 21 text

Ready-made solutions 21 Easy and nice KSP Sample https://github.com/SeongUgJung/KspSample

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

DI Solution 23 DIContainer -> Resolver Scopes define the conditions DIContainer Resolver Scope Scope Scope Resolve Reference -> T? ReferenceReg MainScopeHolder

Slide 24

Slide 24 text

Manual dependency solution 24 What we get in start https://github.com/anioutkazharkova/kmm-di- sample https://github.com/anioutkazharkova/di- multiplatform-lib

Slide 25

Slide 25 text

Adding annotations 25 Scopes - > scope annotations Container - > containers annotations

Slide 26

Slide 26 text

KSP DI Solution 26 Mulitplatform DI core (KMM) Android Native app Mulitplatform DI core (KMM) KSP Compiler KSP Annotations

Slide 27

Slide 27 text

KSP DI Solution 27 KSP Compiler depends on symbol-processing-api

Slide 28

Slide 28 text

KSP DI Solution 28 KSP Compiler depends on symbol-processing-api

Slide 29

Slide 29 text

KSP DI Solution 29 Don’t forget Kotlin Poet

Slide 30

Slide 30 text

KSP DI Solution 30 DI Code Symbol Processor Scanners (Visitors) Code generator Generated_data.kt Found data Search request Found and processed data (tree)

Slide 31

Slide 31 text

Custom DI Symbol provider 31

Slide 32

Slide 32 text

Custom DI Symbol provider 32

Slide 33

Slide 33 text

KSP Resolver 33 We need to get annotated symbols to analyze them

Slide 34

Slide 34 text

KSP Resolver 34

Slide 35

Slide 35 text

Generate file with Kotlin Poet 35 That’s ok to use hardcoded string in template

Slide 36

Slide 36 text

Generate file with Kotlin Poet 36 Put all your stuff in template

Slide 37

Slide 37 text

Let’s build! 37 Sample compilation output

Slide 38

Slide 38 text

Generated DI solution 38 What we got as result https://github.com/anioutkazharkova/ksp-di- playground

Slide 39

Slide 39 text

Generated DI solution 39 What we got as result https://github.com/anioutkazharko va/ksp-di-playground

Slide 40

Slide 40 text

Use it with code 40 Connect and check https://github.com/anioutkazharkova/ks p-di-playground

Slide 41

Slide 41 text

Use it with code 41 Connect and check https://github.com/anioutkazharkova/ks p-di-playground

Slide 42

Slide 42 text

Let’s use in multiplatform project 42

Slide 43

Slide 43 text

Kotlin Multiplatform 43

Slide 44

Slide 44 text

Kotlin Multiplatform 44 • SDK, not framework • JVM for Android and LLVM for iOS • Total sharing (almost)

Slide 45

Slide 45 text

Every platform needs its Kotlin 45 Kotlin Common Kotlin JVM Kotlin JS Kotlin Native

Slide 46

Slide 46 text

Multiplatform – DI 46 Mulitplatform DI core (KMM) Android Native app iOS Native app Shared module KMM

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

KSP and Kotlin/Native problem 50

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

Dealing with KSP in multiplatform 52 • KSP could be used in multiplatform project • To use library with multiplatform it should be multiplatform

Slide 53

Slide 53 text

Dealing with KSP in multiplatform 53 :ksp-di-compiler :ksp-di-shared

Slide 54

Slide 54 text

Dealing with KSP in multiplatform 54 Set in KSP shared compiler just jvm target

Slide 55

Slide 55 text

Multiplatform – DI - KSP 55 Mulitplatform DI core (KMM) Android Native app iOS Native app Shared module KMM :ksp-di-shared Mulitplatform DI core (KMM)

Slide 56

Slide 56 text

Dealing with KSP in multiplatform 56 Connect to shared module of app Use dependencies section

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

Dealing with KSP in multiplatform 58 Files generated!

Slide 59

Slide 59 text

Let’s make it work with iOS 59

Slide 60

Slide 60 text

It will be experimental solution for experimental feature 60

Slide 61

Slide 61 text

KSP and iOS setup 61 Generated file contains code for multiplatform support

Slide 62

Slide 62 text

KSP and iOS setup 62 Multiplatform code should be wrapped into library with source set prior Multiplatform Library Generated common code iOS Native app

Slide 63

Slide 63 text

Setup using generated data 63 Add it to source set for all platforms in shared module

Slide 64

Slide 64 text

KSP DI Multiplatform 64

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

Kotlin compile plugin vs KSP 66 • Analyze Abstract Syntax Tree • Modify existing code • Could be used in Kotlin/Native

Slide 67

Slide 67 text

Kotlin symbol processing. Future and prospects 67 • Make the IDE aware of the generated code. • Support Kotlin Native.

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

Sources 69 • https://github.com/anioutkazharkova/ksp-di-playground • https://github.com/google/ksp#kotlin-symbol-processing-api • https://kotlinlang.org/docs/mobile/home.html • https://github.com/InsertKoinIO/koin-compiler-playground • https://github.com/SeongUgJung/KspSample • https://github.com/FutureMind/koru

Slide 70

Slide 70 text

Thanks for listening! 70 @anioutkajarkova azharkova prettygeeknotes