H. Pauwelyn
H. Pauwelyn

Reputation: 14320

databinding does not exist: How to solve it?

I'm working on an Android application with databinding but I've always next error:

Error: Package my.package.databinding does not exist.

Here is my build.gradle on project level:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
    }
}
allprojects {
    repositories {
        jcenter()
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

I've also enabled binding in the build.gradle file on module level.

Now my question is, why occurs this error and how could I solve it?

Upvotes: 14

Views: 25836

Answers (9)

Owen Chen
Owen Chen

Reputation: 1720

Remember to add

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    ...
    buildFeatures {
        viewBinding true
        dataBinding true
    }
}

Upvotes: 0

Chris Sprague
Chris Sprague

Reputation: 3592

After deleting the build folder for the project and submodules, making, rebuilding, etc etc etc, the only thing that worked for me was:

create a new layout

I think there's something in the generator that gets messed up and that flushes it (totally guessing here)

Upvotes: 0

Artem Winokurov
Artem Winokurov

Reputation: 191

For me doesn't work anything except one: Renamed XML binding class What I tried before: off/on viewBinding renaming folders reinstalling modules renaming modules

Upvotes: 0

AlexPad
AlexPad

Reputation: 10879

To see the error, just edit these lines of code in the app's build.gradle:

dataBinding {
enabled = false
} 

In this way, the last error in your build console is the actual error. Because from the first to the penultimate error, they are all related to the non-generation of the data binding classes, precisely because we have disabled it.

Once you find the error you will enter again :

dataBinding {
enabled = true
} 

Upvotes: 4

Vinod Makode
Vinod Makode

Reputation: 973

dataBinding {
    enabled = true
}

enabled the data binding in app build.gradle file. its worked

Upvotes: 1

Marzieh Bahri
Marzieh Bahri

Reputation: 604

check out your xml files and comment any @{} you have used unless you actually have your data ready at hand. With no data, you'll bump into this error again and again and again.

Upvotes: 4

Kirk
Kirk

Reputation: 16255

I came across this issue in a project of 4 modules in Android Studio 2.3, it is what @F43nd1r indicated, but want to document what I did to resolve this in my case.

One of the 4 modules had an older Android Support library in in the Gradle file for it, while the other 3 were current. This is what prevented the project from compiling properly and causing the databinding error.

The difficult part was that you don't know about this unless you open each build.gradle file and see if there is an error displayed. It did NOT show an error for it on compile.

Effectively I updated this area to the newer version number to match the other 3 module build.gradle files.

dependencies {
    ...
    compile 'com.android.support:appcompat-v7:25.2.0'
    compile 'com.android.support:support-v4:25.2.0'
    compile 'com.android.support:recyclerview-v7:25.2.0'
    compile 'com.android.support:design:25.2.0'
    ...
}

Upvotes: 3

F43nd1r
F43nd1r

Reputation: 7759

This problem occurs usually if your project does not compile. Android databinding should generate code in the named package, but it can't do that if the project doesn't compile in the first place.

To solve this, bring your project to a point where it compiles. If necessary, turn databinding off for this.

Upvotes: 30

bebe
bebe

Reputation: 114

Based on similar issues on SO, the reasons may not be related to android data binding, and instead due to incorrectly calling variables as in this issue or some other factors like in this other issue. You should provide more details if none of these links helps.

Upvotes: 0

Related Questions