android: data binding error: cannot find symbol class

后端 未结 26 2843
生来不讨喜
生来不讨喜 2020-12-14 05:49

I am getting started for using DataBinding feature. I am facing problem with it.

Error:(21, 9) error: cannot find symbol class ContactL

相关标签:
26条回答
  • 2020-12-14 06:05

    I have got the same error, but with Kotlin usage.

    To resolve it, i make some changes in gradles files :

    In project's Gradle file :

    dependencies {
        classpath "com.android.tools.build:gradle:3.1.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.40"
    }
    

    In app's Gradle file :

    dependencies {
    
        ...
        implementation "android.arch.lifecycle:extensions:1.1.1"
        compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.30"
        kapt 'com.android.databinding:compiler:3.1.2'
    }
    
    
    kapt {
        generateStubs = true
    }
    
    //used to resolve annotation conflicts
    configurations.all {
        resolutionStrategy {
            force 'com.android.support:support-annotations:23.1.1'
        }
    }
    
    0 讨论(0)
  • 2020-12-14 06:05

    In my case, there was a package with the same name as a missing import in the generated databinding class.. It seems like the compiler got confused.

    0 讨论(0)
  • 2020-12-14 06:09

    This problem may be occur when there is problem in layout file. In my case I just use wrong way to call method

    android:onClick="@={() -> viewModel.showText()}"
    

    instead of

      android:onClick="@{() -> viewModel.showText()}"
    
    0 讨论(0)
  • 2020-12-14 06:10

    Just remove "build" folder in youy project directory and compile again, i hope it works for you too

    0 讨论(0)
  • 2020-12-14 06:13

    Sometimes the reason of these errors are not the DataBinding itself, but some other part of our code. In my case I had an error in Room database so the compiler couldn't generate the binding classes and it gives me these errors.

    According to Google:

    Previous versions of the data binding compiler generated the binding classes in the same step that compiles your managed code. If your managed code fails to compile, you might get multiple errors reporting that the binding classes aren't found. The new data binding compiler prevents these errors by generating the binding classes before the managed compiler builds your app.

    So to enable new data binding compiler, add the following option to your gradle.properties file:

    android.databinding.enableV2=true
    

    You can also enable the new compiler in your gradle command by adding the following parameter:

    -Pandroid.databinding.enableV2=true
    

    Note that the new compiler in Android Studio version 3.2 is enabled by default.

    0 讨论(0)
  • 2020-12-14 06:17

    Your problem might actually be on this line:

    <include layout="@layout/content_contact_list" />
    

    Android Studio gets a little confused at time and takes the include layout for the layout tag. What's even more frustrating is that this could work the first time, fails to work with a modification on the Java/Kotlin code later, and then work again after a tweak that forces it to rebuild the binding. You may want to replace <include> tags with something that populates it dynamically.

    0 讨论(0)
提交回复
热议问题