【Android Studio】Gradle Version Catalog「Live Template」を使って インライン・テーブル に瞬時に書き換える

だるいですよね、インラインテーブル記述。


name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }
animal = { type.name = "pug" }

👉 TOML: 日本語 v0.5.0 hatena-bookmark

Android Studio でも

Graadle Version Catalog で、最初は、こんな記述でも


[plugins]
android-material = "com.google.android.material:material:1.8.0-alpha02"

やがて、


[plugins]
android-material = { module = "com.google.android.material:material", version = "1.8.0-alpha02" }

となり


[versions]
android-material = "1.8.0-alpha02"

[plugins]
android-material = { module = "com.google.android.material:material", version.ref = "android-material" }

というふうに変わっていきます。

大量にやってるのも地味にだるい。

Live Templete を使って書き換えます。


Preferences

  ↓

Live Templates

から作っていきます。

ここマクロたちは、思ったより使いづらいものが多い。

live template macro

👉 intellij-community/platform/lang-impl/src/com/intellij/codeInsight/template/macro at master · JetBrains/intellij-community hatena-bookmark

割と使える Groovy のマクロ。

groovyScript(, [arg, ...])

Executes the Groovy script passed as a string.

The first argument is a string with either the text of the script or the path to the file that contains the script. The function passes other optional arguments to the script as values for _1, _2, _3, ..., _n variables. Also, you can access the current editor from inside the script using the _editor variable.

The following example shows a groovyScript() function that splits the selected text into words and displays them as a numbered list:


groovyScript("def result = ''; _1.split().eachWithIndex { item, index -> result = result + index.next() + '. ' + item + System.lineSeparator() }; return result;", SELECTION);

👉 Edit Template Variables dialog | IntelliJ IDEA hatena-bookmark

以下の感じで設定して4つのテンプレートを記述しました。

どれも似たような記述なので、一つだけ貼っておきます。

使い回せます。

使い方は、該当文字列をコピーしてから、Abbreviation を入力で、テンプレートを吐き出しながらキーなどを編集してください。

結果。

toml live template

キーの編集に連携されて関連記述が編集されるのがいいところでしょうか。

けどまあ、Android Studio 本体の機能更新が待たれるところですが。

 

🙆 追記

複数行一括のほうがいいので以下の方法を実際は使っています。



「kotlin-android」のような Plugin ID は使いづらい - Plugin(プラグイン) ID の探し方

以下の関係を公開リポジトリで確認しながら、Plugin ID を見つけていきます。


classpath "org.jetbrains.kotlin:kotlin-serialization:1.7.20"


groupId    : org.jetbrains.kotlin
artifactId : kotlin-serialization
version    : 1.7.20

👉 https://plugins.gradle.org/search?term=org.jetbrains.kotlin.android
👉 https://maven.google.com/web/index.html?q=org.jetbrains.kotlin.android
👉 https://central.sonatype.dev/search?q=org.jetbrains.kotlin.android

 

🔍 1. Gradle Plugins で探す

gradle plugin search
👉 Gradle - Plugins


classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.20"
apply plugin: "org.jetbrains.kotlin.android"


plugins {
  id "org.jetbrains.kotlin.android" version "1.7.20"
}

👉 Gradle - Plugin: org.jetbrains.kotlin.android


classpath "org.gradle.kotlin:gradle-kotlin-dsl-plugins:3.1.0"
apply plugin: "org.gradle.kotlin.kotlin-dsl"


plugins {
  id "org.gradle.kotlin.kotlin-dsl" version "3.1.0"
}

👉 Gradle - Plugin: org.gradle.kotlin.kotlin-dsl


classpath "org.jetbrains.compose:compose-gradle-plugin:1.3.0-alpha01-dev827"
apply plugin: "org.jetbrains.compose"


plugins {
  id "org.jetbrains.compose" version "1.3.0-alpha01-dev827"
}

👉 Gradle - Plugin: org.jetbrains.compose


classpath "com.diffplug.spotless:spotless-plugin-gradle:6.11.0"
apply plugin: "com.diffplug.spotless"


plugins {
  id "com.diffplug.spotless" version "6.11.0"
}

👉 Gradle - Plugin: com.diffplug.spotless


classpath "com.github.ben-manes:gradle-versions-plugin:0.43.0"
apply plugin: "com.github.ben-manes.versions"


plugins {
  id "com.github.ben-manes.versions" version "0.43.0"
}

👉 Gradle - Plugin: com.github.ben-manes.versions

よく使いそうな Owner :

Gradle - prod-plugin-portal-publishing (Gradle)
👉 Gradle - prod-plugin-portal-publishing (Gradle)

Gradle - kotlin (Kotlin Team)👉 Gradle - kotlin (Kotlin Team)

Gradle - jetbrains (JetBrains)
👉 Gradle - jetbrains (JetBrains)

Gradle - jb-compose (JetBrains Compose)
👉 Gradle - jb-compose (JetBrains Compose)

 

🔍 2. Google's Maven Repository で探す

https://maven.google.com/web/index.html
👉 Google's Maven Repository

plugin idgradle.plugin で探すとよい。


plugin.id  : plugin.id.gradle.plugin : plugin.version
{group id} : {artifact id}


{artifact id} = "{plugin id}.gradle.plugin"


Group ID	com.android.application
Artifact ID	com.android.application.gradle.plugin
Version	8.0.0-alpha05


Plugin ID → "com.android.application"

👉 com.android.application - Google's Maven Repository


Group ID	com.android.library
Artifact ID	com.android.library.gradle.plugin
Version	8.0.0-alpha05


Plugin ID → "com.android.library"

👉 com.android.library - Google's Maven Repository


Group ID	com.google.firebase.crashlytics
Artifact ID	com.google.firebase.crashlytics.gradle.plugin
Version	2.9.2


Plugin ID → "com.google.firebase.crashlytics"

👉 com.google.firebase.crashlytics - Google's Maven Repository


Group ID	com.google.gms.google-services
Artifact ID	com.google.gms.google-services.gradle.plugin
Version	4.3.14


Plugin ID → "com.google.google-services"

👉 com.google.gms.google-services - Google's Maven Repository

 

🔍 3. Maval Central で探す

3つあるけど、どれがいいのか。

https://central.sonatype.dev/search
👉 Maven Central - Search ← 👌

https://search.maven.org/
👉 Maven Central Repository Search

https://mvnrepository.com/
👉 Maven Repository: Search/Browse/Explore

hilt, gradle.plugin のように入力して探すと良い。


<groupId>com.google.dagger.hilt.android</groupId>
<artifactId>com.google.dagger.hilt.android.gradle.plugin</artifactId>
<version>2.44</version>


Plugin ID → "com.google.dagger.hilt.android"

👉 Maven Central: com.google.dagger.hilt.android:com.google.dagger.hilt.android.gradle.plugin:2.44


<groupId>app.cash.sqldelight</groupId>
<artifactId>app.cash.sqldelight.gradle.plugin</artifactId>
<version>2.0.0-alpha04</version>


Plugin ID → "app.cash.sqldelight"

👉 Maven Central: app.cash.sqldelight:app.cash.sqldelight.gradle.plugin:2.0.0-alpha04

 

🙆 参考

👉 【Plugin DSL】「com.android.tools.build:gradle」の記述は不要? hatena-bookmark
👉 【Gradle Version Catalog】libs.versions.toml キー名の形式 camelCase vs kebab-case hatena-bookmark
👉 「⚠ This project uses Gradle Version Catalogs: this tool may not behave as expected.」→ 今現在、Gradle Version Catalog には gradle-versions-plugin が必須では? hatena-bookmark


【Android】Gradle Version Catalog 関連のプラグインを探す

Gradle Version Catalog。

TOML ファイル作って使ってみました。

Android Studio でこんな。

⚠ This project uses Gradle Version Catalogs: this tool may not behave as expected.

なんか気持ちが悪いですよね。

update dependencies まわりがこれまで通りに正常に動いてるかどうか、心配になります。

そこらをチェックしてくれるツールを探します。

以下、人気順。


👉 ben-manes/gradle-versions-plugin: Gradle plugin to discover dependency updates hatena-bookmark


👉 jmfayard/refreshVersions: Life is too short to google for dependencies and versions hatena-bookmark


👉 littlerobots/version-catalog-update-plugin: Gradle plugin for updating a project version catalog hatena-bookmark

それぞれどこかで見かけたことがあると思います。

機能は、そのうち Android Studio にマージされるんだろうなと妄想。

👉 Jetpack Compose Samples でも使われている「Version catalog update plugin」で libs.versions.toml を書き出してみる hatena-bookmark
👉 「⚠ This project uses Gradle Version Catalogs: this tool may not behave as expected.」→ 今現在、Gradle Version Catalog には gradle-versions-plugin が必須では? hatena-bookmark


【Kotlin】総当りのロジック - AccelerometerPlay

意外と自分で考えても埒が明かないので調べてみたので。


val countries = listOf("brazil", "italy", "england", "spain", "japan")

val games = mutableListOf<String>()


val indices = countries.indices
indices.forEach { left ->
  indices.forEach { right ->
    if (left < right) {
      games.add("${countries[left]}\tvs\t${countries[right]}")
    }
  }
}

// val rights = countries.toMutableList()
// countries.forEach { left ->
//   rights.removeFirst()
//   rights.forEach { right ->
//     games.add("$left\tvs\t$right")
//   }
// }


games.forEachIndexed() { index, game ->
  println("${index + 1}. $game")
}
println("All ${games.size} games")

assertTrue(
  countries.size * (countries.size - 1) / 2 == games.size
)


1. brazil	vs	italy
2. brazil	vs	england
3. brazil	vs	spain
4. brazil	vs	japan
5. italy	vs	england
6. italy	vs	spain
7. italy	vs	japan
8. england	vs	spain
9. england	vs	japan
10. spain	vs	japan
All 10 games

総当りの表を脳内でイメージできないと、このロジックは書けないと想う。

総当り

図表をまずは描いてみること大事。

以下、移植時、ボール同士の衝突判定時に使いました。



👉 Android AccelerometerPlay Sample - Google Archive hatena-bookmark

👉 【当たり判定】円と円の当たり hatena-bookmark


【Jetpack Compose】Accompanist から切り替えた Material の PullRefreshIndicator が消えない件

こんな記事を見つけまして。

まだ @ExperimentalMaterialApi になりますが Pull-to-refresh は Accompanist から卒業し、Material Design ライブラリーに追加されました。でも、Accompanist の SwipeRefreshLayout と異なって全体のレイアウトではなくインジケーターだけが追加されました。


var refreshing by remember { mutableStateOf(false) }

val state = rememberPullRefreshState(
    refreshing = refreshing,
    onRefresh = { .. }
)

Box(modifier = Modifier.pullRefresh(state = state)) {
    LazyColumn {
        if (!refreshing) {
            items(..)
        }
    }

    PullRefreshIndicator(
        refreshing = refreshing, 
        state = state, 
        modifier = Modifier.align(Alignment.TopCenter)
    )
}

👉 Jetpack Compose v1.3.0に気になったAPIのまとめ - Goodpatch Tech Blog hatena-bookmark

便利そうなので使ってみました。

 

■ Indicator が消えない!

公式のサンプルを見ながら、


import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState

val refreshScope = rememberCoroutineScope()
var refreshing by remember { mutableStateOf(false) }
var itemCount by remember { mutableStateOf(15) }

fun refresh() = refreshScope.launch {
    refreshing = true
    delay(1500)
    itemCount += 5
    refreshing = false
}

val state = rememberPullRefreshState(refreshing, ::refresh)

Box(Modifier.pullRefresh(state)) {
    LazyColumn(Modifier.fillMaxSize()) {
        if (!refreshing) {
            items(itemCount) {
                ListItem { Text(text = "Item ${itemCount - it}") }
            }
        }
    }

    PullRefreshIndicator(refreshing, state, Modifier.align(Alignment.TopCenter))
}

👉 androidx.compose.material.pullrefresh  |  Android Developers hatena-bookmark

実装してみましたが、引っ張る前から、表示されたまま消えない!! (少し読み込みが長いです。)

PullRefreshIndicator-scale=false

読み込み終了後、Box の範囲からは外れていくんだけども、消えない。

何なんすか。

バグ?

👉 PullRefresh: indicator left visible on screen [248274004] - Visible to Public - Issue Tracker hatena-bookmark

 

■ 対応策

PullRefreshIndicatorscale = true にします。

The default indicator for Compose pull-to-refresh, based on Android's SwipeRefreshLayout.

👉 androidx.compose.material.pullrefresh  |  Android Developers hatena-bookmark

以下のように追加します。


PullRefreshIndicator(
  refreshing = refreshing,
  state = state,
  modifier = Modifier.align(Alignment.TopCenter),
  scale = true // *
)

結果。

PullRefreshIndicator-scale=true

消えました。とりあえず。

上下に移動しながら 拡大 → 縮小 して、消えてるように見えます。

この Indicator て、確か以前は、scale してましたよね?

👉 Jetpack Compose Accompanist — An FAQ. | by Ben Trengrove | Android Developers | Medium hatena-bookmark