带数据绑定的房间注释处理器 [英] Room annotation processor with Data binding

本文介绍了带数据绑定的房间注释处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在现有代码中使用了数据绑定,现在我要迁移到Room以获得持久性。
我已经按照 Florina的博客中提到的步骤进行操作对于房间

I have used Data binding in my existing code and now I am migrating to Room for persistence. I have followed the steps mentioned in Florina's Blog for room

当我删除房间依赖项时,我的代码构建良好,没有Java代码错误或BR相关错误

My Code builds fine without java code error or BR related error when I remove room dependency

 annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'

及其运行,但是给出了运行时异常,表明database_Impl不存在。

and its runs too, but gives Runtime exception saying database_Impl does not exists. As it couldn't generate that's file.

但是在我将注释处理器放回原位之后,它给了我

But after I put Annotation processor back, it give me

 Error:(29, 37) error: cannot find symbol class BR

我使用的gradle插件是 com.android.tools.build:gradle:3.0.1

My gradle plugin used is com.android.tools.build:gradle:3.0.1

似乎没有一起工作

到目前为止已采取的步骤:

Steps taken so far:


  1. 已更改BaseObservable到Observable 如此处建议

  2. 将Android Studio更新到3.0.1

  3. 使用gradle最新插件canary 6尝试

  4. 清除,清除缓存也已完成

  1. Changed BaseObservable to Observable As suggested here
  2. Updated Android Studio to 3.0.1
  3. Tried using gradle latest plugin canary 6
  4. Clear, Clear Cache also done

有人使用过Room和Data绑定在一起吗?

Has anyone used Room and Data binding together ?

推荐答案

经过4天的努力,我终于使我的代码正常运行。
解决

After 4 days of efforts I finally made my code run properly. Steps to solve the

数据绑定错误的步骤
错误:包com.packagename.databinding不存在
错误:找不到符号类CustomMainActivityBinding

Data binding error like error: package com.packagename.databinding does not exist error: cannot find symbol class CustomMainActivityBinding

应用gradle必须添加以下代码,以查看默认情况下出现的100多个错误

The app gradle must have below code added in order to view more than 100 errors that come by default

allprojects {
gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.compilerArgs << "-Xmaxerrs" << "4000"
        options.compilerArgs << "-Xmaxwarns" << "4000"
    }
  }
}

数据绑定和会议室拱门组件

Gradle dependencies for data binding and Room arch components

annotationProcessor 'com.android.databinding:compiler:3.0.1'

implementation 'android.arch.lifecycle:extensions:1.0.0'
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'

注意:Gradle插件版本为3.0.1

Note: Gradle plugin version is 3.0.1

我更改了所有虚拟机以实现Observable并调用

I changed my all VMs to implement Observable and call

registry.notifyChange(this, BR.bar);

在通知更改并实施覆盖方法的情况下

in case of notify change and also implement overridden methods

@Override
public void addOnPropertyChangedCallback(OnPropertyChangedCallback    
callback) {
registry.add(callback);
}

@Override
public void removeOnPropertyChangedCallback(
OnPropertyChangedCallback callback) {
registry.remove(callback);
}

这些东西构成了我的代码,但是当我解决了房间查询相关的错误。这是主要原因,原因是代码正在构建但未运行。当我再次重新购买我的项目时,我会看到这些错误。

These things made my code Build, but it run without exceptions when I solved the Room query related errors. Which was the main reason, code was building but not running. These errors I could see when I Rebuid my project again.

更新:

Android Studio 3.1.3之后 消息 窗口消失了,现在所有构建错误均显示在 Build 视图下。尽管可以使用切换功能来获取textview错误响应,但对于数据绑定错误而言,这还不够。

After Android studio 3.1.3, Message window is gone and now all build error appears under Build view. Although there is toggle available to get textview response of error, for data-binding errors it isn't sufficient.

对我有帮助的解决方案:

Solution that helped me:


  1. 在命令提示符/终端中导航到项目

  2. 如果使用Mac,请运行此命令 ./gradlew build --stacktrace,如果使用Windows,请运行 .\gradlew
    build --stacktrace。

  3. 现在搜索错误:标记,将显示编译时间错误。

我无法在IDE中出现这些错误。

I couldn't get these errors in IDE.

这篇关于带数据绑定的房间注释处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆