安卓工作室错误➡无法运行程序"git"...创建进程错误 = 2,系统找不到指定的文件



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

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

以下是堆栈跟踪的一部分:

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

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

  1. 关闭安卓工作室

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

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

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

  5. 打开Android Studio,清理并重新构建,您应该可以启动并运行。

我正在和Android-ObservableScrollView一起工作,我遇到了同样的问题。这就是我解决这个问题的方式:

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'

希望它能帮助你:)

相关内容

最新更新