40

I'm currently trying out the new ViewBindings but i'm not getting them to work. I'm on Android Studio 3.6.1. I just created a new project and added viewBinding.enabled = true to my modules build.gradle. But when I try to access the MainActivityBinding class, it says it cannot resolve the symbol. Autocomplete doesn't find anything resembling a binding class. I also tried with a different project using Kotlin but no success there. AS4.0 doesn't help either. What do I need to do to generate the ViewBinding classes?

My build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    viewBinding {
        enabled = true
    }

    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 29
        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'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Fahima Mokhtari
  • 1,691
  • 2
  • 16
  • 30
Bastian Block
  • 576
  • 1
  • 5
  • 10
  • 3
    Ensure you call `File`->`Sync Project with Gradle Files` in Android Studio to get the view binding files generated. – Bruno Bieri Apr 16 '21 at 12:29

8 Answers8

146

I couldn't find my ViewBinding files until it dawned on me that the bindings were taking their names from the XML files ("fragment_home.xml") and not from the class ("HomeFragment.kt"). So I couldn't find them at HomeFragmentBinding but I found them at FragmentHomeBinding.

I found it helpful to think of each ViewBinding as a helper singleton that has been created as that XML file's delegate.

(Edited to removed obsolete Gradle stuff)

John Gorenfeld
  • 2,015
  • 1
  • 18
  • 27
  • 5
    Thank you. The EDIT helped me. They really should name them after the activity and not the xml though. Strange choice. – Asim May 03 '20 at 08:18
  • 4
    Glad I could help. Thinking about it more, I think the reason is this: A Binding could exist for XML even if no fragment for it exists. But binding could not exist for a fragment unless the XML existed. Still, though, in the big picture it could be more intuitive. – John Gorenfeld May 05 '20 at 16:23
  • @Asim I disagree, as John says, you're specifying the binding on the XML file, _not_ the other way around. Thanks John for the (not so) obvious solution!! – jonny Jan 06 '21 at 19:19
  • 1
    @jonny Yes ofc now that I've gotten used to the naming scheme, I realize that naming them after activities would be counter-productive since view binding works for every xml and not just activity or fragment ones. – Asim Jan 06 '21 at 19:32
  • How dumb I am – Sk Suraj Apr 09 '23 at 06:49
4

i have just changed the names of the layouts and it worked just fine

  • I had gone around in circles for days until I read this. So simple but so effective! Still not sure why the caches weren't getting invalidated or remade, even deleting the Build folder didn't help. But this. This solved my issue. – Brandon Mar 23 '23 at 02:33
2

In my case I was trying to bind View

added build.gradle (module)

buildFeatures{
 dataBinding true
 viewBinding true
}

previous one was

viewBinding{
enabled = true
}
Samir
  • 6,405
  • 5
  • 39
  • 42
1

if you have entered fragment/layout name manually, check the spelling for keyword of the layout name like "fragment", a mistake in this would generate a binding file with the same name

Sushruth S
  • 31
  • 1
  • 3
0

Make sure you have added the build features on your module Gradle file

android {

  buildFeatures {
      viewBinding true
  }
}

And make sure your are trying with the correct layout file name. You might be working on ABCFragment but it's layout could be named as small_mistake.xml Then your BindUtil will be something like this.

SmallMistakeViewBinding
Heshan Sandeepa
  • 3,388
  • 2
  • 35
  • 45
0

This Answer is writed on

11.11.2022

in order to generate viewbinding, here is solution:

android {

buildFeatures {
        viewBinding true
    }

}

add above code inside android tag in app module.

your ex- layout files will ne be generated, whenever you create new layout, they will be seen... So it work only with new layout files.. You dont need to add layout tag in every xml...

Ucdemir
  • 2,852
  • 2
  • 26
  • 44
0

Double check that the layout name is correct, name may contain a typo.

Falchio
  • 174
  • 1
  • 14
-3

A couple of things to make sure you have done:

  • Make sure you have a properly formatted layout file, with the layout tag at the top declaring your variables
  • Try a gradle sync

I haven't checked, but I know currently a few members of my team have been reporting binding classes are no longer being resolved in Android studio since upgrading to 3.6, however the project still builds fine so they are actually there, could be a bug with Android studio?

hmcgr
  • 33
  • 3