Android Data Binding library - unresolved reference

青春壹個敷衍的年華 提交于 2020-01-02 08:47:32

问题


I am using Kotlin and trying to use new Data Binding Library, but I get unresolved refernce error for xxxBinding classes.

Top level build.gradle

buildscript {
    ext.kotlin_version = '1.2.30'
    ext.gradle_version = '3.1.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:3.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

App build.gradle

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

android {
    buildToolsVersion "27.0.3"
    compileSdkVersion 27
    defaultConfig {
        applicationId "ru.vstu.clustermonitor"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

repositories {
    maven {
        url 'https://dl.bintray.com/terrakok/terramaven/'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:support-vector-drawable:27.1.1'
    implementation 'com.android.support:support-v4:27.1.1'

    implementation 'com.android.support:recyclerview-v7:27.1.1'                 // RecycleView
    implementation 'com.android.support:cardview-v7:27.1.1'                     // CardView

    implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha01'  // Navigation Architecture Components (remove)

    kapt "com.android.databinding:compiler:$gradle_version"

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}

MainActivity.onCreate

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)        
    val binding : ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
}

ActivityMainBinding should be generated, but It didn't. Is there any way to fix this problem?


回答1:


The problem was in using ConstrainLayout instead of layout.

When using Data Binding, root layout must be "layout" type

Do

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

    <data>
        <variable
            name="MyVariable"
            type="com.mycompany.myproject.MyType"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Views.Fragments.QueueFragment"
        android:background="#fff">

        ...

    </android.support.constraint.ConstraintLayout>
</layout>

Don't

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <data>
        <variable
            name="MyVariable"
            type="com.mycompany.myproject.MyType"/>
    </data>

    ...        
</layout>



回答2:


There is no need to generate bindings while using 'kotlin-android-extensions'. It was needed when we used android data binding. Now you can simply access the views directly by using their view_id from your fragment/Activity



来源:https://stackoverflow.com/questions/50633574/android-data-binding-library-unresolved-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!