I'm unable to set the viewBinding in Android Studio

547 Views Asked by At

I'm stucked like 5 days in this problem! I'm unable to bind the view using viewBinding clause in Android Studio, I believe the problem is in "build.gradle" file.

Initially, I tried to build the project in the way that I knew:

buildFeatures { viewBinding true }

But it didn't work... I went to the Android Official page to see, and apparently they changed the syntax to:

viewBinding { enabled = true }

Even so, it shows an error: the word "enabled" is subbed and do not let the gradle build the program.

I have also tried:

viewBinding { enable = true }
viewBinding { enable }
viewBinding { enable true }
 
buildFeatures { viewBinding { enabled = true } }
buildFeatures { viewBinding.isEnabled } 
 
// Among numerous other possibilities...

I also tried to define the viewBinding globally, in gradle.properties:

android.viewBinding.enabled=true

Did not work.

I looked at the imports as well:

import com.example.mvvmstudy.databinding.ActivityMainBinding
// The mistake does not seems to be here

As well, I tried to verify the gradle version, but it seems to be up to date, I'm using version 8.3 with the 8.1.1 plugin.

What may be the problem?? I have already rebuild the project many many times, I did 2 other new projects and the issue still there hahahahaha

The project is just an exemplo about how MVVM works, I defined a button just to set a Toast and see the binding works, but I'm not been able to do it...

Repository: https://github.com/DaniloVolles/MVVMStudy

I did already seen: 1 - When I add viewBinding in gradle in Android Studio, it comes an error

2 - What's the difference between viewBinding enabled true and buildfeatures viewBinding true

3 - cannot access viewBinding

And some others, but neither worked...

1

There are 1 best solutions below

0
On

In Gradle Kotlin DSL (KTS), the syntax is different from Groovy. Here is the correct way to enable View Binding in an Android project using Gradle Kotlin DSL:

buildFeatures {
        viewBinding = true
}

Instead of

buildFeatures {
        viewBinding true
}

In Gradle Kotlin DSL, assignments are done using the = operator.

For More Info You will find here : https://developer.android.com/topic/libraries/view-binding#setup

and for Gradle Kotlin DSL (KTS) : https://developer.android.com/build/migrate-to-kotlin-dsl#prefix-boolean