Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Bindings not generating

Tags:

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' } 
like image 786
Bastian Block Avatar asked Feb 29 '20 12:02

Bastian Block


People also ask

Is findViewById deprecated?

findViewById. Recently Android has announced that with Kotlin 1.4. 20, their Android Kotlin Extensions Gradle plugin will be deprecated and will no longer be shipped in the future Kotlin releases.

Is view binding deprecated?

Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.

What is ActivityMainBinding in Android?

xml so the corresponding generated class is ActivityMainBinding . This class holds all the bindings from the layout properties (for example, the user variable) to the layout's views and knows how to assign values for the binding expressions.


1 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)

like image 192
John Gorenfeld Avatar answered Oct 27 '22 10:10

John Gorenfeld