如何在Android Studio 3.0.0中使用数据绑定和Kotlin

问题描述 投票:0回答:5

我刚刚开始使用 Android Studio 3.0.0,但每次我尝试构建我的项目时都会收到此错误:

Error:Circular dependency between the following tasks:
:app:compileDebugKotlin
+--- :app:dataBindingExportBuildInfoDebug
|    \--- :app:compileDebugKotlin (*)
\--- :app:kaptDebugKotlin
     \--- :app:dataBindingExportBuildInfoDebug (*)
(*) - details omitted (listed previously)

我正在使用

kapt "com.android.databinding:compiler:2.2.0"

我使用之前

androidProcessor "com.android.databinding:compiler:2.2.0"

它工作得很好......我做错了什么?

谢谢!

android android-studio kotlin android-databinding android-studio-3.0
5个回答
80
投票

UPD: Android Gradle 插件 3.0.0-alpha3 已修复此问题,在 yout 项目根目录

build.gradle
中,将
buildscript
dependencies
更改为使用

classpath 'com.android.tools.build:gradle:3.0.0-alpha3'

这实际上是 Kotlin Gradle 插件 1.1.2-4 与 Android Gradle 插件 3.0.0-alpha1 互操作中的一个错误,由任务的输入和输出的设置方式(以及任务的连接方式)引起具有依赖关系)。

感谢 @VyacheslavGerasimov 创建问题 KT-17936


作为临时解决方法,您可以尝试恢复到 Kotlin Gradle 插件 1.1.2-2 并禁用增量编译:

在项目的根目录

build.gradle
中,更改 Kotlin Gradle 插件的版本:

buildscript {
    ...
    dependencies {
        ...
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-2'
    }
}

local.properties
添加到项目根目录,并添加以下行:

kotlin.incremental=false

这是一个已知问题,Kotlin Gradle 插件 1.1.2-2 及以下版本在最新的 AGP 版本中会崩溃,禁用增量编译似乎可以修复该崩溃。


41
投票

您似乎需要在模块级别的应用程序 .gradle 中添加 3 个 gradle 条目来添加数据绑定

  1. apply plugin: 'kotlin-kapt'
  2. android {
    ...
       dataBinding {
           enabled = true
       }
     }
  3. dependencies {
        ......
        kapt "com.android.databinding:compiler:$compiler_version"
     }
    

请注意,我将编译器版本设置为项目级别构建 gradle 中的变量,以便可以从一个位置对其进行管理

默认为:

ext.kotlin_version = '1.1.3-2'

我添加了括号语法:

ext{
    kotlin_version = '1.1.3-2'
    compiler_version = '3.0.0-beta6'
}

18
投票

对于那些仍在寻找合适解决方案的人,Google 已经在 Android Studio 3.0 Canary 3 版本中修复了此问题。

2017 年 6 月 2 日星期五

我们刚刚向 Canary 发布了 Android Studio 3.0 Canary 3, 开发频道。 Android Gradle Plugin 3.0.0-alpha3也发布了 通过 maven.google.com。此版本修复了 GradleKotlin、 以及许多其他修复。我们继续修复 Studio 所有领域的错误 3.0 随着我们稳定功能,请继续传递反馈。

工作gradle配置:

build.gradle(项目)

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-alpha3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

build.gradle(模块)

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


android {
    dataBinding.enabled = true
}
dependencies {
    kapt "com.android.databinding:compiler:3.0.0-alpha3"
}

4
投票

如果您使用 Kotlin Gradle 插件 1.3 及更高版本,则无需指定

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

https://youtrack.jetbrains.com/issue/KT-32057

在 build.gradle 文件中指定

dataBinding
就足够了:

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

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

0
投票

在 Android Studio 中 Iguana | 2023.2.1 补丁 1 和 gradle v8.7 对于科特林 build.gradle.kts(项目)添加:

plugins {
    alias(libs.plugins.androidApplication) apply false
    alias(libs.plugins.jetbrainsKotlinAndroid) apply false
    kotlin("kapt") version "1.9.0"  apply false //add this line<don't tell anyone:)
}

和 build.gradle.kts(应用程序)添加:

plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.jetbrainsKotlinAndroid)
    id ("kotlin-kapt")// add this line

}

android {
...
    buildFeatures {
        compose = true
        viewBinding = true//this 
        dataBinding = true//and this add both!

    }
}

注意其他需要 只是不要忘记在 xml 中必须使用数据类:

<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="user"
            type="com.example.model.User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}" />

    </LinearLayout>
</layout>

在您的活动中:

// Get a binding instance for your layout
val binding: ActivityMainBinding =  DataBindingUtil.setContentView(this, R.layout.activity_main)
// Create a User object with data
val user = User("John")
// Set the user data on the binding variable
binding.user = user
© www.soinside.com 2019 - 2024. All rights reserved.