how to fix Kotlin Multiplatform Mobile gradle red error

Viewed 63

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.

1 Answers

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 ruby@3.1
% brew uninstall cocoapods

Install ruby 2.6 or 2.7

% brew install ruby@2.6

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/ruby@2.6/2.6.10/lib

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

Related