Android Studio 解决Could not resolve com.android.tools.build:gradle:7.4.2问题

A problem occurred configuring root project 'MyApplication2'.

> Could not resolve all files for configuration ':classpath'.

   > Could not resolve com.android.tools.build:gradle:7.4.2.

     Required by:

         project : > com.android.application:com.android.application.gradle.plugin:7.4.2

         project : > com.android.library:com.android.library.gradle.plugin:7.4.2

      > No matching variant of com.android.tools.build:gradle:7.4.2 was found. The consumer was configured to find a runtime of a library compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '7.5' but:

          - Variant 'apiElements' capability com.android.tools.build:gradle:7.4.2 declares a library, packaged as a jar, and its dependencies declared externally:

              - Incompatible because this component declares an API of a component compatible with Java 11 and the consumer needed a runtime of a component compatible with Java 8

              - Other compatible attribute:

                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')

          - Variant 'javadocElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally:

              - Incompatible because this component declares documentation and the consumer needed a library

              - Other compatible attributes:

                  - Doesn't say anything about its target Java version (required compatibility with Java 8)

                  - Doesn't say anything about its elements (required them packaged as a jar)

                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')

          - Variant 'runtimeElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a library, packaged as a jar, and its dependencies declared externally:

              - Incompatible because this component declares a component compatible with Java 11 and the consumer needed a component compatible with Java 8

              - Other compatible attribute:

                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')

          - Variant 'sourcesElements' capability com.android.tools.build:gradle:7.4.2 declares a runtime of a component, and its dependencies declared externally:

              - Incompatible because this component declares documentation and the consumer needed a library

              - Other compatible attributes:

                  - Doesn't say anything about its target Java version (required compatibility with Java 8)

                  - Doesn't say anything about its elements (required them packaged as a jar)

                  - Doesn't say anything about org.gradle.plugin.api-version (required '7.5')

      昨天用老版本的Android Studio 新建了一个Kotlin项目,. 之类的提示没有,应该是环境问题,就升级了Android Studio到最新的电鳗版本,新建kotlin项目就报上述错误。

      搜索了一圈,原来是gradle插件7.4.2版本比较新,需要Java11,以前配置的是Java8,修改下配置就可以了,以mac电脑为例,操作如下:

1、左上角 File ------- Project Structure, 如图:

 2、SDK Location -----------  Gradle Settings ,如图:

 3、Gradle JDK, 选择11的,点击OK , 如图:

 4、Settings.gradle文件里,配置一些仓库地址,如图:

 代码如下:

pluginManagement {
    repositories {
        maven { url 'https://maven.aliyun.com/repository/releases' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url "https://www.jitpack.io" }
        google()
        mavenCentral()
        gradlePluginPortal()
        jcenter()

    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven { url 'https://maven.aliyun.com/repository/releases' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/jcenter' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        maven { url "https://www.jitpack.io" }
        google()
        mavenCentral()
        jcenter()
    }
}
rootProject.name = "MyApplication2"
include ':app'

 5、点击同步按钮,下载相应的库,如图:

6、 等待下载完毕,可能需要几分钟到几十分钟,只要下载进度是对的,就OK,如图:

7、. 已经可以正常提示语法了

好了,又可以愉快的玩耍了。