View Bindings not generating

Bastian Block picture Bastian Block · Feb 29, 2020 · Viewed 8.1k times · Source

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

Answer

John Gorenfeld picture John Gorenfeld · Mar 3, 2020

I couldn't find my ViewBinding files until it dawned on me that the bindings were being named after the XML files ("fragment_home.xml") and not after 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)