View Bindings not generating

Viewed 17003

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

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)

The layout's root tag has to be <layout>. Make sure it's not your case.

Had to change my original layout XML file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/welcome_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="Hello World!" />

</androidx.constraintlayout.widget.ConstraintLayout>

to

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/welcome_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:text="Hello World!" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

In my case I was trying to bind View

added build.gradle (module)

buildFeatures{
 dataBinding true
 viewBinding true
}

previous one was

viewBinding{
enabled = true
}

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

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

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

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?

Related