Android Studio Giraffe(2023.07)になって、ビルドスクリプトの推奨がKotlin DSLになりました。
この機会に、ビルドスクリプトと記述言語についてまとめます。
※環境:Android Studio Giraffe | 2022.3.1
:Android Gradle Plugin 8.1.0
:Gradle 8.0
GroovyとKotlin
Android StudioはビルドツールにGradleを採用しています。そして、Gradleはビルドスクリプト(パラメータや手順を記した指示書)をGroovyで記述します。
GroovyはJavaプラットフォーム上で動作するプログラミング言語です。ソースコートはコンパイルされてJavaのバイナリコードになり、JVM上で動作します。
また、KolinもJavaプラットフォーム上で動作するプログラミング言語です。
従って、JVM上で動作するアプリはGroovyとKotlinのどちらの言語を用いても作れます。

言語の特性があるので、向き不向きはありますが…
DSLとビルドスクリプト
DSL(Domain Specific Language)とは「特定の処理に特化して作られたプログラミング言語」のことを言います。「プログラミング言語」と書きましたが、実際は「関数・クラスを格納したライブラリ」です。
高度な機能がDSL(関数・クラス)に集約されているので、これらを使えば特定の処理が簡素に記述できます。
GradleはJavaプラットフォーム向けのビルドに特化したDSLを提供しています。ビルドスクリプトはDSLを使って簡素に記述できます。

現在、Gradle(Ver4.0以降)が提供するDSLは、Groovy用の「Groovy DSL」とKotlin用の「Kotlin DSL」があります。

ですので、ビルドスクリプトは、GroovyまたはKotlinを使った2通りの記述が可能です。
推奨のビルトスクリプト
Android StudioがFlamingo⇒Giraffeに代わって、推奨のビルドスクリプトがGroovy⇒Kotlinへ変更になりました。

推奨が変わっただけで、両方の記述が可能です。ただし、Kotlinへ淘汰される可能性は大きいと思います。早めにKotlinへ移行した方が良いでしょう!
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}
android {
namespace = "com.example.myapplication"
compileSdk = 33
defaultConfig {
applicationId = "com.example.myapplication"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
release {
isMinifyEnabled = 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"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.4.3"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.2")
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
}
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.myapplication'
compileSdk 33
defaultConfig {
applicationId "com.example.myapplication"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
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'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion '1.4.3'
}
packaging {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
implementation 'androidx.activity:activity-compose:1.7.2'
implementation platform('androidx.compose:compose-bom:2023.03.00')
implementation 'androidx.compose.ui:ui'
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.compose.ui:ui-tooling-preview'
implementation 'androidx.compose.material3:material3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation platform('androidx.compose:compose-bom:2023.03.00')
androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
debugImplementation 'androidx.compose.ui:ui-tooling'
debugImplementation 'androidx.compose.ui:ui-test-manifest'
}
Kotlinを使ったビルドスクリプトの記述方法について、「ビルドを構成する」が参考になります。GroovyとKotlinの記述の対比ができて、便利です。
関連記事:
