NAM
NAM

Reputation: 147

Can not resolve symbol 'DataBindingUtil' in android?

I want to DataBindingUtil class in java but I cannot import this. It seems to have no DataBindingUtil class even android.databinding.DataBindingUtil in my project. I have tried a reinstall but it not solve my problem. This is my code:

import android.databinding.DataBindingUtil;//cannot import


public class DeviceFragment extends Fragment{

    private DeviceViewModel deviceViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        //cannot use it
        DeviceFragmentBinding binding = DataBindingUtil.setContentView(this, R.layout.device_fragment);

And my gradle file (I have enabled viewBinding):

apply plugin: 'com.android.application'

android {
    viewBinding {
        enabled = true
    }

    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.example.aqiapp"
        minSdkVersion 16
        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'
        }
    }

    //custom
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.2.2'
    implementation 'androidx.navigation:navigation-ui:2.2.2'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    //custom
    implementation 'com.squareup.retrofit2:retrofit:2.8.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.8.2'
    implementation 'com.google.dagger:dagger:2.27'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.27'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'
}

Upvotes: 0

Views: 3394

Answers (2)

Karan
Karan

Reputation: 11

I faced same issue when upgarded to SDK 31, and the reason was databinding is no more under android package but androidx

It worked after import android.databinding.DataBindingUtil; was changed to import androidx.databinding.DataBindingUtil;

Upvotes: 1

Công Hải
Công Hải

Reputation: 5241

You should enable dataBinding instead of viewBinding try this

android {
    ...
    dataBinding {
        enabled = true
    }
}

Upvotes: 3

Related Questions