databinding.ActivityMainBinding unable to be imported in Android project

Viewed 7798

I have a working Android Studio project, also installed via USB on my smartphone, it's a simply basic calculator. Now I have to rewrite the code for a lesson I teach, but Android Studio denies this import

import [com.username.projectname].databinding.ActivityMainBinding;

The app uses dataBinding I have enabled in Module:app and resync everything.

The old project works, the new one not, with the same code, cause of this import is refused.

3 Answers

The XML needs a <layout> wrap alike this ...so that the desired class will be generated:

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

    <data class=".databinding.ActivityMainBinding">
        <!-- add variables & imports into here -->
    </data>

    <!-- and add the RelativeLayout into here -->

</layout>

Just add in the module based Grade file, the following

buildFeatures{
  viewBinding true
}

add this code in the build Gradle (:app) file :

buildFeatures{ viewBinding true;}
Related