10

I created a default KMP project, and changed nothing. It runs fine on android, but when I try to run it on iOS it tells me that it cant find the gradle plugin:

A problem occurred configuring root project 'KMPSandBox'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:7.4.0-rc01.
     Required by:
         project : > com.android.application:com.android.application.gradle.plugin:7.4.0-rc01
         project : > com.android.library:com.android.library.gradle.plugin:7.4.0-rc01
      > No matching variant of com.android.tools.build:gradle:7.4.0-rc01 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.0-rc01 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.0-rc01 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')
Siavash
  • 7,583
  • 13
  • 49
  • 69
  • Could yo provide link to repository or share version of Android Studio / IDEA and version of KMM plugin? – Evgeny K Dec 24 '22 at 10:12

5 Answers5

14

To me it looks like you have installed an incompatible JDK or at least targeting the wrong JDK in your Gradle Build Script.

And please have a look at the Gradle Settings.

enter image description here

The following error line, gives my this hint:

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

Starting from with Gradle 7.3.0, the minimum JDK Version is 11.

https://developer.android.com/studio/releases/gradle-plugin#compatibility-7-3-0

Could you check if you have JDK 11 installed?

And in your app/build.gradle.kts (or build.gradle - if you use Groovy) if you have these lines and they might be on Java 8? I personally always prefer to set these lines in my script files explicitly and don't rely on the default behavior.

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_11
        targetCompatibility = JavaVersion.VERSION_11
    }

Otherwise the default:

By default, the Java language version used to compile your project is based on your project's compileSdkVersion, because different versions of Android support different versions of Java. If necessary, you can override this default Java version by adding the following compileOptions block to your build.gradle file. https://developer.android.com/studio/intro/studio-config#jdk

Radu M
  • 1,188
  • 5
  • 16
10

Best and simple working solution is go to file option in android studio then click settings then under Build, Execution, Deployment option click on gradle then under gradle properties click on project name then under gradle sub category in gradle JDK choose Embedded JDK, and then click on apply and sync your gradle files then it will surely work.

enter image description here

Tanmay Agarwal
  • 476
  • 2
  • 13
1

always for this error search for the latest version of the Gradle which is 7.6 now. then in the gradle-wrapper.properties file change the version number to the new version. rebuild the app and it will be ok.

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

just like this.

you can find the latest version here: https://gradle.org/releases/

1

I had the same issue. At the end of the error message you probably see something similar to this:

The following build commands failed
PhaseScriptExecution Run\ Script ... (in target 'iosApp' from project 'iosApp')

If you've set the correct java version in AndroidStudio and it still doesn't work, then it could be, that you've set the JAVA_HOME path variable before, which is different than what you want to use now in your project.

On another question I answered this in detail, please see:
https://stackoverflow.com/a/75562246/6631062

katazina
  • 11
  • 2
0

I had this problem and I did a COMPLETE uninstall (for step by step instructions, follow this link) and then I installed Android Studio again.

After that, everything was working great. I don't know why this happened, but this worked for me.

Hope it helps!