Gradle Dependency バージョンのチェックを「最新」に更新する方法

便利そうなのでました!

Android Developers Blog: Announcing new SDK versioning in Google Play services and Firebase

com.google.android.gms:play-services-*
com.google.firebase:firebase-*

に関しては、それぞれが独立したバージョンで記述してよい。

なので、以下のような「バージョンをを揃える」記述は不要。


buildscript {
    ext {
        play_version = '15.0.0'
    }
}

dependencies {
    // DON'T DO THIS!!
    // The following use of the above buildscript property is no longer valid.
    implementation "com.google.android.gms:play-services-auth:${play_version}"
    implementation "com.google.firebase:firebase-auth:${play_version}"
    implementation "com.google.firebase:firebase-firestore:${play_version}"
}

利用方法は以下2パターン。


classpath 'com.google.gms:google-services:3.3.0'

// 最終行
apply plugin: 'com.google.gms.google-services' 

かまたは、


classpath 'com.google.android.gms:strict-version-matcher-plugin:1.0.0'

// 最終行
apply plugin: 'com.google.android.gms.strict-version-matcher-plugin'

とのこと。

 

やってみたが...

エラー。

ビルドできず。


The library com.google.android.gms:play-services-measurement-base
is being requested by various other libraries at [[15.0.0,15.0.0], [15.0.2,15.0.2]],
but resolves to 15.0.2. Disable the plugin and check your dependencies tree
using ./gradlew :app:dependencies.

記述にはない「play-services-measurement-base」が

内部的に呼ばれてこけている。

メッセージに書いてある


./gradlew :app:dependencies

は実行すらできない。

それぞれは、Andrid Studioの自動チェックで最新のはずなのだが。

 

それぞれのバージョンを確認してみる

目視でGoogleリポジトリを確認。

Google's Maven Repository

AndroidStudioの自動チェックで最新版の「15.0.0」だと思っていたが

実は、古いバージョンのままだった。

実際は「15.0.2」が最新。

これに書き換えたらいけた。

しかし、AndroidStudioの自動最新バージョンチェック機能らしきは、

機能してなくね?

かまたは、微妙に古くね?

 

Lint「Newer Library Versions Available」 を確認する

「Preference」-「Editor」-「Inspections」 にある

「Lint」-「Newer Library Versions Available」

OFFになってるが。

Description
Newer Library Versions Available This detector checks with a central repository to see if there are newer versions available for the dependencies used by this project. This is similar to the GradleDependency check, which checks for newer versions available in the Android SDK tools and libraries, but this works with any MavenCentral dependency, and connects to the library every time, which makes it more flexible but also much slower.

「毎回チェックするのでとろい」ということで

デフォルトでOFFになっているのか。

毎回でなく適時、意図的に実行したい場合は、

「Analyze」-「Run Inspection by Name」からいきましょう。

 

グレーのハイライトと同時にツールチップで最新版である

「15.0.2」

をサジェストしてくれました!

【Android Studio】ビルド環境を安定した最新バージョンにする


IntelliJ IDEA プラグイン「Rainbow Brackets」を使う

IntelliJ IDEAベースのIDEで使えるプラグインです。

なので、Android Studioでも使えます。

こういうかんじに括弧に色が付きます。

izhangzhihao/intellij-rainbow-brackets: Rainbow Brackets / Rainbow Parentheses for IntelliJ IDEA based IDEs

最近は、言語進化の影響か、

そういえば、多重に入れ子になった括弧を

使うことが多くなったような気もしますね。

👉 Rainbow Brackets - IntelliJ IDEs Plugin | Marketplace hatena-bookmark

👉 【AndroidStudio】プラグイン Rainbow Brackets が括弧だけでなく 変数 まで色が付くようになる【IDEA】 hatena-bookmark

つづいて、インデントも色付けしますか。

👉 【AndroidStudio / IDEA】プラグイン「Indent Rainbow」を使う hatena-bookmark


【kotlin】コールバック をラッピングして見通し良く

なんだか微妙な感じします。

APIの仕様がなのか、

サンプルのコードがなのか、

Camera2。

android-Camera2Basic/Camera2BasicFragment.kt at master · googlesamples/android-Camera2Basic

いくつかのコールバック処理が

連続して処理されますが

kotlin の芸当で分かりやすくしてみましょう。

 

コールバックのラッピング

多くの非同期処理APIは

コールバックスタイルのインターフェースを持ってます。

suspendCoroutine の 「suspend function」 を使うと

簡単にコールバックをその中にラッピングすることができます。

簡単な例を挙げてみます。


fun longComputation(params: Params, callback: (Result) -> Unit)

longComputation という function があって、

それのコールバックは Result という計算結果を受け取ります。

これは、以下のように簡単にラッピングできます。


suspend fun longComputation(params: Params): Result = suspendCoroutine { cont ->
  longComputation(params) { cont.resume(it) }
}

分かりやすく計算結果を返し、同じ非同期ですがスレッドをブロックしません。

kotlin-coroutines/kotlin-coroutines-informal.md at master · Kotlin/kotlin-coroutines

 

コールバックのラッピング

例えば、Camera2 APIでは必須のこれ。


void openCamera (String cameraId,
                CameraDevice.StateCallback callback,
                Handler handler)

CameraManager | Android Developers

コールバックとバックグラウンドハンドラやスレッドの

準備や後始末の処理が必要ですが、

それらがあちこちに分散してしまい辛くなりますが、

Kotlin coroutine に頼ると、

きれいにラッピングできます。


suspend fun CameraManager.openCamera(cameraId: String): CameraDevice? =
  suspendCoroutine { cont ->

    val callback = object : CameraDevice.StateCallback() {

      override fun onOpened(camera: CameraDevice) {
        cont.resume(camera)
      }

      override fun onDisconnected(camera: CameraDevice) {
        cont.resume(null)
      }

      override fun onError(camera: CameraDevice, error: Int) {
        cont.resume(null)
      }
   }
   openCamera(cameraId, callback, null)
 }

android - Existing 3-function callback to Kotlin Coroutines - Stack Overflow

他のいくつかのコールバックも

同じように書き換えていくと

かなり見通しよくなります。

しかし、

このGサンプルコードは分かりづらすぎぢゃんね?