Room Requirements

To use Room, you need two dependencies in your module’s build.gradle file:

  1. The runtime library
  2. An annotation processor

In a Kotlin project, those will be:

For example, in the NoteBasics module of the book’s primary sample project, we have a build.gradle file that pulls in those two artifacts:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 31

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 30
      testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "androidx.appcompat:appcompat:1.3.1"
    implementation "androidx.core:core-ktx:1.6.0"
    implementation "androidx.constraintlayout:constraintlayout:2.1.1"
    implementation "androidx.room:room-ktx:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    androidTestImplementation "androidx.test.ext:junit:1.1.3"
    androidTestImplementation 'androidx.test:runner:1.4.0'
    androidTestImplementation "androidx.arch.core:core-testing:2.1.0"
    androidTestImplementation "com.natpryce:hamkrest:1.7.0.0"
}

Note that Room has a minSdkVersion requirement of API Level 15 or higher. If you attempt to build with a lower minSdkVersion, you will get a build error. If you try to override Room’s minSdkVersion using manifest merger elements, while the project will build, expect Room to crash horribly.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.