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 Slide

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

    View Slide

  3. Episode 1.
    Kotlin

    View Slide

  4. SDKs

    View Slide

  5. SIZE

    View Slide

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

    View Slide

  7. View Slide

  8. Show me the
    numbers!!!

    View Slide

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

    View Slide

  10. Nope.

    View Slide

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

    View Slide

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

    View Slide

  13. Nope.

    View Slide

  14. View Slide

  15. View Slide

  16. 30.5%
    SDK / Libraries

    View Slide

  17. 77%
    Extension
    functions

    View Slide

  18. Nope.

    View Slide

  19. View Slide

  20. View Slide

  21. Nope.

    View Slide

  22. Navigation SDK

    View Slide

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

    View Slide

  24. All in on Kotlin
    or not?
    Sep 2019

    View Slide

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

    View Slide

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

    View Slide

  27. Oct 8, 2019

    View Slide

  28. View Slide

  29. Episode 2.
    Conversion

    View Slide

  30. Code style
    ktlint

    View Slide

  31. View Slide

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

    View Slide

  33. Convert Java to
    Kotlin
    ⌥⇧⌘K

    View Slide

  34. Polish Kotlin code

    View Slide

  35. if null else / let Elvis operator

    View Slide

  36. if null else / let Elvis operator

    View Slide

  37. switch / when

    View Slide

  38. switch / when

    View Slide

  39. callback

    View Slide

  40. callback

    View Slide

  41. Utils

    View Slide

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

    View Slide

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

    View Slide

  44. Episode 3.
    1.0

    View Slide

  45. 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 Slide

  46. To be continued//.

    View Slide

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

    View Slide

  48. Thanks.

    View Slide

  49. Questions?
    Pablo Guardiola
    @Guardiola31337

    View Slide

  50. 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 Slide