Kotlin coroutine library error “Program type already present” resolved

Recently, I learned about the Coroutines library from Kotlin, which is the Coroutines library for Android. It is not easy to learn, and kotlin is still in a period of rapid change, that sour cool is simply, no more talk, get to the point.

Github address for Kotlinx. coroutines

Documentation for Android in the coroutine library

In order to implement some of the features of the Kotlin coroutine library in Android, we need to introduce the Coroutines-Android library as follows: The kotlin-gradle-plugin and the Kotlinx-Coroutines-Android plugin are the latest versions of 1.2.61 and 0.25.0. 1, respectively. Build.

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

buildscript {
    ext.kotlin_version = '1.2.61'
    ext.coroutines_version = '0.25.0'
    repositories {
        mavenCentral()
        jcenter()
        google()
        maven { url 'https://maven.google.com' }
    }
    dependencies {
        classpath 'com. Android. Tools. Build: gradle: 3.1.4'
        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 {
        mavenCentral()
        jcenter()
        google()
    }
}

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

Copy the code

2, app/build. Gradle:

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

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.tinytongtong.kotlincoroutineapplication"
        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'}}// Configure only for each module that uses Java 8
    // language features (either in its source code or
    // through dependencies).
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

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. The constraint, the constraint - layout: 1.1.2'
    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'

    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

    //okhttp
    api 'com. Squareup. Okhttp3: okhttp: 3.11.0'
    api 'com. Squareup. Okhttp3: logging - interceptor: 3.4.1 track'
}

kotlin {
    experimental {
        coroutines "enable"}}Copy the code

After the configuration is complete, sync and then run will appear in the Build window.

Problem solving:

The simplest and most crude way is to roll back the version as follows:

ext.kotlin_version = '1.2.61'
ext.coroutines_version = '0.25.0'
Copy the code

Sync –> run again, and the code is ready to run.