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

Our Journey from Java to Kotlin-first!

Our Journey from Java to Kotlin-first!

At Mapbox, it took time, sweat and tears, but we finally adopted Kotlin as the main development language for all Android based projects.

In this session, we will talk about all the bumps we ran into along the way, and describe all the reasons that led us to make the decision to move to Kotlin. We will also show the problems and findings that we encountered along the journey of converting the Mapbox Navigation SDK for Android from 0.x Java to 1.0 Kotlin-first. We will illustrate how we replaced Java tooling and coding style guidelines with Kotlin specific ones, and how we took advantage of some language features and constructs to design APIs that are great for both Kotlin and Java. All this in addition to making sure the SDK was ready for prime time i.e. with S.O.L.I.D. architecture, APIs that are hard to misuse, a low crash rate, well tested and performant.

It's been quite a ride. Do you want to know how it went? Join us!

Pablo Guardiola

December 20, 2019
Tweet

More Decks by Pablo Guardiola

Other Decks in Programming

Transcript

  1. Our Journey from Java to
    Kotlin-first!
    Pablo Guardiola
    @Guardiola31337

    View full-size slide

  2. Navigation SDK 1.0
    - Kotlin-first
    - Modularized
    - Clean and performant
    APIs that are hard to
    misuse

    View full-size slide

  3. Episode 1.
    Kotlin

    View full-size slide

  4. SIZE
    & Number of methods
    Too many field references: max is 65536

    View full-size slide

  5. Show me the
    numbers!!!

    View full-size slide

  6. Kotlin stdlib increased the
    apk size by around 380 KB and
    added around 6K methods

    View full-size slide

  7. Benefits
    Developer
    - Intuitive
    - Statically typed
    - Type inference
    - Outstanding IDE support
    - Functional and
    object-oriented
    - Expressive
    - Concise
    - null safe
    - //.

    View full-size slide

  8. Client / User
    - More strongly typed
    than Java
    - DSLs
    - Modern, powerful and
    safe
    - Fewer methods, less
    weight
    Benefits

    View full-size slide

  9. 30.5%
    SDK / Libraries

    View full-size slide

  10. 77%
    Extension
    functions

    View full-size slide

  11. Navigation SDK

    View full-size slide

  12. YES
    - Still in Beta (0.x)
    - Young
    - Client apps in Kotlin
    - Excitement

    View full-size slide

  13. All in on Kotlin
    or not?
    Sep 2019

    View full-size slide

  14. NO
    - stdlib adds binary size
    - Kotlin versions
    incompatibility
    - Extra effort to make
    APIs Java friendly

    View full-size slide

  15. YES
    - Google’s preferred
    language for Android
    - Productivity
    - New Java features
    adoption is slow
    - OkHttp (v4.x)
    - Concurrency
    - Hiring

    View full-size slide

  16. Episode 2.
    Conversion

    View full-size slide

  17. Code style
    ktlint

    View full-size slide

  18. @NonNull and @Nullable
    annotations
    Analyze > Infer Nullity//.

    View full-size slide

  19. Convert Java to
    Kotlin
    ⌥⇧⌘K

    View full-size slide

  20. Polish Kotlin code

    View full-size slide

  21. if null else / let Elvis operator

    View full-size slide

  22. if null else / let Elvis operator

    View full-size slide

  23. switch / when

    View full-size slide

  24. switch / when

    View full-size slide

  25. Special
    attention
    - /!
    - lateinit / lazy
    - @NotNull non-annotated
    parameters
    - @JvmStatic and @get:
    - @throws methods
    - Writing Java in Kotlin

    View full-size slide

  26. MockK
    - relaxed = true
    RETURNS_DEEP_STUBS
    - relaxedUnitFun = true
    - Kotlin idiomatic
    - mockkObject
    - mockkStatic

    View full-size slide

  27. Episode 3.
    1.0

    View full-size slide

  28. Public APIs
    - Don’t expose Kotlin
    types
    - Great for both Java and
    Kotlin
    - Java and Kotlin
    examples
    - Master @Jvm annotations
    - Rest of the code
    internal

    View full-size slide

  29. To be continued//.

    View full-size slide

  30. Takeaways
    - El que la sigue la
    consigue (Keep
    insisting)
    - Baby steps
    - Test harness
    - Watch out null checks
    - Designing Good APIs is
    hard

    View full-size slide

  31. Questions?
    Pablo Guardiola
    @Guardiola31337

    View full-size slide

  32. Sources
    https://realm.io/realm-report/2017-q4/
    https://pusher.com/state-of-kotlin
    https://developer.squareup.com/blog/okio-2
    https://mockk.io/

    View full-size slide