Java Room - 架构导出目录未提供给注释处理器,因此我们无法导出架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44322178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:30:05  来源:igfitidea点击:

Room - Schema export directory is not provided to the annotation processor so we cannot export the schema

javaandroiddatabaseandroid-room

提问by Misha Akopov

I am using Android Database Component Room

我正在使用 Android 数据库组件室

I've configured everything, but when I compile, Android Studio gives me this warning:

我已经配置了一切,但是当我编译时,Android Studio 给了我这个警告:

Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocationannotation processor argument OR set exportSchema to false.

模式导出目录未提供给注释处理器,因此我们无法导出模式。您可以提供 room.schemaLocation注释处理器参数或将 exportSchema 设置为 false。

As I understand it is the location where DB file will be located

据我了解,这是 DB 文件所在的位置

How can it affect my app? What is the best practice here? Should I use the default location (falsevalue)?

它如何影响我的应用程序?这里的最佳做法是什么?我应该使用默认位置(false值)吗?

采纳答案by DoruChidean

As per the docs:

根据文档

You can set annotation processor argument (room.schemaLocation) to tell Room to export the schema into a folder. Even though it is not mandatory, it is a good practice to have version history in your codebase and you should commit that file into your version control system (but don't ship it with your app!).

您可以设置注释处理器参数 (room.schemaLocation) 来告诉 Room 将架构导出到文件夹中。尽管这不是强制性的,但在您的代码库中包含版本历史记录是一个很好的做法,您应该将该文件提交到您的版本控制系统中(但不要随您的应用程序一起提供!)。

So if you don't need to check the schema and you want to get rid of the warning, just add exportSchema = falseto your RoomDatabase, as follows.

因此,如果您不需要检查架构并且想要摆脱警告,只需添加exportSchema = false到您的RoomDatabase, 如下所示。

@Database(entities = { YourEntity.class }, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {
   //...
}

If you follow @mikejonesguy answerbelow, you will follow the good practice mentioned in the docs :). Basically you will get a .jsonfile in your ../app/schemas/folder. And it looks something like this:

如果您遵循下面的@mikejonesguy答案,您将遵循文档中提到的良好做法:)。基本上你会.json在你的../app/schemas/文件夹中得到一个文件。它看起来像这样:

{
  "formatVersion": 1,
  "database": {
    "version": 1,
    "identityHash": "53db508c5248423325bd5393a1c88c03",
    "entities": [
      {
        "tableName": "sms_table",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `message` TEXT, `date` INTEGER, `client_id` INTEGER)",
        "fields": [
          {
            "fieldPath": "id",
            "columnName": "id",
            "affinity": "INTEGER"
          },
          {
            "fieldPath": "message",
            "columnName": "message",
            "affinity": "TEXT"
          },
          {
            "fieldPath": "date",
            "columnName": "date",
            "affinity": "INTEGER"
          },
          {
            "fieldPath": "clientId",
            "columnName": "client_id",
            "affinity": "INTEGER"
          }
        ],
        "primaryKey": {
          "columnNames": [
            "id"
          ],
          "autoGenerate": true
        },
        "indices": [],
        "foreignKeys": []
      }
    ],
    "setupQueries": [
      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"53db508c5248423325bd5393a1c88c03\")"
    ]
  }
}

If my understanding is correct, you will get such a file with every database version update, so that you can easily follow the history of your db.

如果我的理解是正确的,你会在每次数据库版本更新时得到这样一个文件,这样你就可以轻松地跟踪你的数据库历史。

回答by mikejonesguy

In the build.gradlefile for your app module, add this to the defaultConfigsection (under the androidsection). This will write out the schema to a schemassubfolder of your project folder.

build.gradle应用程序模块的文件中,将此添加到defaultConfig部分(在该android部分下)。这会将架构写出到schemas项目文件夹的子文件夹中。

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)

}

// ...

回答by Ivanov Maksim

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'

apply plugin: 'kotlin-kapt'

For more information about kotlin annotation processor please visit: Kotlin docs

有关 kotlin 注释处理器的更多信息,请访问: Kotlin 文档

回答by chebaby

@mikejonesguy answeris perfect, just in case you plan to test room migrations (recommended), add the schema location to the source sets.

@mikejonesguy答案很完美,以防万一您计划测试房间迁移(推荐),请将架构位置添加到源集。

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.

在您的 build.gradle 文件中,您指定一个文件夹来放置这些生成的架构 JSON 文件。当您更新架构时,您最终会得到多个 JSON 文件,每个版本一个。确保将每个生成的文件提交到源代码管理。下次您再次增加版本号时,Room 将能够使用 JSON 文件进行测试。

  • Florina Muntenescu(来源

build.gradle

构建.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())
    }

    // [...]
}

回答by LunaRivolxoxo

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:

因为“Schema导出目录没有提供给注解处理器”,所以我们需要提供schema导出的目录:

Step [1] In your file which extends the RoomDatabase, change the line to:

步骤 [1] 在扩展 RoomDatabase 的文件中,将行更改为:

`@Database(entities = ???.class,version = 1, exportSchema = true)`

Or

或者

`@Database(entities = ???.class,version = 1)` 

(because the default value is always true)

(因为默认值总是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:

步骤 [2] 在您的 build.gradle(project:????) 文件中,在defaultConfig{ }(在android{ }大部分)中,添加javaCompileOptions{ }部分,它将如下所示:

         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

$projectDir: 是一个变量名,你不能改变它。它将获得您自己的项目目录

schemas:is a string, you can change it to any you like. For example: "$projectDir/MyOwnSchemas".toString()

schemas:是一个字符串,您可以将其更改为您喜欢的任何内容。例如: "$projectDir/MyOwnSchemas".toString()

回答by jsa

I use .ktsGradle files (Kotlin Gradle DSL) and the kotlin-kaptplugin but I still get a script compilation error when I use Ivanov Maksim's answer.

我使用.ktsGradle 文件(Kotlin Gradle DSL)和kotlin-kapt插件,但是当我使用 Ivanov Maksim 的答案时,我仍然遇到脚本编译错误。

Unresolved reference: kapt

For me this was the only thing which worked:

对我来说,这是唯一有效的方法:

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = mapOf("room.schemaLocation" to "$projectDir/schemas")
            }
        }
    }
}

回答by Max Zonov

Probably you didn't add your room class to child RoomDatabasechild class in @Database(entities = {your_classes})

可能您没有将您的房间类添加到RoomDatabase子类中@Database(entities = {your_classes})