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