Bastian Block
Bastian Block

Reputation: 616

View Bindings not generating

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'
}

Upvotes: 44

Views: 27927

Answers (8)

Falchio
Falchio

Reputation: 606

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

Upvotes: 0

Ucdemir
Ucdemir

Reputation: 3098

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...

Upvotes: 0

John Gorenfeld
John Gorenfeld

Reputation: 2425

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)

Upvotes: 164

Heshan Sandeepa
Heshan Sandeepa

Reputation: 3688

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

Upvotes: 1

Sushruth S
Sushruth S

Reputation: 41

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

Upvotes: 2

Samir
Samir

Reputation: 6605

In my case I was trying to bind View

added build.gradle (module)

buildFeatures{
 dataBinding true
 viewBinding true
}

previous one was

viewBinding{
enabled = true
}

Upvotes: 4

Mohamed Ali
Mohamed Ali

Reputation: 61

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

Upvotes: 5

hmcgr
hmcgr

Reputation: 33

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?

Upvotes: -3

Related Questions