Android Studio3.0中使用kotlin + databinding

遇到了这个令人绝望的bug,我还以为不支持了那.

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"

于是各种找:
源头:
@VyacheslavGerasimov 这个哥们已经在jb家提了issus
https://youtrack.jetbrains.com/oauth?state=%2Fissue%2FKT-17936

解决方案: 其实就是降低插件版本,3.0里使用的是1.1.2-4
降低到1.1.2-2即可

  • root build.gradle
buildscript {
    ext.kotlin_version = '1.1.2-2'
    ext.support_version = '25.3.1'
    ext.lifecycle_version = '1.0.0-alpha1'

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

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
  • module build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
      ...    

    //为了避免rxjava的重复问题
    packagingOptions {
        exclude 'META-INF/rxjava.properties'
    }
    
    //打开dataBinding
    dataBinding {
        enabled true
    }
    
}

//kapt
kapt {
    generateStubs = true
}

dependencies {
     ...
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
    kapt 'com.android.databinding:compiler:2.3.2'
}
  • local.properties文件中
    kotlin.incremental=false

  • 最好

干掉  Users/{用户名}/Library/Application Support/AndroidStudioPreview3.0/Kotlin (if you're using mac for example)

这样子就可以了

你可能感兴趣的:(Android Studio3.0中使用kotlin + databinding)