-
Notifications
You must be signed in to change notification settings - Fork 54
/
build.gradle
115 lines (103 loc) · 3.42 KB
/
build.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//from http://www.egeek.me/2013/12/07/gradle-auto-detect-android-sdk-and-build-tools-versions/
import org.codehaus.groovy.runtime.StackTraceUtils
int[] sdksAvailable() {
def sdks = new ByteArrayOutputStream()
def androidSdkDir = androidSDKDir()
exec {
workingDir androidSdkDir + "/tools"
commandLine './android', 'list'
standardOutput = sdks
}
sdks = sdks
// get the output
.toString()
// split to strings
.split('\n')
// leave only strings with API levels
.findAll { it ==~ /\s*API level:\s*(\d+).*/ }
// extract the API levels
.collect { (it =~ /\s*API level:\s*(\d+).*/)[0][1].toInteger() }
// sort from highest to lowest
.sort( { a, b -> b <=> a } )
sdks
}
int highestSdkAvailable(int defaultSdk) {
try {
def sdks = sdksAvailable()
def highestSdk = sdks[0]
if (highestSdk != null) {
println "Using highest found SDK " + highestSdk
highestSdk
} else {
println "No installed SDKs found. Using default SDK " + defaultSdk
defaultSdk
}
} catch (any) {
println "Exception while determining highest SDK. Using default SDK " +
defaultSdk
StackTraceUtils.sanitize(any).printStackTrace()
defaultSdk
}
}
String androidSDKDir() {
def androidExecPath = new ByteArrayOutputStream()
try {
exec {
commandLine 'which', 'android'
standardOutput = androidExecPath
}
println "find android exec path: " + androidExecPath.toString()
file(androidExecPath.toString().trim()).getParentFile().getParentFile()
} catch (ExecException) {
exec {
commandLine 'grep', 'sdk.dir', 'local.properties'
standardOutput = androidExecPath
}
println "find android sdk with local.properties: " + androidExecPath.toString()
file(androidExecPath.toString().trim().split('=')[1])
}
}
String[] buildToolsAvailable() {
println "sdk dir: " + androidSDKDir()
def buildToolsDir = new File(androidSDKDir(), "build-tools")
buildToolsDir.list([accept:{d, f-> !f.contains("android") }] as FilenameFilter).sort { a, b -> b <=> a }
}
String latestBuildToolsAvailable(String defaultBuildTools) {
try {
def buildToolsVersions = buildToolsAvailable()
def latestBuildTools = buildToolsVersions[0]
if (latestBuildTools != null) {
println "Using latest found build tools " + latestBuildTools
latestBuildTools
} else {
println "No installed build tools found. Using default build tools " +
defaultBuildTools
defaultBuildTools
}
} catch (any) {
println "Exception while determining latest build tools. Using default build tools " +
defaultBuildTools
StackTraceUtils.sanitize(any).printStackTrace()
defaultBuildTools
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.+'
}
}
allprojects {
repositories {
mavenCentral()
}
ext {
COMPILE_SDK_VERSION=highestSdkAvailable(21)
BUILD_TOOLS_VERSION=latestBuildToolsAvailable("21.1.2")
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
}