简体   繁体   中英

DuplicateFileException in apk build using Kotlin

I am trying to build a basic app using Kotlin/Anko but I am getting the following duplicate file exception

Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK kotlin/internal/internal.kotlin_builtins File1: C:\\Users\\mahesh.gradle\\caches\\modules-2\\files-2.1\\org.jetbrains.kotlin\\kotlin-runtime\\1.0.6.\\3562c66f648480d3bd4f76cff722488ced13445b\\kotlin-runtime-1.0.6.jar File2: C:\\Users\\mahesh.gradle\\caches\\modules-2\\files-2.1\\org.jetbrains.kotlin\\kotlin-compiler-embeddable\\1.0.6\\4008eb91a337b377dae7e4572b8b543e5321f549\\kotlin-compiler-embeddable-1.0.6.jar

Following is the code for the app level gradle file:

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

buildscript {
    ext.kotlin_version = '1.0.6'

    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

repositories {
    jcenter()
}

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.kotlin.androdikotlinexample"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'

    //kotlin
    compile "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

    //anko
    compile "org.jetbrains.anko:anko-design:0.8.3"
    compile "org.jetbrains.anko:anko-support-v4:0.8.3"
    compile "org.jetbrains.anko:anko-sdk15:0.8.3"
    compile 'org.jetbrains.anko:anko-appcompat-v7:0.8.3'

}

Following is the code for Project level gradle file:

buildscript {
    ext.kotlin_version = '1.0.6'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        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()
    }
}

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

And this is the basic code for the MainUI file

class MainUI : AnkoComponent<MainActivityKotlin> {
    override fun createView(ui: AnkoContext<MainActivityKotlin>): View = with(ui) {
        coordinatorLayout {
            verticalLayout {
                // maybe put some content here
            }
            floatingActionButton {
                imageResource = android.R.drawable.ic_menu_edit
                onClick {
                    ui.owner.startActivityForResult<MainActivityKotlin>(1)
                }
            }.lparams {
                gravity = Gravity.BOTTOM or Gravity.END
            }
        }
    }
}

And the content is set on MainActivityKotlin file using the method MainUI().setContentView(this) in its onCreatemethod .

I have tried deleting the gradle cache and building the project again but nothing works. Please let me know where am I going wrong?

Remove compile "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" from your gradle

Remove also all buildscript section from you app level gradle

Try this one:

compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile ("org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version") {
    transitive = false
}

Also you don't need the android-extensions dependency anymore, as it's bundled into the plugin. Just having the apply plugin: 'kotlin-android-extensions' in your gradle is enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM