3

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.

Kartik Agarwal
  • 1,129
  • 1
  • 8
  • 27
Joky
  • 88
  • 1
  • 8

7 Answers7

7

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
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
6

I had this problem or did I? I found that the find in Android Studio was including old projects which did have kotlinx.android.synthetic imports. Could not find out how this occurred so closed studio and rm -rf the .idea. Not only did it cure the problem but speed it back up to a usable state. (Still not as good as VS Code) Hope it helps someone and ask an adult before deleting .idea in your project.

bibble235
  • 788
  • 9
  • 15
1

For me the error fixed when I changed the viewBinding build option to true in the module-level build.gradle file from Groovy mode to Kotlin mode, as shown below :

android {
    ...
    buildFeatures {
        viewBinding = true
    }
}

source : link

Amin
  • 73
  • 8
0

I'm not sure but the problem may lie in the fact that you're using both dataBinding and viewBinding. Try removing one of them then rebuild your Android Studio project.

Andrew Chelix
  • 1,012
  • 11
  • 16
0

I fixed it by adding in build.gradle

plugins{
    id 'kotlin-android'
}

android{
    buildFeatures{
        viewBinding true
    }
}

0

I don't know why but my same problem got solved by removing below line from gradle.properties(project properties)

android.useAndroidX=true
sandesh
  • 27
  • 5
0

I got the same problem and this is the solution I got

  1. go to the project gradle.properties
  2. delete the android.useAndroidX=true line (in my enviroment this is set by default)
  3. Sync gradle
  4. add the deleted line again
  5. Sync again

I noticed that the gradle has a comment that said that this setting was a duplicate so im guessing there was some kind of internal conflict

Used environment:

  • Android Studio Flamingo|2022.1 Patch 1
  • Build #AI-222.4459.24.2221.9971841, built on April 20, 2023
e_l_m_t_7
  • 61
  • 1
  • 4