Kotlin Multi Platform default project error running iOs App: could not resolve com.android.tools.build:gradle:7.4.0-rc01
Asked Answered
F

6

13

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')
Fixture answered 18/12, 2022 at 23:12 Comment(1)
Could yo provide link to repository or share version of Android Studio / IDEA and version of KMM plugin?Mauricio
I
15

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

Imbalance answered 22/12, 2022 at 9:35 Comment(0)
I
11

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

Icecold answered 28/1, 2023 at 9:13 Comment(0)
T
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/

Tennietenniel answered 23/1, 2023 at 12:56 Comment(0)
D
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://mcmap.net/q/904181/-kmm-in-android-studio-build-return-command-phasescriptexecution-failed-with-a-nonzero-exit-code

Different answered 24/2, 2023 at 23:54 Comment(0)
I
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!

Illfounded answered 19/1, 2023 at 13:26 Comment(0)
A
0

tl;dr: For Android Studio on macOS with dependency AGP 8.2.2, run this terminal command:

launchctl setenv JAVA_HOME $(/usr/libexec/java_home -v 17)

Details:

I had this problem on my Macbook as well (Sonoma 14.3.1). Any new project created through Android Studio's KMM plugin with "iOS framework distribution" option set to "CocoaPods dependency manager" would fail when building the iOS app through Android Studio.

Tried the following; none worked:

  • Running kdoctor (all checks passed)
  • Modifying configs in build.gradle.kts for shared and androidApp modules related to java version (kotlin jvmTarget, sourceCompatibility, targetCompatibility)
  • Adding jvmToolchain to build.gradle.kts
  • Setting Gradle JDK to different JDK versions (8, 11, 17) through Android Studio's Settings -> Build, Execution, Deployment
  • Adding a run script to iosApp.xcodeproj to export JAVA_HOME (like described here)
  • Adding JAVA_HOME export statement in .zshrc (like described here)
  • Updating Kotlin to latest version (1.9.22)

What finally gave me a clue was this post, where the poster said

Looks like the problem is because xcode doesn't use .zshrc so it cannot load my export JAVA_HOME with AS embedded jdk.

The poster then mentioned setting the org.gradle.java.home Gradle property, which can specify the Java home for Gradle build scripts (Gradle doc). Following their advice, I was finally able to build iosApp successfully after adding the following line in the gradle.properties file:

org.gradle.java.home={PATH_TO_YOUR_JDK_VERSION}

For example:

org.gradle.java.home=/Applications/Android\ Studio.app/Contents/jbr/Contents/Home/

This was fine, but I didn't like that I had to hard-code the path in, as I'll need to do it again if I ever want to build on another Mac in the future. I couldn't find a way in Gradle to avoid this hard-coding, so I switched to finding a way to change JAVA_HOME in Macbook so that it'll be available everywhere.

Eventually, this led me to this answer, which suggest running this terminal command:

launchctl setenv JAVA_HOME $(/usr/libexec/java_home -v {JDK_VERSION_YOU_WANT})

After running launchctl setenv JAVA_HOME $(/usr/libexec/java_home -v 17), I was finally able to build iosApp in Android Studio, even without setting org.gradle.java.home.

Overall, this problem might have been caused by the multiple versions of Java I installed in the past, with the global JAVA_HOME being set to Java 8 somehow.

Ahvenanmaa answered 3/3, 2024 at 8:6 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.