77

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 “Binding” to it. 
The above layout file was activity_main.xml so the generate class was ActivityMainBinding.

When will the Binding class , here say ActivityMainBinding, will be generated. I have compile time error. "cannot resolve ActivityMainBinding".

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);

Any help is appreciated. Thanks

Sowmia Sundararajan
  • 1,613
  • 2
  • 14
  • 17
  • @DerGol...lum, Ya i know, it is beta release. I just want to try it out. – Sowmia Sundararajan Jul 12 '15 at 08:14
  • According to this Paragraph it is ok from Google's perspective to publish apps using this library. You just have to keep aware of the beta status and the implications: *** Developers should feel free to publish apps built with the Data Binding library beta release, with the caveats that the standard Android SDK and Google Play terms of service apply, and it’s always a great idea to test your app thoroughly when adopting new libraries or tools. – Jürgen 'Kashban' Wahlmann Jul 28 '15 at 06:24
  • Can anyone point to where the generated classes/files are located in the project? – CodyF Sep 29 '15 at 14:48
  • Just for the completeness : all generated code is inside "module_dir"/build/generated. Further below as per resources there is res directory as per java classes there is src with affinities (e.g. dataBinding). This structure is generated by gradle 2.2+ – lukassos Sep 23 '17 at 09:55
  • In my case, the databingclass is updated timely if there is element in the xml file, or it is only updated after reopening the studio IDE. – Freddie Jun 20 '18 at 00:57
  • 3 years later and Android Studio *still* doesn't generate the binding files reliable. PRO TIP: avoid MVVM in Android. – Someone Somewhere Jul 31 '18 at 09:44

20 Answers20

99

What is your layout name?

The above layout file was activity_main.xml so the generate class was ActivityMainBinding.

What this means is that the generated class name will depend on your layout's name

activity_main.xml -> ActivityMainBinding.java

I think your activity name is "main_activity", so the generated binding class name should be MainActivityBinding not ActivityMainBinding

Wilder Pereira
  • 2,249
  • 3
  • 21
  • 31
Mao
  • 1,398
  • 10
  • 8
  • It's definitely confusing in this case that classes are postfixed with "Activity" while xmls are prefixed! – Dmitry Mar 11 '16 at 00:03
  • 35
    This doesnt answer the question of "how and when". I'm having the same issue as the OP, the binding file is NOT GENERATED for me. This answer just tells you about the naming convention, and doesnt give any clue as to how to resolve the issue of the missing file. – Siavash Dec 04 '16 at 21:05
  • 2
    @Siavash I don't know 'How & When', but I had a similar problem, and what fixed it for me was putting all of the xmlns:app="http://schemas.android.com/apk/res-auto" stuff in the layout tag instead of the top view tag. It was just an oversight on my part, but it was driving me crazy, so I figured I'd pass it on in case it helped someone else. – Aezur Apr 02 '17 at 17:16
  • Kindly help me i have a package name like com.asadmukhtar.show and xml file name is activity_splash, after data binding enable my binding file always generate in package like com.example why this happen i am following MVVM model. kindly help me – Asad Mukhtar May 13 '19 at 20:25
35

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.

Community
  • 1
  • 1
Frank Cheng
  • 5,928
  • 9
  • 52
  • 80
  • yes restart worked and issue is still there in Android Studio 2.1 – Chatura Dilan Jul 31 '16 at 10:04
  • True and better to Invalidate cache And restart ... that happens if you activate Databinding after creating layout.. – Maher Abuthraa Aug 08 '16 at 03:40
  • As 08/16 I have this issue if more than one instance of AndroidStudio is open. I had to restart to see the binding suffix class. – BlackCath Aug 29 '16 at 10:07
  • with me, the binding file exists in `\Project\app\build\generated\source\apt\development\debug\project\android\app\databinding` HOWEVER Android Studio 3.1.3 is reporting the class as Unknown. Invalidate Caches and Restart is not fixing it. – Someone Somewhere Jul 31 '18 at 09:59
  • also, Android Studio keeps generating an error `com.intellij.psi.PsiInvalidElementAccessException: Element: class com.intellij.psi.impl.source.xml.XmlFileImpl because: different providers`. What a waste of time ! – Someone Somewhere Jul 31 '18 at 10:09
32

Did you update your layout file for data binding? They are generated only for layouts which have data binding.

It has to start with a layout tag which has 2 child tags (data & your root view).

Something like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout ...
   </LinearLayout>
</layout>

When your layout has this form, AndroidStudio should auto complete the class. Also, by default, it is generated in <your.app.package>.databinding package.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
yigit
  • 37,683
  • 13
  • 72
  • 58
  • 2
    Even if you're not using Android Studio, the gradle plugin will generate the code during the annotation processing stage. Without Android Studio, you'll have to have faith that the class will be generated and import the Binding class yourself. Something like "import com.my.package.databinding.ActivityMainBinding;" for activity_main.xml – George Mount Jul 20 '15 at 21:27
  • 1
    Class wasn't generated when I had only and no . When I added with some , class got generated. – David Vávra Aug 30 '15 at 14:54
  • Thanks for this answer, no tag was my problem. Even works without adding a variable, by the way. – ScottyC Oct 30 '17 at 20:18
  • This solved it for me. That layout section is needed for the binding to be generated – joehinkle11 Aug 20 '20 at 14:18
16

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.

Nahro
  • 406
  • 3
  • 12
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
15

Try to rename layout to main_activity.xml.

In my case, it didn't work to generate binding class for "activity_main.xml".
But renaming the layout file to main_activity.xml worked just fine.

surlac
  • 2,961
  • 2
  • 22
  • 31
  • It worked for me too. From now on, I will use ´xxxxxx_activity.xml´ for all of my activities/fragments as a new naming convention. Thanks! – Van Nov 08 '17 at 16:11
4

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.

CodyF
  • 4,977
  • 3
  • 26
  • 38
4

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>
Bharatesh
  • 8,943
  • 3
  • 38
  • 67
  • Restarting Android Studio as posted by @Frank Cheng does work, However it makes it tedious to restart it every time a new layout is created. I opted for this option of naming the binding classes my self as it seems to work without problems. – velval Sep 06 '16 at 00:32
4

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

Kamil Nękanowicz
  • 6,254
  • 7
  • 34
  • 51
2

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.

IsaiahJ
  • 454
  • 7
  • 19
1

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

Клаус Шварц
  • 3,158
  • 28
  • 44
  • 1
    this will cause error posted in http://stackoverflow.com/questions/35250956/error3-0-cause-org-apache-commons-lang3-stringutils (Error:(3, 0) Cause: org/apache/commons/lang3/StringUtils) – Dika Jul 28 '16 at 00:13
1

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.

1

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.*;

Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166
0

A weird "bug" in android-studio causes the generated BR.java file not to use the @Bindable fields and still only have the _all property if a res/layout directory doesn't exist.

It happened to me when I wanted to create a "No Activity" App to have a library that contained my different bindable data.

Aurélien Lemaitre
  • 121
  • 1
  • 1
  • 7
0

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>
Qasim
  • 5,181
  • 4
  • 30
  • 51
0
<data class="MainActivityBinding">
    <variable
        name="user"
        type="com.example.administrator.my_mvvm.User" />
</data>

//Remember need Build-> Rebuild Project

a442509097
  • 69
  • 3
0

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

0

If you don't want to restart android studio, one way is to rename activity_main.xml to something else e.g activity_m.xml and then rename it back to activity_main.xml. It is much faster than restarting android studio.

To rename, right click on activity_main.xml and choose Refactor => Rename

DevD'Silva
  • 101
  • 1
  • 1
  • 4
0

I ran into a similar issue that hasn't been mentioned here yet. In my case, I incorrectly added <Layout> as the tag versus <layout>. <Layout> is apparently a valid tag because Android Studio didn't complain (other than requiring layout_width and layout_height which aren't required for <layout>). The capitalization difference will manifest in similar behavior where you cannot import the databinding class (import com.example.android.aboutme.databinding.ActivityMainBinding in my case) in the MainActivity class even though <Layout> is a valid tag and the build.gradle file is defined properly to support data binding.

In my case, I did not need the <data> tags defined yet in order for ActivityMainBinding to validly import and be referenced in:

private lateinit var binding: ActivityMainBinding

I was working through the this codelab when I ran into this behavior: https://developer.android.com/codelabs/kotlin-android-training-data-binding-basics/index.html

0

Hi maybe you can try this as well include this in build.gradle(:app)

inside the android block

buildFeatures { viewBinding true }

worked for me, maybe it could for you as well

Zaid Zakir
  • 543
  • 3
  • 7
0

activity_main.xml file needs to start with layout tag as shown below.

sbchitra
  • 51
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29825019) – Stefano Sansone Sep 14 '21 at 22:40