Android Studio 建议将 Gradle 升级到 8.2.1,但不能

Android Studio suggests upgrading Gradle to 8.2.1, but can't

提问人:Sean Zlatnik 提问时间:9/9/2023 更新时间:9/9/2023 访问量:434

问:

我正在尝试将 Gradle 升级到最新版本。我知道 8.2.1 是可能的——不仅 Android Studio 将其作为一种选项提供,而且 Gradle 网站甚至说 8.2.1 不再是最新版本(8.3 是)。但是,当它在 maven 存储库中查找 8.2.1 时,它找不到它。我还下载了 8.2.1,并在 Gradle-wrapper.properties 中放置了一个指向它的链接,这也无济于事。当我尝试同步 Gradle 时,它给了我这个错误:

  • 出了什么问题: 配置根项目“StarterApp”时出现问题。

无法解析配置“:classpath”的所有文件。 无法找到 com.android.tools.build:gradle:8.2.1。 在以下位置搜索: - https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/8.2.1/gradle-8.2.1.pom

我查看了谷歌存储库,当然,它的网站上没有最新版本的 Gradle。SMH 关于如何让 Android Studio 找到正确信息以便它可以完成升级到 8.2.1(或者哎呀,即使是 8.3 也会更好,尽管我知道 Android Studio 没有提供它)的任何想法都会很好。

以下是我的一些文件: build.gradle (项目):

buildscript {
            ext.kotlin_version = '1.8.20'
    repositories {
        google()
    }
    dependencies {
       classpath 'com.android.tools.build:gradle:8.2.1'
    }
  }

allprojects {
    repositories {
        mavenLocal()
        maven { url "https://jitpack.io" }
    }
}
buildscript {
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

build.gradle (模块):

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.starter'
    compileSdk 34

    defaultConfig {
        applicationId "com.example.starter"
        minSdk 24
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

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

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
    }
}

tasks.withType(JavaCompile).configureEach {
    options.compilerArgs += '-Xlint:deprecation'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation 'androidx.exifinterface:exifinterface:1.3.6' // check for the latest version online
    implementation 'androidx.activity:activity:1.7.2'
    implementation 'androidx.activity:activity-ktx:1.7.2'
    implementation 'androidx.fragment:fragment:1.6.1'
    implementation 'com.google.android.gms:play-services-maps:18.1.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.2'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2'
    implementation 'androidx.navigation:navigation-fragment:2.7.2'
    implementation 'androidx.navigation:navigation-ui:2.7.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}'

gradle.properties:

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true

gradle-wrapper.properties:

#Tue Aug 08 17:09:22 PDT 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=file:///C:/Gradle/gradle-8.2.1-bin.zip

zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
java android gradle android-gradle-plugin build.gradle

评论


答:

2赞 Scott Stanchfield 9/9/2023 #1

您将 Android Gradle 插件 (AGP) 与 Gradle 混为一谈。

AGP 是您正在调整的依赖项。最新版本是 8.1、8.2.0-beta3,8.3 即将推出。不应将其设置为 8.2.1;它与 Gradle 版本是分开的。com.android.tools.build:gradle

Gradle 版本在 中指定。你只应该改变gradle/wrapper/gradle-wrapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip

更新到 8.2.1 以更新 Gradle 版本。不要直接下载;让 为您处理它 .distributionUrlservices.gradle.org

我还建议您查看最新的构建结构(无论是在 Groovy 还是 Kotlin 中)。您可以通过在正在使用的最新版本的 Studio 中创建一个新项目来查看其外观。

评论

0赞 Sean Zlatnik 9/11/2023
你说的很有道理,但是......是 Android Studio 告诉我将 com.android.tools.build:gradle 设置为 8.2.1,所以......虽然你是对的是有道理的,但我不明白为什么 Android Studio 希望我将其设置为 8.2.1。另外,如果你是对的,这个页面 developer.android.com/build/releases/gradle-plugin 对我来说并不完全有意义(它说插件版本的价值高于分发价值——这不是你说的相反吗?除非你是对的,否则我无法对此做出正面或反面的判断。
0赞 Scott Stanchfield 9/11/2023
您使用的是哪个版本的 Android Studio?您能否在您的问题中附上带有插件的 8.2.1 建议的屏幕截图。这可能是一个错误,我想将这些信息带给工作室团队。另外 - 你能指出你看到插件版本更高的地方吗?该页面上的表格列出了每个 AGP 版本的最低 Gradle 版本。例如,AGP 8.1 至少需要 Gradle 8.0,但通常会在更高的 Gradle 版本上运行,直到 API 发生变化,这通常只发生在主要版本上。