31

I'm trying to use data-binding with Android.

I can not anymore build my project. I got this error :

"Error:(13, 46) error: package ch.company.project.databinding does not exist"

Here my gradle :

http://pastebin.com/dkXd1Mxr

and

http://pastebin.com/n9hkFWGQ

And here the gradle output :

https://pastebin.com/w93Rausg

Xero
  • 3,951
  • 4
  • 41
  • 73
  • 1
    Can you share the gradle output as well? (with --debug and --stacktrace) – yigit Nov 26 '15 at 09:13
  • 3
    Here is your error: /Users/anthonybernardo/Documents/Geomatic/GeomaticReborn/app/src/main/java/ch/geomatic/geomaticreborn/Views/SearchActivity.java:69: error: cannot find symbol 10:21:38.018 [ERROR] [system.err] (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search)); Fix it and the project should compile. If not, please provide the full output (not just the error). I want to see the tasks that are run but anyways, fixing this R.id.search should fix your compilation. – yigit Nov 26 '15 at 09:27
  • 1
    omg! I was think that it was a issue with Android Databinding... Thank you a lot! – Xero Nov 26 '15 at 09:32

22 Answers22

41

Thanks to Yigit!

The issue was not directly link to Android Databinding. There were a bug in the project (some variables not correctly setted)

I would recommend to use gradle with "--debug and --stacktrace" for more informations, it's helping a lot.

Xero
  • 3,951
  • 4
  • 41
  • 73
  • 3
    This was my problem, too. I was passing in an incorrect value into a BindingAdapter. AS2.0 didn't tell me, just the errors about not finding the package above. Fun. – mmm111mmm Feb 23 '16 at 23:13
  • I encountered the same problem and still couldn't figure it out. I am using android studio in windows machine, can you please share the command to use gradle with the --debug and --stacktrace – Angus Apr 01 '21 at 22:30
38

earlier my package name was "com.xyz.abc.Models" changing the package name to all small letters "Models" -> "models"

solved the issue.

Kamlesh Sahu
  • 493
  • 4
  • 8
12

The bug is not the DataBinding Package, it's a syntactic or logical error. For example, you have the attribute "lastName" in your POJO, but in the layout it's android:text="@{user.lastname}".

Check your "layout" and do Rebuild Project.

knjk04
  • 115
  • 2
  • 11
Edhy
  • 121
  • 1
  • 4
7

I am not satisfied with accepted answer, that tell you to stack trace without hints.

Here are some possible causes that lead to this problem. Check if you are not doing any of the following.

Basically Android DataBinding is not that mature still. It will fail without appropriate errors many times.

So if you have an issue like package ch.company.project.databinding does not exist".

Possible causes of fail:

  • First of all check your recently edited layouts xml one by one for errors (for wrong imports & variables). I don't get proper error in this case usually.

  • Check your data binding syntax in binding block ({...}) in layout element for errors. Always Rebuild (not Build) project after working in one layout.

  • Check your @BindingAdapter method having correct parameters. For example imageUrl binding adapter would accept ImageView or View as first parameter.

  • You should always Rebuild project after doing work in one layout.

  • If you are not able to find errors by above steps, then try --debug and --stacktrace in compile option of

    File> Settings> Build, Execution, Deployment> Compiler> Command-line Options

Community
  • 1
  • 1
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
7

Make sure your package name start with lowercase letter. in my case issue solved after two hours of struggle

3

enter image description here

Package name should start with small letter. for example Activities is wrong it'll give an error instead refactor->rename to activities

Siddharth Vaish
  • 402
  • 3
  • 4
2

I got the error:

Error:(9, 46) error: package com.company.www.bar.databinding does not exist.

i just remove "=" sign . it worked for me

From this :

 <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@={()->activity.onButtonClick()}"/>

to :

<Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{()->activity.onButtonClick()}"/>
  • 1
    This occurs because the "=" means a two way data binding, for buttons does not makes sense but if you use that in some EditText when you change the data in the EditText the data will change in the model too. – Lucas Queiroz Ribeiro Mar 04 '17 at 17:35
2

I had similar problems with my project

You could try:

  • check xml files for errors that cause a build failure
  • clean project
  • File -- invalidate caches / restart
Liviu
  • 768
  • 8
  • 9
2

On my particular case, I was using Dagger 2. This package error appears in a lot of classes without any relation with the original error: a dependency injection error.

Happy reminder: Scroll more on your stacktrace to know what is the real problem.

2

I was stuck with same error for hours. After trying several solution from stackoverflow, I updated my project with stable gradle dependencies.

Still it was not solved, however with the same gradle dependency DataBinding was working fine in another project of mine.

So, I went project folder using explorer and Deleted 2 things.

  1. build folder
  2. all files from .idea/libraries

After that i synced the project and it continued to work just fine.

Ifta
  • 1,536
  • 18
  • 25
2

Package names have to START with Small Letters. Otherwise, Binding library cannot understand that is it class or package. Moreover, you do NOT need to do all of it with small letters.

Example, wrong usage:

package com.thecompany.activity.ContactInfo; //Problem is ContactInfo, 'C'.

Example, TRUE usage:

package com.thecompany.activity.contactInfo; //Solution is contactInfo, 'c'.
canerkaseler
  • 6,204
  • 45
  • 38
1

Make sure your model's fields you reference in layout have public access modifiers

YTerle
  • 2,606
  • 6
  • 24
  • 40
1

Change

{ databinding = true}

to

buildFeatures{
     dataBinding = true
    
}
Crucialjun
  • 193
  • 1
  • 8
1

If you're coming to this question because you switched to JDK11 in Android Studio Artic Fox and your view binding broke in the UI but not during execution then be aware that this is a known issue and should be resolved in Bumble Bee:

https://issuetracker.google.com/issues/180946610

The current fix is to switch back to JDK8 (or install the Bumble Bee canary release).

Cory Charlton
  • 8,868
  • 4
  • 48
  • 68
  • What's funny, adding this to my build gradle seems to fix the problem `apply plugin: 'kotlin-android-extensions` – Frischling Oct 08 '21 at 14:00
  • Thanks, i need it to update from Arctix Fox to Bumblebee and updating the gradle plugin to the lastest for fixing the issue with databinding package. – Kostadin Georgiev Feb 01 '22 at 13:34
0

To get rid of this error just enclose your complete layout design inside a plain layout tag in the activity_main.xml file.

After wasting many hours in finding solution this worked for me. Give it a try.

Ben
  • 1,414
  • 2
  • 13
  • 18
0

if you tried this steps

  1. invalidate/restart`
  2. keeping this properties in gradel.properties
android.databinding.enableV2=false
android.enableExperimentalFeatureDatabinding=true

and checking all xml files looks good. then you should go with this solution, add below code in project level build.gradle

    allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xmaxerrs" << "1000"
        }
    }
}

this will give you exact error where you have actual error explanation: above code will increase the size of the compile error

Xero
  • 3,951
  • 4
  • 41
  • 73
Mohd Qasim
  • 896
  • 9
  • 20
0

in my case, i follow the android documentation :

buildFeatures {
    viewBinding true
}

use "=" instead of space

buildFeatures {
    viewBinding = true
}
icgoogo
  • 401
  • 5
  • 7
0

Try following Refactor -> migrate to androidx

and in the build.grade(:app)

implementation 'androidx.appcompat:appcompat:1.0.0'

or use new version is released

implementation 'androidx.databinding:databinding-runtime:4.1.0'
Islam Alshnawey
  • 692
  • 10
  • 15
0

Make sure that if your layout filename is named in the following format: <name>_activity.xml that your binding class name complies the following format as well: <name>ActivityBinding

For me, changing my layout filename from activity_login.xml to login_activity.xml resolved this issue because my binding class name was LoginActivityBinding.

Here's an except from the Android Layouts and binding expressions page mentioning this:

A binding class is generated for each layout file. By default, the name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it. The above layout filename is activity_main.xml so the corresponding generated class is ActivityMainBinding

flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25
0

if you use a model in your layout, make sure you dont have the model and the package named same and also the paackage name should start with small letter.

i changed mine from Model>Model.class to modelPac>Model.class

0

In my case the problem appeared when I was creating productFlavors and set sourceSets. Changing

sourceSets {
    develop {
        res.srcDirs = ['myApp/src/develop/res']
    }

to

sourceSets {
    develop {
        res.srcDirs = ['src/develop/res']
    }
}

solved my issue.

soulflavacrew
  • 1,456
  • 1
  • 6
  • 3
0

Hope this will help someone. In my case I was using ZoomImageView which i Programmed in Java file and was using in XML. When i migrated my project to another. The package name changed and i copied same xml file to new project. Which was causing conflict.

I changed

<com.coremacasia.okk.utility.ZoomImageView
    android:id="@+id/zoomImage"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

To

<in.theclink.clink.utility.ZoomImageView
    android:id="@+id/zoomImage"
    android:adjustViewBounds="true"
    android:scaleType="fitCenter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

It worked.

Vikash Sharma
  • 539
  • 8
  • 13