建议进行项目更新:可以升级Android Gradle插件。错误信息:在构建文件中找不到AGP版本。

13
在Android Studio中建议将Android Gradle插件从7.0.0升级到7.0.2之后,升级助手通知 无法在构建文件中找到AGP版本,因此我无法进行升级。 我该怎么办? 谢谢。 build.gradle(项目)的代码。
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
        }
    }
}

build.gradle(模块)中的代码

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
}

build.gradle.kts 的代码

repositories {
    jcenter()
}

plugins {
    `kotlin-dsl`
}

2
请仅发布 build.gradle 文件。 - iamanbansal
@iamanbansal 我马上就去做。谢谢。 - lbarqueira
你是否升级了Libs.androidGradlePlugin的版本? - iamanbansal
4个回答

7

在我的情况下,以下步骤解决了这个问题:

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

请在Gradle发布说明中查看版本7.0.2的升级说明


谢谢,我会尝试的。 - lbarqueira

3

我不知道对你的问题是否关键,但修改这个可能是必要的。

repositories {
    jcenter()
}

to

repositories {
    mavenCentral()
}

应该需要这样做,因为jcenter()已经过时了。


不是我问题的关键,感谢您的回答。 - lbarqueira

1
在我的情况下,我使用自定义属性来维护插件版本。因此,升级助手会抛出此错误(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
   // ...
}

我需要手动更新versionManagement.android的值。

我看到你有(ext)属性来保存插件(classpath)。

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

在这种情况下,工具无法更新AGP版本。您需要手动更新Libs.androidGradlePlugin

1
当我尝试使用“Tools> AGP Upgrade Assistant”时,在Android Studio中也遇到了同样的错误。
对于那些不知道的人来说,“Android Gradle Plugin”是指在项目级build.gradle中指定的依赖项com.android.tools.build:gradle:(明确一点,不是我的模块级build.gradle)。
在我的情况下,问题在于我的项目级build.gradle文件使用变量指定了Android Gradle Plugin的版本,而不是硬编码版本。
我的项目在gradle.properties中有以下变量,并在build.gradle文件中引用它:
gradle_version=7.2.2

它在项目级别的build.gradle文件中像这样引用它:
classpath "com.android.tools.build:gradle:$gradle_version"

为了解决这个问题,我修改了我的项目级build.gradle文件,从这个开始:
classpath "com.android.tools.build:gradle:$gradle_version"

到这个:
classpath 'com.android.tools.build:gradle:7.3.1'

在进行了这些更改之后,Tools > AGP Upgrade Assistant不再抱怨“在构建文件中找不到AGP版本”。此外,我还能够使用AGP Upgrade Assistant将com.android.tools.build:gradle:7.2.2升级到com.android.tools.build:gradle:7.3.1。

我的答案与@suryavel-tr的答案类似,但在我的情况下,版本号是由gradle变量指定的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接