Android data binding: cannot resolve character

I tried to use beta functions (data binding) in Android studio. Following the instructions from android studio, I can find the associated DataBindingInfo class in android studio. But the data binding class is not generated after the project is created. Can anyone help?

build.gradle for application 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); } } 
+13
source share
5 answers

enable data binding in your build.gradle application level file

 android { ... dataBinding{ enabled=true } } 
+35
source

I had the same problem. I delved into the gradle settings, cleaned, rebuilt ... nothing worked. Finally, I only had to restart Android Studio

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

At the time of this writing, this integration needs a little leap to get going. To make ListItemCrimeBinding available after adding the tag, you must restart Android Studio and then rebuild the project.

+18
source

Either click the sync button, if a dialog appears, click the sync button next to the save or restart Android Studio.

+1
source

To implement the binding, make sure that:

  1. in app.gradle enable dataBinding :

     apply plugin: 'com.android.application' ... android { ... dataBinding { enabled = true } ... } dependencies { ... } 
  2. wrap existing XML layout in <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 binding

     val binding = XmlLayoutNameBinding.inflate(context.layoutInflater) // use the layout 
  4. if you cannot resolve the XxxBinding class (even if it can enable XxxBindingImpl ) restart Android Studio , synchronize & rebuild the project

0
source

1. Add below in Gradle app

  dataBinding { enabled = true } 

2. In XML format write the code below

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

UPDATED: CLEAN AND REbuild A PROJECT

-1
source

All Articles