Todo Application Part-1: Project Gradle Setup — Setting up Gradle and managing dependencies.

Android-World
5 min readNov 29, 2023

--

Photo by Marvin Meyer on Unsplash

Welcome back to our series on developing “My Daily Planner,” an innovative Android app designed to enhance daily task management. In our first article, we introduced the project and outlined our ambitious roadmap. Now, in this installment, we delve into the technicalities of setting up a robust and efficient Gradle configuration, utilizing advanced tools like Hilt, Jetpack Compose BOM, and version catalogs.

Why Choose Hilt for Dependency Injection?

Hilt, a modern dependency injection library built on top of Dagger, simplifies the process of injecting dependencies in Android applications. Here’s why Hilt is a game-changer:

  1. Simplified Configuration: Hilt reduces boilerplate code and simplifies the setup process compared to traditional Dagger implementations.
  2. Compile-Time Validation: It offers compile-time correctness of dependencies, minimizing runtime errors.
  3. Android Lifecycle Awareness: Hilt is designed to work seamlessly with Android components, respecting their lifecycles.
  4. Scalability and Maintainability: As your project grows, Hilt makes it easier to manage dependencies, ensuring a clean and maintainable codebase.

Embracing Jetpack Compose for UI Development

Jetpack Compose, Android’s modern toolkit for building native UI, offers several advantages:

  1. Declarative UI: Compose allows you to describe your UI in a more intuitive and concise way.
  2. Less Code: It significantly reduces the amount of boilerplate code needed for UI development.
  3. Reactive Programming: Compose fits well with the reactive programming paradigm, making your UI code more robust and predictable.
  4. Interoperability: It can coexist with existing UI toolkit components, allowing for gradual adoption.

The Advantages of Using Jetpack Compose BOM

Jetpack Compose, Android’s modern toolkit for building native UI, has transformed how developers create interfaces. By using the Bill of Materials (BOM), we gain several advantages:

  1. Consistent Versioning: The BOM ensures that all Compose libraries are compatible and are of the same version, reducing conflicts and simplifying dependency management.
  2. Ease of Updates: Updating Compose libraries becomes a matter of changing a single version number in the BOM.
  3. Reduced Errors: It minimizes the risk of inadvertently using incompatible library versions, which can lead to runtime crashes.

Embracing Version Catalogs in Gradle

Version catalogs represent a new way to manage dependencies in Gradle. They bring several benefits:

  1. Centralized Dependency Management: Version catalogs allow you to define and manage all your dependencies in one place, making it easier to update and maintain them.
  2. Improved Readability: They make build scripts cleaner and more readable by abstracting version numbers and group IDs.
  3. Enhanced Collaboration: With a clear and centralized list of dependencies, team members can easily understand and work with the project’s dependencies.

Implementing Our Gradle Setup

With these tools and practices, “My Daily Planner” is set up for success. Our Gradle configuration will be clean, maintainable, and scalable, accommodating the complexities of modern Android development.

Gradle Configuration

Version Catalogs Setup

Create a libs.versions.toml file in the gradle folder:

[versions]
agp = "8.2.0-rc03"
kotlin = "1.9.20"
core-ktx = "1.12.0"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
lifecycle-runtime-ktx = "2.6.2"
activity-compose = "1.8.1"
compose-bom = "2023.10.01"
hilt = "2.48.1"
hiltExt = "1.1.0"


[libraries]
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle-runtime-ktx" }
activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activity-compose" }
compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "compose-bom" }
ui = { group = "androidx.compose.ui", name = "ui" }
ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
material3 = { group = "androidx.compose.material3", name = "material3" }

hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hilt" }
hilt-android-testing = { module = "com.google.dagger:hilt-android-testing", version.ref = "hilt" }
hilt-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
hilt-ext-compiler = { module = "androidx.hilt:hilt-compiler", version.ref = "hiltExt" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
kapt = { id = "org.jetbrains.kotlin.kapt", version.ref = "kotlin" }

App Module Gradle Configuration (app/build.gradle.kts)

plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.kotlinAndroid)

alias(libs.plugins.kapt)
alias(libs.plugins.hilt)
}

android {
namespace = "com.androidworld.mydailyplanner"
compileSdk = 34

defaultConfig {
applicationId = "com.androidworld.mydailyplanner"
minSdk = 24
targetSdk = 34
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"
)
}
}
// Tests can be Robolectric or instrumented tests
sourceSets {
val sharedTestDir = "src/sharedTest/java"
getByName("test") {
java.srcDir(sharedTestDir)
}
getByName("androidTest") {
java.srcDir(sharedTestDir)
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = "1.5.4"
}

packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {

implementation(libs.core.ktx)
implementation(libs.lifecycle.runtime.ktx)
implementation(libs.activity.compose)
implementation(platform(libs.compose.bom))
implementation(libs.ui)
implementation(libs.ui.graphics)
implementation(libs.ui.tooling.preview)
implementation(libs.material3)

implementation(libs.hilt.android)
kapt(libs.hilt.compiler)
kapt(libs.hilt.ext.compiler)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
androidTestImplementation(libs.espresso.core)
androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation(libs.ui.test.junit4)


debugImplementation(libs.ui.tooling)
debugImplementation(libs.ui.test.manifest)
}

In conclusion, the setup of “My Daily Planner” is not just about writing code; it’s about crafting a sustainable and efficient development environment. By choosing Hilt, Jetpack Compose BOM, and version catalogs, we are not only embracing modern Android development practices but also paving the way for a smoother, more enjoyable development journey.

This is just our first step! As we progress through the series, we will continuously improve the project and add more features. Each part of the series is designed to build upon the previous, ensuring a comprehensive understanding and application of Android development techniques.

If you enjoyed the article and would like to show your support, be sure to:👏 Applaud for the story (50 claps) to help this article get featured
👉Follow me on
Medium
👉Follow me on Twitter
Check out more content on my Medium profile

--

--

Android-World

Experienced Senior Android Developer with a passion for developing high-quality, user-friendly apps. https://twitter.com/MyAndroid_World