为什么 Android Studio 无法应用插件 'com.cinnober.gradle.semver-git'?

Why Android Studio fails to apply plugin 'com.cinnober.gradle.semver-git'?

敏锐的眼睛能发现上周困扰我的问题吗? 我克隆了这个 repo 但构建失败并出现以下异常:

FAILURE: Build failed with an exception.

  • Where: Script "C:\Users\MyUser\Desktop\repos\project\Launcher\git.gradle" line: 4

  • What went wrong: A problem occurred evaluating script. Failed to apply plugin 'com.cinnober.gradle.semver-git'

Cannot run program "git" (in directory "C:\Users\MyUser\Desktop\repos\project\Launcher"): CreateProcess error=2, System cannot find specified file

我的build.gradle:

apply from: "check.gradle"

buildscript {

    ext.buildConfig = [
            "sdkCompile" : 31,
            "sdkMinimum" : 19,
            "sdkTarget"  : 28,
            "tools"      : "30.0.2",
            "versionCode": 1549,
            "versionName": "1.0.14",
    ]

    ext.versions = [
            "kotlin"  : "1.3.72",
            "okhttp"  : "3.12.12", // This version is the latest supporting API 19
            "retrofit": "2.6.4", // This version is the latest supporting API 19
            "room"    : "2.2.5",
    ]

    ext.lib = [
            "androidx": [
                    "appcompat"    : "androidx.appcompat:appcompat:1.1.0",
                    "async_inflate": "androidx.asynclayoutinflater:asynclayoutinflater:1.0.0",
                    "cardview"     : "androidx.cardview:cardview:1.0.0",
                    "constraint"   : "androidx.constraintlayout:constraintlayout:1.1.3",
                    "core"         : "androidx.core:core-ktx:1.3.1",
                    "fragment"     : "androidx.fragment:fragment-ktx:1.2.5",
                    "material"     : "com.google.android.material:material:1.1.0",
                    "multidex"     : "androidx.multidex:multidex:2.0.1",
                    "preference"   : "androidx.preference:preference-ktx:1.1.1",
                    "swipe_refresh": "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0",
                    "work"         : "androidx.work:work-runtime-ktx:2.4.0",
            ],
            "date"    : [
                    "core": "com.jakewharton.threetenabp:threetenabp:1.2.4",
            ],
            "debug_db": "com.amitshekhar.android:debug-db:1.0.6",
            "firebase": [
                    "analytics": "com.google.firebase:firebase-analytics:17.4.4",
                    "crash"    : "com.google.firebase:firebase-crashlytics:17.1.1@aar",
            ],
            "glide"   : "com.github.bumptech.glide:glide:4.11.0",
            "viewpage": [
                    "indicator": "com.romandanylyk:pageindicatorview:1.0.3@aar",
            ],
            "kotlin"  : "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${versions.kotlin}",
            "kb_event": "net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.3.0",
            "okhttp"  : [
                    "core"   : "com.squareup.okhttp3:okhttp:${versions.okhttp}",
                    "curl"   : "com.github.mrmike:ok2curl:0.4.5",
                    "logging": "com.squareup.okhttp3:logging-interceptor:${versions.okhttp}",
            ],
            "play"    : [
                    "location": "com.google.android.gms:play-services-location:17.0.0",
            ],
            "retrofit": [
                    "core": "com.squareup.retrofit2:retrofit:${versions.retrofit}",
                    "gson": "com.squareup.retrofit2:converter-gson:${versions.retrofit}",
            ],
            "room"    : [
                    "compiler"  : "androidx.room:room-compiler:${versions.room}",
                    "core"      : "androidx.room:room-runtime:${versions.room}",
                    "coroutines": "androidx.room:room-ktx:${versions.room}",
            ],
            "timber"  : "com.jakewharton.timber:timber:4.7.1",
    ]

    repositories {
        google()
        mavenCentral()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
        classpath "com.google.gms:google-services:4.3.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}"
        classpath "com.cinnober.gradle:semver-git:2.5.0"
        classpath "com.google.firebase:firebase-crashlytics-gradle:2.2.0"
        classpath "com.google.firebase:firebase-appdistribution-gradle:2.0.0"
        classpath "com.github.ben-manes:gradle-versions-plugin:0.29.0"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
        flatDir {
            dirs "${rootProject.rootDir}/libs"
        }
        maven { url 'https://jitpack.io' }
    }
}

task sendHomologToFirebase {
    group = "Firebase"
    dependsOn "Launcher:assembleHomologRelease"
    finalizedBy "Launcher:appDistributionUploadHomologRelease"
}

task sendProductionToFirebase {
    group = "Firebase"
    dependsOn "Launcher:assembleProductionRelease"
    finalizedBy "Launcher:appDistributionUploadProductionRelease"
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我的 git.gradle:

ext.nextVersion = "patch"
ext.snapshotSuffix = "<count><dirty>"
ext.dirtyMarker = ".d"
apply plugin: "com.cinnober.gradle.semver-git"

task git_count {
    def process = ("git rev-list --count HEAD").execute(null, project.rootDir)
    process.waitFor()
    if (process.exitValue() != 0) {
        ext.count = 0
        return
    }
    ext.count = process.text.trim().toInteger()
}

我尝试过的事情:

本文中描述的两种解决方案 Gradle semver-git plugin page

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "gradle.plugin.com.cinnober.gradle:semver-git:2.5.0"
  }
}

apply plugin: "com.cinnober.gradle.semver-git"

并且:

plugins {
  id "com.cinnober.gradle.semver-git" version "2.5.0"
}

还尝试直接下载 .pom 文件并使用以下方法在本地加载:

flatDir {
    dirs "${rootProject.rootDir}/libs"
}

我什至尝试 this solution 来自另一个 SO 答案:

将此添加到我的 build.gradle

plugins {
    id 'com.cinnober.gradle.semver-git'
}

并将此添加到我的 settings.gradle

pluginManagement {
    buildscript {
        repositories {
            flatDir {
                dirs '/libs'
            }
        }
        dependencies {
            classpath ':semver-git:2.5.0'
        }
    }
}

错误消息的相关部分可能是结尾:

Cannot run program "git" (in directory "C:\Users\MyUser\Desktop\repos\project\Launcher"): CreateProcess error=2, System cannot find specified file

它说它试图 运行 程序 git 但找不到它。插件似乎试图 运行 git 作为应用过程的一部分,但在这样做时失败了。

自从克隆回购后,您的系统中某处已经有了 git,但它可能尚未添加到您的 PATH 中,因此脚本在尝试 [=44= 时找不到它] 命令 git

  1. 第一步是找到 git 可执行文件在您系统上的位置。
  2. 假设您在 windows,您将需要编辑您的系统环境变量。它应该在 Windows Settings -> System Environment Variables -> Path 下。通过将完整路径附加到 git 可执行文件所在的文件夹来编辑 Path 变量。
    即:C:\Program Files\Git\bin 如果那是你 git.exe 住的地方。
  3. 之后重新启动 android studio 以强制其加载新的环境变量。

Here is a related question 其中包含有关如何将 git 添加到 PATH 的更详细说明以及详细示例。