【问题标题】:how to fix Kotlin Multiplatform Mobile gradle red error如何修复 Kotlin Multiplatform Mobile gradle 红色错误
【发布时间】:2022-12-30 16:14:25
【问题描述】:

我创建了一个新的 KMM 项目,但在所有 Gradle 文件中出现红色错误

医生:

[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.

我创建了一个带有常规 ios 分发框架的项目。 我什至尝试使用自制软件安装 ruby​​ 2.7 并将路径添加到 env 并且错误仍然相同所以我将所有更改还原

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)
}

我尝试了太多解决方案,我已经阅读了文档,但没有任何效果。

【问题讨论】:

    标签: android android-studio kotlin gradle-kotlin-dsl kotlin-multiplatform-mobile


    【解决方案1】:

    我在带有 M1 的 Mac mini 上遇到了同样的问题。我认为主要问题是您需要确保 cocoapods 运行的是 Ruby 2.6 或 2.7,而不是 3.1。

    我发现你使用 home brew 安装 cocoapods 它也安装了 Ruby 3.1,如下所示:

    % 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
    

    我所做的是使用自制软件卸载 Ruby 3.1:

    % brew uninstall ruby@3.1
    % brew uninstall cocoapods
    

    安装红宝石 2.6 或 2.7

    % brew install ruby@2.6
    

    使用 gem install 安装 cocoapods:

    % gem install cocoapods
    

    最后验证 cocoapods 环境:

    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
    

    这对我有用,希望它也对你有用。

    【讨论】:

    • 我也使用 flutter,我认为 flutter 需要默认的 ruby​​ 版本,所以我应该删除它吗?
    • 我总是得到这个错误:cocoapods-generate plugin not found 从github.com/square/cocoapods-generate#installation获取cocoapods-generate 即使在安装插件
    • @AhmedRiyadh 你能在你的终端上运行这个命令并分享输出吗? % pod env --verbose 我认为您可能至少安装了两个不同的 Ruby。我没有使用颤振。我正在使用 Kotlin Multiplatform Mobile 配置一个项目。
    【解决方案2】:

    此问题的原因 Kdoctor 采用默认系统 ruby​​ 版本,不支持 KMM,因此安装任何 ruby​​ 版本管理,如 brew install rbenv 然后 rbenv install 2.7.0 将安装 ruby​​ 2.7.0 然后添加路径vim ~/.zshrc 添加路径export PATH="$HOME/.rbenv/versions/2.7.0/bin:$PATH"

    笔记: 确保将 :$PATH 添加到最后,否则系统 ruby​​ 将根据路径顺序优先于安装路径

    如有疑问请评论

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 2021-04-06
      • 2022-10-05
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多