4

After removing the existing DataBinding from the project, I also wanted to get rid of the kotlinx.android.synthetic and replace it with ViewBinding.

However, after enabling ViewBinding in my project, I'm still not able to do a proper setup.

Trying to invoke the ViewBinding into my MainActivity or in one of my fragments I get the following error in the inflate method:

Cannot access 'no_name_in_PSI_...' which is a supertype of 'com.example.projectname.databinding.ActivityMainBinding'. Check your module classpath for missing or conflicting dependencies

For example:

MainActivity.kt

class MainActivity : BaseActivity<SharedViewModel>() {
      private lateinit var binding: ActivityMainBinding
      ...
      override fun onCreate(savedInstanceState: Bundle?) {
        setTheme(R.style.AppTheme)
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
      }
}

enter image description here

My build.gradle (:app) is properly set up:

build.gradle (:app)

android {
  ...
  buildFeatures {
        viewBinding true
    }
}

I've used ViewBinding in another project of mine and it worked out of the box. Please help. Thank you so much.

BWappsAndmore
  • 491
  • 3
  • 17
  • Do you have this `tools:viewBindingIgnore="true"` in root tag of your activity_main.xml file? – Jeel Vankhede Jan 29 '21 at 15:43
  • @JeelVankhede thank you so much for your quick response. I've added this tag into the `activity_main.xml` file. The error dissapeared, but I'm still facing the same issue in the fragments where I actually need to use the `ViewBinding`. – BWappsAndmore Jan 29 '21 at 15:50
  • That's weird because this tag basically tells view binding extension whether to generate bindings for specific file or not. May be cleaning-rebuilding project helps.. – Jeel Vankhede Jan 29 '21 at 15:52
  • I've cleaned, rebuilt, invalidated caches, restarted, deleted the `idea` folder, did almost everything, but still it's not working. – BWappsAndmore Jan 29 '21 at 15:58
  • I faced the same issue a few days back, try using the DataBindingUtil class to do the inflation, that worked for me. – rsd96 Jan 31 '21 at 11:00

3 Answers3

2

I had the same error. I added the layout tag in my xml files and that's not correct. Layout tag is for data binding but here we are only enabling view binding feature. That's why you are getting that error. Just remove the layout tag. Edit- I am elaborating this answer since it is not clear. Remove the tag from your XML file. Reason- As stated in android data binding documentation, is used when "dataBinding" is enabled. But here you have enabled "viewBinding" for which should not be used in XML file.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Abhishek Dutt Jan 08 '22 at 03:33
  • I found this answer correct. Just remove the ```` tag in your xml, because that is the configuration for data binding, not view binding. – moonLander_ Feb 14 '23 at 09:28
0

I had same problem yesterday. Only new update of android studio to version was working for me.

kacen
  • 1
0

Try to remove .idea folder

  • Step 1- Remove .Idea Folder
  • Step 2- Close android studio
  • Step 3- Reopen android studio with same project path (This will genarate new .Idea folder)

https://stackoverflow.com/a/71036526/4741746

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55