Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When / how is the Room schema file being generated?

Tags:

Which gradle task / Studio feature generates the Room schema file? Are there any circumstances in which the file generation is skipped?

Weeks ago I made changes which should have changed the schema file, but the file was not changed. Now I made a new change (deleted an entity, including the entity's reference in the Room database class) and now all the changes appeared in the schema file.

-> Why was the schema file generated now, but not in one of the many builds in the last days / weeks?

The schema seems to get generated more reliably when I delete it before building the project. But that obviously is not mandatory because it also worked when I deleted that entity today...

I read this question, but I already have the following lines in my build.gradle:

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
    def room_version = "2.1.0"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
like image 912
WhiteIron Avatar asked Jul 03 '19 09:07

WhiteIron


People also ask

How does room migration work?

Stay organized with collections Save and categorize content based on your preferences. As you add and change features in your app, you need to modify your Room entity classes and underlying database tables to reflect these changes.

How do you populate data in a room database?

To pre-populate a Room database, register a RoomDatabase#Callback in the database builder. Override the Callback#onCreate method to insert the data.

How can we check data is inserted or not in room database?

Go to view> Tools Window> Device File explorer and download the instance of your DB.

When using room a table is defined using a class marked with which annotation?

You define each Room entity as a class that is annotated with @Entity . A Room entity includes fields for each column in the corresponding table in the database, including one or more columns that comprise the primary key.


1 Answers

Room will generate a new schema JSON file if it 'sees' you changed something in your @Entity class that might lead to a new schema and/or if you change your something in your @Database, such as updating the version, adding or removing entities, etc.

For example, renaming a field in a @Entity annotated class should cause the file to be generated. Meanwhile adding a new field with @Ignore should not.

The Gradle task that actually generates the schema is compileDebugJava or kaptDebugKotlin if in Kotlin (both for the debug variant). Room is an annotation processor so it does its work during compilation, analyzing your code, generating new code and generating the schema JSON files.

like image 62
DanyBoricua Avatar answered Sep 30 '22 20:09

DanyBoricua