ライブラリのバージョン管理を少しだけ楽にする

2016.12.13

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

まえがき

便利なライブラリがたくさんあふれていて、是非使いたいっと思うのですが、それぞれのライブラリでライブラリの依存関係が発生し、入れられないっということが起きてします。
そういった場合に、なぜ入らないのか原因を探る方法があります。

ライブラリの依存関係を表示する

特にGooglePlayService関連を使っているライブラリをいれると
意外と依存関係でたまにビルドエラーになってしまって、どれを入れればいいんだ?っとなります。

まずは、それぞれのライブラリがどんな依存関係があるのか知る必要があります。
標準でついていますので、新しいライブラリ、バージョンアップしたライブラリを導入する際に、チェックするといいと思います。

$ ./gradlew app:dependencies 
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html.
Incremental java compilation is an incubating feature.
:app:dependencies

------------------------------------------------------------
Project :app
------------------------------------------------------------

_debugAndroidTestAnnotationProcessor - ## Internal use, do not manually configure ##
No dependencies

_debugAndroidTestApk - ## Internal use, do not manually configure ##
\--- com.android.support.test.espresso:espresso-core:2.2.2
     +--- com.squareup:javawriter:2.1.1
     +--- com.android.support.test:rules:0.5
     |    \--- com.android.support.test:runner:0.5
     |         +--- junit:junit:4.12
     |         |    \--- org.hamcrest:hamcrest-core:1.3
     |         \--- com.android.support.test:exposed-instrumentation-api-publish:0.5
     +--- com.android.support.test:runner:0.5 (*)
     +--- javax.inject:javax.inject:1
     +--- org.hamcrest:hamcrest-library:1.3
     |    \--- org.hamcrest:hamcrest-core:1.3
     +--- com.android.support.test.espresso:espresso-idling-resource:2.2.2
     +--- org.hamcrest:hamcrest-integration:1.3
     |    \--- org.hamcrest:hamcrest-library:1.3 (*)
     +--- com.google.code.findbugs:jsr305:2.0.1
     \--- javax.annotation:javax.annotation-api:1.2

_debugAndroidTestCompile - ## Internal use, do not manually configure ##
\--- com.android.support.test.espresso:espresso-core:2.2.2
     +--- com.squareup:javawriter:2.1.1
     +--- com.android.support.test:rules:0.5
     |    \--- com.android.support.test:runner:0.5
     |    
     |

ライブラリの最新版を表示する

いちいちライブラリの最新版を設定するのはめんどくさいので、+を使ってビルドに最新版を使うようにする事があると思います。

dependencies {
    compile 'com.android.support:appcompat-v7:25.+'
}

しかし、たまにライブラリのバージョンアップで動かなくなるときが出てきます。
どれが原因だ。。。っと途方にくれるときがあります。
個人アプリのときはいいですが、プロダクトの場合、リリース直前にそんなエラーになったら、変な汗がでてしまいますよね?
プロダクトの場合は、ライブラリバージョンはなるべく固定したほうがいいと思います。
しかし、最新版も使いたい。っというジレンマが発生します。

ライブラリの最新版を探してくれる便利なプラグインがあります!
ben-manes/gradle-versions-plugin: Gradle plugin to discover dependency updates

定期的にチェックして、
マイナーバージョンアップだから更新する
メジャーバージョンアップだから次の開発スタート時からいれて、罠を早めに踏んでいこう
など戦略を立てることができます。
1個ずつライブラリのバージョンをあげていくことで、原因も比較的簡単にみつけることができます。

設定

build.gradle

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
+        classpath 'com.github.ben-manes:gradle-versions-plugin:0.13.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

app/build.gradle

apply plugin: 'com.android.application'
+ apply plugin: 'com.github.ben-manes.versions'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.kamedon.androidsample"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
}

実行

 ./gradlew dependencyUpdates                                                                                                                                                                      rbenv:system 
To honour the JVM settings for this build a new JVM will be forked. Please consider using the daemon: https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html.
Incremental java compilation is an incubating feature.
:app:dependencyUpdates

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

The following dependencies are using the latest milestone version:
 - com.android.support:appcompat-v7:25.0.1
 - junit:junit:4.12

The following dependencies have later milestone versions:
 - com.android.support.test.espresso:espresso-core [2.2.2 -> 2.3-alpha]

Generated report file build/dependencyUpdates/report.txt

BUILD SUCCESSFUL

他にもオプションでreleaseのバージョン検索するなどあります。
GitHubのREADMEもご覧ください。

まとめ

ライブラリ依存関係を表示したり、最新版を知ることができました。
CIなどで流して、たまにチェックしたりするのもいいと思います。

参考