0

I'm having an issue adding a custom method in my build.gradle file to retrieve the git branch and commit hash. Here's my code:

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "***.***.***"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        buildConfigField "String", "GIT_HASH", getGitHash()
        buildConfigField "String", "GIT_BRANCH", getGitBranch()
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    def getGitHash = { ->
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-parse', 'HEAD'
            standardOutput = stdout
        }
        return "\"" + stdout.toString().trim() + "\""
    }

    def getGitBranch = { ->
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
            standardOutput = stdout
        }
        return "\"" + stdout.toString().trim() + "\""
    }
}

The problem? Well I'm getting the following error:

BUILD FAILED in 0s
ERROR: Gradle DSL method not found: 'getGitHash()'

My gradle plug-in ver is 3.5

Jeff Hatfield
  • 131
  • 2
  • 8

2 Answers2

0

The custom method "getGitHash()" defined in gradle is only available in this gradle file. Please check if you calling it from somewhere outside. Also you need to define the method before you use it, i.e place it above in your code.

Mayuri Khinvasara
  • 1,437
  • 1
  • 16
  • 12
  • I'm not using it outside of my gradle file. In fact my implementation is very similar to this answer: https://stackoverflow.com/a/35041457/10952276. Even following that answer exactly yields the same error. And what specifically do you mean by place it above in my code? Trying to access the values from the BuildConfig isn't possible until I'm able to sync my project. Which is what this error is preventing – Jeff Hatfield Sep 26 '19 at 21:59
  • I fixed the issue in my answer below – Jeff Hatfield Sep 26 '19 at 22:20
  • yes, thats exactly what i meant. Putting the function definition ABOVE, in your code. i.e define the function before you use it, put it above the android section – Mayuri Khinvasara Sep 27 '19 at 04:05
0

The problem is that the definitions for the methods getGitHash and getGitBranch are WITHIN the android section of the build.gradle file. Instead they need to be ABOVE the android section. Such as:

def getGitHash = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-parse', 'HEAD'
        standardOutput = stdout
    }
    return "\"" + stdout.toString().trim() + "\""
}

def getGitBranch = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD'
        standardOutput = stdout
    }
    return "\"" + stdout.toString().trim() + "\""
}

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "***.***.***"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        buildConfigField "String", "GIT_HASH", getGitHash()
        buildConfigField "String", "GIT_BRANCH", getGitBranch()
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

Doing this has fixed the issue

Jeff Hatfield
  • 131
  • 2
  • 8