【问题标题】:Kotlin 1.3.61 problem with Kotlin Coroutines in Android studioKotlin 1.3.61 Android Studio 中 Kotlin Coroutines 的问题
【发布时间】:2019-12-11 11:07:30
【问题描述】:

问题已解决: 发布在下面的答案部分

问题
将 kotlin 版本升级到 1.3.61 后。我无法使用withContext、Dispatchers IO, Main 没有任何效果。它显示未解决的参考。 kotlin 的新更新中是否有任何错误?要不然是啥?因为之前一切正常。
我已经打开了我的旧项目相同的错误unresolved reference,每个协程函数都像我说的withContext() 等。但是suspend 关键字正在使用小协程图标/标记也显示,当我尝试调用另一个suspend功能。

我尝试什么?
将 kotlin 版本降级为 1.3.50 或尝试升级协程版本 1.3.61(这很愚蠢)但没有任何效果..
不断尝试寻找,但不幸的是找不到任何解决方法。
制作了一个新项目并尝试在 onCreate() 方法中使用,但仍然相同。

编辑:
添加 gradles:
build.gradle(Module:应用程序)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.daggerpractise"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    //Dagger 2
    implementation 'com.google.dagger:dagger:2.15'
    kapt 'com.google.dagger:dagger-compiler:2.15'
    // For android
    implementation 'com.google.dagger:dagger-android:2.15'
    implementation 'com.google.dagger:dagger-android-support:2.15'
    // if you use the support libraries
    kapt 'com.google.dagger:dagger-android-processor:2.15'

     // AndroidX support design (for nav drawer)
    // def material_version = "1.0.0-rc02"
    def material_version = "1.0.0"
    implementation "com.google.android.material:material:$material_version"

    implementation 'com.github.bumptech.glide:glide:4.10.0'
    kapt 'com.github.bumptech.glide:compiler:4.10.0'

    //MVVM and livedata
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.6.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.1'

    // RxJava
    def rxjava_version = '2.1.1'
    implementation "io.reactivex.rxjava2:rxjava:$rxjava_version"
    // Rx-Retrofit Call Adapter
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
    // Retrofit call adapter

    // Kotlin Android Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'

}

build.gradle(项目:DaggerPractise)

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

buildscript {
    ext.kotlin_version = '1.3.61'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.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 {
        google()
        jcenter()

    }
}

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

【问题讨论】:

  • 你们是否都包括: implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2' ?
  • @NicolaGallazzi 两者都是,但我删除了一次core,但也没有用。
  • 请发布您的应用程序 gradle
  • @NicolaGallazzi 更新了,看看看看。

标签: android android-studio kotlin kotlin-coroutines


【解决方案1】:

我使用的是 kotlin 1.3.60,但在看到此问题后升级到 1.3.61 以便调试您的问题。

升级后,没有任何错误。

尝试使 android studio 缓存失效并重新启动您的 android studio。这可能是 android studio 缓存问题。

如果它对你有用,请告诉我。
通过单击左上角的文件选项卡来执行此操作,然后向下滚动以使缓存无效/重新启动并单击

【讨论】:

  • 谢谢,做了两次,但还是不行。即使withContext() 没有显示在建议中,但是当我尝试Dispachers.IO 时,IO 显示为string 而它应该是协程调度程序。顺便说一句,你身边有什么?升级后是否正常?
【解决方案2】:

修复:
当我发现我的其他一些东西,比如 some::class.java 这里 java 是未解决的错误。然后我只需跳入xml 并尝试添加一些小部件以确保 xml 的行为方式,他们没有添加意味着他们的属性没有像自动完成一样显示,但幸运的是我已经修复了这个问题。我认为这可能会发生,因为它解决了我的所有问题。
这是您只需要做的事情,顺便说一句,它是在某些文件夹中添加 .bak 扩展名的 android studio 子文件夹,它是:
https://stackoverflow.com/a/54007742/12397978

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 2018-03-21
    • 1970-01-01
    • 2021-07-27
    • 2017-08-25
    • 1970-01-01
    • 2019-11-28
    相关资源
    最近更新 更多