Android Studio solves the problem of 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')

      Yesterday, I used the old version of Android Studio to create a new Kotlin project. There were no prompts such as ., which should be an environmental problem. I upgraded Android Studio to the latest version of the electric eel, and the above error was reported when I created a new kotlin project.

      After searching around, it turns out that the 7.4.2 version of the gradle plugin is relatively new and requires Java11. The previous configuration is Java8. It is enough to modify the configuration. Taking a mac computer as an example, the operation is as follows:

1. File ------- Project Structure in the upper left corner , as shown in the figure:

 2. SDK Location -----------  Gradle Settings , as shown in the figure:

 3. Gradle JDK , select 11, click OK, as shown in the figure:

 4. In the Settings.gradle file, configure some warehouse addresses, as shown in the figure:

 code show as below:

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. Click the sync button to download the corresponding library, as shown in the figure:

6. Wait for the download to complete, it may take several minutes to tens of minutes, as long as the download progress is correct, it is OK, as shown in the figure:

7. The syntax can be prompted normally

Well, you can play happily again.

Guess you like

Origin blog.csdn.net/msn465780/article/details/129981203