Sattar
Sattar

Reputation: 2633

Unresolved reference: databinding in Android studio 4.1.3

I face a wired behavior after following a lot of answers here in SO and other answers to solve but still stuck in it.

I added to app Gradle

  buildFeatures {
    dataBinding true
}

and my activity_main.xml is wrapped by layout

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

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.home.MainActivity" />
  </layout>

and I tried as well to add these to gradle.properties

android.databinding.enableV2=true
android.useAndroidX=true

and I already have

android.useAndroidX=true
android.enableJetifier=true

Main Activity it shows no error when open the activity and as well when changing the name to another it also appears to be generated but whenever I run I got this error

Unresolved reference: databinding

enter image description here

How I can solve this?

**update 1 I already did clean and build multiple times, closed the android studio and opened it, as well did Invalidate Cache/Restart.

Upvotes: 3

Views: 5352

Answers (3)

Yilmaz
Yilmaz

Reputation: 49182

After you add this to the gradle file:

buildFeatures {
    dataBinding true
}

Make sure you click Sync Now. Otherwise you wont be able to do the next step.

enter image description here

Next step, in xml file, on

<androidx.constraintlayout.widget.YourAppLayout xmlns:android="http://schemas.android.com/apk/res/android"

right click, and you will get this menu:

enter image description here

Click on "convert to data binding layout". If you do not get this setting in your android setup, this should be a boilerplate layout file

<?xml version="1.0" encoding="utf-8"?>
<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">
    <data>
    </data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

Upvotes: 2

werfar
werfar

Reputation: 121

Note that when using databinding the view-ids change names as described here: https://developer.android.com/codelabs/kotlin-android-training-data-binding-basics#2

Step 4: Use the binding object to replace all calls to findViewById() You can now replace all calls to findViewById() with references to the views that are in the binding object. When the binding object is created, the compiler generates the names of the views in the binding object from the IDs of the views in the layout, converting them to camel case. So, for example, done_button is doneButton in the binding object

Upvotes: 0

Sajad Hosseini
Sajad Hosseini

Reputation: 155

Please go to File->Invalidate Cache/Restart-> Invalidate and Restart

Upvotes: 4

Related Questions