Android - DataBinding - How and when the Binding classes will be generated?

后端 未结 18 1727
小蘑菇
小蘑菇 2021-02-03 16:51

DataBinding Guide States

  By default, a Binding class will be generated based on the name of the layout 
file, converting it to Pascal case and suffixing “Bindi         


        
相关标签:
18条回答
  • 2021-02-03 17:23

    If you ever wanted to give your own Binding class name just add class attribute to the data element with class name

    Binding classes may be renamed or placed in different packages by adjusting the class attribute of the data element.

    <data class="ContactItem">
        ...
    </data>
    
    <data class="com.example.ContactItem">
    ...
    </data>
    
    0 讨论(0)
  • 2021-02-03 17:23

    Faced the same issue.

    Reason for "cannot resolve ActivityMainBinding" is that your Binding file isn't generated. Try to clean and rebuild project. Also make sure you have followed the steps :

    1. classpath "com.android.databinding:dataBinder:1.0-rc4" --> include this in main module build file
    2. apply plugin: 'com.android.databinding' to your app module you are building
    3. Clean
    4. Rebuild. Check under ,if build->indermediates->your package name->databinding folder is generated containing ActivityMainBinding class.

    Alternatively if you want to see the generated source.

    1. classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7' -->apply to your main module.
    2. apply plugin: 'com.neenbedankt.android-apt' --> to you app module
    3. apt 'com.android.databinding:compiler:1.0-rc0' --> include in your dependencies.

    These classes will be generated on first build. Also make sure the applicationId in your application module is same as the package name in AndroidManifest.xml of your app module. Hope this helps.

    0 讨论(0)
  • 2021-02-03 17:27

    In my case Restarting Android Studio does work. It is bug in android-apt

    0 讨论(0)
  • 2021-02-03 17:27

    Just a simple restart didn't work for me. I had to Invalidate Caches and Restart then I could see the binding classes. It's under the File menu for those who don't know.

    0 讨论(0)
  • 2021-02-03 17:31
    <data class="MainActivityBinding">
        <variable
            name="user"
            type="com.example.administrator.my_mvvm.User" />
    </data>
    

    //Remember need Build-> Rebuild Project

    0 讨论(0)
  • 2021-02-03 17:32

    check the fields access level of your object, when i turned public it worked here

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