Search code examples
androidandroid-studiokotlinandroid-viewbinding

cannot access viewBinding


I'm now using viewBinding with Kotlin.

here is my build.gradle build.gradle

enable ViewBinding and has auto import ViewBinding dependency.

but Android Studio still shows error like this Android Studio Error

but I can build and run with no problem.

and if I add viewBinding in dependencies, then the error is gone.

Does anyone know why?

UPDATE:

I know why. I'm using AndroidX, and the IDE says can not access 'android.viewbinding.ViewBinding', but my dependency is AndroidX, it is 'androidx.viewbinding.ViewBinding', package name changed.

SOLUTION: add android.useAndroidX=true to project gradle.properties file.

global gradle.properties setting does not work, don't know why.


Solution

  • 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 samll 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