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.

5701f31a8433a22ae736282de8d08cd6?s=128

Sam Edwards

August 27, 2019
Tweet

Transcript

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

    @ Capital One
  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
  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
  4. !

  5. !

  6. # D C N Y C 1 9 @ H

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

    A N D S TA N D S A M Sharing Brings Confidence
  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
  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
  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
  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
  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
  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
  14. !

  15. Pair Programming

  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
  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!
  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
  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
  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
  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
  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?
  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?
  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
  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
  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
  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
  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
  29. Our Technical Requirements

  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
  31. Evaluating Database Libraries

  32. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Room SQLDelight Evaluating Database Libraries
  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)
  34. Room

  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)
  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
  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
  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
  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
  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
  41. Validation/Introspection/Debugging

  42. # D C N Y C 1 9 @ H

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

    A N D S TA N D S A M Android Debug Database
  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
  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
  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
  47. SQLDelight

  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)
  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)
  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
  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
  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
  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
  54. # D C N Y C 1 9 @ H

    A N D S TA N D S A M Running SQLDelight SessionGraph.kt
  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
  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
  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
  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
  59. There Is No Magic

  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
  61. @ H A N D S TA N D S

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

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

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

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

    A M # D C N Y C 1 9 SQLDelight
  66. And The Winner Is…

  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/
  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
  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
  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
  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
  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
  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
  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
  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
  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
  77. THE END