25

I have tried to use the beta features(data binding) in the android studio. After following the guides from the android studio, I can find the related class DataBindingInfo in the android studio. But the databinding class does not generate after I create the project. Can someone help?

build.gradle for the app module

apply plugin: 'com.android.application'

apply plugin: 'com.android.databinding'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.pigfamily.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.0.1'
}

build.gradle for the project

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        dependencies {
            classpath "com.android.tools.build:gradle:1.3.0"
            classpath "com.android.databinding:dataBinder:1.0-rc1"
        }
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>

        <variable
            name="user"
            type="com.example.pigfamily.myapplication.User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
 >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}" />

    </LinearLayout>
</layout>

MainActivity.java

package com.example.pigfamily.myapplication;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityMainBinding //cannot resolve the symbol here
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Abhijith Konnayil
  • 4,067
  • 6
  • 23
  • 49
Long Ranger
  • 5,888
  • 8
  • 43
  • 72

15 Answers15

50

enable dataBinding in your App Level build.gradle file

android {
    ...
    dataBinding{
        enabled=true
    }
}
Kishan Solanki
  • 13,761
  • 4
  • 85
  • 82
KishoreKittu467
  • 639
  • 5
  • 6
  • 1
    After doing this, you need to go to : File - Sync Project with Gradle Files to sync the gradle file. – live-love Dec 07 '17 at 18:01
  • I have posted a question and one of the answers told me about the `dataBinding` but I am facing an issue with it kindly check the [answer](https://stackoverflow.com/a/73494115/6854117) – Moeez Aug 27 '22 at 09:45
  • I have been fighting this issue for days and finally after inspecting all layout files, everyplace I could think of and find for misspellings, etc,, I came upon this answer. Bingo it worked like a charm... – Hank Wilson Oct 21 '22 at 17:40
  • Works, thanks! This is so weird, I was just using the template app from Android Studio Dolphin, and one day the error suddenly appeared. Why didn't Google just include this setting in the app level build.gradle file in the template in the first place? – auspicious99 Mar 01 '23 at 06:04
33

I had this same problem. I was digging through gradle settings, cleaning, rebuilding... nothing worked. Finally all I had to do was restart Android Studio

https://www.bignerdranch.com/blog/descent-into-databinding/

As of this writing, this integration needs a little jump-start to get going. To make ListItemCrimeBinding available after adding the tag, you must restart Android Studio, then rebuild the project.

tenprint
  • 1,123
  • 1
  • 11
  • 29
  • I have posted a question and one of the answers told me about the `dataBinding` but I am facing an issue with it kindly check the [answer](https://stackoverflow.com/a/73494115/6854117)? – Moeez Aug 27 '22 at 09:45
9

to implement binding, make sure that:

  1. in the app.gradle, enable dataBinding:

     apply plugin: 'com.android.application'
     ...
    
     android {
         ...
         buildFeatures {
             dataBinding true
         }
         ...
     }
    
     dependencies {
         ...
     }
    
  2. wrap the existing XML layout in a <layout> element:

     <layout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         xmlns:tools="http://schemas.android.com/tools">
    
         <!-- note: xmlns above only needed if used in original XML -->
         <!-- original XML here -->
    
     </layout>
    
  3. in Activity/Fragment, use the Binding

     // inflating xml_layout_name.xml...
     val binding = XmlLayoutNameBinding.inflate(LayoutInflater.from(context))
    
  4. if is unable to resolve the XxxBinding class (even though it can resolve XxxBindingImpl)

    1. first try changing a character in ANY XML layout with the tag
    2. if that doesn't work, restart Android Studio, sync & rebuild project
Eric
  • 16,397
  • 8
  • 68
  • 76
  • I have posted a question and one of the answers told me about the `dataBinding` but I am facing an issue with it kindly check the [answer](https://stackoverflow.com/a/73494115/6854117)? – Moeez Aug 27 '22 at 09:45
3

Add on build.gradle(:app) viewBinding true

android {
  buildFeatures {
        viewBinding true
    }

}
Mohit Singh
  • 1,402
  • 1
  • 10
  • 19
  • I have posted a question and one of the answers told me about the `dataBinding` but I am facing an issue with it kindly check the [answer](https://stackoverflow.com/a/73494115/6854117)? – Moeez Aug 27 '22 at 09:46
1

Either click sync if a dialogue pops up, hit the sync button next to save, or restart Android Studio.

Ethan Reinsch
  • 137
  • 1
  • 10
1

If clear Project and Rebuild Project don't work for you try the simple solution. It sometimes doesn't work when you clone a project from remote git branches.You need to just close your project and delete the .gradle and .idea folder of your android project. Then reopen the project.

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
1

For me I had to add:

buildFeatures {
    viewBinding true
}

to my build.gradle file, then run File->Sync Project With Gradle Files

Zock77
  • 951
  • 8
  • 26
0

In my case, my layout file had some errors and therefore AS was not creating the binding class. I could find it by expecting the XML file and seeing the error messages.

Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39
0

be sure you add data binding in your build.gradle

and after that sync project after that Restart and clear caches from the file in android studio. this way solve my problem

Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
0

For my case I added :

         dataBinding {
                  enabled=true
         }

Then went to File > Settings > Gradle > Untick offline work> Apply> Ok Then cleaned my project.

bensalcie
  • 801
  • 5
  • 9
0

I've seen this answer nowhere so I suggest: if you added some activity from context menu by clicking on some package like 'data' or 'ui', Android Studio saved path to binding like so: import com.damian.yourappname.ui.databinding.ActivitySearchBinding;

I tested it a few times with my previous projects and it occurs only when sample activity like ScrollingActivity is added to a package other package for all classes. import com.damian.yourappname.databinding.ActivitySearchBinding;

So best way is just creating sample activity in root folder and after gradle sync moving to some package

baobab159
  • 51
  • 6
0

I found a way without restart or invalidate cache. I thing this because binding need to restart or something, so I re-write private ActivityMainBinding binding; and viewBinding work again.

I don't know why this work but this save my time than restart/invalidate cache

Sasmita
  • 137
  • 1
  • 12
0

In my case i deleted build, build-cache folders in project scope, then build folder in app module. Afterwards restarted PC and it started working

Martynas B
  • 2,843
  • 2
  • 12
  • 15
0

I came to this question based on the title (cannot resolve symbol). Oddly the description is about a different issue (databinding class does not generate) and I think all the existing answers are about that. Basically, the OP asked two questions.

I'm sure one does need the dataBinding{enabled=true} config to make data binding work, but adding that config does not fix the 'cannot resolve symbol' error. The namespace changed to androidx. Change the import to androidx.databinding.DataBindingUtil

steve
  • 1,021
  • 1
  • 14
  • 29
-1

1.Add Below in app gradle

 dataBinding {
        enabled = true
    }

2.In xml layout write below code

<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
  <data></data>
</layout>

UPDATED: CLEAN AND REBUILD THE PROJECT

Rohan Lodhi
  • 1,385
  • 9
  • 24