【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


【Plugin DSL】「com.android.tools.build:gradle」の記述は不要?

Plugin DSL に移行ながらの気持ちが悪いのは以下の変化。


// 2020-10-13
// build.gradle ( root | Project )
classpath "com.android.tools.build:gradle:4.0.2"

// build.gradle ( module | Module )
apply plugin: 'com.android.application'


// 2022-10-25
// build.gradle ( root | Project )
plugins {
  id 'com.android.application' version '7.3.1' apply false
  id 'com.android.library' version '7.3.1' apply false
}

// build.gradle ( module | Module )
plugins {
  id 'com.android.application'
}

👉 今現在 AndroidStudio が推してくる Gradle の設定記述と構成を2年前と比較 - libs.versions.toml, settings.gradle, build.gradle hatena-bookmark

ここに登場する3つの文字列。


com.android.tools.build:gradle
com.android.application
com.android.library

ここらがなんとなくあやふやに使っていたのでよく調べてみます。

 

■ アプリケーションかライブラリか

root の build.gradle でプラグインのバージョンを指定しておいて、


// 2022-10-25
// build.gradle ( root | Project )
plugins {
  id 'com.android.application' version '7.3.1' apply false
  id 'com.android.library' version '7.3.1' apply false
}

その子のモジュールの build.gradle で適用する、


// 2022-10-25
// build.gradle ( module | Module )
plugins {
  id 'com.android.application'
}

ということです。

👉 Applying external plugins with same version to subprojects - Using Gradle Plugins hatena-bookmark

そのときに、そのモジュールがアプリケーションかライブラリかで適用するGradleプラグインのIDが変わります。


plugins {
  id 'com.android.application'
}


plugins {
  id 'com.android.library'
}

👉 Android ライブラリの作成  |  Android デベロッパー  |  Android Developers hatena-bookmark

 

■ 'com.android.tools.build:gradle' の記述

Plugin DSL が登場する前は以下のような記述でしたが、


// 2020-10-13
// build.gradle ( root | Project )
classpath "com.android.tools.build:gradle:4.0.2"

// build.gradle ( module | Module )
apply plugin: 'com.android.application'

Plugin DSL では消えてます、"com.android.tools.build:gradle"

👉 【Plugin DSL】Android Gradle Plugin のバージョンがわからない hatena-bookmark

ここで、Plugin ID から参照されるリポジトリの内容を見てみます。

Google Maven Repository では、

Plugin Marker Artifacts
Since the plugins {} DSL block only allows for declaring plugins by their globally unique plugin id and version properties, Gradle needs a way to look up the coordinates of the plugin implementation artifact. To do so, Gradle will look for a Plugin Marker Artifact with the coordinates plugin.id:plugin.id.gradle.plugin:plugin.version. This marker needs to have a dependency on the actual plugin implementation. Publishing these markers is automated by the java-gradle-plugin.

プラグインマーカーアーティファクト
plugins {} DSLブロックでは、グローバルにユニークなプラグインIDとバージョンプロパティによってのみプラグインを宣言できるので、Gradleはプラグイン実装アーティファクトの座標を検索する方法が必要です。そのために、Gradleは plugin.id:plugin.id.gradle.plugin:plugin.version という座標を持つプラグインマーカーアーティファクトを探します。このマーカーは、実際のプラグイン実装への依存関係を持つ必要があります。これらのマーカーの発行は、java-gradle-pluginによって自動化されます。

👉 Plugin Marker Artifacts - Using Gradle Plugins hatena-bookmark


plugin.id:plugin.id.gradle.plugin:plugin.version
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ということなので、

Plugin ID : com.android.application は、


Group Id    : com.android.application
Artifact ID : com.android.application.gradle.plugin

で、

Plugin ID : com.android.library は、


Group Id    : com.android.library
Artifact ID : com.android.library.gradle.plugin

で、検索します。


Group ID	com.android.application
Artifact ID	com.android.application.gradle.plugin
Version	8.0.0-alpha06
Gradle Groovy DSL	
implementation 'com.android.application:com.android.application.gradle.plugin:8.0.0-alpha06'
Gradle Kotlin DSL	
implementation("com.android.application:com.android.application.gradle.plugin:8.0.0-alpha06") 
Last Updated Date	10/24/2022


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.android.application</groupId>
  <artifactId>com.android.application.gradle.plugin</artifactId>
  <version>8.0.0-alpha06</version>
  <packaging>pom</packaging>
  <description>Gradle plug-in to build Android applications.</description>
  <url>https://developer.android.com/studio/build</url>
  <name>com.android.tools.build.gradle</name>
  <licenses>
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>The Android Open Source Project</name>
    </developer>
  </developers>
  <scm>
    <connection>git://android.googlesource.com/platform/tools/base.git</connection>
    <url>https://android.googlesource.com/platform/tools/base</url>
  </scm>
  <dependencies>
    <dependency>
      <groupId>com.android.tools.build</groupId>
      <artifactId>gradle</artifactId>
      <version>8.0.0-alpha06</version>
    </dependency>
  </dependencies>
</project>

👉 com.android.application.com.android.application.gradle.plugin-8.0.0-alpha06 - Google's Maven Repository hatena-bookmark


Group ID	com.android.library
Artifact ID	com.android.library.gradle.plugin
Version	8.0.0-alpha06
Gradle Groovy DSL	
implementation 'com.android.library:com.android.library.gradle.plugin:8.0.0-alpha06' 
Gradle Kotlin DSL	
implementation("com.android.library:com.android.library.gradle.plugin:8.0.0-alpha06")
Last Updated Date	10/24/2022


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.android.library</groupId>
  <artifactId>com.android.library.gradle.plugin</artifactId>
  <version>8.0.0-alpha06</version>
  <packaging>pom</packaging>
  <description>Gradle plug-in to build Android applications.</description>
  <url>https://developer.android.com/studio/build</url>
  <name>com.android.tools.build.gradle</name>
  <licenses>
    <license>
      <name>The Apache Software License, Version 2.0</name>
      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
      <distribution>repo</distribution>
    </license>
  </licenses>
  <developers>
    <developer>
      <name>The Android Open Source Project</name>
    </developer>
  </developers>
  <scm>
    <connection>git://android.googlesource.com/platform/tools/base.git</connection>
    <url>https://android.googlesource.com/platform/tools/base</url>
  </scm>
  <dependencies>
    <dependency>
      <groupId>com.android.tools.build</groupId>
      <artifactId>gradle</artifactId>
      <version>8.0.0-alpha06</version>
    </dependency>
  </dependencies>
</project>

👉 com.android.library.com.android.library.gradle.plugin-8.0.0-alpha06 - Google's Maven Repository hatena-bookmark

それぞれ、同じ


<dependency>
  <groupId>com.android.tools.build</groupId>
  <artifactId>gradle</artifactId>
  <version>8.0.0-alpha06</version>
</dependency>

となってますので、以下の実装アーティファクトを解決できてるのでしょう。


Group ID	com.android.tools.build
Artifact ID	gradle
Version	8.0.0-alpha06
Gradle Groovy DSL	
implementation 'com.android.tools.build:gradle:8.0.0-alpha06'
Gradle Kotlin DSL	
implementation("com.android.tools.build:gradle:8.0.0-alpha06") 
Last Updated Date	10/24/2022

👉 com.android.tools.build.gradle-8.0.0-alpha06 - Google's Maven Repository hatena-bookmark

よって、'com.android.tools.build:gradle' の記述は、plugin DSL では記述の必要はないのでしょう。

少し古いコードに以下のような記述が見られるのは、プラグインマーカーアーティファクトが公開される前のものだったということで納得できます。


pluginManagement {
  // ...
  resolutionStrategy {
    eachPlugin {
      if(requested.id.namespace == "com.android") {
        useModule("com.android.tools.build:gradle:${requested.version}")
      }
    }
  }

 

■ まとめ

最後に、AndroidStudio のプロジェクトテンプレートを利用して確認します。

例にならって、 New Project から Empty Activity を選択後、 New Module から Android Library を作成します。

【Plugin DSL】'com.android.tools.build:gradle'

以下、Gradle まわりの設定ファイル該当部分です。


// settings.gradle
pluginManagement {
  repositories {
    gradlePluginPortal()
    google()
    mavenCentral()
  }
  repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
  repositories {
    google()
    mavenCentral()
  }

include ':app'
include ':el'


// build.gradle
plugins {
  id 'com.android.application' version '7.3.1' apply false
  id 'com.android.library' version '7.3.1' apply false
  id 'org.jetbrains.kotlin.android' version '1.7.20' apply false


// app/build.gradle
plugins {
  id 'com.android.application'
  id 'org.jetbrains.kotlin.android'
  implementation project(path: ':el')


// el/build.gradle
plugins {
  id 'com.android.library'
  id 'org.jetbrains.kotlin.android'

'com.android.tools.build:gradle' は見当たりませんね!

素晴らしいです、Plugin DSL !!

👉 Jetpack Compose Samples でも使われている「Version catalog update plugin」で libs.versions.toml を書き出してみる hatena-bookmark
👉 今現在 AndroidStudio が推してくる Gradle の設定記述と構成を2年前と比較 - libs.versions.toml, settings.gradle, build.gradle hatena-bookmark