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

时间:2014-12-04 05:18:25

标签: android git android-studio android-gradle

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

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

或者

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`

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

2 个答案:

答案 0 :(得分: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

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

  1. 关闭Android Studio

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

  3. 点击路径&#39;然后单击编辑

  4. 在“变量值”的末尾&#39;添加像&#39 ;; C:\ Program Files \ Git \ cmd&#39; (但当然没有引用)。不要添加:&#39;;&#34; C:\ Program Files \ Git \ cmd&#34;&#39;因为它不会起作用。这就是我遇到的问题。

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

答案 1 :(得分:0)

我正在与Android-ObservableScrollView合作,我遇到了同样的问题。这就是我解决这个问题的方法:

build.gradle(项目):

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


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


allprojects {
    repositories {
        jcenter()
    }
}

build.gradle(示例):

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

//project.ext.gitHash = "git rev-parse --short HEAD".execute().text.trim()

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')
    // Release build uses the synced latest version
    releaseCompile "com.github.ksoichiro:android-observablescrollview:${SYNCED_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 14
        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)
    }*/
}

<强>的build.gradle(库)

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

apply plugin: 'com.android.library'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.android.support:recyclerview-v7:21.0.0'
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        minSdkVersion 9
    }
}

//apply from: 'https://raw.githubusercontent.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'

我希望它可以帮到你:)。