Slide 1

Slide 1 text


 Pitfalls when mixing Kotlin & Java Bob Dahlberg Hedy Tech Hedy

Slide 2

Slide 2 text

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!

Slide 3

Slide 3 text

Background Android Been an Android developer since Android Cupcake (2009). Only Java 6

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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.

Slide 7

Slide 7 text

Background Android Scala Kotlin Found Kotlin M12 in 2015 and it blended almost seamlessly with Java. 


Slide 8

Slide 8 text

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. 


Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

The goal should be to move completely to Kotlin.

Slide 11

Slide 11 text

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.


Slide 12

Slide 12 text

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!

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

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.

Slide 17

Slide 17 text

Pace yourself

Slide 18

Slide 18 text

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


Slide 19

Slide 19 text

Pace yourself Auto convert Don’t use it, please!
 Only one file at the time!

Slide 20

Slide 20 text

Pace yourself Auto convert Explanation mark bonanza

Slide 21

Slide 21 text

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. 
 


Slide 22

Slide 22 text

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
 


Slide 23

Slide 23 text

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


Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Where to start?

Slide 29

Slide 29 text

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.

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Path of least resistance Hit it were it don’t hurt Isolated Small Independent Utilities & Helpers

Slide 33

Slide 33 text

Preparations

Slide 34

Slide 34 text

Decide upfront How should you handle…

Slide 35

Slide 35 text

Decide upfront How should you handle… static java code
 
 
 
 
 
 
 


Slide 36

Slide 36 text

Decide upfront How should you handle… static java code
 null
 
 
 
 
 
 


Slide 37

Slide 37 text

Decide upfront How should you handle… static java code
 null
 extensions
 
 
 
 
 


Slide 38

Slide 38 text

Decide upfront How should you handle… static java code
 null
 extensions
 visibility modifiers
 
 
 
 


Slide 39

Slide 39 text

Decide upfront How should you handle… static java code
 null
 extensions
 visibility modifiers
 POJOs
 
 
 


Slide 40

Slide 40 text

Decide upfront How should you handle… static java code
 null
 extensions
 visibility modifiers
 POJOs
 singeltons
 
 


Slide 41

Slide 41 text

Decide upfront How should you handle… Differences Read up on the big differences and decide how you will tackle those challenges.

Slide 42

Slide 42 text

When you need more

Slide 43

Slide 43 text

Bits & Bytes Byte code In IntelliJ there’s a tool to see the byte code generated from your code. 


Slide 44

Slide 44 text

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.

Slide 45

Slide 45 text

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.

Slide 46

Slide 46 text

Let’s see some code

Slide 47

Slide 47 text

Code typealias

Slide 48

Slide 48 text

Code typealias function parameters

Slide 49

Slide 49 text

Code typealias function parameters

Slide 50

Slide 50 text

Code typealias function parameters static-ish

Slide 51

Slide 51 text

Code typealias function parameters static-ish

Slide 52

Slide 52 text

Code typealias function parameters static-ish

Slide 53

Slide 53 text

Code typealias function parameters static-ish

Slide 54

Slide 54 text

Code typealias function parameters static-ish singelton

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

To sum up

Slide 65

Slide 65 text

Take aways Differences Learn the differences - static
 - visibility
 - conventions
 - null
 - etc

Slide 66

Slide 66 text

Take aways Differences Pragmatic Set a solid goal!
 But be pragmatic about how to reach it.

Slide 67

Slide 67 text

Take aways Differences Pragmatic Iterate As in all development, but maybe more important in this case. Convert
 Refactor, Refactor, Refactor
 Repeat

Slide 68

Slide 68 text

Take aways Differences Pragmatic Iterate
 Cheat Decompile the kotlin byte code to see what to expect from java. There’s no need to guess.

Slide 69

Slide 69 text

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.

Slide 70

Slide 70 text

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.

Slide 71

Slide 71 text

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.

Slide 72

Slide 72 text

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.

Slide 73

Slide 73 text

Thank you!

Slide 74

Slide 74 text

Bob Dahlberg [email protected] @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.