Kotlin พร้อม Android DataBinding ใน Android Studio 3.0 canary 2 [ซ้ำกัน]

โครงการของฉันมี 2 โมดูลคือ แอป และ apphelper เป็นไลบรารี ฉันพยายามใช้ kotlin กับ android Databinding ด้วย แต่ฉันไม่สามารถเข้าใจได้ว่าทำไมมันถึงแสดงข้อผิดพลาดเหล่านี้ให้ฉันเมื่อฉันสร้างโครงการ:

ความล้มเหลว: การสร้างล้มเหลวโดยมีข้อยกเว้น

* What went wrong:
A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:stagingRuntimeClasspath'.
   > Unable to find a matching configuration in project :apphelper:
       - Configuration 'debugApiElements':
           - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'.
           - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=staging}' and found incompatible value 'BuildTypeAttr{name=debug}'.
           - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=debug}' but wasn't required.
           - Required org.gradle.api.attributes.Usage 'for runtime' and found incompatible value 'for compile'.
       - Configuration 'debugRuntimeElements':
           - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'.
           - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=staging}' and found incompatible value 'BuildTypeAttr{name=debug}'.
           - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=debug}' but wasn't required.
           - Required org.gradle.api.attributes.Usage 'for runtime' and found compatible value 'for runtime'.
       - Configuration 'releaseApiElements':
           - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'.
           - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=staging}' and found incompatible value 'BuildTypeAttr{name=release}'.
           - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=release}' but wasn't required.
           - Required org.gradle.api.attributes.Usage 'for runtime' and found incompatible value 'for compile'.
       - Configuration 'releaseRuntimeElements':
           - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'AndroidTypeAttr{name=Aar}' and found compatible value 'AndroidTypeAttr{name=Aar}'.
           - Required com.android.build.gradle.internal.dependency.BuildTypeAttr 'BuildTypeAttr{name=staging}' and found incompatible value 'BuildTypeAttr{name=release}'.
           - Found com.android.build.gradle.internal.dependency.VariantAttr 'VariantAttr{name=release}' but wasn't required.
           - Required org.gradle.api.attributes.Usage 'for runtime' and found compatible value 'for runtime'.

บันทึกข้อผิดพลาดด้านบนจะแสดงขึ้นเมื่อฉันเพียงคลิกที่ ปุ่มสร้าง และจะแปลกกว่านั้นหากฉันคลิก ปุ่มเรียกใช้ เพื่อติดตั้งแอปลงในอุปกรณ์ที่มีลักษณะดังนี้:

:app:transformDexArchiveWithDexMergerForDebug
AGPBI: {"kind":"error","text":"Error converting bytecode to dex:\nCause: com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/PrintFormatPattern;","sources":[{}],"original":"UNEXPECTED TOP-LEVEL EXCEPTION:\ncom.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/PrintFormatPattern;\n","tool":"Dex"}
AGPBI: {"kind":"error","text":"com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/PrintFormatPattern;","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:608)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:563)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:545)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:167)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.dx.merge.DexMerger.merge(DexMerger.java:194)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.builder.dexing.DexArchiveMergerCallable.mergeDexes(DexArchiveMergerCallable.java:66)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:54)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat com.android.builder.dexing.DexArchiveMergerCallable.call(DexArchiveMergerCallable.java:37)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinTask$AdaptedCallable.exec(ForkJoinTask.java:1424)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)","sources":[{}]}
AGPBI: {"kind":"error","text":"\tat java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)","sources":[{}]}

 FAILED
:app:buildInfoGeneratorDebug

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: com.android.dex.DexException: Multiple dex files define Lorg/intellij/lang/annotations/PrintFormatPattern;

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED in 4s
74 actionable tasks: 7 executed, 67 avoided (91%)

ฉันดาวน์เกรดเวอร์ชัน kotlin ให้เป็น '1.1.2-2' เพื่อหลีกเลี่ยง ข้อผิดพลาด และยังเพิ่ม 'kotlin.increational=false' ใน local.properties

นี่คือไฟล์ build.gradle ทั้งหมดของฉัน:

โมดูลแอป:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: 'test-environment.gradle'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'


repositories {
    maven { url 'https://maven.fabric.io/public' }
    maven { url "https://jitpack.io" }
    mavenCentral()
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }

    }
}

android {
    compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
    defaultConfig {
        applicationId "com.myapp.android"
        minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
        targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        versionCode 1
        versionName "1.0"
    }

    dataBinding {
        enabled = true
    }

    dexOptions {
        jumboMode = true
    }

    if (System.getenv("CIRCLE")) {
        defaultConfig {
            versionCode Integer.parseInt(System.getenv("CIRCLE_BUILD_NUM"))
            versionName "1.0." + System.getenv("CIRCLE_BUILD_NUM")
        }
    } else {
        defaultConfig {
            versionCode 1
            versionName "1.0"
        }
    }



    signingConfigs {
        debug {
            storeFile file("../myappdebug.keystore")
            storePassword "myapprocks"
            keyAlias "myapp"
            keyPassword "myapprocks"
        }
        staging {
            storeFile file("../myapp_staging.keystore")
            storePassword "myapprocks"
            keyAlias "myapp"
            keyPassword "myapprocks"
        }


        release {
            storeFile rootProject.file('myapp.keystore')
            storePassword System.getenv("KEYSTORE_PASS")
            keyAlias System.getenv("ALIAS_NAME")
            keyPassword System.getenv("ALIAS_PASS")
        }
    }

    buildTypes {
        debug {
            ext.betaDistributionNotifications = false
            signingConfig signingConfigs.debug
            applicationIdSuffix ".dev"
            minifyEnabled false
            shrinkResources false
            proguardFile 'proguard-release.cfg'
            testProguardFile 'proguard-release.cfg'
            manifestPlaceholders = [providerSuffix: ".dev"]
        }
        staging {
            ext.betaDistributionEmailsFilePath = "staging_distribution_emails.txt"
            ext.betaDistributionReleaseNotesFilePath = "release_notes.txt"
            signingConfig signingConfigs.staging
            applicationIdSuffix ".staging"
            shrinkResources true
            minifyEnabled true
            proguardFile 'proguard-release.cfg'
            manifestPlaceholders = [providerSuffix: ".staging"]

        }

        release {
            signingConfig signingConfigs.release
            shrinkResources true
            minifyEnabled true
            proguardFile 'proguard-release.cfg'

        }
    }
}


kapt {
    generateStubs = true
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    api project(':apphelper')
    compile 'com.android.support:appcompat-v7:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile 'com.android.support:cardview-v7:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile 'com.android.support:design:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile 'com.android.support:support-v13:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile 'hanks.xyz:smallbang-library:0.1.2'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.facebook.device.yearclass:yearclass:1.0.1'
    compile 'com.facebook.android:facebook-android-sdk:4.+'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.2@aar') {
        transitive = true
    }
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile "android.arch.lifecycle:runtime:" + project.ANDROID_ARCH_VERSION
    compile "android.arch.lifecycle:extensions:" + project.ANDROID_ARCH_VERSION
    annotationProcessor "android.arch.lifecycle:compiler:" + project.ANDROID_ARCH_VERSION
    compile "android.arch.persistence.room:runtime:" + project.ANDROID_ARCH_VERSION
    annotationProcessor "android.arch.persistence.room:compiler:" + project.ANDROID_ARCH_VERSION
    kapt "com.android.databinding:compiler:$android_plugin_version"


}

โมดูลห้องสมุด:

apply plugin: 'com.android.library'

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION


    defaultConfig {
        minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
        targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/maven/com.squareup.okio/okio/pom.properties'
        exclude 'META-INF/maven/com.squareup.okio/okio/pom.xml'
        exclude 'META-INF/maven/com.squareup.retrofit/retrofit/pom.xml'
        exclude 'META-INF/maven/com.squareup.retrofit/retrofit/pom.properties'
    }
}

kapt {
    generateStubs = true
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile group: 'com.squareup.retrofit2', name: 'converter-gson', version: '2.1.0'
    compile group: 'com.squareup.okhttp3', name: 'logging-interceptor', version: '3.4.1'
    compile 'org.jetbrains:annotations-java5:15.0'
    compile 'hanks.xyz:smallbang-library:0.1.2'
    compile 'com.android.support:support-annotations:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile 'com.android.support:support-v13:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile 'com.android.support:appcompat-v7:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile 'com.android.support:design:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile 'com.android.support:customtabs:' + project.ANDROID_SUPPORT_LIBRARY_VERSION
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    compile 'com.github.bumptech.glide:glide:3.7.0'
}

repositories {
    mavenCentral()
}

สคริปต์สร้าง gradle หลัก:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.2-2'
    ext.android_plugin_version = '3.0.0-alpha2'

    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
        maven { url 'https://maven.fabric.io/public' }

    }
    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
        classpath "com.android.tools.build:gradle:$android_plugin_version"
        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
    }
}

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
        mavenCentral()
    }
}

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

ใครมีปัญหาเหมือนผมบ้าง แบ่งปันวิธีแก้ปัญหาครับ ขอบคุณ.


person Jutikorn    schedule 27.05.2017    source แหล่งที่มา


คำตอบ (3)


ลบ:

kapt {
    generateStubs = true
}

และเพิ่มการพึ่งพา:

kapt "com.android.databinding:compiler:$android_plugin_version"
person John    schedule 27.05.2017
comment
ขอบคุณ @John สำหรับคำตอบของคุณ แต่มันใช้งานไม่ได้แม้จะลบสองสิ่งนี้ออกไปและมันเกิดข้อผิดพลาดใหม่ซึ่งมีลักษณะคล้ายกัน - person Jutikorn; 27.05.2017
comment
ลองเปิดใช้งาน multiDex ด้วย multiDexEnabled จริง - person John; 27.05.2017
comment
ลองใช้ 3.0.0-alpha1 ด้วย ฉันมีปัญหากับ alpha2 เช่นกัน - person John; 27.05.2017
comment
ดูเหมือนว่าไม่อนุญาตให้ดาวน์เกรดเป็น 3.0 alpha1 ระบบขอให้ฉันอัปเดตเนื่องจากปลั๊กอิน Android เวอร์ชันไม่รองรับ - person Jutikorn; 27.05.2017

ตอนนี้ใช้งานได้แล้ว ฉันเพิ่งดาวน์เกรด com.android.tools.build:gradle เป็น 2.3.2 เปลี่ยน distributionUrl ให้เป็น https://services.gradle.org/distributions/ gradle-3.3-all.zip และใช้ Android Studio ที่เสถียร (2.3.2) ปัญหานี้ควรจะถูกกำจัดออกไปเมื่อมีการเปิดตัวความเสถียรของ Android Studio 3.0

person Jutikorn    schedule 28.05.2017
comment
ฉันลงเอยด้วยการทำเช่นนี้ :( - person amouly; 29.05.2017

ฉันคิดว่าวิธีแก้ปัญหาที่ถูกต้องนั้นรอให้ Google แก้ไข: https://issuetracker.google.com/issues/62170415

หรือตอนนี้ใช้วิธีแก้ปัญหาตามที่อธิบายไว้ที่นี่: https://stackoverflow.com/a/44346259/4024146

person mtrakal    schedule 03.06.2017