$30 off During Our Annual Pro Sale. View Details »

SELECT * FROM Kotlin - Droidcon NYC 2019

SELECT * FROM Kotlin - Droidcon NYC 2019

Now that Kotlin is the preferred language of Android developers, what is the best library to work with SQLite databases using Kotlin? Room just released support for Kotlin Coroutines, and SqlDelight released support for Kotlin Multi-platform. Room has great documentation and hides a lot of implementation detail, while SqlDelight lets you write raw SQL statements with an Android Studio plugin and then generates type-safe code for you. In this session, you'll see a rundown of these two libraries, along with live coding demos to help you decide which library sounds right for you in this Kotlin world.

Sam Edwards

August 27, 2019
Tweet

More Decks by Sam Edwards

Other Decks in Programming

Transcript

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

    View Slide

  2. @ 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

    View Slide

  3. # 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

    View Slide

  4. !

    View Slide

  5. !

    View Slide

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

    View Slide

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

    View Slide

  8. @ 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

    View Slide

  9. @ 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

    View Slide

  10. @ 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

    View Slide

  11. @ 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

    View Slide

  12. @ 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

    View Slide

  13. @ 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

    View Slide

  14. !

    View Slide

  15. Pair Programming

    View Slide

  16. # 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

    View Slide

  17. # 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!

    View Slide

  18. # 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

    View Slide

  19. # 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

    View Slide

  20. # 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

    View Slide

  21. # 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

    View Slide

  22. # 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?

    View Slide

  23. # 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?

    View Slide

  24. # 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

    View Slide

  25. # 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

    View Slide

  26. # 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

    View Slide

  27. # 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

    View Slide

  28. # 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

    View Slide

  29. Our Technical Requirements

    View Slide

  30. # 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

    View Slide

  31. Evaluating Database Libraries

    View Slide

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

    View Slide

  33. # 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)

    View Slide

  34. Room

    View Slide

  35. # 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)

    View Slide

  36. # 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

    View Slide

  37. # 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

    View Slide

  38. # 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

    View Slide

  39. # 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

    View Slide

  40. # 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

    View Slide

  41. Validation/Introspection/Debugging

    View Slide

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

    View Slide

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

    View Slide

  44. # 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

    View Slide

  45. # 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

    View Slide

  46. # 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

    View Slide

  47. SQLDelight

    View Slide

  48. # 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)

    View Slide

  49. # 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)

    View Slide

  50. # 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

    View Slide

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

    View Slide

  52. # 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

    View Slide

  53. # 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

    View Slide

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

    View Slide

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

    View Slide

  56. # 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

    View Slide

  57. # 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

    View Slide

  58. # 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

    View Slide

  59. There Is No Magic

    View Slide

  60. # 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  66. And The Winner Is…

    View Slide

  67. # 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/

    View Slide

  68. # 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

    View Slide

  69. # 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

    View Slide

  70. # 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

    View Slide

  71. # 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

    View Slide

  72. # 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

    View Slide

  73. # 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

    View Slide

  74. # 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

    View Slide

  75. # 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

    View Slide

  76. # 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

    View Slide

  77. THE END

    View Slide