SQLDelight in Android: Getting Started

Aug 3 2021 · Kotlin 1.4, Android 11, Android Studio 4.1

Part 1: Preparation & Setup

01. Prepare Your Project

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Next episode: 02. Configure SQLDelight

Notes: 01. Prepare Your Project

The course won’t go into a lot of detail regarding common SQL Syntax not specific to the SQLDelight library. To refresh your memory, the W3Schools SQL Tutorial might be a good place to start.

Transcript: 01. Prepare Your Project

SQLDelight is a multi-platform library for generating typesafe Kotlin APIs by Cash App. It helps with a common problem that many developers face: Managing an SQL database.

SQLDelight is a code generator.

Basically, we are going to write actual SQL statements for our database and the library will then transform them into classes and methods that we can call from Kotlin. Have a look here: There is an identifier on the left - findById - and there is a corresponding method in Kotlin with the same name.

SQLDelight works across many different dialects of SQL, we get compile-time validation of schemas and migrations, and it integrates nicely into our favorite IDE.

We are going to work on an existing application for bug collectors. No no no, wait wait wait, not those kinds of bugs - these ones here!

Users will be able to create and manage collections and the bugs within them through a simplified user interface, so that they can become the best there ever was. The app is built on top of a basic MVVM rchitecture with ViewModels and a few screens, but our focus is going to be the persistence layer backed by SQLDelight.

Let’s get our development environment set up for it - it’s just a few steps! I will be using Android Studio 4.1 for this, but any recent version will do.

First up, we will install the SQLDelight IDE plugin and add it to our Android Studio. This will give us auto-completion and syntax highlighting for the scripts that will make up our database.

After opening Android Studio, open its Preferences through the Configure button and then go to “Plugins”. Switch to the Marketplace tab on the top and in the search box, type in “sqldelight”.

Finally, install the plugin of the same name - you can see here that it’s the real deal since it’s being published by Square Inc, the makers of Cash App. We click Install and restart the IDE after the installation to complete it.

Next, we will add the Gradle plugin and library for SQLDelight to our starter project. After opening the project, open its build.gradle file and find the buildscript block.

We want to keep the different parts of the library in sync with each other, so let’s define a variable for the version number here and reuse it throughout the rest of this setup.

Inside the buildscript block, create an ext block (unless there is one already) and add a variable for the version number there. At the time of recording, the latest version is 1.5.0, but please check the SQLDelight website for updates, just to be safe.

buildscript {
    ext {
        sqldelightVersion = "1.5.0"
    }
}

Now, let’s add a classpath dependency to the Gradle plugin for SQLDelight to our buildscript. Down here in the dependencies is where we can put that in.

dependencies {
    classpath("com.squareup.sqldelight:gradle-plugin:$sqldelightVersion")
}

This plugin is what’s going to connect the generated code to the rest of your app so you can call it. Write out the dependency coordinates like so and make use of the version number variable from above.

Finally, we can apply this Gradle plugin to our app module and add the client library to it as well. Navigate to the build.gradle file of your app module and apply the SQLDelight Gradle plugin at the top. The order doesn’t matter usually but I like to add it below the Android and Kotlin plugins!

apply plugin: "com.squareup.sqldelight"

Okay, one more step: Let’s scroll down to the dependencies block of our app and add the Android Driver library with this statement. As you know, SQLDelight works on many different platforms and the Android Driver is specialized for integration with the Android world - you know, like, Contexts and so on.

dependencies {
    // SQLDelight
    implementation "com.squareup.sqldelight:android-driver:$sqldelightVersion"
}

After adding everything, hit the Sync button to configure your project again and you should be good to go!

Click the Run button on the top to launch the app - the UI is already built, but most of the logic won’t work just yet. I mean, we don’t have a database behind here after all. Well, I guess we have some work ahead of us, don’t we?