build.gradle.kts conversion for Android Studio Empty Activity

build.gradle.kts conversion for Android Studio Empty Activity

Quick blog posting showing you the build.gradle.kts version of the default build.gradle file from the “empty activity” Android Studio template.

This post will go stale fast, get it whilst its hot. 🙂

The original templates come from the New Project, other templates are explained here: https://developer.android.com/studio/projects/templates

Google has an explainer of converting build configuration scripts from Groovy to Kotlin: https://developer.android.com/build/migrate-to-kotlin-dsl

Gradle has a primer for showing what build.gradle commands in Groovy look like in Kotlin build.gradle.kts files: https://docs.gradle.org/current/userguide/kotlin_dsl.html

Finally Google announced that Kotlin build.gradle.kts would be the new default for build configuration files: https://android-developers.googleblog.com/2023/04/kotlin-dsl-is-now-default-for-new-gradle-builds.html

Here is the current new project Groovy build.gradle file:

Groovy DSL

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

android {
   namespace 'com.blundell.demo'
   compileSdk 33
   defaultConfig {
       applicationId "com.blundell.demo"
       minSdk 29
       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.3.2'
   }
   packagingOptions {
       resources {
           excludes += '/META-INF/{AL2.0,LGPL2.1}'
       }
   }
}

dependencies {
   implementation 'androidx.core:core-ktx:1.8.0'
   implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0')
   implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
   implementation 'androidx.activity:activity-compose:1.5.1'
   implementation platform('androidx.compose:compose-bom:2022.10.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:2022.10.00')
   androidTestImplementation 'androidx.compose.ui:ui-test-junit4'
   debugImplementation 'androidx.compose.ui:ui-tooling'
   debugImplementation 'androidx.compose.ui:ui-test-manifest'
}

>> You probably are looking for this KTS <<

And here is the Kotlin build.gradle.kts version.

Kotlin DSL

plugins {
   id("com.android.application")
   id("org.jetbrains.kotlin.android")
}

android {
   namespace = "com.blundell.demo"
   compileSdkVersion(33)
   defaultConfig {
       applicationId = "com.blundell.demo"
       minSdk = 29
       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_11
       targetCompatibility = JavaVersion.VERSION_11
   }
   kotlinOptions {
       jvmTarget = "11"
   }
   buildFeatures {
       compose = true
   }
   composeOptions {
       kotlinCompilerExtensionVersion = "1.3.2"
   }
   packagingOptions {
       resources {
           excludes += listOf("/META-INF/{AL2.0,LGPL2.1}")
       }
   }
}

dependencies {
   implementation("androidx.core:core-ktx:1.8.0")
   implementation(platform("org.jetbrains.kotlin:kotlin-bom:1.8.0"))
   implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
   implementation("androidx.activity:activity-compose:1.5.1")
   implementation(platform("androidx.compose:compose-bom:2022.10.00"))
   implementation("androidx.compose.ui:ui")
   implementation("androidx.compose.ui:ui-graphics")
   implementation("androidx.compose.ui:ui-tooling-preview")
   implementation("androidx.compose.material3:material3")
   debugImplementation("androidx.compose.ui:ui-tooling")
   debugImplementation("androidx.compose.ui:ui-test-manifest")
   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:2022.10.00"))
   androidTestImplementation("androidx.compose.ui:ui-test-junit4")
}

Hope that quick conversion to Kotlin build.gradle.kts helps you! And hopefully this blog post will be deprecated when they update the templates to create Kotlin build Gradle files. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *