Project update recommended: Android Gradle Plugin can be upgraded. Error message: Can not find AGP version in build files

Viewed 5099

After a recommendation in Android Studio to upgrade Android Gradle Plugin from 7.0.0 to 7.0.2 the Upgrade Assistant notifies that Cannot find AGP version in build files, and therefore I am not able to do the upgrade.

What shall I do?

Thanks

Code at build.gradle (project)

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath Libs.androidGradlePlugin
        classpath Libs.Kotlin.gradlePlugin
        classpath Libs.Hilt.gradlePlugin
    }
}

plugins {
    id 'com.diffplug.spotless' version '5.12.4'
}

subprojects {
    repositories {
        google()
        mavenCentral()

        if (!Libs.AndroidX.Compose.snapshot.isEmpty()) {
            maven { url Urls.composeSnapshotRepo }
        }

        if (Libs.Accompanist.version.endsWith('SNAPSHOT')) {
            maven { url Urls.mavenCentralSnapshotRepo }
        }
    }

    apply plugin: 'com.diffplug.spotless'
    spotless {
        kotlin {
            target '**/*.kt'
            targetExclude("$buildDir/**/*.kt")
            targetExclude('bin/**/*.kt')
            ktlint(Versions.ktLint)
            licenseHeaderFile rootProject.file('spotless/copyright.kt')
        }
    }

    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
        kotlinOptions {
            jvmTarget = "1.8"

            // Use experimental APIs
            freeCompilerArgs += '-Xopt-in=kotlin.RequiresOptIn'
        }
    }
    // androidx.test and hilt are forcing JUnit, 4.12. This forces them to use 4.13
    configurations.configureEach {
        resolutionStrategy {
            force Libs.JUnit.junit
        }
    }
}

Code at build.gradle (module)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'dagger.hilt.android.plugin'
}

// Reads the Google maps key that is used in the AndroidManifest
Properties properties = new Properties()
if (rootProject.file("local.properties").exists()) {
    properties.load(rootProject.file("local.properties").newDataInputStream())
}

android {
    compileSdkVersion 31
    defaultConfig {
        applicationId "androidx.compose.samples.crane"
        minSdkVersion 21
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "androidx.compose.samples.crane.CustomTestRunner"

        manifestPlaceholders = [ googleMapsKey : properties.getProperty("google.maps.key", "") ]
    }

    signingConfigs {
        // We use a bundled debug keystore, to allow debug builds from CI to be upgradable
        // Cert fingerprint = 
        debug {
            storeFile rootProject.file('debug.keystore')
            storePassword 'xxxxxxxxxx'
            keyAlias 'xxxxxxxxxxx'
            keyPassword 'xxxxxxxxxx'
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }

        release {
            minifyEnabled true
            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

        // Disable unused AGP features
        buildConfig false
        aidl false
        renderScript false
        resValues false
        shaders false
    }

    composeOptions {
        kotlinCompilerExtensionVersion Libs.AndroidX.Compose.version
    }

    packagingOptions {
        // Multiple dependency bring these files in. Exclude them to enable
        // our test APK to build (has no effect on our AARs)
        excludes += "/META-INF/AL2.0"
        excludes += "/META-INF/LGPL2.1"
    }
}

dependencies {
    implementation Libs.Kotlin.stdlib
    implementation Libs.Kotlin.Coroutines.android
    implementation Libs.GoogleMaps.maps
    implementation Libs.GoogleMaps.mapsKtx
    constraints {
        // Volley is a transitive dependency of maps
        implementation(Libs.Volley.volley) {
            because("Only volley 1.2.0 or newer are available on maven.google.com")
        }
    }

    implementation Libs.Accompanist.insets
    implementation Libs.AndroidX.Activity.activityCompose
    implementation Libs.AndroidX.appcompat
    implementation Libs.AndroidX.Compose.runtime
    implementation Libs.AndroidX.Compose.foundation
    implementation Libs.AndroidX.Compose.material
    implementation Libs.AndroidX.Compose.layout
    implementation Libs.AndroidX.Compose.animation
    implementation Libs.AndroidX.Compose.tooling
    implementation Libs.AndroidX.Lifecycle.viewModelCompose
    implementation Libs.AndroidX.Lifecycle.viewModelKtx
    implementation Libs.Coil.compose
    implementation Libs.Hilt.android
    kapt Libs.Hilt.compiler

    androidTestImplementation Libs.JUnit.junit
    androidTestImplementation Libs.AndroidX.Test.runner
    androidTestImplementation Libs.AndroidX.Test.espressoCore
    androidTestImplementation Libs.AndroidX.Test.rules
    androidTestImplementation Libs.AndroidX.Test.Ext.junit
    androidTestImplementation Libs.Kotlin.Coroutines.test
    androidTestImplementation Libs.AndroidX.Compose.uiTest
    androidTestImplementation Libs.Hilt.android
    androidTestImplementation Libs.Hilt.testing
    kaptAndroidTest Libs.Hilt.compiler
}

Code at build.gradle.kts

repositories {
    jcenter()
}

plugins {
    `kotlin-dsl`
}
3 Answers

In my case the following steps solved this:

cd to project directory
bash ./gradlew help --scan
bash ./gradlew wrapper --gradle-version 7.0.2

See the upgrade instructions to version 7.0.2 in the gradle release notes.

I don't know if it is critical for your problem but modifying this

repositories {
    jcenter()
}

to

repositories {
    mavenCentral()
}

should be needed. Because jcenter() is obsolete.

In my case, I used custom properties for maintaining the version of plugins. So, upgrade assistant was throwing this error (Can not find AGP version in build files).

plugins {
   id 'com.android.application' version versionManagement.android apply false
   id 'com.android.library' version versionManagement.android apply false
   // ...
}

I had to manually update the value for versionManagement.android

I see you have (ext) properties to hold the plugin (classpath)

buildscript {
    repositories {
        google()
    }
    dependencies {
        classpath Libs.androidGradlePlugin
        classpath Libs.Kotlin.gradlePlugin
        classpath Libs.Hilt.gradlePlugin
    }
}

In this case, tool cannot update the AGP version. You should manually update Libs.androidGradlePlugin

Related