Include (but don’t apply) in project-level build configuration
Reference this plugin in the plugins block of the project-level build.grade or build.grade.kts file of an Android project—but without applying it. For example:
Kotlin
plugins {
// ...
id("com.nickbenn.room-schema-parser") version "2.0.0" apply false
// ...
}
Groovy
plugins {
// ...
id 'com.nickbenn.room-schema-parser' version '2.0.0' apply false
// ...
}
Include (and apply) the plugin in the app-level build configuration
Reference this plugin in the plugins section of the app- or library-level build.gradle or build.gradle.kts file:
Kotlin
plugins {
// ...
id("com.nickbenn.room-schema-parser") version "2.0.0"
// ...
}
Groovy
plugins {
// ...
id 'com.nickbenn.room-schema-parser' version '2.0.0'
// ...
}
Configure the plugin
In the app- or library-level build.gradle file, include a roomDdl section with source and destination properties:
sourceFilespecifying the path to the schema file generated by Room. The first part of this path should match the value corresponding to theroom.schemaLocationkey of the Room-related annotation processor options.Within the
schemasdirectory, the Room annotation processor creates—as part of the build process—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 forsourcemust refer to that file.destinationFilespecifying the path to the output file for the extracted DDL. Defaults to"build/ddl/ddl.sql", relative to the Android subproject directory. Any directories included (implicitly or explicitly) will be created as needed; if such a directory can’t 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/com.nickbenn.myapp.database.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:
Kotlin
roomDdl {
source = project.file("schemas/com.nickbenn.myapp.database.MyDatabase/1.json")
destination = project.file("../docs/sql/ddl.sql")
}
Groovy
roomDdl {
source = project.file('schemas/com.nickbenn.myapp.database.MyDatabase/1.json')
destination = project.file('../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 the command line:
./gradlew extractRoomDdl