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

KSP+Kotlin compiler plugin

KSP+Kotlin compiler plugin

Anna Zharkova

April 20, 2022
Tweet

More Decks by Anna Zharkova

Other Decks in Programming

Transcript

  1. 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
  2. Goals. Today discuss: • Kotlin Multiplatform and its conception. •

    Kotlin Symbol Processing, how it works. • Simplify Dependency Injection. Using KSP with common code • Kotlin compile plugins. Plugins vs KSP 3
  3. Kotlin Multiplatform 4 • SDK, not framework • JVM for

    Android and LLVM for iOS • Total sharing (almost)
  4. Multiplatform – DI. Manual dependency solution 6 Mulitplatform DI core

    (KMM) Android Native app iOS Native app Shared module KMM Multiplatform-DI solution https://github.com/anioutkazharkova/kmm-di-sample https://github.com/anioutkazharkova/di-multiplatform-lib
  5. DI Solution 7 DIContainer -> Resolver Scopes define the conditions

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

    sample https://github.com/anioutkazharkova/di- multiplatform-lib
  7. Optimize code 9 • Generate common code with KSP •

    Modify existed files with Compiler plugins
  8. Kotlin Symbol Processing 11 First experimental release in 2020 Announced

    by Google in 10 Feb 2021 Compatible with Kotlin (since 1.4.30) Stable since 1.5.31
  9. Annotation processing. Purpose 13 • Analyze code • Generate common

    code for some purposes • Simplify abstractions and etc
  10. KAPT (Kotlin annotation processing tool) 2/3 14 Kotlin KAPT AST

    for JAVA stub Javac Java classes JSR 269
  11. KAPT vs KSP 15 • KAPT generates Java code •

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

    to get info about types and components.
  13. Kotlin symbol processing. How it works 21 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.
  14. Kotlin poet 22 Kotlin and Java API for generating .kt

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

    code • Generates new files • Not enough documentation for different cases
  16. KSP DI Solution 29 DI Code Symbol Processor Scanners (Visitors)

    Code generator Generated_data.kt Found data Search request Found and processed data (tree)
  17. Kotlin symbol processing. Main challenges 39 • Generated files are

    not used by default • Not to much documentation for purposes
  18. Kotlin compile plugin vs KSP 41 • Analyze Abstract Syntax

    Tree • Modify existing code • Could be also used in Kotlin/Native
  19. Plugin structure 42 Kmm-di-inject-gradle InjectDIGradlePlugin Kmm-di-inject-plugin InjectDIComponentRegistrar Kmm-di-inject-plugin InjectDIIrGenerationExtension @Container

    @Single Kmm-di-inject-plugin ContainerGenerationExtension InjectTransformer ContainerClassLoweringPass @InjectService
  20. Injection. New body. Lazy field 57 Call the methods of

    other class directly Note: we need to use an entry to class
  21. Injection. New body. Lazy field 58 Call the methods of

    other class directly Note: we need to use an entry to class
  22. Dispatch receiver 60 Dispatch receiver – the host of method

    or field function ConfigurationApp fun<T> resolve(t: String):T? Parent class Fun getter() Field
  23. Add to container. The begin 69 Simplify registration Add to

    Scope Type: String (class name) Fabric: () -> T (Lambda
  24. Plugin structure 70 Kmm-di-inject-gradle InjectDIGradlePlugin Kmm-di-inject-plugin InjectDIComponentRegistrar Kmm-di-inject-plugin InjectDIIrGenerationExtension @Container

    @Single Kmm-di-inject-plugin ContainerGenerationExtension InjectTransformer ContainerClassLoweringPass @InjectService
  25. Kotlin Compiler plugins. Pro and cos 85 + Modify existed

    files + Lightweight - Not enough documentation - Still be not stable and experimental
  26. Summary. 86 • KSP is faster than KAPT, better performance

    • KSP is stable, but still be developed. • Support Kotlin/Native and multiple round in future • Plugins could be lightweight and more flexible with multiplatform • Lack of documentation for plugins. Need to use others samples
  27. Sources 87 • https://github.com/anioutkazharkova/ksp-di-playground • https://github.com/google/ksp#kotlin-symbol-processing-api • https://github.com/anioutkazharkova/ksp-library-template • https://github.com/anioutkazharkova/kmm-di-plugin

    • https://github.com/SeongUgJung/KspSample • https://github.com/bnorm/kotlin-ir-plugin-template • https://blog.bnorm.dev/writing-your-second-compiler-plugin-part-5 • https://github.com/sergeshustoff/dikt