15

I am getting errors after i enabled view binding in my project.

I have tried resyncing the project, invalidating cache. My app gradle file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.learning.aboutme"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    viewBinding.enabled = true

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

My project gradle file

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
     ext.kotlin_version = '1.3.72'
     repositories {
          google()
          jcenter()

     }
     dependencies {
         classpath 'com.android.tools.build:gradle:3.6.3'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

          // NOTE: Do not place your application dependencies here; they belong
          // in the individual module build.gradle files
     }
}

allprojects {
     repositories {
          google()
          jcenter()

     }
}

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

My activity main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingStart="@dimen/padding"
    android:paddingEnd="@dimen/padding"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/name_text"
        style="@style/name_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textAlignment="center" />

    <EditText
        android:id="@+id/nickname_edit"
        style="@style/name_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/name_it_what_is_your_nickname"
        android:inputType="textPersonName"
        android:textAlignment="center" />

    <Button
        android:id="@+id/done_button"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/layout_margin"
        android:fontFamily="@font/roboto"
        android:text="@string/done" />

    <TextView
        android:id="@+id/nickname_text"
        style="@style/name_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:visibility="gone" />

    <ImageView
        android:id="@+id/star_Image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/layout_margin"
        android:contentDescription="@string/yellow_star"
        app:srcCompat="@android:drawable/btn_star_big_on" />

    <ScrollView
        android:id="@+id/bio_scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="@dimen/spacing_multiplier"
            android:text="@string/bio"
            android:textColor="@android:color/black" />
    </ScrollView>
</LinearLayout>

Here the android studio gives a warning on android:contentDescription,textAlignment, lineSpacingMultiplier, text etc

Here is my main activity.kt file:

package com.learning.aboutme

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.learning.aboutme.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(R.layout.activity_main)
    }

    private fun addNickname(view: View) {

    }
}

Here android studio says: Cannot access android.viewbinding.ViewBinding which is a supertype of com.learning.aboutme.databinding.ActivityMainBinding. On ActivityMainBinding.inflate()

I tried creating a new project and moving my files there but the same errors ( Tried moving activity_main.xml and the same errors.

How to fix this?

yakobom
  • 2,681
  • 1
  • 25
  • 33
Wizard28
  • 355
  • 1
  • 3
  • 12

13 Answers13

43

For me the error went away when I added

implementation 'com.android.databinding:viewbinding:4.0.1'

to the build.gradle file.

This dependency contains the class android.viewbinding.ViewBinding which AndroidStudio could not access.

uwe
  • 3,931
  • 1
  • 27
  • 19
  • 4
    We use `compile` instead of `implementation` so the dependency won't be included in the APK since this issue only appears in Android Studio and not while running the app. – John A Qualls Dec 18 '20 at 19:26
  • 6
    Rather `compileOnly` in modern Gradle. As docs [source](https://blog.gradle.org/introducing-compile-only-dependencies) state: "Dependencies whose API is required at compile time but whose implementation is to be provided by a consuming library, application or runtime environment." – Michał Dobi Dobrzański Oct 10 '21 at 11:45
  • In our project we use androidX dependency but I still see the error in android studio the it require `android.viewbinding.ViewBinding` class. This is kind of AS glitch. Hope it will be fixed soon. – Yazon2006 Jan 02 '22 at 10:31
12

In my case the following steps (clearing of cache) did help:

Step 1 : open Terminal and goto your project path in terminal.
Step 2 : Optional Hit this command git clean -xfd or safe variant git clean. Be careful with -xfd this as this removes untracked files and .gitignore files
Step 3 : Goto : File -> Invalidate caches/restart.

Roman Nazarevych
  • 7,513
  • 4
  • 62
  • 67
  • Hey! this worked – Abhijith Brumal Nov 25 '21 at 15:40
  • this actually solved it for me too, ty :) – JozeRi Feb 23 '22 at 10:39
  • 4
    BE CAREFUL WITH THIS COMMAND - it will delete ANY file that is referenced in gitignore. if you have some untracked security files or keys or whatever that you don't push but have in your project, they will be deleted. people should stop posting commands and not explaining what they do – TootsieRockNRoll Mar 17 '22 at 19:19
  • 1
    @Trevor Sorry that this messed up your project, If you use IDEA you can use Local history to restore you project state https://www.jetbrains.com/help/idea/local-history.html – Roman Nazarevych May 12 '22 at 11:07
11

Facing same issue exact error yesterday. Solution work for me is

  • Step 1- Remove .Idea Folder
  • Step 2- Close android studio
  • Step 3- Reopen android studio with same project path (This will genarate new .Idea folder)

and Error is gone

Making sure

  1. android.useAndroidX=true. inside gradle.properties file
  2. inside app build.gradle
 buildFeatures {
       viewBinding true
 }
  1. Always Use folder structure with small case latter only class name is allowed to be Uppercase
  2. If you are using any Copyright-Comment at top of xml or Layout tag without variable tag or we can say without any data then remove that and try to clean/rebuild project
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
3

I found the solution from here: https://cdmana.com/2021/05/20210511224308742x.html . Just add at gradle.properties

//...
android.enableD8=true
android.injected.testOnly=false

I hope it will solving your problem

fahrizal89
  • 598
  • 5
  • 13
2

Had the same problem with that error

android.viewbinding.ViewBinding which is a supertype of com.learning.aboutme.databinding.ActivityMainBinding. On ActivityMainBinding.inflate()

What I did and solved it was erasing from the build.grade module

viewBinding{
   enabled = true
}

And Sync it, after re added the erased code from the module and sync again and voila! That solved it for me. Thanks for all other responses tho

Junaid Khalid
  • 811
  • 2
  • 11
  • 26
1

I faced this exact error today when I used viewBinding in my fragment so what helped was

  1. Clean and Rebuild project.
  2. Invalidate caches and restart.

Somehow the Invalidate caches and restart to be specific solved the issue.

Daniel Iroka
  • 103
  • 8
0

Error Solved for me By: Adding below line in the gradle.properties(Project Propoerties) file:

android.useAndroidX=true

Sijan Neupane
  • 116
  • 2
  • 8
0

You can try this solution:

add "multiDexEnabled true" in gradle file like below

android {
compileSdk 30

defaultConfig {
    applicationId "com.example.test"
    minSdk 19
    targetSdk 30
    versionCode 1
    versionName "1.0"
    multiDexEnabled true

    testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}}
0

i adding dependencies in build.gradle

  • implementation 'com.android.databinding:viewbinding:4.0.1'
0

I faced the same problem and I fix it by doing the steps below:

1- go to build.gradle file and remove buildFeatures { viewBinding = true } if you already put it there
2-synch
3-Clean and Rebuild the project.
4-put viewBinding back again buildFeatures { viewBinding = true}
5-synch again

jenos kon
  • 504
  • 4
  • 7
0

For me the error went away when I added

implementation 'com.android.databinding:viewbinding:7.3.1' to the build.gradle file.

This dependency contains android.viewbinding.ViewBinding which android studio may not be able to access without this dependency.

JustSightseeing
  • 1,460
  • 3
  • 17
  • 37
0

To me this happened because I had my layout wrapped into <layout> tag. This tag is only needed if you use data binding and causes issues when used with view binding alone.

Primož Ivančič
  • 1,984
  • 1
  • 17
  • 29
-1

Here's what worked for me:

I initially had the build.gradle(Module: App_name.app) file opened in Android Studio.

Then I opened the "Project Structure" window ... There, at the left pane, I opened the "suggestions" tab.

Several Suggestions for updating things were there, which I selected and pressed OK , after which they started getting updated from internet, and after that, did Gradle sync, and all Errors were gone in the MainActivity.kt file.

yakobom
  • 2,681
  • 1
  • 25
  • 33
some1
  • 1
  • 1