3

My android studio version is

Arctic Fox | 2020.3.1 Patch 3
Build #AI-203.7717.56.2031.7784292, built on October 1, 2021

After updating IDE and kotlin to 1.6, I opened a project I was working on and Android Stidio marked data binding fields as red, showing the following error:

enter image description here

In text:

Cannot access 'androidx.databinding.Observable' which is a supertype of 
'com.cioccarellia.wordbucket.databinding.ActivityMainBinding'. 
Check your module classpath for missing or conflicting dependencies

The weird thing is that everything compiles and the app works just fine, these errors are just in the IDE, and they pop up every time I access views through data binding.

I haven't changed my dependencies since updating android studio. Anyone else has the same problem / solution for this situation?

The Main Activity xml:

<?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.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.cioccarellia.wordbucket.activity.MainActivity">

        ...

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>

General build.gradle:

buildscript {
    ext {
        kotlin_version = "1.6.0"
    }

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        maven { url 'https://jitpack.io' }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
        classpath "com.google.dagger:hilt-android-gradle-plugin:2.40.3"
        classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
        classpath 'com.google.firebase:firebase-crashlytics-gradle:2.8.1'
        classpath "com.github.ben-manes:gradle-versions-plugin:0.39.0"
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir

Application build.gradle:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "kotlin-kapt"
    id "dagger.hilt.android.plugin"

    id "com.google.gms.google-services"
    id "androidx.navigation.safeargs.kotlin"
    id "com.github.ben-manes.versions"
    id "kotlin-parcelize"
    id "com.google.firebase.crashlytics"
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.cioccarellia.wordbucket"
        minSdk 23
        targetSdk 31

        versionCode 1
        versionName "1.0.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary true
    }

    buildTypes {
        debug {
            versionNameSuffix ".dev"
        }

        release {
            debuggable false

            // Enables code shrinking, obfuscation, and optimization
            minifyEnabled true

            // Enables resource shrinking, which is performed by the
            // Android Gradle plugin.
            shrinkResources true

            // Includes the default ProGuard rules files that are packaged with
            // the Android Gradle plugin. To learn more, go to the section about
            // R8 configuration files.
            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
    }

    sourceSets {
        main.res.srcDirs = [
                "src/main/res",
                "src/main/res-public"
        ]

        android.sourceSets.all {
            java.srcDir("src/$name/kotlin")
        }
    }

    lintOptions {
        // if set to true (default), stops the build if errors are found.
        abortOnError false
        // if true, only report errors.
        ignoreWarnings false
    }
}

dependencies {
    // AndroidX
    implementation "androidx.core:core-ktx:1.7.0"
    implementation "androidx.appcompat:appcompat:1.4.0"
    implementation "androidx.constraintlayout:constraintlayout:2.1.2"
    implementation "com.google.android.material:material:1.6.0-alpha01"
    implementation "androidx.legacy:legacy-support-v4:1.0.0"

    // Lifecycle
    def lifecycle_version = "2.4.0"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"

    // Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.5"


    // Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"



    // DI -> Hilt
    implementation "com.google.dagger:hilt-android:2.40.3"
    kapt "com.google.dagger:hilt-compiler:2.40.3"



    // Import the Firebase BoM
    implementation platform("com.google.firebase:firebase-bom:28.4.2")

    // When using the BoM, you don"t specify versions in Firebase library dependencies
    implementation "com.google.firebase:firebase-crashlytics-ktx"
    implementation "com.google.firebase:firebase-analytics-ktx"
    implementation "com.google.firebase:firebase-auth-ktx"
    implementation "com.google.firebase:firebase-firestore-ktx"


    // LeakCanary
    debugImplementation "com.squareup.leakcanary:leakcanary-android:2.7"


    // Logging
    implementation "com.jakewharton.timber:timber:5.0.1"


    // Styles
    implementation 'com.airbnb.android:paris:2.0.1'
    kapt 'com.airbnb.android:paris-processor:2.0.1'


    // JUnit Default Dependencies
    testImplementation "junit:junit:4.13.2"
    androidTestImplementation "androidx.test.ext:junit:1.1.3"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.4.0"
}
ci0ccarellia
  • 795
  • 9
  • 26
  • *possible duplicate* - just checkout this [link](https://stackoverflow.com/questions/20226912/android-studio-inline-compiler-showing-red-errors-but-compilation-with-gradle-w). also you can try invalidate cache and restart the IDE then rebuild and sync project. – Ali Babadi Dec 04 '21 at 22:17
  • I tried, but it doesn't fix the issue – ci0ccarellia Dec 07 '21 at 10:34

2 Answers2

0

Try to restart the IDE. If that does not work, clean the project be going to Build > Clean Project at the top of the window, then completely rebuild it by going to Build > Rebuild Project.

Edit: Another reason is that Android Studio will no longer support binding to straight elements, so you have have to specify the binding yourself.

To do this, add BuildFeatures in the application gradle file:

android {
compileSdk 31

//This right here
buildFeatures{
    viewBinding true
}

defaultConfig {
    applicationId "com.cioccarellia.wordbucket"
    minSdk 23
    targetSdk 31

    versionCode 1
    versionName "1.0.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary true
}

Then create a new binding by adding this to your activity file:

//Declare the binding
private lateinit val binding : ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    //Initialize the binding
    binding = ActivityMainBinding.inflate(layoutInflater)

    super.onCreate(savedInstanceState)
    //Change the content view to the root of the binding
    setContentView(binding.root)
}

Then reference binding when using your views:

binding.toolbar.title = bucket.name
ToxicFlame427
  • 355
  • 2
  • 13
  • Putting buildFeatures outside defaultConfig does not work, and this worked before kotlin 1.6. I tried invalidating caches but still does not work – ci0ccarellia Dec 07 '21 at 10:34
  • Do you get a message from Android Studio that your project can be upgraded? Also, make sure that your dependencies are up-to-date. – ToxicFlame427 Dec 21 '21 at 18:03
0

Updating android studio fixed the issue

ci0ccarellia
  • 795
  • 9
  • 26