12

我应该在我的build.gradle文件中更改什么或在类中导入以在我的带有 Kotlin 1.3 的 Android 项目中使用稳定的协程函数?

我的关于协程的片段build.gradle

implementation "org.jetbrains.kotlin:kotlin-coroutines-core:$coroutines_version" implementation "org.jetbrains.kotlin:kotlin-coroutines-android:$coroutines_version"

当然我用的是Android Studio 3.3 Preview

4

4 回答 4

27

build.gradle将库更改为

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1'.

删除,如果添加:

kotlin {
    experimental {
        coroutines "enable"
    }
}

在代码中更改launchGlobalScope.launch(Dispatchers.IO)or GlobalScope.launch(Dispatchers.Main)

更新

请使用本地协程上下文而不是全局范围(例如,参见http://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html)。

对于活动

请参阅https://github.com/Kotlin/kotlinx.coroutines/blob/master/ui/coroutines-guide-ui.md

实施CoroutineScope

class YourActivity : AppCompatActivity(), CoroutineScope {

添加一个局部变量job并初始化它:

private lateinit var job: Job

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    job = Job()
}

创建协程上下文并在 Activity 销毁时取消它:

override fun onDestroy() {
    job.cancel()
    super.onDestroy()
}

override val coroutineContext: CoroutineContext
    get() = Dispatchers.Main + job

对于 Fragment(与 中相同Activity

实现 CoroutineScope:

class YourFragment : Fragment(), CoroutineScope {

创建一个局部变量job并在onCreate(). (我试着写private val job: Job = Job(),但遇到了一个问题,ViewPager你会创建Fragments 和他们的工作。因为我们会在刷入的过程中取消jobonDestroy()所以ViewPager我们应该重新创建工作)。

private lateinit var job: Job

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    ...
    job = Job()
}

创建协程上下文并在 Fragment 销毁时将其取消:

override val coroutineContext: CoroutineContext
    get() = Dispatchers.Main + job // You can use different variants here. 

override fun onDestroy() {
    job.cancel()
    super.onDestroy()
}

启动示例

launch照常使用:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    launch {
        // Wait for result of I/O operation without blocking the main thread.
        withContext(Dispatchers.IO) {
            interactor.getCountry().let {
                countryName = it.name
            }
        }

        // Update views in the UI thread.
        country.updateCaption(countryName)
    }
}

在我的情况下,当我使用带有通常回调的 API 请求时出现问题。launch尚未调用回调内部的内部。所以我用交互器重写了那个代码。

于 2018-11-02T11:30:51.767 回答
7

我的队友帮助我找到了解决方案。我不得不将协程版本增加到 1.0.0-RC1。对于可能不知道使用Android协程的变化的每个人:

  • 我不得不将协程的 UI 上下文更改为 Dispatchers.Main
  • 我使用了旧的实验性协程版本(可能是 0.23),所以对于不知道的每个人来说 - 现在启动已被弃用,您应该改用结构化并发(例如coroutineScope)。
  • 现在异步函数不能在范围之外运行。

我希望我能帮助别人。不要浪费时间。快乐编程!

于 2018-10-25T06:14:39.400 回答
0

只需将“mavenCentral()”添加到 build.gradle 波纹管构建脚本:我修复了使用它。

buildscript {
    ext.kotlin_version = '1.3.72'

    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.0'
        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()
        mavenCentral() //Add this Line only <<<<<<<<<<<<<<<<<<
    }
}
于 2020-06-28T12:59:48.130 回答
0

实现版本必须 >=测试版本

build.gradle (:mobile)

dependencies {
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines")

    // Testing
    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines"
    androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines"
}

build.gradle (项目)

buildscript {
    ...
    ext.coroutines = '1.3.6'
}

故障修复

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:processDebugAndroidTestManifest'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
    > Could not resolve org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5.
    Required by:
        project :app
     > Cannot find a version of 'org.jetbrains.kotlinx:kotlinx-coroutines-core' that satisfies the version constraints:
          Dependency path 'Open Weather:app:unspecified' --> 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5'

总帐

于 2020-05-17T00:25:07.287 回答