how to fix Kotlin Multiplatform Mobile gradle red error
Asked Answered
F

4

5

I have created a new KMM project and I get red errors in all Gradle files

kdoctor:

[v] System                                           
    OS: macOS (12.5.1)
    CPU: Apple M1 Pro
[v] Java
    Java (openjdk version "17.0.4" 2022-07-19 LTS)
    Location: /Library/Java/JavaVirtualMachines/openjdk17-corretto/Contents/Home/bin/java
    
    JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk17-corretto/Contents/Home
    
    * Note that, by default, Android Studio uses bundled JDK for Gradle tasks execution.
          Gradle JDK can be configured in Android Studio Preferences under Build, Execution, Deployment -> Build Tools -> Gradle section
    
[v] Android Studio
    Android Studio (2021.2)
    Location: /Users/ahmedhnewa/Library/Application Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/212.5712.43.2112.8815526/Android Studio.app
    Bundled Java: openjdk 11.0.12 2021-07-20
    Kotlin Plugin: 212-1.7.10-release-333-AS5457.46
    Kotlin Multiplatform Mobile Plugin: 0.3.3(212-1.7.0-RC-release-217-IJ)-104
    
[v] Xcode
    Xcode (13.4.1)
    Location: /Applications/Xcode.app
    
[x] Cocoapods
    ruby (ruby 2.6.8p205 (2021-07-07 revision 67951) [universal.arm64e-darwin21])
    
    * System ruby is currently used
          CocoaPods is not compatible with system ruby installation on Apple M1 computers.
          Please install ruby 2.7 via Homebrew, rvm, rbenv or other tool and make it default
    
    ruby gems (3.0.3.1)
    
    cocoapods (1.11.3)
    
    * cocoapods-generate plugin not found
          Get cocoapods-generate from https://github.com/square/cocoapods-generate#installation
    
Failures: 1
KDoctor has diagnosed one or more problems while checking your environment.
Please check the output for problem description and possible solutions.

I created a project with regular ios distribution framework. I have even tried to install ruby 2.7 using homebrew and I add the path to env and the error is still the same so I revert all the changes back

settings.gradle.kts:
pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
    }
}

rootProject.name = "DemoKMM"
include(":androidApp")
include(":shared")

build.gradle.kts:

buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10")
        classpath("com.android.tools.build:gradle:7.2.2")
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

The error

I have tried too many solutions and I already read the documentation and nothing work.

Fostoria answered 11/9, 2022 at 11:59 Comment(0)
F
0

I just update Android Studio to Electric Eel (2022.1.1) and KMM Plugin and it solved

Error is gone

Edit 2:

The issue came again to me,

I have fixed the issue by removing android studio and all its files, including ~/.android, ~/.gradle

so even all IDE that uses gradle, so I just removed the cache folder of .gradle

I also removed Android SDK and everything

You can use something like App Cleaner even though I don't really trust the app since it is not open source

drag and drop android studio into it, if you install Android Studio using JetBrains toolbox, please find the path of it and open it in the toolbox and drag and drop

before clicking uninstall, click it from JetBrains toolbox first and then uninstall it from the app cleaner

and do a clean install, just install the KMM plugin without anything, like the Flutter plugin and all that stuff

if you want to manually delete everything using the terminal, see this on Stackoverflow

while I did fix the issue, I still don't know what exactly was the issue,

and I need to know so I make sure it will not happen again,

please if you know, share your thought

Please follow this

Fostoria answered 15/1, 2023 at 14:48 Comment(2)
But I still have some other issues it wasn't there before the update of android studio and kmm plugin, but at least the red error goneFostoria
The issue came again after updating android studio to the latest version (flamingo latest patch)Fostoria
F
4

enter image description hereThe reason for this issue Kdoctor is taking default system ruby version, which doesn't support for KMM, so install any ruby version Management like brew install rbenv and then rbenv install 2.7.0 which will install ruby 2.7.0 and then add the path vim ~/.zshrc add the path export PATH="$HOME/.rbenv/versions/2.7.0/bin:$PATH"

Notes: Make sure to add :$PATH to the last, If not system ruby will get preference over install path based on path order

If any doubts please comment it

Flyte answered 30/12, 2022 at 7:56 Comment(5)
I use both native development (iOS, android) and flutter, and flutter require the default ruby version so will installing it make a conflict?Fostoria
No it's wouldn't make any conflict, I'm also developing flutter app i have crossed checked it. If you find the answer useful upvote itFlyte
Sorry, but I still get the same red errorFostoria
check with this command ruby -v , it should return "ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [-darwin21]" . If not then you have made mistake in ruby configFlyte
Sorry but cocapods have nothing to do with gradle build system, cocapods is dependency manager for iOS, even though I fix the issue of ruby I still get the same errorFostoria
H
3

I was facing the same issues on a Mac mini with M1. I think that the main problem is that you need to make sure that cocoapods are running with Ruby 2.6 or 2.7, but not 3.1.

I found that is you use home brew to install cocoapods it also installs Ruby 3.1, like in here:

% pod env --verbose

### Stack

```
   CocoaPods : 1.11.3
        Ruby : ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]
    RubyGems : 3.3.11
        Host : macOS 12.5.1 (21G83)
       Xcode : 13.4.1 (13F100)
         Git : git version 2.37.3
Ruby lib dir : /opt/homebrew/Cellar/ruby/3.1.2_1/lib

What I did was uninstall Ruby 3.1 using home brew:

% brew uninstall [email protected]
% brew uninstall cocoapods

Install ruby 2.6 or 2.7

% brew install [email protected]

Install cocoapods using gem install:

% gem install cocoapods

And finally verify cocoapods environment:

luix@mac-mini android % pod env --verbose
### Stack

```
   CocoaPods : 1.11.3
        Ruby : ruby 2.6.10p210 (2022-04-12 revision 67958) [arm64-darwin21]
    RubyGems : 3.0.3.1
        Host : macOS 12.5.1 (21G83)
       Xcode : 13.4.1 (13F100)
         Git : git version 2.37.3
Ruby lib dir : /opt/homebrew/Cellar/[email protected]/2.6.10/lib

That worked for me, and hope it works for you too.

Hoeve answered 11/9, 2022 at 18:23 Comment(5)
I use flutter too and I thing flutter needs the default ruby version so should I delete it?Fostoria
I always get this error: cocoapods-generate plugin not found Get cocoapods-generate from github.com/square/cocoapods-generate#installation even after install the pluginFostoria
@AhmedRiyadh can you run this command on your terminal and share the output? % pod env --verbose I think it's likely that you have at least two different Ruby installations. I'm not using Flutter. I'm configuring a project using Kotlin Multiplatform Mobile.Hoeve
Sorry but cocapods have nothing to do with gradle build system, cocapods is dependency manager for iOS, even though I fix the issue of ruby I still get the same errorFostoria
Sorry but cocapods have nothing to do with gradle build system, cocapods is dependency manager for iOS, even though I fix the issue of ruby I still get the same errorFostoria
R
2

I had the same issue, this is how I fixed it:

  1. Download latest ruby with homebrew

    brew install ruby

  2. Add recent ruby downloaded location to the PATH

    echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.zshrc

  3. Apply changes and you're ready to go!

    source ~./zshrc

kdoctor

Rhodic answered 1/7, 2024 at 18:43 Comment(1)
Thank you, it's false positive warning bug in the IDE, the code can be compiled without any issues, for me I updated Gradle wrapper and related tools, and Android studio and it fixed the issue.Fostoria
F
0

I just update Android Studio to Electric Eel (2022.1.1) and KMM Plugin and it solved

Error is gone

Edit 2:

The issue came again to me,

I have fixed the issue by removing android studio and all its files, including ~/.android, ~/.gradle

so even all IDE that uses gradle, so I just removed the cache folder of .gradle

I also removed Android SDK and everything

You can use something like App Cleaner even though I don't really trust the app since it is not open source

drag and drop android studio into it, if you install Android Studio using JetBrains toolbox, please find the path of it and open it in the toolbox and drag and drop

before clicking uninstall, click it from JetBrains toolbox first and then uninstall it from the app cleaner

and do a clean install, just install the KMM plugin without anything, like the Flutter plugin and all that stuff

if you want to manually delete everything using the terminal, see this on Stackoverflow

while I did fix the issue, I still don't know what exactly was the issue,

and I need to know so I make sure it will not happen again,

please if you know, share your thought

Please follow this

Fostoria answered 15/1, 2023 at 14:48 Comment(2)
But I still have some other issues it wasn't there before the update of android studio and kmm plugin, but at least the red error goneFostoria
The issue came again after updating android studio to the latest version (flamingo latest patch)Fostoria

© 2022 - 2025 — McMap. All rights reserved.