33

I am trying to test data binding as given in the guide here. I have included this in my build.gradle file (of module app) :

compileSdkVersion 'android-MNC'
buildToolsVersion '23.0.0 rc2'

In the project build.gradle file, I have included this in my dependencies :

classpath "com.android.tools.build:gradle:1.3.0-beta2"
classpath "com.android.databinding:dataBinder:1.0-rc0"

The layout file is exactly the same as the one given in the guide.

<?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
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   <TextView android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{user.firstName}"/>
   <TextView android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{user.lastName}"/>
</LinearLayout>
</layout>

Initially it gave an error Element layout does not have the required attribute layout_width and layout_height.

I tried to fix it by assigning match_parent to both. Then I got the error Error parsing XML: duplicate attribute on the lines where i assigned layout_height and layout_width to the linear layout.

Again I tried to fix this by removing these attributes. Now every time I try to compile, I see this- error: package my.package.name.databinding does not exist.

The code completion is working perfectly in my Fragment where I am trying to use this layout.

So what did I miss ?

axay
  • 1,657
  • 2
  • 18
  • 22
  • can you post your `user` POJO class and `Activity` class – N J Jun 12 '15 at 05:31
  • Double check package reference in the xml `type` attribute. `my.package.User` or `com.example.User` – J.G.Sebring Aug 06 '15 at 08:59
  • I ran into this error and finally discovered it was because I had a duplicated android xml namespace. The DataBinding library in Android Studio is reeeeaaaally bad about letting you know where the problem is right now. Hopefully the next update will clear things up. – Jim Pekarek Sep 16 '15 at 19:32
  • You can also check [this](http://chintanrathod.com/develop_apps_faster_using_data_binding_part2/) tutorial for your reference. – Chintan Rathod Apr 11 '16 at 06:35

12 Answers12

20

Your bindings in the xml file might be invalid.

Make sure to double check

  1. Are type attributes valid reference to data object? type="my.package.Class"
  2. Are the binding valid? name="client" -> "@{client.field}"
  3. Are data fields accessible? public or encapsulated with getters
  4. Are field names in java and the xml file matching, check for typos
  5. If you have renamed the xml file, make sure you also update the Binding objects. OldNameBinding -> NewNameBinding
  6. Clean Project
J.G.Sebring
  • 5,934
  • 1
  • 31
  • 42
  • 1
    I recently renamed a package and Android Studio kept looking for 'com.oldpackage.app.databinding..' despite cleaning of project. Restarting A.Studio did the trick though. – J.G.Sebring Aug 20 '15 at 11:42
  • 1
    Perfect checklist, thanks, worked for me (left incorrect binding after copy-paste from the net) – Ivan Fazaniuk Feb 28 '16 at 18:10
  • 1
    Between 3 and 4 I would add "Check for typos and mismatches in the field/getter names and the names used in the XML." That was the case for me. The incorrect was grayed out as unused in the java file. – WindRider Jun 01 '16 at 13:06
  • It's obvious in hindsight, but for the `BaseObservable`/Data class to be accessible to the `databinding` package, fields/setters must be `public`. – Angad Jan 27 '17 at 06:58
  • A single error in one XML layout file can cause the same error for all XML layout files using data binding in a package even all other layout files are flawless. – Hong Mar 24 '17 at 00:08
17

removing apply plugin: 'com.neenbedankt.android-apt' from my build.gradle solved my problem.

sakirk
  • 304
  • 2
  • 2
  • 2
    Same here, I believe data-binding already uses apt internally. If you're using dagger or any other apt-dependent library just use provided "com.google.dagger:dagger-compiler:2.0.1" instead of apt "com.google.dagger:dagger-compiler:2.0.1" – frankelot Oct 13 '15 at 19:29
  • 10
    for me the problem was `classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'` upgrading to 1.8 solved the issue – miv Nov 16 '15 at 11:09
  • 1
    if anybody cares, you can ready why @miv has the right answer here: https://bitbucket.org/hvisser/android-apt/issues/38/android-apt-breaks-brand-new-data-binding – tir38 May 02 '16 at 15:48
  • I'm getting `NPA` in `butterknife `@BindView`` after removing this `apt` stuff – Kishore Jethava Jul 28 '16 at 17:32
  • @sakirk could you help me on this topic: http://stackoverflow.com/q/39837222/6599627 – mahdi pishguy Oct 03 '16 at 17:52
3

it’s usually because there is an error in your XML layout file and it can’t generate the binding object. Make sure you write data type correctly, check lower or upper of your writing.

    <data>
        <variable name="yourObject" type="com.example.simple.YourObject"/>
    </data>

And make sure object is right calling:

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{yourObject.name}"/>
    <TextView
Lay Leangsros
  • 9,156
  • 7
  • 34
  • 39
  • 1
    After searching for a while, I found this post. I finally commented out most of my XML file and then the error went away. From there, I was able to debug the issue that was in fact in my xml (that I was not seeing in the compiler output), and was causing the error for *"Can't find databinding package"*. Thanks! – Gene Bo Aug 29 '16 at 07:54
1

This appears to be an issue in android studio. The editor will flag it as an error however it will still compile.

I had the same issue and so I cloned this example repo to test my system is configured correctly to support the databinding api https://github.com/mgrzechocinski/android-bindings-example

Ignore the error warning on the layout tag. It is wrong.

Something I noticed is you don't necessarily require the data tag if are simply using the databinding to get access to the views items through the BindingActivity class that is generated.

Alec Holmes
  • 3,625
  • 4
  • 22
  • 23
  • You don't NEED the `data` tag?? This article says otherwise! https://developer.android.com/tools/data-binding/guide.html – IgorGanapolsky Jun 24 '15 at 20:56
  • 1
    You do not need the data tag if you only want to take advantage of the BindingActivity that is generated by the plugin. Ie MainActivityBinding.myview – Alec Holmes Jun 27 '15 at 15:37
  • @Alec He does need the data tag, as he's clearly going to use the setUser() method on the generated class and set the data in the layout automatically. Also, you can use the data tag to set the generated class name, i.e. `` and then the generated class will be UserData rather than LayoutNameBinding. To say he doesn't NEED the data tag ignores it's uses and what you gain by using it. – LukeWaggoner Jul 21 '15 at 23:32
1

If you are using 'android-apt' plugin for dagger or other libraries, after enabling data binding, you have to remove 'android-apt' plugin and use 'provided' instead of 'apt' in your dependencies. For example if you are using dagger, remove this

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'

and this

apply plugin: 'com.neenbedankt.android-apt' 

and then replace

apt 'com.google.dagger:dagger-compiler:2.0'

with

provided 'com.google.dagger:dagger-compiler:2.0'
hadilq
  • 1,023
  • 11
  • 25
0

According to the current version of the guide (2015-11-11) you just have to add two dependencies: In the file build.gradle of the project add the gradle dependency. Currently the most recent version is 1.5.0-beta3

dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0-beta3'
    }

And in the file build.gradle of the module include the dataBinding section:

android{
  ...
  dataBinding {
        enabled = true
    }
  ...
}

The error

error: package my.package.name.databinding does not exists

may ocurr because a mismatch between the xml and the POJO class

Community
  • 1
  • 1
Jonnathan
  • 121
  • 3
0

You have to be very careful that your POJO class data members should be public if you are not creating the getter setters.

If you have simple POJO without any getter or setter

public class User {
   public final String firstName;
   public final String lastName;
   public User(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
   }
}

If you make getter setter, javaBean style class

public class User {
   private final String firstName;
   private final String lastName;
   public User(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
   }
   public String getFirstName() {
       return this.firstName;
   }
   public String getLastName() {
       return this.lastName;
   }
}
Salmaan
  • 3,543
  • 8
  • 33
  • 59
0

In my case, the problem was that I was adding layout_height and layout_width to the layout tag and also to my root layout. I've just removed both attributes from the layout tag and solved the problem.

Wilder Pereira
  • 2,249
  • 3
  • 21
  • 31
0

You may have a problem or mismatch between your model and your layout file. In my case I had:

...
<TextView
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_toLeftOf="@+id/photo"
            android:layout_toStartOf="@+id/photo"
            tools:text="0912454433"
            android:textStyle="bold"
            android:text="@{contact.cellPhoneNumber}"
            android:layout_below="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

but my model class was like so:

public class MyContact {
    public String name;
    public String cellphoneNumber; // <-- Notice the lowercase 'p'
}
Tash Pemhiwa
  • 7,590
  • 4
  • 45
  • 49
-1

Looks like you forgot to add data binding plugin to your app's gradle file:

apply plugin: 'com.android.databinding'

You have to apply it to any module you want to use data binding.

Also, you should not add layout_w/h to the layout tag, it is just an Android Studio bug, already fixed in aosp.

yigit
  • 37,683
  • 13
  • 72
  • 58
-1

I also suffer with the same problem. But my mistake is

User class variables declared as not a public. So we should declare the POJO class variables as public.

public class User {


    public String firstName,lastName;

        public User(String fname,String lname){
            this.firstName=fname;
            this.lastName=lname;
        }
    }

Then solved my problem.

Kona Suresh
  • 1,836
  • 1
  • 15
  • 25
-1

// This is under activity_main xml .

  <data>
  <variable
      name="student"
      type="com.example.vishalkamboj.testdatabinding.Student"    />
</data>

// This is Student Class

public class Student {

public String firstname ; public String lastname ;

public Student(String firstname , String lastname)
{
    this.firstname = firstname;
    this.lastname = lastname;

}

}