3

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)
    }
}
2
  • There should be more detail in the error message to indicate which command is failing to run. Commented Dec 4, 2014 at 17:30
  • That's as much as we're given in the error message. The problem is that git cannot be found. Commented Feb 7, 2016 at 22:22

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's it! After years putting double quotes in the path variable because of spaces, it was automatic to me. Your observation #4 nailed it!
Restart the machine if you think you have updated the path variable already.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.