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' }
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.
Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With