Slide 1

Slide 1 text

Kotlin & RxJava in Android project One year later Anton Rutkevich, Juno Igor Korotenko, Juno

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Contents 1. Stack benefits 2. Interop 3. Challenges

Slide 4

Slide 4 text

Stack benefits

Slide 5

Slide 5 text

What’s in the stack?

Slide 6

Slide 6 text

What’s is the stack?

Slide 7

Slide 7 text

Core ideas

Slide 8

Slide 8 text

Core ideas 1. Safety a. Immutability b. Easy concurrency 2. Concise code 3. Modularity (SOLID-inspired, Clean architecture) 4. Test-ready

Slide 9

Slide 9 text

Safety. Immutability

Slide 10

Slide 10 text

Why immutable? 1. No state - simpler program a. “Simple made easy” talk by Rich Hickey 2. Mutable state is the core of concurrency issues 3. Immutability allows to perform some optimizations

Slide 11

Slide 11 text

Immutability is transitive Reference Data Collection

Slide 12

Slide 12 text

Reference immutability

Slide 13

Slide 13 text

Objects immutability. Data classes

Slide 14

Slide 14 text

Collections immutability

Slide 15

Slide 15 text

Rx 1. Implicitly relies on immutable data in streams 2. Hides the state in operators & subjects

Slide 16

Slide 16 text

Safety. Easy concurrency

Slide 17

Slide 17 text

Concurrency is hard

Slide 18

Slide 18 text

Rx approach to concurrency

Slide 19

Slide 19 text

Safety. Other

Slide 20

Slide 20 text

Safe by default 1. Immutability by default 2. Classes & methods are final by default 3. ...

Slide 21

Slide 21 text

Null safety

Slide 22

Slide 22 text

Advanced type safety. Why? More checks are guaranteed at compile time

Slide 23

Slide 23 text

Type safety. Sum types 1. TimeDiff

Slide 24

Slide 24 text

Why sum types? Example Long Long Long if (timeDiff != -1) if (timeDiff != -1) TimeDiff if (timeDiff is TimeDiff.Known) TimeDiff.Known

Slide 25

Slide 25 text

Type safety. Sum types 2. Network response

Slide 26

Slide 26 text

Concise code

Slide 27

Slide 27 text

Smart casts

Slide 28

Slide 28 text

Stream-like API

Slide 29

Slide 29 text

Easy to understand complex data-flow

Slide 30

Slide 30 text

Modularity. Architecture

Slide 31

Slide 31 text

High-level view on architecture ViewModel View starts here Retrofit BL home SOL ID Dagger 2 helps here Service A Service B Service D Networking ends here

Slide 32

Slide 32 text

Testability

Slide 33

Slide 33 text

Main ideas 1. Separation of concerns (clean architecture principles) 2. Dependencies of SUT are interfaces 3. Business logic is isolated from platform code 4. UI layer can be replaced for tests

Slide 34

Slide 34 text

Kotlin

Slide 35

Slide 35 text

Spek

Slide 36

Slide 36 text

Interop

Slide 37

Slide 37 text

Koltin interop’s rule of thumb 1. No black magic -> works fine 2. Black magic -> most probably works fine, but need to re-check

Slide 38

Slide 38 text

Koltin + Dagger 2 Initially DI code was in Java Now, with kapt, DI can also be in Kotlin

Slide 39

Slide 39 text

Koltin + Retrofit Just works :)

Slide 40

Slide 40 text

Koltin + Rx Works even better than Java + RxJava 1. Lambdas (non-capturing) 2. Extension functions

Slide 41

Slide 41 text

Koltin + Gson Gson uses reflection to set field values -> can set null into non-nullable field. Beware!

Slide 42

Slide 42 text

Challenges

Slide 43

Slide 43 text

Testing. Koltin + Mockito 1. Everything is final by default in Kotlin a. Solution 1: PowerMock. Dirty b. Solution 2: Clean architecture. Clean 2. Mockito returns nulls where Koltin expects non-nullable a. Use Mockito-Kotlin library or write your own matchers

Slide 44

Slide 44 text

Java -> Kotlin interop Beware of nulls that come from the Java’s dark side String!

Slide 45

Slide 45 text

Compile time 1. Was kind of a problem: a. Clean ~3 min b. Incremental ~55 sec c. Of which compile Kotlin ~50 sec 2. Much better with Koltin 1.0.2 & dex in process a. Clean ~2m b. Incremental ~30 sec c. Of which compile Kotlin clean ~50 sec, incremental ~20 sec

Slide 46

Slide 46 text

Method count 1. Kotlin ~6K 2. RxJava ~5K 3. Multidex works

Slide 47

Slide 47 text

Proguard Works, causes ~ same amount of issues as in Java

Slide 48

Slide 48 text

Summay

Slide 49

Slide 49 text

Core benefits 1. Runtime checks go to compile time 2. Kindly pushes you to write better code 3. Expressive code

Slide 50

Slide 50 text

Challenges 1. Testing -> solved 2. Compile time -> not significant 3. Java interop -> be careful

Slide 51

Slide 51 text

Be a stream

Slide 52

Slide 52 text

Questions?