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!

3c2bdd16c0ea8511dc254b8497a06f78?s=128

Pablo Guardiola

December 20, 2019
Tweet

Transcript

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

  2. Navigation SDK 1.0 - Kotlin-first - Modularized - Clean and

    performant APIs that are hard to misuse
  3. Episode 1. Kotlin

  4. SDKs

  5. SIZE

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

    is 65536
  7. None
  8. Show me the numbers!!!

  9. Kotlin stdlib increased the apk size by around 380 KB

    and added around 6K methods
  10. Nope.

  11. Benefits Developer - Intuitive - Statically typed - Type inference

    - Outstanding IDE support - Functional and object-oriented - Expressive - Concise - null safe - //.
  12. Client / User - More strongly typed than Java -

    DSLs - Modern, powerful and safe - Fewer methods, less weight Benefits
  13. Nope.

  14. None
  15. None
  16. 30.5% SDK / Libraries

  17. 77% Extension functions

  18. Nope.

  19. None
  20. None
  21. Nope.

  22. Navigation SDK

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

    apps in Kotlin - Excitement
  24. All in on Kotlin or not? Sep 2019

  25. NO - stdlib adds binary size - Kotlin versions incompatibility

    - Extra effort to make APIs Java friendly
  26. YES - Google’s preferred language for Android - Productivity -

    New Java features adoption is slow - OkHttp (v4.x) - Concurrency - Hiring
  27. Oct 8, 2019

  28. None
  29. Episode 2. Conversion

  30. Code style ktlint

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

  33. Convert Java to Kotlin ⌥⇧⌘K

  34. Polish Kotlin code

  35. if null else / let Elvis operator

  36. if null else / let Elvis operator

  37. switch / when

  38. switch / when

  39. callback

  40. callback

  41. Utils

  42. Special attention - /! - lateinit / lazy - @NotNull

    non-annotated parameters - @JvmStatic and @get: - @throws methods - Writing Java in Kotlin
  43. MockK - relaxed = true RETURNS_DEEP_STUBS - relaxedUnitFun = true

    - Kotlin idiomatic - mockkObject - mockkStatic
  44. Episode 3. 1.0

  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
  46. To be continued//.

  47. Takeaways - El que la sigue la consigue (Keep insisting)

    - Baby steps - Test harness - Watch out null checks - Designing Good APIs is hard
  48. Thanks.

  49. Questions? Pablo Guardiola @Guardiola31337

  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/