DataBinding not working at Android Studio 2.3,cannot be cast to android.databinding.tool.expr.StaticIdentifierExpr
Asked Answered
C

5

6

after upgrade Android Studio 2.3 today,build my project failed.

when build project gradle console show this error:

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

use support version is 25.3.0

build tools version is 25.0.2

compile sdk version is 25

project use apt replaced annotationProcessor.

any help is welcome!

Caiman answered 18/3, 2017 at 12:19 Comment(1)
What is your apt line for data binding specifically?Dionedionis
J
2

I have add import view into xml and issue resolved <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/>
Jozef answered 22/12, 2020 at 6:24 Comment(0)
F
0

Check your xml layout; I was trying to use java.lang.String in my layout as a variable and this was causing the same issue.

Fionnula answered 7/10, 2017 at 5:21 Comment(0)
S
0

try removing static from the convertion methods:

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();
    }
}
Shantishantung answered 12/2, 2020 at 17:41 Comment(0)
R
0

With the new data binding + kotlin, sometimes its easy to forget that the layout imported is not the layout inflated in your fragment or activity

For example I am currently uplifting a screen called SettingsFragment and the layout is called fragment_settings, my new screen is called SettingsFragmentV2 and I was importing:

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

instead of import kotlinx.android.synthetic.main.fragment_settingsv2.*

As the new binding relies on FragmentSettingsV2Binding rather than FragmentSettingsBinding this caused a class cast exception for me.

Ruffle answered 10/9, 2020 at 8:1 Comment(0)
L
0

My problem was in Layout -

    <variable
        name="SomeDataClass"
        type="SomeDataClass" />

name must be different name from type - so you kind of create instance, For exaple

   <variable
        name="someDataClass"
        type="SomeDataClass" />
Lissalissak answered 5/8, 2022 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.