Android Studio错误➡无法运行程序"git"... CreateProcess error = 2,系统找不到指定的文件

Luf*_*ffy 1 git android android-studio android-gradle-plugin

在Android Studio for Windows上导入之前在Mac OS X或Linux上设置的项目时,gradle build会产生错误:

-->Error:(12, 0) CreateProcess error=2, The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

要么

Error:(12, 0) A problem occurred evaluating project ':app'.
> Cannot run program "git" (in directory "<project path>"): CreateProcess error=2, The system cannot find the file specified`
Run Code Online (Sandbox Code Playgroud)

的build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.14.1'
    }
}

def project
def  var

//Error line pointing out below line 

var = project.ext.gitHash = "git rev-parse --short HEAD".execute().text.trim() var --> Error:(12,0)

apply plugin: 'com.android.application'

repositories {
    mavenCentral()

    // for using SNAPSHOT
    //maven {
    //    url uri('https://oss.sonatype.org/content/repositories/snapshots/')
    //}
}

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.melnykov:floatingactionbutton:1.0.7'
    debugCompile project(':observablescrollview')
    releaseCompile "com.github.ksoichiro:android-observablescrollview:$VERSION_NAME"

    // for using SNAPSHOT
    //compile "com.github.ksoichiro:android-observablescrollview:$VERSION_NAME"
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        applicationId "com.github.ksoichiro.android.observablescrollview.samples"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        buildConfigField "String", "GIT_HASH", "\"${project.ext.gitHash}\""
    }

    signingConfigs {
        release {
            def filePrivateProperties = file("private.properties")
            if (filePrivateProperties.exists()) {
                Properties propsPrivate = new Properties()
                propsPrivate.load(new FileInputStream(filePrivateProperties))

                storeFile file(propsPrivate['key.store'])
                keyAlias propsPrivate['key.alias']
                storePassword propsPrivate['key.store.password']
                keyPassword propsPrivate['key.alias.password']
            }
        }
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            versionNameSuffix "-debug"
            buildConfigField "String", "LIB_VERSION", "\"${project.ext.gitHash}\""
        }

        release {
            buildConfigField "String", "LIB_VERSION", "\"${VERSION_NAME}\""

            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            def filePrivateProperties = file("private.properties")
            if (filePrivateProperties.exists()) {
                signingConfig signingConfigs.release
            }
        }
    }

    lintOptions {
        abortOnError false
    }

    applicationVariants.all { variant ->
        def output = variant.outputs.get(0)
        File apk = output.outputFile
        String newName = output.outputFile.name.replace(".apk", "-${variant.mergedFlavor.versionCode}-${variant.mergedFlavor.versionName}-${project.ext.gitHash}.apk")
                .replace("app-", "${variant.mergedFlavor.applicationId}-")
        output.outputFile = new File(apk.parentFile, newName)
    }
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*sen 5

问题是gradle git在路径中找不到.如果您在Android Studio中打开编译器选项,则--stacktrace --debug可以获得有关此内容的更多信息.

这是stacktrace的一部分:

Caused by: java.io.IOException: Cannot run program "git" (in directory "..."): CreateProcess error=2, The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

解决方案是安装git二进制文件并将其添加到路径中.在打开最初在Mac OS X上创建的项目时,我在Windows上遇到过此问题.

  1. 关闭Android Studio

  2. 在Windows上打开系统属性>高级>环境变量...

  3. 单击"路径",然后单击"编辑"

  4. 在"变量值"的末尾添加类似"; C:\ Program Files\Git\cmd"的内容(但当然没有引用).不要添加:';"C:\ Program Files\Git\cmd"'因为它不起作用.这是我遇到的问题.

  5. 打开Android Studio,清理并再次构建,您应该启动并运行.