forked from gfx/gradle-android-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle
185 lines (160 loc) · 5.3 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import javax.imageio.ImageIO
private int toGray(color) {
def a = (color & 0xFF000000)
def r = (color & 0x00FF0000) >> 16
def g = (color & 0x0000FF00) >> 8
def b = (color & 0x000000FF)
def c = (int)((2.0*r + 4.0*g + b) / 7.0) //
return a | (c << 16) | (c << 8) | c
}
ext.iconToGrayScale = { File inputFile, File outputFile ->
if (outputFile.exists() && outputFile.lastModified() >= inputFile.lastModified()) {
return;
}
def img = ImageIO.read(inputFile)
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
def color = img.getRGB(x, y)
img.setRGB(x, y, toGray(color))
}
}
outputFile.getParentFile().mkdirs()
ImageIO.write(img, "png", outputFile)
}
/**
* Makes gray-scaled launcher icons for the "debug" build variant.
*/
ext.makeGrayscaleLauncherIcon = { File dir, String name, List<String> buildTypes = ["debug"] ->
fileTree(dir: dir, include: "res/drawable*/$name").each { File ic ->
buildTypes.each { String buildType ->
def outputFile = file(ic.getPath().replaceAll("/main/", "/$buildType/"))
iconToGrayScale(ic, outputFile)
}
}
}
ext.writeProperties = { File propertiesFile, Map<String, String> dict ->
def s = ""
dict.each { key, value ->
s += "$key=$value\n"
}
logger.info("writing $propertiesFile")
propertiesFile.write(s)
}
/**
* Initialize local.properties which includes sdk.dir, ndk.dir and extra properties.
*/
ext.initSdkDir = { Map<String, String> extra = null ->
def localPropFile = file("local.properties")
if (!localPropFile.exists()) {
def dict = new HashMap<String, String>()
if (extra != null) {
dict.putAll(extra)
}
// sdk.dir is required
[
System.getenv("ANDROID_HOME"),
System.getenv("ANDROID_SDK"),
"/usr/local/opt/android-sdk",
"/Applications/Android Studio/sdk"
].each { dir ->
if (dir != null && file(dir).exists()) {
dict["sdk.dir"] = dir
return;
}
}
if (dict["sdk.dir"] == null) {
throw new RuntimeException("No Android SDK found.")
}
// ndk.dir is optional
[
System.getenv("ANDROID_NDK"),
"/usr/local/opt/android-ndk",
].each { dir ->
if (dir != null && file(dir).exists()) {
dict["ndk.dir"] = dir
return;
}
}
writeProperties(localPropFile, dict)
}
}
/**
* Writes Android resources.
* e.g. writeResources(file("src/main/res/values/_generated.xml"), ["foo" : "bar"])
*/
ext.writeResources = { File file, Map<String, Object> dict ->
def s = ""
s += """<?xml version="1.0" encoding="utf-8"?>\n<resources>\n"""
dict.each { key, value ->
if (value in Boolean) {
s += """<bool name="$key">$value</bool>\n"""
} else if (value in Integer) {
s += """<integer name="$key">$value</integer>\n"""
} else {
s += """<string name="$key">$value</string>\n"""
}
}
s += """</resources>\n"""
logger.info("writing $file")
file.write(s)
}
/**
* Create `outputFile` from `inputFile` if it does not exist.
* You can force overwrite with specifies the `true` to the 3rd argument.
*/
ext.copyFile = { File inputFile, File outputFile, boolean toOverwrite = false ->
if (!inputFile.exists()) {
throw new FileNotFoundException("Template file does not exist: ${inputFile.getAbsolutePath()}")
}
outputFile.getParentFile().mkdirs()
if (toOverwrite || !outputFile.exists()) {
outputFile.bytes = inputFile.readBytes()
}
}
/**
* Representation of Semantic Version (major.minor.patch-level)
*/
public class Version {
public final int major;
public final int minor;
public final int patchLevel;
public final String source;
public Version(String v) {
source = v.trim()
def parts = source.split("\\.", 3)
major = Integer.valueOf(parts[0])
minor = Integer.valueOf(parts[1])
patchLevel = Integer.valueOf(parts[2])
}
@Override
public String toString() {
return String.format("%s (%s)", toVersionName(), toVersionCode())
}
public String toVersionName() {
return source
}
public int toVersionCode() {
return major * (1000 * 1000) + minor * 1000 + patchLevel;
}
}
ext.parseVersion = { String versionString ->
return new Version(versionString)
}
ext.readVersion = { File file ->
def s = new String(file.readBytes())
return parseVersion(s)
}
/**
* Set buildConfigField from "Map" to buildType or productFlavors.
* You can specifies buildType or flavor object to the fist argument.
* But don't provided `buildConfigField` in variant with `android-gradle-plugin` v0.9.0.
*/
ext.setBuildConfigField = { def buildType, Map<Object, Object> buildConfigMap ->
for (def entry in buildConfigMap) {
if (entry.value instanceof String) {
buildType.buildConfigField "${entry.value.getClass().getName()}", "${entry.key}", "\"${entry.value}\""
} else {
buildType.buildConfigField "${entry.value.getClass().getName()}", "${entry.key}","${entry.value}"
}
}
}