Android数据绑定库 - 未解析的参考

时间:2018-05-31 22:18:05

标签: android kotlin android-databinding

我正在使用Kotlin并尝试使用新的数据绑定库,但我得到了xxxBinding类的未解决的引用错误。

顶级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,但事实并非如此。有没有办法解决这个问题?

2 个答案:

答案 0 :(得分:2)

问题在于使用ConstrainLayout代替layout

使用数据绑定时,根布局必须是" layout"输入

待办事项

<?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>

唐&#39;吨

<?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>

答案 1 :(得分:0)

使用'kotlin-android-extensions'时无需生成绑定。我们使用android数据绑定时需要它。现在,您可以使用片段/活动

中的view_id直接访问视图