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

「推してくる」ていうか、AndroidStudio で、プロジェクトをシンプルに作ろうとするときのテンプレートの記述がどうなっているか、の話です。

利用した AndroidStudio は、当時と今現在の Stable 最新です。

Build #AI-213.7172.25.2113.9123335, built on September 30, 2022


Android Studio Dolphin | 2021.3.1 Patch 1
Build #AI-213.7172.25.2113.9123335, built on September 30, 2022
Runtime version: 11.0.13+0-b1751.21-8125866 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.6
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 16
Registry:
    external.system.auto.import.disabled=true
    ide.text.editor.with.preview.show.floating.toolbar=false
    documentation.show.toolbar=true

Non-Bundled Plugins:
    manjaro.mpb (1.7)
    com.thvardhan.gradianto (4.5)
    com.imuxuan.core.search (1.1)
    com.cursive-ide.emacsplus (0.3.900)
    org.jetbrains.compose.desktop.ide (1.2.0)
    com.developerphil.adbidea (1.6.8)
    com.squareup.sqldelight (1.5.3)
    com.jetbrains.kmm (0.3.4(213-1.7.11-357-IJ)-241)
    GradleDependenciesHelper (1.16)

最も使いやすい「Empty Activity」でプロジェクトを構成したときの、Gradle 設定まわりのファイルがどうなってるかの確認です。

Empty Activity

2020-10-13 と2年後の 2022-10-25 のテンプレートを比較してみます。

👉 benigumocom/EmptyActivity hatena-bookmark

 

■ gradle/libs.versions.toml

2020-10-13 と 2022-10-25 ともに作成されませんでした。

gradle/libs.versions.toml

Version Catalog は、今現在はまだ推されてません。

👉 「⚠ This project uses Gradle Version Catalogs: this tool may not behave as expected.」→ 今現在、Gradle Version Catalog には gradle-versions-plugin が必須では? hatena-bookmark
👉 【Gradle Version Catalog】libs.versions.toml キー名の形式 camelCase vs kebab-case hatena-bookmark
👉 Gradle Version Catalog への書き換えツールを作る【python】 hatena-bookmark

 

■ settings.gradle

2020-10-13


include ':app'
rootProject.name = "EmptyActivity"

2022-10-25


pluginManagement {
  repositories {
    gradlePluginPortal()
    google()
    mavenCentral()
  }
}
dependencyResolutionManagement {
  repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
  repositories {
    google()
    mavenCentral()
  }
}
rootProject.name = "EA"
include ':app'

pluginManagement, dependencyResolutionManagement が新しく使われています。

プラグインやライブラリのリポジトリ指定は、優先順にここに記述するようになっています。

 

■ build.gradle ( root | Project )

2020-10-13


// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
  ext.kotlin_version = "1.4.10"
  repositories {
    google()
    jcenter()
  }
  dependencies {
    classpath "com.android.tools.build:gradle:4.0.2"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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

allprojects {
  repositories {
    google()
    jcenter()
  }
}

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

2022-10-25


// Top-level build file where you can add configuration options common to all sub-projects/modules.
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
}

プラグイン、ライブラリの取得先リポジトリの記述がなくなり、

プラグインに関しては、


classpath "{groupId}:{artifactId}:{version}"

の記述から、plugin DSL を利用した


plugins {
  id "{pluginId}" version '{version}' apply false
}

の記述になりました。

{groupId}:{artifactId} から {pluginId} へのの変換は、Gradleプラグインと公開リポジトリの連携で解決されます。

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

あと、task clean(type: Delete) は消えています、不要なのか。

 

■ build.gradle ( module | Module )

2020-10-13


apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
  compileSdkVersion 29
  buildToolsVersion "29.0.3"

  defaultConfig {
    applicationId "com.example.emptyactivity"
    minSdkVersion 24
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  implementation fileTree(dir: "libs", include: ["*.jar"])
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation 'androidx.core:core-ktx:1.3.2'
  implementation 'androidx.appcompat:appcompat:1.2.0'
  implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'androidx.test.ext:junit:1.1.2'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

2022-10-25


plugins {
  id 'com.android.application'
  id 'org.jetbrains.kotlin.android'
}

android {
  namespace 'com.template.ea'
  compileSdk 32

  defaultConfig {
    applicationId "com.template.ea"
    minSdk 28
    targetSdk 32
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }

  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
  kotlinOptions {
    jvmTarget = '1.8'
  }
}

dependencies {

  implementation 'androidx.core:core-ktx:1.7.0'
  implementation 'androidx.appcompat:appcompat:1.5.1'
  implementation 'com.google.android.material:material:1.7.0'
  implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
  testImplementation 'junit:junit:4.13.2'
  androidTestImplementation 'androidx.test.ext:junit:1.1.3'
  androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

apply plugin:Plugin DSL 記述となっています。

あとは、namespace と Java のバージョン指定


namespace 'com.template.ea'


compileOptions {
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
  jvmTarget = '1.8'
}

が追加されています。

 

■ まとめ

大まかに全体的に見て、理解して変更しやすいのですが、気になるのは、


// 2020-10-13
// build.gradle ( root | Project )
classpath "com.android.tools.build:gradle:4.0.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

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


// 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
  id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
}

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

となる変化です。

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

あと、buildSrc や build.gradle.kts は推されてないのでしょうかね。


👉 「⚠ This project uses Gradle Version Catalogs: this tool may not behave as expected.」→ 今現在、Gradle Version Catalog には gradle-versions-plugin が必須では? hatena-bookmark
👉 【Gradle Version Catalog】libs.versions.toml キー名の形式 camelCase vs kebab-case hatena-bookmark
👉 Gradle Version Catalog への書き換えツールを作る【python】 hatena-bookmark


関連ワード:  AndroidAndroidStudioGoogleGradleIDEAJetBrainsKotlinツールニュースライブラリ開発