Build Environment Overview
The following environment configuration was utilized when encountering the compilation failure:
Android Studio Version: Bumblebee | 2021.1.1 Patch 1
Build Number: AI-211.7628.21.2111.8139111
Runtime: OpenJDK 11.0.11+9-b60-7590822 amd64
OS: Windows 10
Memory Allocation: 1280MB
Cores: 6
Compilation Error Description
Upon atempting to run the application, the build process failed with conflicting artifact definitions related to the AndroidX Lifecycle library. Specifically, two distinct module versions introduced identical classes into the compilation path.
Duplicate class androidx.lifecycle.ViewModelLazy found in modules
lifecycle-viewmodel-2.5.1-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.1)
and lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1)
Duplicate class androidx.lifecycle.ViewTreeViewModelKt found in modules
lifecycle-viewmodel-2.5.1-runtime (androidx.lifecycle:lifecycle-viewmodel:2.5.1)
and lifecycle-viewmodel-ktx-2.3.1-runtime (androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1)
This typically occurs in projects utilizing default configurations without explicit dependency version synchronization. In this scenario, the project mixes lifecycle-viewmodel version 2.5.1 with lifecycle-viewmodel-ktx version 2.3.1, causing symbol collisions.
Resolution Straetgy
To resolve the ambiguity without altering the entire dependency tree, exclude the conflicting Kotlin extensions from all configurations within the app/build.gradle file. This forces the build system to rely on the primary runtime modules.
Insert the following block inside the android section:
android {
compileSdk 33
defaultConfig {
applicationId "com.project.demo.app"
minSdk 24
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// Dependency Conflict Mitigation
configurations.all {
exclude group: 'androidx.lifecycle', module: 'lifecycle-runtime-ktx'
exclude group: 'androidx.lifecycle', module: 'lifecycle-viewmodel-ktx'
}
}
Complete Module Configuration
The full dependency list below reflects the modified state required to prevent the duplicate clas error while maintaining standard testing and UI libraries.
plugins {
id 'com.android.application'
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.annotation:annotation:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
// Core Lifecycle dependencies
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
// Testing suites
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}