简体   繁体   中英

Android Studio error ➡ Cannot run program “git” … CreateProcess error=2, The system cannot find the file specified

On Android Studio for Windows after importing a project,previously set up on Mac OS X or Linux, gradle build produces an error:

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

Or

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

The problem is that gradle cannot find git in the path. If you turn on the compiler options in Android Studio --stacktrace --debug you get more information on this.

Here is part of the stacktrace:

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

The solution is to install git and add the location of the binary to the path. I experienced this issue on Windows when opening a project originally created on Mac OS X.

  1. Close Android Studio

  2. On Windows open up System Properties > Advanced > Environment Variables...

  3. Click 'Path' and then click edit

  4. At the end of the 'Variable Value' add something like ';C:\\Program Files\\Git\\cmd' (but without the quote of course). Don't add this: ';"C:\\Program Files\\Git\\cmd"' because it won't work. This was the issue I was having.

  5. Open Android Studio, clean, and build again and you should be up and running.

I was working with Android-ObservableScrollView and I encountered the same problem with you. This is how I fixed that:

build.gradle (project):

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 (sample):

/*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(library)

/*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'

I hope it can help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM