apache / cordova-android

Apache Cordova Android
https://cordova.apache.org/
Apache License 2.0
3.59k stars 1.52k forks source link

Could not resolve com.android.tools.build:gradle:7.4.2 after update to cordova-android@12 #1636

Closed GitFr33 closed 9 months ago

GitFr33 commented 9 months ago

Bug Report

Problem

Build fails with Could not resolve com.android.tools.build:gradle:7.4.2 after updating android platform to version 12.0.0

What is expected to happen?

Build succeeded

What does actually happen?

Build failed

Information

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'Project Name'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:7.4.2.
     Required by:
         project :
      > 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.6' 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.6')
          - 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.6')
          - 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.6')
          - 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.6')

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

Command or Code

cordova build android --debug

Environment, Platform, Device

Environment: Ubuntu 22.04 Platform: cordova-android Device: none

Version information

Cordova CLI 11.1.0 Cordova platform android 12.0.0 Operating System: Ubuntu 22.04 Android sdk build tools: 33.0.2 openjdk version: 11.0.19 Gradle: 8.1.1 (though the Deprecated Gradle features... output makes me wonder about what version of gradle is actually being used...)

Checklist

breautek commented 9 months ago

There are 2 things that I see...

Cordova uses AGP 7.x (7.4.2) which is only compatible Gradle 7 and there are known issues when trying to initialize the wrapper on gradle 8 (at least in 8.0, maybe that's fixed now). So try having a system gradle 7 instead of 8.

Second, which it seems that you're running Java 8. Java 11 is required. Make sure your JAVA_HOME or CORDOVA_JAVA_HOME is set to a JDK 11 environment .

GitFr33 commented 9 months ago

Thanks for your help @breautek. You were correct, although I had Java 11 selected with update-alternatives my JAVA_HOME was pointing to java 8. Fixing that helped — but now I'm getting the following error:

The Android Gradle plugin supports only kotlin-android-extensions Gradle plugin version 1.6.20 and higher.
The following dependencies do not satisfy the required version:
root project 'Project Name' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20

I'm not sure if this is due to an out of date plugin or due to using gradle 8 instead of 7 but I'm confused about the relation or difference between Gradle version and Android Gradle plugin versions.

My projects gradle-wrapper.properties file defines: distributionUrl=https://services.gradle.org/distributions/gradle-7.6-all.zip likewise cdv-gradle-config.json "GRADLE_VERSION":"7.6" and "AGP_VERSION":"7.4.2" but running gradle -v returns Gradle 8.1.1 and I'm not sure what's the correct way to downgrade it.

Everything I've found about downgrading to a previous version of gradle has to do with project level gradle version such as defined in gradle-wrapper.properties

breautek commented 9 months ago

I'm not sure if this is due to an out of date plugin or due to using gradle 8 instead of 7 but I'm confused about the relation or difference between Gradle version and Android Gradle plugin versions.

To put it simply, Gradle is a build system. It's mainly used for Java/Kotlin projects. The Android Gradle Plugin is Android's Gradle extension to support building Android libraries and applications.

As of 7.x, Android's Gradle Plugin version scheme is their major version dictates the supported version of Gradle itself. So AGP 7.x will run on Gradle 7.

Gradle has a concept of "wrappers" which is a project-level install of Gradle, at the version that the project needs. Gradle 8.0 would not properly initialize a gradle 7 wrapper, but it appears that has been fixed in some patch, at least in 8.1. So downgrading your system gradle is probably not necessary.

The Android Gradle plugin supports only kotlin-android-extensions Gradle plugin version 1.6.20 and higher. The following dependencies do not satisfy the required version: root project 'Project Name' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20

As for this, it's kinda interesting. Do you have GradlePluginKotlinVersion defined? The defualt version on cordova-android@12 should be 1.7.21. You can try setting the preference in your config.xml:

<preference name="GradlePluginKotlinVersion" value="1.7.21" />

I'd also try doing a search in folder of the plugins/ directory and see if any plugins is either defining GradlePluginKotlinVersion in their plugin.xml files, or if they are declaring any dependencies on org.jetbrains.kotlin:kotlin-gradle-plugin themselves.

GitFr33 commented 9 months ago

I super appreciate your help @breautek! I did indeed find a plugin specifying a GradlePluginKotlinVersion and building succeeded after specifying GradlePluginKotlinVersion in config.xml

light-Unknow commented 7 months ago

Failed to apply plugin 'com.android.internal.application'. Could not create an instance of type com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated. Could not create an instance of type com.android.build.gradle.internal.dsl.LintImpl$AgpDecorated. Could not generate a decorated class for type LintImpl$AgpDecorated. com/android/tools/lint/model/LintModelSeverity has been compiled by a more recent version of the Java Runtime (class file version 61.0), this version of the Java Runtime only recognizes class file versions up to 55.0