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

Pitfalls when mixing Kotlin & Java

Pitfalls when mixing Kotlin & Java

I've had my fair share of android projects being converted to Kotlin from Java and I thought I'd share the lessons I've learned doing so. There are a few subtle pitfalls that are very easy to fall into.
Here I share my experience with mixing and what to look out for.

0297b9b4bfd45c0f9c6c52bf696b7735?s=128

Bob Dahlberg

February 05, 2019
Tweet

Transcript

  1. 
 Pitfalls when mixing Kotlin & Java Bob Dahlberg Hedy

    Tech Hedy
  2. Bob Dahlberg Hedy Tech
 Challenge Accepted GDG Stockholm Android Stockholm

    Kotlin Meetup Who’s Bob?
 - Building software
 - CrossFit enthusiast
 - Father of one
 - Arranging meetups
 - Loves a good challenge!
  3. Background Android Been an Android developer since Android Cupcake (2009).

    Only Java 6
  4. Background Android Scala Learned Scala and tried to build Android

    with it..
  5. Background Android Scala Learned Scala and tried to build Android

    with it.. It’s plausible.
  6. Background Android Scala Learned Scala and tried to build Android

    with it.. It’s plausible. But you rather die a little than trying to mix it with Java.
  7. Background Android Scala Kotlin Found Kotlin M12 in 2015 and

    it blended almost seamlessly with Java. 

  8. Background Android Scala Kotlin Found Kotlin M12 in 2015 and

    it blended almost seamlessly with Java. A few years later even Google understood it and made it an official language on Android. 

  9. Background Android Scala Kotlin Found Kotlin M12 in 2015 and

    it blended almost seamlessly with Java. A few years later even Google understood it and made it an official language on Android. But there are a few things to keep in mind when mixing.
  10. The goal should be to move completely to Kotlin.

  11. All the way Blending Although blending Java and Kotlin works

    great you can’t write idiomatic Java nor Kotlin code when blending. You should choose.

  12. All the way Blending Although blending Java and Kotlin works

    great you can’t write idiomatic Java nor Kotlin code when blending. You should choose.
 You have to choose!
  13. All the way Blending Choose Java - If you’re fairly

    new to Kotlin and wicked at Java. - Don’t know how to write idiomatic Kotlin code. - Can’t invest in learning in the short run or want to slowly adapt to it.
  14. All the way Blending Choose Java Choose Kotlin - Already

    know how to write idiomatic Kotlin code. - Can invest in learning. - If it doesn’t mean to re-write the entire app upfront.
  15. All the way Blending
 Choose Java Choose Kotlin Be pragmatic

    The idioms should be a goal to strive for. 
 Not enforced in every change. Don’t mix if you feel you need to rewrite the entire app before releasing.
  16. All the way Blending
 Choose Java Choose Kotlin Be pragmatic

    Permanent mixing If you have a great separation of concern and code, a modular architecture, you can have idiomatic Java and idiomatic Kotlin mixed permanently. But it’s a rare case.
  17. Pace yourself

  18. Pace yourself Auto convert Don’t use it, please!


  19. Pace yourself Auto convert Don’t use it, please!
 Only one

    file at the time!
  20. Pace yourself Auto convert Explanation mark bonanza

  21. Pace yourself Auto convert Explanation mark bonanza Kotlin Friendly If

    you’ve written your Java code so that it can be easily interpreted, the converted Kotlin code is better. 
 

  22. Pace yourself Auto convert Explanation mark bonanza Kotlin Friendly If

    you’ve written your Java code so that it can be easily interpreted, the converted Kotlin code is better. final
 

  23. Pace yourself Auto convert Explanation mark bonanza Kotlin Friendly If

    you’ve written your Java code so that it can be easily interpreted, the converted Kotlin code is better. final
 @NotNull

  24. Pace yourself Auto convert Explanation mark bonanza Kotlin Friendly If

    you’ve written your Java code so that it can be easily interpreted, the converted Kotlin code is better. final
 @NotNull
 @Nullable
  25. Pace yourself Auto convert Explanation mark bonanza Kotlin Friendly Refactor

    the converted code
  26. Pace yourself Auto convert Explanation mark bonanza Kotlin Friendly Refactor

    the converted code
  27. Pace yourself Auto convert Explanation mark bonanza Kotlin Friendly Refactor

    the converted code
  28. Where to start?

  29. Path of least resistance Hit it were it don’t hurt

    Proof of Concept
 Prove that your project works with Kotlin and that you and the team understand how to mix it.
  30. Path of least resistance Hit it were it don’t hurt

    Isolated Tests
  31. Path of least resistance Hit it were it don’t hurt

    Isolated Small POJOs
  32. Path of least resistance Hit it were it don’t hurt

    Isolated Small Independent Utilities & Helpers
  33. Preparations

  34. Decide upfront How should you handle…

  35. Decide upfront How should you handle… static java code
 


    
 
 
 
 
 

  36. Decide upfront How should you handle… static java code
 null


    
 
 
 
 
 

  37. Decide upfront How should you handle… static java code
 null


    extensions
 
 
 
 
 

  38. Decide upfront How should you handle… static java code
 null


    extensions
 visibility modifiers
 
 
 
 

  39. Decide upfront How should you handle… static java code
 null


    extensions
 visibility modifiers
 POJOs
 
 
 

  40. Decide upfront How should you handle… static java code
 null


    extensions
 visibility modifiers
 POJOs
 singeltons
 
 

  41. Decide upfront How should you handle… Differences Read up on

    the big differences and decide how you will tackle those challenges.
  42. When you need more

  43. Bits & Bytes Byte code In IntelliJ there’s a tool

    to see the byte code generated from your code. 

  44. Bits & Bytes Byte code
 Decompile In IntelliJ there’s a

    tool to see the byte code generated from your code. And if you decompile that you’ll see the equivalent java code.
  45. Bits & Bytes Byte code
 Decompile Try In IntelliJ there’s

    a tool to see the byte code generated from your code. And if you decompile that you’ll see the equivalent java code. Try with a simple data class.
  46. Let’s see some code

  47. Code typealias

  48. Code typealias function parameters

  49. Code typealias function parameters

  50. Code typealias function parameters static-ish

  51. Code typealias function parameters static-ish

  52. Code typealias function parameters static-ish

  53. Code typealias function parameters static-ish

  54. Code typealias function parameters static-ish singelton

  55. Code typealias function parameters static-ish singelton extensions & default values

  56. Code typealias function parameters static-ish singelton extensions & default values

  57. Code typealias function parameters static-ish singelton extensions & default values

    data classes
  58. Code typealias function parameters static-ish singelton extensions & default values

    data classes null
  59. Code typealias function parameters static-ish singelton extensions & default values

    data classes null
  60. Code typealias function parameters static-ish singelton extensions & default values

    data classes null deconstructing
  61. Code typealias function parameters static-ish singelton extensions & default values

    data classes null deconstructing
  62. Code typealias function parameters static-ish singelton extensions & default values

    data classes null deconstructing
  63. Code typealias function parameters static-ish singelton extensions & default values

    data classes null deconstructing more conventions
  64. To sum up

  65. Take aways Differences Learn the differences - static
 - visibility


    - conventions
 - null
 - etc
  66. Take aways Differences Pragmatic Set a solid goal!
 But be

    pragmatic about how to reach it.
  67. Take aways Differences Pragmatic Iterate As in all development, but

    maybe more important in this case. Convert
 Refactor, Refactor, Refactor
 Repeat
  68. Take aways Differences Pragmatic Iterate
 Cheat Decompile the kotlin byte

    code to see what to expect from java. There’s no need to guess.
  69. Take aways Differences Pragmatic Iterate
 Cheat Help Kotlin Use final,

    @Nullable and @NotNull extensively in java. And set lint warnings to errors instead to avoid as many problems with null.
  70. Take aways Differences Pragmatic Iterate
 Cheat Help Kotlin Organic Let

    the Kotlin code base grow and take over organically so that your team have time to adopt and learn. Isolated, small, independent.
  71. Take aways Differences Pragmatic Iterate
 Cheat Help Kotlin Organic Conventions

    Use the strengths in Kotlin that theres a lot of conventions that is adopted for Java.
  72. Take aways Differences Pragmatic Iterate
 Cheat Help Kotlin Organic Conventions

    Null And where kotlin has features to be better than Java. Be sure to use them smart.
  73. Thank you!

  74. Bob Dahlberg bob@hedy.tech @dahlberg.bob - medium.com bobdahlberg - speakersdeck.com When

    writing a lot of Kotlin code in a mixed project you’ll start to get a false sense of security around null.