Vitalii
Vitalii

Reputation: 11071

Android data binding class is not generated in my new module

I added a module to my project and now I want to put there some fragments.

In my fragment I initialize data binding like this

class MyTestFragment : Fragment() {

    private lateinit var binding: 
    MyTestFragmentNewBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        binding = DataBindingUtil.inflate(inflater, R.layout.my_test_fragment_new, container, false)

        binding.test = DataModel("zzz")
        return binding.root
    }
}

Then in Android Studio I click make module and it compiles. After it I try to run my app and see two errors Unresolved reference: MyTestFragmentNewBinding and

import com.example.common.databinding.MyTestFragmentNewBinding

I see Unresolved reference: databinding I tried a lot of solutions like rebuilding, invalidating cache, closing and reopening Android Studio, nothing works. The most interesting thing is that after renaming of layout.xml and after importing new reference to a binding class it works till the next build. Than the same issue.

Did someone have something like this? What can be wrong? It seems that in my second module probably something delete binding class during build or something like this

Upvotes: 4

Views: 3326

Answers (3)

Dragoslav Petrovic
Dragoslav Petrovic

Reputation: 118

After losing an hour scouring through all the possible reasons why this is not working for me, I found out that I made a simple mistake...

instead of:

    dataBinding {
    enabled = true
}

I had:

    dataBinding {
    true
}

It was a silly mistake.
Hope that someone will find it useful.

Upvotes: 0

qianlv
qianlv

Reputation: 550

seem likes android studio bugs. Try to rename the layout file name and rebuild the module.

Upvotes: 1

Vitalii
Vitalii

Reputation: 11071

Thanks to this thread I found the forgotten part that was already present in my main project.

Remember, to enable databinding you need to add

dataBinding {
    enabled = true
}

to your build.gradle file but if you use Kotlin don't forget to add

apply plugin: 'kotlin-kapt'

plugin too to your build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 27
    defaultConfig {
       ... 
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

Upvotes: 0

Related Questions