Git rev-list not working in Android Studio build.gradle

前端 未结 5 514
清歌不尽
清歌不尽 2020-12-20 01:36

I\'ve setup my Android Studio Project to work with Github. Here is my Manifext

apply plugin: \'com.android.application\'


def gitVersion() {
    def counter         


        
相关标签:
5条回答
  • 2020-12-20 02:13

    I found the problem. Android studio performs command line operations in the project folder by default. I had made my project such that the git repo lived inside my app directory instead of in my project directory, so any git commands on the project level would error, since it didn't know about the git repo one directory down.

    My solution was to move my git repo out of the app directory and into the project directory. Once I did that, the script worked perfectly.

    0 讨论(0)
  • 2020-12-20 02:13

    Try this:

    def gitCommitCount = Integer.parseInt(['sh', '-c', 'git rev-list HEAD | wc -l | tr -d " "'].execute([], project.rootDir).text.trim())
    
    defaultConfig {
      ...
      versionCode gitCommitCount
      ...
    }
    
    0 讨论(0)
  • 2020-12-20 02:21

    This method will help you get your revision number:

    int extractVersionCode() {
        def stdout = new ByteArrayOutputStream()
        exec {
            workingDir projectDir
            executable 'git'
            args 'rev-list', '--count', 'HEAD'
            standardOutput = stdout
        }
        return stdout.toString().trim().toInteger()
    }
    

    Usage:

    versionCode extractVersionCode()
    
    0 讨论(0)
  • 2020-12-20 02:30

    I wrote and tested that script for use on OSX Mavericks. What kind of OS platform are you using to make your build? Also, have you tried it without the ".text" ? You may have to play around with different combinations on that line to see what will work.

    0 讨论(0)
  • 2020-12-20 02:30

    Just specify the working directory for execute():

    .execute([], project.rootDir)
    
    0 讨论(0)
提交回复
热议问题