Search code examples
androidgradle

Gradle keeps promoting dependency version in my Android Compose project


So in my project, Gradle keeps promoting my android compose library (to be specific: 'androidx.activity:activity-compose') to the latest 1.8.0-alpha05 version. However, this version requires compileSdk 34 to run and Gradle Version 8.0.2 doesnt seem to support it.

My app's build.gradle:

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id("com.google.dagger.hilt.android")
    id("com.google.devtools.ksp")
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.20'
    id 'com.google.gms.google-services'
}

android {
    namespace 'com.my_app'
    compileSdk 33

    defaultConfig {
        applicationId "com.my_app"
        minSdk 21
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.debug
        }
    }

    applicationVariants.all { variant ->
        variant.addJavaSourceFoldersToModel(
                new File(buildDir, "generated/ksp/${variant.name}/kotlin")
        )
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_18
        targetCompatibility JavaVersion.VERSION_18
    }
    kotlinOptions {
        jvmTarget = '18'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.6'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {
    def exoPlayerVersion = "1.0.2"
    def room_version = "2.5.1"

    def composeBom = platform('androidx.compose:compose-bom:2023.05.01')
    implementation composeBom
    androidTestImplementation composeBom

    implementation 'androidx.core:core-ktx:1.10.1'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.1'
    implementation("androidx.lifecycle:lifecycle-runtime-compose:2.6.1")
    implementation 'androidx.activity:activity-compose:1.7.2' // <-- the culprit
    implementation "androidx.compose.animation:animation-graphics"
    implementation 'androidx.compose.ui:ui'
    implementation 'androidx.compose.ui:ui-graphics'
    implementation 'androidx.compose.ui:ui-tooling-preview'
    implementation 'androidx.compose.material3:material3'
    implementation 'androidx.compose.foundation:foundation'
    implementation 'com.google.android.material:material:1.10.0-alpha04'
    implementation('org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.1')

    // ... Other dependencies
}

Running ./gradlew checkDebugAarMetadata throws:

> A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction
   > 3 issues were found when checking AAR metadata:

       1.  Dependency 'androidx.activity:activity-compose:1.8.0-alpha05' requires libraries and applications that
           depend on it to compile against version 34 or later of the
           Android APIs.

           :app is currently compiled against android-33.

           Also, the maximum recommended compile SDK version for Android Gradle
           plugin 8.0.2 is 33. // <-- this

       2.  Dependency 'androidx.activity:activity-ktx:1.8.0-alpha05' requires libraries and applications that
           depend on it to compile against version 34 or later of the
           Android APIs.

           // same comment above

       3.  Dependency 'androidx.activity:activity:1.8.0-alpha05' requires libraries and applications that
           depend on it to compile against version 34 or later of the
           Android APIs.

           // same comment above

I tried using Gradle's resolution strategy but doesn't seem to work:

configurations.all {
    resolutionStrategy {
        force 'androidx.activity:activity-compose:1.7.2'
    }
}

Solution

  • Your version of androidx.activity is being upgraded because of your dependency:

    implementation 'com.google.android.material:material:1.10.0-alpha04'
    

    As per the Material 1.10.0-alpha04 release notes, that version requires AndroidX Activity 1.8.0-alpha05 and a compileSdkVersion of 34.

    As explained in this tweet:

    With Android 14's API 34 being finalized, ~all new Jetpack library releases have started to compile with API 34. That means your app is also going to need to compile with API 34.

    So you're likely to run into this more and more as time goes on.

    If you want to not use API 34 yet, you'll need to reduce your dependency on Material to use the latest stable - 1.9.0:

    implementation 'com.google.android.material:material:1.9.0'