官术网_书友最值得收藏!

  • Learning Concurrency in Kotlin
  • Miguel Angel Castiblanco Torres
  • 265字
  • 2021-08-05 10:46:46

Adding support for coroutines

Now that the project has been created, it's time to add coroutines support. To do this, open the Project section and double-click in build.gradle for the project. It is located inside the Gradle Scripts section:

The correct build.gradle file says Project: RssReader inside parenthesis 

Inside this document, you will add a new variable to centralize the coroutines version. In a new line below ext.kotlin.version, add ext.coroutines_version, as shown here, for example:

buildscript {
ext.kotlin_version = '1.2.50'
ext.coroutines_version = '0.23.3'
...
}
Every time you modify the  build.gradle  files, Android Studio will ask for the configuration to be synchronized again. Until the synchronization is made, the changes will not be applied. 

Now you need to add the dependencies for coroutines. Append the dependency configuration in the build.gradle file for the Module:

The build.gradle file for the Module

First, we will add the dependency for coroutines:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-
jdk7:
$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-
core:
$coroutines_version"
<...>
}

The application can be executed either on an emulator or on a real device from within Android Studio or from the command line. Android Studio's User Guide offers excellent information on how to do this, in their documentation at  https://developer.android.com/studio/run/index.html

Currently, coroutines are considered an experimental feature. In order to remove any warning when compiling or running apps using coroutines, it's necessary to also append this to the build.gradle file for the Module:

kotlin {
experimental {
coroutines "enable"
}
}
Once coroutines are no longer considered experimental, you can safely avoid adding the previous snippet of code.
主站蜘蛛池模板: 辽中县| 林州市| 黔西县| 东阳市| 平南县| 监利县| 简阳市| 宜春市| 达拉特旗| 昌平区| 阜康市| 拉萨市| 罗江县| 古田县| 怀来县| 泸州市| 鹤岗市| 江安县| 新营市| 宁远县| 保亭| 弋阳县| 天峻县| 肥西县| 闽清县| 桂东县| 新兴县| 太仆寺旗| 武山县| SHOW| 松阳县| 贵溪市| 鄱阳县| 长泰县| 定南县| 安丘市| 龙海市| 曲阜市| 陕西省| 花莲市| 比如县|