I am using Android Database Component Room
I've configured everything, but when I compile, Android Studio gives me this warning:
Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide
room.schemaLocation
annotation processor argument OR set exportSchema to false.
As I understand it is the location where DB file will be located
How can it affect my app? What is the best practice here? Should I use the default location (false
value)?
This question is related to
java
android
database
android-room
If like me you recently moved certain classes to different packages ect. and you use android navigation. Make sure to change the argType to you match you new package address. from:
app:argType="com.example.app.old.Item"
to:
app:argType="com.example.app.new.Item"
In the build.gradle
file for your app module, add this to the defaultConfig
section (under the android
section). This will write out the schema to a schemas
subfolder of your project folder.
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
Like this:
// ...
android {
// ... (compileSdkVersion, buildToolsVersion, etc)
defaultConfig {
// ... (applicationId, miSdkVersion, etc)
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
// ... (buildTypes, compileOptions, etc)
}
// ...
I use .kts
Gradle files (Kotlin Gradle DSL) and the kotlin-kapt
plugin but I still get a script compilation error when I use Ivanov Maksim's answer.
Unresolved reference: kapt
For me this was the only thing which worked:
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = mapOf("room.schemaLocation" to "$projectDir/schemas")
}
}
}
}
Probably you didn't add your room class to child RoomDatabase
child class in @Database(entities = {your_classes})
Kotlin? Here we go:
android {
// ... (compileSdkVersion, buildToolsVersion, etc)
defaultConfig {
// ... (applicationId, miSdkVersion, etc)
kapt {
arguments {
arg("room.schemaLocation", "$projectDir/schemas")
}
}
}
buildTypes {
// ... (buildTypes, compileOptions, etc)
}
}
//...
Don't forget about plugin:
apply plugin: 'kotlin-kapt'
For more information about kotlin annotation processor please visit: Kotlin docs
Above answers are correct. This version is easy to follow:
Because "Schema export directory is not provided to the annotation processor", So we need to provide the directory for schema export:
Step [1] In your file which extends the RoomDatabase, change the line to:
`@Database(entities = ???.class,version = 1, exportSchema = true)`
Or
`@Database(entities = ???.class,version = 1)`
(because the default value is always true)
Step [2] In your build.gradle(project:????) file, inside the defaultConfig{ } (which is inside android{ } big section), add the javaCompileOptions{ } section, it will be like:
android{
defaultConfig{
//javaComplieOptions SECTION
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":"$projectDir/schemas".toString()]
}
}
//Other SECTION
...
}
}
$projectDir:is a variable name, you cannot change it. it will get your own project directory
schemas:is a string, you can change it to any you like. For example:
"$projectDir/MyOwnSchemas".toString()
@mikejonesguy answer is perfect, just in case you plan to test room migrations (recommended), add the schema location to the source sets.
In your build.gradle file you specify a folder to place these generated schema JSON files. As you update your schema, you’ll end up with several JSON files, one for every version. Make sure you commit every generated file to source control. The next time you increase your version number again, Room will be able to use the JSON file for testing.
- Florina Muntenescu (source)
build.gradle
android {
// [...]
defaultConfig {
// [...]
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
// add the schema location to the source sets
// used by Room, to test migrations
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
// [...]
}
Source: Stackoverflow.com