Androidアプリ開発探求記(その9)


はじめに

前回は、buildSrcDependencies.kt にて、依存関係の一元管理をしてみました。

しかし、管理対象ライブラリのバージョンは日々更新されていくため、必要に応じて更新していく必要があります。

ということで、今回は、ライブラリのバージョン保守に関して模索してみようと思います。

Gradle Versions Plugin の導入

基本的に、Gradle Versions Plugin によるバージョン管理 をそのまま踏襲しようと思います。

追加コード

build.gradle.kts
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask

plugins {
    id("com.github.ben-manes.versions") version "0.29.0"
}

fun isNonStable(version: String): Boolean {
    val stableKeyword = listOf("RELEASE", "FINAL", "GA").any { version.toUpperCase().contains(it) }
    val regex = "^[0-9,.v-]+(-r)?$".toRegex()
    val isStable = stableKeyword || regex.matches(version)
    return isStable.not()
}

// configure Component Selection Rules.
// @See https://docs.gradle.org/current/userguide/dynamic_versions.html#sec:component_selection_rules
tasks.named("dependencyUpdates", DependencyUpdatesTask::class.java).configure {
    rejectVersionIf {
        isNonStable(candidate.version) && !isNonStable(currentVersion)
    }
}

実行

依存関係修正前

$ ./gradlew dependencyUpdates

> Task :dependencyUpdates

------------------------------------------------------------
: Project Dependency Updates (report to plain text file)
------------------------------------------------------------

The following dependencies are using the latest milestone version:
 - androidx.constraintlayout:constraintlayout:1.1.3
 - androidx.test.espresso:espresso-core:3.2.0
 - androidx.test.ext:junit:1.1.1
 - com.android.tools.build:gradle:4.0.1
 - com.github.ben-manes.versions:com.github.ben-manes.versions.gradle.plugin:0.29.0
 - org.jetbrains.kotlin:kotlin-android-extensions:1.3.72
 - org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.72
 - org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72
 - org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72

The following dependencies have later milestone versions:
 - androidx.appcompat:appcompat [1.1.0 -> 1.2.0]
     https://developer.android.com/jetpack/androidx
 - junit:junit [4.12 -> 4.13]
     http://junit.org

Gradle release-candidate updates:
 - Gradle: [6.1.1 -> 6.5.1 -> 6.6-rc-6]

依存関係修正

Dependencies.kt
object Deps {
    const val androidx_appcompat__appcompat = "androidx.appcompat:appcompat:1.2.0"
    const val junit__junit = "junit:junit:4.13"
}

Gradle のバージョン変更はリスクが高いので、具体的な必要性が生じるまでは Android Studio の配布に合わせたバージョンを利用することとします。

依存関係修正後

$ ./gradlew dependencyUpdates

> Task :buildSrc:compileKotlin
The `kotlin-dsl` plugin applied to project ':buildSrc' enables experimental Kotlin compiler features. For more information see https://docs.gradle.org/6.1.1/userguide/kotlin_dsl.html#sec:kotlin-dsl_plugin

> Task :buildSrc:jar
:jar: No valid plugin descriptors were found in META-INF/gradle-plugins

> Task :dependencyUpdates

------------------------------------------------------------
: Project Dependency Updates (report to plain text file)
------------------------------------------------------------

The following dependencies are using the latest milestone version:
 - androidx.appcompat:appcompat:1.2.0
 - androidx.constraintlayout:constraintlayout:1.1.3
 - androidx.test.espresso:espresso-core:3.2.0
 - androidx.test.ext:junit:1.1.1
 - com.android.tools.build:gradle:4.0.1
 - com.github.ben-manes.versions:com.github.ben-manes.versions.gradle.plugin:0.29.0
 - junit:junit:4.13
 - org.jetbrains.kotlin:kotlin-android-extensions:1.3.72
 - org.jetbrains.kotlin:kotlin-android-extensions-runtime:1.3.72
 - org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72
 - org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.72

Gradle release-candidate updates:
 - Gradle: [6.1.1 -> 6.5.1 -> 6.6-rc-6]

まとめ

今回は、Gradle Versions Plugin を導入し、ライブラリをアップデートしてみました。

次回のネタは考え中。

Appendix

◆ Pull Request