DataBinding无法在Android Studio 2.3上运行,无法转换为android.databinding.tool.expr.StaticIdentifierExpr

时间:2017-03-18 12:19:57

标签: android android-studio data-binding android-studio-2.3

今天升级Android Studio 2.3后,构建我的项目失败。

当构建项目gradle控制台显示此错误时:

FAILURE: Build failed with an exception.

What went wrong:
Execution failed for task ':app:compileChildrenDebugJavaWithJavac'.
 java.lang.RuntimeException: failure, see logs for details.
cannot generate view binders java.lang.ClassCastException: android.databinding.tool.expr.IdentifierExpr cannot be cast to android.databinding.tool.expr.StaticIdentifierExpr
    at android.databinding.tool.expr.ExprModel.staticIdentifier(ExprModel.java:194)
    at android.databinding.tool.expr.ExprModel.addImport(ExprModel.java:352)
    at android.databinding.tool.expr.ExprModel.staticIdentifierFor(ExprModel.java:270)
    at android.databinding.tool.expr.FieldAccessExpr.replaceStaticIdentifier(FieldAccessExpr.java:270)
    at android.databinding.tool.expr.FieldAccessExpr.resolveType(FieldAccessExpr.java:256)
    at android.databinding.tool.expr.Expr.getResolvedType(Expr.java:359)
    at android.databinding.tool.expr.Expr.unwrapChildTo(Expr.java:887)
    at android.databinding.tool.expr.Expr.unwrapObservableFieldChildren(Expr.java:873)
    at android.databinding.tool.expr.Expr.getResolvedType(Expr.java:353)
    at android.databinding.tool.expr.Expr.unwrapChildTo(Expr.java:887)
    at android.databinding.tool.expr.Expr.unwrapObservableFieldChildren(Expr.java:873)
    at android.databinding.tool.expr.Expr.getResolvedType(Expr.java:353)
    at android.databinding.tool.BindingTarget.resolveMultiSetters(BindingTarget.java:214)
    at android.databinding.tool.LayoutBinder.<init>(LayoutBinder.java:249)
    at android.databinding.tool.DataBinder.<init>(DataBinder.java:52)
    at android.databinding.tool.CompilerChef.ensureDataBinder(CompilerChef.java:88)
    at android.databinding.tool.CompilerChef.sealModels(CompilerChef.java:187)
    at android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:184)
    at android.databinding.annotationprocessor.ProcessExpressions.onHandleStep(ProcessExpressions.java:86)
    at android.databinding.annotationprocessor.ProcessDataBinding$ProcessingStep.runStep(ProcessDataBinding.java:189)
    at android.databinding.annotationprocessor.ProcessDataBinding$ProcessingStep.access$000(ProcessDataBinding.java:174)
    at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:79)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
    at com.sun.tools.javac.main.Main.compile(Main.java:523)
    at com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:129)
    at com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:138)
    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:46)
    at org.gradle.api.internal.tasks.compile.JdkJavaCompiler.execute(JdkJavaCompiler.java:33)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.delegateAndHandleErrors(NormalizingJavaCompiler.java:104)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:53)
    at org.gradle.api.internal.tasks.compile.NormalizingJavaCompiler.execute(NormalizingJavaCompiler.java:38)
    at org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:35)
    at org.gradle.api.internal.tasks.compile.CleaningJavaCompilerSupport.execute(CleaningJavaCompilerSupport.java:25)
    at org.gradle.api.tasks.compile.JavaCompile.performCompilation(JavaCompile.java:206)
    at org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:187)
    at org.gradle.api.tasks.compile.JavaCompile.compile(JavaCompile.java:130)
    at com.android.build.gradle.tasks.factory.AndroidJavaCompile.compile(AndroidJavaCompile.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$IncrementalTaskAction.doExecute(DefaultTaskClassInfoStore.java:163)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:134)
    at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:123)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:95)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:76)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:55)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88)
    at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:51)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.execute(DefaultTaskGraphExecuter.java:236)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.execute(DefaultTaskGraphExecuter.java:228)
    at org.gradle.internal.Transformers$4.transform(Transformers.java:169)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:106)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:61)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:228)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:215)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:77)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:58)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)


Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

使用支持版本是25.3.0

构建工具版本是25.0.2

编译sdk版本是25

项目使用apt替换了annotationProcessor。

欢迎任何帮助!

4 个答案:

答案 0 :(得分:0)

检查你的xml布局;我试图在我的布局中使用java.lang.String作为变量,这导致了同样的问题。

答案 1 :(得分:0)

尝试从转换方法中删除静态

import androidx.databinding.InverseMethod;

public class Conv {

    public Double toDoubleAmount(String amount) {
        return Double.parseDouble(amount);
    }

    @InverseMethod("toDoubleAmount")
    public String toStringAmount(Double amount) {
        return amount == null ? "" : amount.toString();
    }
}

答案 2 :(得分:0)

使用新的数据绑定+ Kotlin,有时很容易忘记导入的布局不是inflatedfragment中的布局activity 不是 < / p>

例如,我当前正在举起一个名为SettingsFragment的屏幕,其布局名为fragment_settings,我的新屏幕名为SettingsFragmentV2,而我正在导入:

import kotlinx.android.synthetic.main.fragment_settings.*

代替import kotlinx.android.synthetic.main.fragment_settingsv2.*

由于新的绑定依赖于FragmentSettingsV2Binding而不是FragmentSettingsBinding,因此对我造成了class cast exception

答案 3 :(得分:0)

我已将导入视图添加到 xml 中,问题已解决 <import type="android.view.View" />

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data>

    <import type="android.view.View" />//i miss this line

    <variable
        name="model_name"
        type="com.model.ModelClassName" />

</data>

<View
android:visibility='@{(model_name.list!=null &amp;&amp;  
model_name.list.size>0)?View.VISIBLE:View.GONE}'
/>
....
<layout/>