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

后端 未结 18 1726
小蘑菇
小蘑菇 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:17

    In my case i just enclosed my layout xml in <layout></layout> tags and that's it, without adding the data tag, i was successful to generate Data Bindings. Hope this helps.

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

    When it's not generating the binding class, I restart Android studio. Then the binding class will be generated. Isn't it caused by cache of android studio?

    BTW, if you are using android-apt, please check it. Because it will cause binding class not to be generate.

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

    Rule

    Layout name is in snake_case, and generated binding class name will be PascalCase.

    If you layout name is activity_home.xml then binding class name will be ActivityHomeBinding.class.

    Problem

    1. Many times you don't get import suggestion of DataBinding class.
    2. Binding class is not generated when there is some issue in layout.
    3. Binding class are not available when build is failed

    Here is hack

    When you Don't get Import Suggestion

    • When you don't get import suggestion. Import manually your binding class like this. (IDE often does not show suggestions for binding classes)

      import <yourpackage>databinding.ActivityMainBinding;
      

    Import line still shows error?

    • If your import line shows error, then try make project (ctrl + F9) / Build> Make Project. .
      1. If Build is failed due to some error, then solve it.
      2. If build is successful then binding class will be generated.

    Quick hack for generating binding class-

    • If your binding class not generated then close project (File > Close Project) and open from recent.

    Note that I recommend close and open from recent because it takes less time than Rebuild / Restart IDE.

    Quick hack for generating layout variables in binding class-

    • If your layout data variable are not generated then close project (File > Close Project) and open from recent.

    If you still have issues. Let me know in comments, or see this answer for better understanding.

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

    For android databinding to work properly you have to use android tools for gradle (com.android.tools.build:gradle) >=1.3.0.

    So your project build.gradle must look like:

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.3.0'
            classpath "com.android.databinding:dataBinder:1.0-rc1"
        }
    }
    
    allprojects {
        repositories {
            jcenter()
        }
    }
    

    And module build.gradle must have that plugin:

    apply plugin: 'com.android.databinding'
    

    After all, check in your module you're using latest buildToolsVersion (right now it is 22.0.1). I am not sure that's required but it possibly will make you feel good about you're on the "bleeding edge of technology" ^_^ :

    buildToolsVersion '22.0.1'
    

    Resync gradle and rebuild your project. It is likely possible that without rebuild of project you might not get SomeLayoutBinding classes generated.

    In Android Studio that could be done from application menu: Build -> Rebuild project

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

    My experience is that Android Studio 3.1.3 generated the databinding class after I clicked "Build > Rebuild Project". The file was stored in:

    \Project\app\build\generated\source\apt\development\debug\project\android\app\databinding

    However, Android Studio still reported the binding class as "Unknown" in source code.

    To finally fix it, I manually added the import statement:

    import project.android.app.databinding.*;

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

    Make sure that the layout of the:

     <data>
       <variable name="user" type="com.example.User"/>
     </data>
    

    Is in the activity_main.xml if you are looking for ActivityMainBinding. In most boilerplate projects you set your content view to activity_main.xml, but then inflate you fragment_main.xml.

    If you put your <data> in fragment_main.xml layout then the class that is generated will be FragmentMainBinding.

    Note: This may seem obvious after reading, but it is something that can be easily overlooked when following the android documentation.

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