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

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

问题描述

我在现有代码中使用了数据绑定,现在我正在迁移到 Room 进行持久化.我已按照 Florina 的博客 中提到的步骤操作>

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

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

并且它也能运行,但给出运行时异常,说 database_Impl 不存在.因为它无法生成那个文件.

但在我放回注解处理器后,它给了我

 错误:(29, 37) 错误:找不到符号类 BR

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

他们似乎不能一起工作

目前采取的步骤:

  1. 将 BaseObservable 更改为 Observable 作为此处建议
  2. 将 Android Studio 更新至 3.0.1
  3. 尝试使用 gradle 最新插件金丝雀 6
  4. 清除,清除缓存也完成

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

解决方案

经过 4 天的努力,我的代码终于可以正常运行了.解决步骤

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

应用程序 gradle 必须添加以下代码才能查看默认出现的 100 多个错误

所有项目{gradle.projectsEvaluated {任务.withType(JavaCompile){options.compilerArgs <<-Xmaxerrs"<<4000"options.compilerArgs <<-Xmaxwarns"<<4000"}}}

数据绑定和 Room arch 组件的 Gradle 依赖

annotationProcessor 'com.android.databinding:compiler:3.0.1'实现 'android.arch.lifecycle:extensions:1.0.0'实现 '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

我改变了我所有的虚拟机来实现 Observable 并调用

registry.notifyChange(this, BR.bar);

如果通知更改并实现覆盖方法

@Overridepublic void addOnPropertyChangedCallback(OnPropertyChangedCallback打回来) {注册表添加(回调);}@覆盖public void removeOnPropertyChangedCallback(OnPropertyChangedCallback 回调) {registry.remove(回调);}

这些事情使我的代码构建,但当我解决与 Room 查询相关的错误时,它无一例外地运行.这是主要原因,代码正在构建但没有运行.当我再次重建我的项目时,我可以看到这些错误.

更新:

Android studio 3.1.3 之后,Message 窗口消失了,现在所有构建错误都出现在 Build 视图.尽管可以使用切换来获取错误的 textview 响应,但对于数据绑定错误,这还不够.

对我有帮助的解决方案:

  1. 在命令提示符/终端中导航到项目根目录.
  2. 运行此命令./gradlew build --stacktrace",如果是 Mac 或.gradlew"build --stacktrace"(如果是 Windows).
  3. 现在搜索error:"标签,编译时错误就会显示出来.

我在 IDE 中找不到这些错误.

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

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'

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

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

They both don't seem to work together

Steps taken so far:

  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

Has anyone used Room and Data binding together ?

解决方案

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

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

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'

Note: Gradle plugin version is 3.0.1

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.

UPDATE:

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. In Command promt/Terminal navigate to project root.
  2. Run this command "./gradlew build --stacktrace" if Mac or ".gradlew build --stacktrace" if Windows.
  3. Now search for "error:" tag and the compile time errors will show up.

I couldn't get these errors in IDE.

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

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