Marian Pavel
Marian Pavel

Reputation: 2886

Android Studio fails to generate databinding after 3.1.0 update

This morning I made an update to android studio from 3.0.1 to 3.1.0. After updating the gradle to latest version I still get build error regarding data binding.

My gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

All my errors are like the one below:

/Users/mp/Documents/GitHub/projectx/app/build/generated/source/dataBinding/baseClasses/Staging/debug/me/projectx/asdasd/databinding/GridItemActivityTypeBinding.java:57: error: cannot find symbol
      @Nullable DataBindingComponent component) {
                ^
  symbol:   class DataBindingComponent
  location: class GridItemActivityTypeBinding

Does anyone have any idea why would my data binding not generate after the android studio 3.1 update? Thanks in advance

Edit 1: Forgot to say, I tried clean/rebuild/invalidate cache & restart/deleted build folder.

Upvotes: 41

Views: 45189

Answers (20)

theAshutoshAJ
theAshutoshAJ

Reputation: 51

Just connect to internet and sync project with gradle files. It'll do.

Upvotes: 0

Gurjinder Singh
Gurjinder Singh

Reputation: 10329

I got this while updating gradle to 3.4.2. All you need to do is remove the import statement of java.lang.

Caution: Android Studio doesn't yet handle imports so the autocomplete for imported variables may not work in your IDE. Your app still compiles and you can work around the IDE issue by using fully qualified names in your variable definitions.

Source: https://developer.android.com/topic/libraries/data-binding/expressions#import-classes

Upvotes: 0

Barungi Stephen
Barungi Stephen

Reputation: 847

Just Commenting these lines out in graddle-wrapper.properties file helped me solve my problem

#android.enableExperimentalFeatureDatabinding = true
#android.databinding.enableV2=true

Upvotes: 5

Gaurav1991
Gaurav1991

Reputation: 1

  • In the gradle.properties add:
    android.databinding.enableV2=true

  • In build.gradle(module:app) file add:
    dataBinding {enabled = true}

  • Clean project and rebuid it.

It will start working...

Upvotes: 0

Yudi karma
Yudi karma

Reputation: 324

FIRST OF ALL
1. add "layout" to your root layout

  1. Build -> Make Project (for create generate class after add "layout")
  2. //binding private lateinit var binding:ActivityLoginBinding
  3. in oncreate view //setContentView(R.layout.activity_login) binding = DataBindingUtil.setContentView(this@LoginActivity,R.layout.activity_login)

Upvotes: 0

Mohsen Hameed
Mohsen Hameed

Reputation: 19

I got this error after making some modifications in Room Entity classes. So I feel that This error is somehow related to Room library. Try to revert changes in Room classes and entities or comment them to see if error is fixed.

In my case the error appeared because I was returning int from insert and update methods. These methods should not return anything. So removing return fixed the error.

@Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(student: Student):Int

to

@Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(student: Student)

Upvotes: 0

ddy214
ddy214

Reputation: 21

I had the same issue as @Marian Pavel where my project couldn't compile the databinding components unless I had the class thats used in databinding in the root folder.

I fixed the issue by doing this:

Android Studio: 3.2.1 stable

**project build.gradle**
classpath 'com.android.tools.build:gradle:3.2.1'

**module build.gradle**
apply plugin: 'kotlin-kapt'
kapt "androidx.databinding:databinding-compiler:3.2.1"

**gradle.properties**
android.databinding.enableV2=false

Upvotes: 2

Ifta
Ifta

Reputation: 1595

This may seems strange but I wasted a few hours facing the error and after a inspection in my latest changes I found that it was related to an error in Room database.

I declared one of the Dao interface but forgot to anotate it with @Dao.

After fixing that the data binding error was fixed.

I guess this is a bug of android studio.

Upvotes: 0

Kebab Krabby
Kebab Krabby

Reputation: 1699

Non of these solutions worked for me so i found out its bug in 3.2 beta 4 version of android studio:

buildscript {

repositories {
...
}
dependencies {
    //classpath "com.android.tools.build:gradle:3.2.0-beta04"  // buggy databinding
    classpath "com.android.tools.build:gradle:3.1.3" // working
}}

after this i sync, rebuild and run everyting correctly

Upvotes: 2

Xavier
Xavier

Reputation: 341

Following the update to Android Studio 3.2, this line works for me. I have both Java and Kotlin code (compiler) running in my project.

Add the following to your gradle.properties: android.databinding.enableV2=false

Reason:

Data Binding V2

Data Binding V2 is now enabled by default and is compatible with V1. This means that, if you have library dependencies that you compiled with V1, you can use them with projects using Data Binding V2. However, note that projects using V1 cannot consume dependencies that were compiled with V2.

source (Release Note): https://developer.android.com/studio/releases/

Upvotes: 21

Squimon
Squimon

Reputation: 591

This might not be the most helpful answer, but in my case this was caused by a completely unrelated issue in my code.

I was receiving 51 error: cannot find symbol: DataBindingComponent errors (in every single Data Binding generated class), and I spent ages removing changes to my XML and ViewModel code trying to find what was causing it.

The problem actually lay in an invalid change I made a Room model. I guess that a Room error might have been obfuscated by all the databinding errors, but the Debug/Scan logs in the terminal didn't point to it.

So review all recent code, even seemingly unrelated changes if you encounter this problem.

Edit: See this SO post about these databinding errors obfuscating other kapt issues (like Room / Dagger)

Upvotes: 4

slenderm4n
slenderm4n

Reputation: 302

if you're using kotlin on android studio 3.2 , replace the distributionurl with this line

distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

and you'll be asked to change the build tools version to the apprpriate version. once you've done that , remove this line from the app level build.gradle file

kapt 'com.android.databinding:compiler:3.0.1

and build the project. it worked for me.

Upvotes: 4

Nestor Lara
Nestor Lara

Reputation: 53

Check your xml files if you are using databinding. I wasted one hour today because I renamed one class and Android Studio decided to make changes in my xml files. For example, I had a class named LiveGameModel and I renamed to LiveGameView and AS decided to make changes in xml files which are not related to this view. I know, this bug doesn't make sense.

Upvotes: 0

Sergio
Sergio

Reputation: 2473

This one is a very tricky bug with android studio and databinding! I had to test all this proposed solutions and some more for an entire day to finally make the databinding compile at least.

So I had to disable all databindind settings in gradle.properties file, just comment these lines or delete them:

android.databinding.enableV2 = true
android.enableExperimentalFeatureDatabinding = true

remove buildToolsVersion from build.gradle and have the following sdk versions:

compileSdkVersion 27
defaultConfig {
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Plus a couple more clean/rebuild invalidate caches and restart, and it FINALLY compiled. AS engineers are great at creating bugs!

Upvotes: 0

sami qaiser
sami qaiser

Reputation: 1

I was having the same issue. Fixed it by adding google() to Project build.gradle

allprojects {
    repositories {
        jcenter()
        **google()**
    }
}

make sure you add in allProjects

Upvotes: 0

Moses Gitau
Moses Gitau

Reputation: 1057

Adding these lines in grade.properties helped me save the issue

android.enableExperimentalFeatureDatabinding = true
android.databinding.enableV2=true

Upvotes: 0

Marian Pavel
Marian Pavel

Reputation: 2886

Ok, so those who are wondering how I fixed this. The solution is quite simple but probably you won't like it.

I had to move all my classes that were used in data binding in the project root package and after it started to work again.

Upvotes: 2

AbuBakar Sohail
AbuBakar Sohail

Reputation: 11

I was having the same issue. I disabled databinding in gradle properties and it worked. dataBinding.enabled = false

Upvotes: -10

Alex_297
Alex_297

Reputation: 249

To fix this error in Java project you shouild rollback to supportLibraryVersion - 27.0.2 from 27.1.0 Works fine with AndroidStudio 3.1 and com.android.tools.build:gradle:3.1.0

Waiting for a fix from the Google

Upvotes: 1

Sagar Kacha
Sagar Kacha

Reputation: 8463

You need to change three things when you update from Android Studio 3.0.1 to 3.1.0. This is as listed below

1) You need to change in gradle.wrapper-properties in distributionUrl. Your URL must be distributionUrl=https://services.gradle.org/distributions/gradle-4.4-all.zip

enter image description here To enter image description here

2) Need to update data binding dependancy in app level gradle file from kapt 'com.android.databinding:compiler:3.0.1' to kapt 'com.android.databinding:compiler:3.1.0'

enter image description here

And if you are develop using kotlin then,

3) Third and last thing is need to update kotlin gradle plug in classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.30" to classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.31" in project level gradle dependancy. Also you can update build gradle version as seen in below image.

enter image description here

after all above step just clean build and rebuild project. Hope it will work to solve your problem.

Thanks!! Happy coding!!

Upvotes: 2

Related Questions