Create a Database

The first part of the process here is going to be very familiar if you’ve gone through Chapter 5, Persist Game Data with Room, so I’ll try to speed through things to get to the new stuff.

Before you do anything, add in the Room dependencies:

»implementation ​"androidx.room:room-ktx:$room_version"
»implementation ​"androidx.room:room-runtime:$room_version"
 implementation ​"com.google.android.material:material:$material_version"
 
»kapt ​"androidx.room:room-compiler:$room_version"

Then, inside a new data package, create an abstract BaseballDatabase class that inherits from RoomDatabase. It’ll start out pretty empty, but we’ll go through and add pieces in as we go along. For now, it looks like this:

 @Database(
  entities ...

Get Kotlin and Android Development featuring Jetpack now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.