forked from protyposis/MediaPlayer-Extended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitversioning.gradle
66 lines (61 loc) · 1.82 KB
/
gitversioning.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Checks if git is available to the Gradle environment and stops the build if not.
*/
def checkForGit() {
try {
def dummyOutput = new ByteArrayOutputStream()
exec {
commandLine 'git'
standardOutput = dummyOutput // prevent output being printed to stdout
ignoreExitValue = true // don't throw exception on exit code > 0
}
}
catch(ignored) {
throw new GradleException('git not found')
}
}
// Automatic git version numbering: http://stackoverflow.com/a/18021756
def getVersionCode = { ->
try {
def code = new ByteArrayOutputStream()
exec {
commandLine 'git', 'tag', '--list'
standardOutput = code
}
return code.toString().split("\n").size()
}
catch (ignored) {
return -1;
}
}
def getVersionName = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags', '--dirty', '--long', '--always'
standardOutput = stdout
}
return stdout.toString().trim()
}
catch (ignored) {
return null;
}
}
android {
defaultConfig {
versionCode getVersionCode()
versionName getVersionName()
}
/* On a release build, assert that Git exists - otherwise the version code and name cannot be
* automatically set. This is done so complicated here because the Android tasks are built
* dynamically at execution time and cannot be accessed at configuration time. */
(project.isLibrary ? libraryVariants : applicationVariants).all { variant ->
//if(variant.name.equalsIgnoreCase('release')) {
variant.outputs.each { output ->
output.processManifest.doFirst {
checkForGit()
}
}
//}
}
}