あかんわ

覚えたことをブログに書くようにすれば多少はやる気が出るかと思ったんです

Android StudioのOutdated Kotlin Runtime

Android StudioでGradle pluginをアップデートしたら、Outdated Runtime Kotlinと警告されるようになりました。

Your version of Kotlin runtime in 'org.jetbrains.kotlin:kotlin-stdlib:1.1.51@jar' library is 1.1.51, while plugin version is 1.2.31-release-Studio3.0-1.
Runtime library should be updated to avoid compatibility problems.
Update Runtime Ignore

ところが、RuntimeをUpdateしろと言うならそうしましょうと思ってUpdate Runtimeを選択しても、自動でUpdateはできないので自分でbuild.gradleを Updateしろと言われます。

Update Kotlin Runtime Library
Automatic library version update for Maven and Gradle projects is currently unsupported. Please update your build.gradle manually.

じゃあ、build.gradleを書き換えようとモジュールディレクトリ(app)のbuild.gradleを開いたものの、1.1.51の記述が見つかりません。

build.gradle(Module: app)
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.b0npu.hellokotlin"
        minSdkVersion 23
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

build.gradle(Project: Project Name)

Outdated Runtime Kotlinで検索してみると、いつものようにStack Overflowに辿り着き、ここで初めて、build.gradleにはProjectとModuleの2種類が存在する事を知りました。

・プロジェクトディレクトリ(Application Name)→モジュールディレクトリ(app)→build.gradle(Module: Module Name) ・プロジェクトディレクトリ(Application Name)→build.gradle(Project: Project Name)

プロジェクトディレクトリ直下のbuild.gradle(Project: Project Name)を開くと、あっさりとext.kotlin_version = '1.1.51' の記述を見つけてUpdateできました。

build.gradle(Project: HelloKotlin)
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.51'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.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()
        jcenter()
    }
}

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

開発環境