I’m having trouble implementing data binding in my Android Studio project. I get the following error:
Cannot access ‘androidx.databinding.Observable’ which is a supertype
of ‘com.russ.beatbox.databinding.MainActivityBinding’. Check your
module classpath for missing or conflicting dependencies
Even though i have all the Gradle dependencies and the binding class is generated. I’ve tried rebuilding, invalidate cache/restart, clean project, renaming the file, and using different Gradle import syntax, nothing seems to work. Is this Android Studio bug?
Here’s my MainActivity
, the error comes from binding.recView.apply
:
package com.russ.beatbox
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.russ.beatbox.databinding.MainActivityBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: MainActivityBinding =
DataBindingUtil.setContentView(this, R.layout.main_activity)
binding.recView.apply{
layoutManager = GridLayoutManager(context, 3)
}
}
}
My layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rec_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</layout>
And my module Gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdk 31
defaultConfig {
applicationId "com.russ.beatbox"
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures{
dataBinding = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
2
Answers
In your module build.gradle file add the implementation for databinding, fragment, and livedata.
By the time you are done, your file should look like this:
If you already have these dependencies, update them to the latest version. Note that the minimum compileSdk version to use the latest dependencies in the android library is 31, so you may want to update your project accordingly if it’s not up to date
Try with removing
layout
tag form xml file it is used when you have data that need to bind with viewsalso try to update build file like