Link Search Menu Expand Document

Declare dependency in build.gradle

Reference the plugin in the dependencies task of the buildscript section, in the project-level build.grade file of an Android project, e.g.

buildscript {
    // ...
    dependencies {
        // ... 
        classpath 'com.nickbenn:room-schema-parser-plugin:1.0.0'
    }
}

Include the plugin in the build script

Add the plugin to the plugins section of the app- or library-level build.gradle file. For an app project, this would look something like this:

plugins {
    id 'com.android.application'
    // ...
    id 'com.nickbenn.room-schema-parser'
}

Configure the plugin

In the app- or library-level build.gradle file, include a roomDdl section with source and destination properties:

source

String specifying the path to the schema file generated by Room. The first part of this path is the same as that specified as the value corresponding to the room.schemaLocation key of the arguments property in android.defaultConfig.javaCompileOptions.annotationProcessorOptions, e.g.

  'room.schemaLocation': "$projectDir/schemas".toString()

Within the schemas directory, the Room annotation processor creates (when building) a directory named with the fully qualified class name of the database class; inside that directory, room creates a JSON file, named according to the version number of the database. The value specified for source must refer to that file.

destination

String specifying the path to the output file for the extracted DDL. Defaults to "$projectDir/build/ddl/ddl.sql". Any directories included (implicitly or explicitly) will be created, if necessary; if such a directory cannot be created, or if the file itself cannot be written to, the task will fail with an exception.

Example

For example, the following roomDdl section specifies that the JSON schema file generated by Room can be found in the schemas/edu.cnm.deepdive.myproject.service.MyDatabase subdirectory of the app module, in the 1.json file, and that the extracted DDL should be written to the ddl.sql file in the docs/sql subdirectory of the parent directory of the app module:

roomDdl {
    source "$projectDir/schemas/edu.cnm.deepdive.myproject.service.MyDatabase/1.json"
    destination "$projectDir/../docs/sql/ddl.sql"
}

Execute the task

After loading the Gradle build script changes, the extractRoomDdl task will be available for execution—either from the Gradle tool window of Android Studio or IntelliJ IDEA, or from from the command line:

./gradlew extractRoomDdl

Alternatively, by modifying build.gradle or making the appropriate selections in the Gradle tool window of Android, extractRoomDdl can be set to run automatically, before or after another Gradle task. However, this isn’t recommended in most cases, since this will generally result in unnecessary re-execution of the extractRoomDdl task if the output file has been modified—e.g. by using the Code/Reformat Code option to format the DDL.