2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IntelliJ IDEAでKotlinのcoroutineに触れてみる(ための準備)

Posted at

はじめに

今ごろですが、Kotlinの環境を最新バージョン(1.0.0 → 1.3.71)にアップしたので、coroutine 1 に触れてみたいと思い、IntelliJ-IDEAで新規プロジェクトを作成しましたが、ビルドを通すまでに手こずったのでまとめておきます。

執筆時の環境は以下の通り;

  • macOS Catalina version 10.15.3
  • IntelliJ IDEA 2020.1 (Community Edition)
  • Kotlin version 1.3.71-release-431 (JRE 11.0.6+10)
  • Java openjdk version "11.0.6" 2020-01-14

Gradle Kotlin DSL 2

coroutineを使うためには、kotlinx-coroutines-coreのライブラリが必要で、gradle 3 に依存関係を追加するのですが、gradle未経験であったため、ネットの情報を参考にして試行錯誤の末、下記の内容でやっとビルド及び実行ができました。
ポイントはkotlinx-coroutines-coreの依存設定の書き方です。

bulid.gradle.kts
plugins {
    kotlin("jvm") version "1.3.71"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath(kotlin("gradle-plugin", version = "1.3.71"))
    }
}

dependencies {
    implementation(kotlin("stdlib"))
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.5")
}

tasks {
    compileKotlin {
        kotlinOptions.jvmTarget = "11"
    }
    compileTestKotlin {
        kotlinOptions.jvmTarget = "11"
    }
}

サンプルソース

下記がcoroutineを使ったの最小限のソースコードになります。今後、いろいろ付け加えてみて、理解を深めていきます。

Test.kt
import kotlinx.coroutines.*
fun main() = runBlocking {
    println("Hello!")
    GlobalScope.launch {
        println("CoRoutine!!")
    }.join()
    println("end!")
}

以上

  1. Kotlin 1.3をサクッと学ぶ - CoroutineとKotlin/Nativeを触って理解しよう

  2. Gradle Kotlin DSL入門

  3. IntelliJ+Gradleのプロジェクト作成方法メモ

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?