[FIXED] Android Studio – How to upgrade jetpack compose?

Issue

I am currently using Android studio (2021.2.1 Patch 2). On choosing empty compose activity, the compose version being used is 1.1.0-beta01 .How can I upgrade it to 1.2.0-beta01 as the earlier one gives error while using LazyVerticalGrid? I am beginner to android. Any help would be greatly appreciated.

Here is the app/build.gradle file of the project :

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

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.courses"
        minSdk 21
        targetSdk 32
        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 compose_version
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation "androidx.compose.ui:ui:$compose_version"
    implementation "androidx.compose.material:material:$compose_version"
    implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.activity:activity-compose:1.3.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
    debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
    debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
}

Toplevel build.gradle file :

buildscript {
    ext {
        compose_version = '1.1.0-beta01'
    }
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    id 'com.android.application' version '7.2.2' apply false
    id 'com.android.library' version '7.2.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

I have tried changing compose version in toplevel build.gradle file to 1.2.0-beta01 but the build of the project fails.

Please specify in comments if you need some more information.
Thanks

Solution

Without seeing the compilation error message I can only guess at what might be the cause. But I see a couple of things that might give you problems.

The Compose Compiler for 1.2.0-beta01 requires Kotlin Plugin version 1.6.21 See Compose Kotlin Compatibility Map

Additionally, from version 1.2.0 onwards they’ve introduced independent versioning for Compose Compiler from the rest of the Compose libraries, meaning that kotlinCompilerExtensionVersion needs to point to a different version. As of right now the latest compiler version is 1.3.1 which needs kotlin plugin version 1.7.10.

Try these changes.

ext {
    compose_version = '1.3.0-beta02' // or 1.2.1 if you want the latest stable version
}
composeOptions {
    kotlinCompilerExtensionVersion "1.3.1"
}
plugins {
    // others omitted
    id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}

Answered By – Mellow

Answer Checked By – David Marino (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published