skip to Main Content

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


  1. 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:

    plugins {
        id 'com.android.application'
        id 'kotlin-android'
        id 'kotlin-kapt'
    }
    
    android {
        compileSdk 30
    
    
        defaultConfig {
            configurations.all {
                resolutionStrategy { force 'androidx.core:core-ktx:1.6.0' }
            }
            applicationId "org.evolvedigital.unscrambleit"
            minSdk 23
            targetSdk 30
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
    
        buildFeatures {
            dataBinding = true
        }
    }
    
    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'
        implementation 'androidx.databinding:databinding-runtime:7.0.3'
        implementation 'androidx.fragment:fragment-ktx:1.4.0'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
        implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
        testImplementation 'junit:junit:4.+'
        androidTestImplementation 'androidx.test.ext:junit:1.1.3'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    }
    

    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

    Login or Signup to reply.
  2. Try with removing layout tag form xml file it is used when you have data that need to bind with views

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rec_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    

    also try to update build file like

     buildFeatures {
            viewBinding true
            dataBinding true
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search