Slide 1

Slide 1 text

SELECT * FROM Kotlin @HandstandSam Sam Edwards Lead Android Engineer @ Capital One

Slide 2

Slide 2 text

@ H A N D S TA N D S A M # D C N Y C 1 9 WARNING: These slides were not meant to be standalone, but I wanted to share them. This talk heavily switches between Android Studio, Emulator and Chrome. Every time you see it means that I switched over and showed something else. The files are in https://github.com/handstandsam/ShoppingApp. The video will be better to watch when it is out, but it’s not published at this time. E X P O RT E D S L I D E S WA R N I N G Filename.kt

Slide 3

Slide 3 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Why Are We Talking About This Today? ! Kotlin for over 2 years and LOVE it ! Needed to evaluate databases ORMs ! Submitted this talk… and it got accepted ! So… I went a little overboard and decided to dive in heads first

Slide 4

Slide 4 text

!

Slide 5

Slide 5 text

!

Slide 6

Slide 6 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Coding Brings Confidence

Slide 7

Slide 7 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Sharing Brings Confidence

Slide 8

Slide 8 text

@ H A N D S TA N D S A M # D C N Y C 1 9 B E G I N N E R E X P E RT

Slide 9

Slide 9 text

@ H A N D S TA N D S A M # D C N Y C 1 9 B E G I N N E R E X P E RT

Slide 10

Slide 10 text

@ H A N D S TA N D S A M # D C N Y C 1 9 B E G I N N E R E X P E RT

Slide 11

Slide 11 text

@ H A N D S TA N D S A M # D C N Y C 1 9 B E G I N N E R E X P E RT

Slide 12

Slide 12 text

@ H A N D S TA N D S A M # D C N Y C 1 9 B E G I N N E R E X P E RT

Slide 13

Slide 13 text

@ H A N D S TA N D S A M # D C N Y C 1 9 B E G I N N E R E X P E RT

Slide 14

Slide 14 text

!

Slide 15

Slide 15 text

Pair Programming

Slide 16

Slide 16 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Shopping App on GitHub github.com/handstandsam/ShoppingApp

Slide 17

Slide 17 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Shopping App on GitHub github.com/handstandsam/ShoppingApp Shopping App!

Slide 18

Slide 18 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Our Task at Hand ! Make our Shopping Cart functionality persist between sessions as it should increase sales $$$ ! Our codebase has no database currently

Slide 19

Slide 19 text

# D C N Y C 1 9 @ H A N D S TA N D S A M In-Memory Version ! Use an In-Memory Version (No database, but can test functionality) ! Create Interfaces to allow for swapping of DB implementations ! Create a reactive stream that be subscribed to for updates

Slide 20

Slide 20 text

# D C N Y C 1 9 @ H A N D S TA N D S A M In-Memory Version ! Use an In-Memory Version (No database, but can test functionality) ! Create Interfaces to allow for swapping of DB implementations ! Create a reactive stream that be subscribed to for updates ShoppingCartDao.kt & InMemoryShoppingCartDao.kt

Slide 21

Slide 21 text

# D C N Y C 1 9 @ H A N D S TA N D S A M What Library Should We Use? Room SQLDelight

Slide 22

Slide 22 text

# D C N Y C 1 9 @ H A N D S TA N D S A M What Library Should We Use? Room SQLDelight Do We NEED a Local Database?

Slide 23

Slide 23 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Do We NEED a Local Database? ! Shared Preferences? ! Local JSON File ! On the Server?

Slide 24

Slide 24 text

# D C N Y C 1 9 @ H A N D S TA N D S A M With Great Power Comes Great Responsibility ! You are responsible for every byte of data you persist ! Any writes you make will need to be supported during any upgrade ! Migrations can be used to modify data structure

Slide 25

Slide 25 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Reactive Stream for UI Updates ! Shopping Cart reflect the most recent changes to the data. ! Options ! Register a Listener or a callback to our datastore ! RxJava - Flowable ! Android Architecture Components - LiveData ! Kotlin Coroutines - Channels or Flow

Slide 26

Slide 26 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Reactive Stream for UI Updates ! Shopping Cart reflect the most recent changes to the data. ! Options ! Register a Listener or a callback to our datastore ! RxJava - Flowable ! Android Architecture Components - LiveData ! Kotlin Coroutines - Channels or Flow ShoppingCart.kt

Slide 27

Slide 27 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Unit Tests ! Validate our current implementation ! Give confidence in our code

Slide 28

Slide 28 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Unit Tests ! Validate our current implementation ! Give confidence in our code ShoppingCartInMemoryTest.kt

Slide 29

Slide 29 text

Our Technical Requirements

Slide 30

Slide 30 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Our Technical Requirements ! Use a stable, proven version of a library ! Don’t break incremental builds ! Publish database changes reactively ! Make sure you separate code in modules: ! shopping-cart ! shopping-cart-sqldelight ! shopping-cart-room

Slide 31

Slide 31 text

Evaluating Database Libraries

Slide 32

Slide 32 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room SQLDelight Evaluating Database Libraries

Slide 33

Slide 33 text

# D C N Y C 1 9 @ H A N D S TA N D S A M ! Amazing online documentation ! Proven and written by Google ! Code Generation via Annotation Processing (kapt) breaks incremental builds. (Compiler option, off by default until stable) ! It’s last stable release was June 13 with 2.1.0, but 2.2.0-beta01 just came out on August 22 Room (JetPack Library)

Slide 34

Slide 34 text

Room

Slide 35

Slide 35 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Setup ! Gradle implementation Dependencies (For Runtime Logic) ! Gradle kapt Dependencies (For Annotation Processing)

Slide 36

Slide 36 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Setup ! Gradle implementation Dependencies (For Runtime Logic) ! Gradle kapt Dependencies (For Annotation Processing) build.gradle

Slide 37

Slide 37 text

# D C N Y C 1 9 @ H A N D S TA N D S A M ! Needed: Entity, Dao, Database Written Code for Room RoomItemInCartEntity RoomItemInCartDao RoomItemInCartDatabase

Slide 38

Slide 38 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room Generated Code RoomItemInCartDao_Impl RoomItemInCartDatabase_Impl

Slide 39

Slide 39 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Generated Code from Annotations is ! Annotations @Entity, @Dao, @Query(“…”) ! Deterministic generated code ! Can break incremental builds

Slide 40

Slide 40 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Using Room in Shopping App SessionGraph.kt

Slide 41

Slide 41 text

Validation/Introspection/Debugging

Slide 42

Slide 42 text

# D C N Y C 1 9 @ H A N D S TA N D S A M I ❤ Debugging Features

Slide 43

Slide 43 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Android Debug Database

Slide 44

Slide 44 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Android Debug Database ! https://github.com/amitshekhariitbhu/Android-Debug-Database ! debugImplementation 'com.amitshekhar.android:debug-db:1.0.6' ! Custom Port Config, forwarded from Emulator: ! debug { resValue("string", "PORT_NUMBER", “8081") } ! adb forward tcp:8081 tcp:8081 ! http://localhost:8081 ! Image: https://www.todayifoundout.com/wp-content/uploads/2017/11/rick- astley.png

Slide 45

Slide 45 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Testing Room ! Unit test must run on an Android device ! https://developer.android.com/training/data-storage/room/testing-db ! Can be run without UI ! ./gradlew shopping-cart-room:connectedAndroidTest --parallel

Slide 46

Slide 46 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Testing Room ! Unit test must run on an Android device ! https://developer.android.com/training/data-storage/room/testing-db ! Can be run without UI, so runs fast ! ./gradlew shopping-cart-room:connectedAndroidTest --parallel RoomShoppingCartTest.kt

Slide 47

Slide 47 text

SQLDelight

Slide 48

Slide 48 text

# D C N Y C 1 9 @ H A N D S TA N D S A M ! Generates Kotlin code as of v1.0.0 ! Is a stable release ! Does incremental code generation from SQL files, without using annotations (kapt) ! Supports Kotlin Multiplatform SQLDelight (Written by Square)

Slide 49

Slide 49 text

# D C N Y C 1 9 @ H A N D S TA N D S A M SQLDelight ! Setup ! Gradle Plugin (To Perform Code Generation) ! Gradle implementation Dependencies (For Runtime Logic)

Slide 50

Slide 50 text

# D C N Y C 1 9 @ H A N D S TA N D S A M SQLDelight ! Setup ! Gradle Plugin (To Perform Code Generation) ! Gradle implementation Dependencies (For Runtime Logic) build.gradle

Slide 51

Slide 51 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Written SQL for SQLDelight ItemInCartEntity.sq

Slide 52

Slide 52 text

# D C N Y C 1 9 @ H A N D S TA N D S A M SQLDelight Code Generation ! Leverages Gradle Plugin ! Detects changes in the .sq files and will regenerate if needed ! Provides incremental builds

Slide 53

Slide 53 text

# D C N Y C 1 9 @ H A N D S TA N D S A M ItemInCart.kt ItemInCartEntityQueries.kt
 DatabaseImpl.kt Generated Code for SQLDelight

Slide 54

Slide 54 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Running SQLDelight SessionGraph.kt

Slide 55

Slide 55 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Showing SQLDelight Works Android Debug Database

Slide 56

Slide 56 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Unit Testing ! JVM Driver allowing it to run on the JVM without any Android dependencies. ! Fast TDD development ! ./gradlew shopping-cart-sqldelight:testDebug

Slide 57

Slide 57 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Unit Testing ! JVM Driver allowing it to run on the JVM without any Android dependencies. ! Fast TDD development ! ./gradlew shopping-cart-sqldelight:testDebug SqlDelightShoppingCartTestDelegate.kt

Slide 58

Slide 58 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Kotlin Multiplatform ! https://github.com/square/sqldelight/tree/master/sample ! Android ! iOS

Slide 59

Slide 59 text

There Is No Magic

Slide 60

Slide 60 text

# D C N Y C 1 9 @ H A N D S TA N D S A M AndroidX SupportSQLiteDatabase ! “A database abstraction which removes the framework dependency and allows swapping underlying sql versions. It mimics the behavior of SQLiteDatabase” ! https://developer.android.com/reference/androidx/sqlite/db/ SupportSQLiteDatabase

Slide 61

Slide 61 text

@ H A N D S TA N D S A M # D C N Y C 1 9

Slide 62

Slide 62 text

@ H A N D S TA N D S A M # D C N Y C 1 9

Slide 63

Slide 63 text

@ H A N D S TA N D S A M # D C N Y C 1 9 SQLDelight Room

Slide 64

Slide 64 text

@ H A N D S TA N D S A M # D C N Y C 1 9 SQLDelight Room

Slide 65

Slide 65 text

@ H A N D S TA N D S A M # D C N Y C 1 9 SQLDelight

Slide 66

Slide 66 text

And The Winner Is…

Slide 67

Slide 67 text

# D C N Y C 1 9 @ H A N D S TA N D S A M https://handstandsam.com/2019/03/10/it-depends-is-the-answer-to-your-android-question/

Slide 68

Slide 68 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 69

Slide 69 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 70

Slide 70 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 71

Slide 71 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 72

Slide 72 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 73

Slide 73 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 74

Slide 74 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 75

Slide 75 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 76

Slide 76 text

# D C N Y C 1 9 @ H A N D S TA N D S A M Room ! Uses SupportSQLiteDatabase ! Maintained by Google ! Write some SQL in Annotations ! KAPT Annotation Processing ! Great Documentation ! Hard to read generated code ! Tried and Tested ! Requires testing on Device SQLDelight ! Uses SupportSQLiteDatabase ! Maintained by Square ! Write your own SQL ! Code Generation from SQL ! Not Great Android Documentation ! Super clean generated code ! Not widely adopted (yet) ! JVM Unit Tests

Slide 77

Slide 77 text

THE END