/
GoogleServicesPlugin.groovy
266 lines (242 loc) · 9.44 KB
/
GoogleServicesPlugin.groovy
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gms.googleservices
import com.google.android.gms.dependencies.DependencyAnalyzer
import com.google.android.gms.dependencies.DependencyInspector
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.util.regex.Matcher
import java.util.regex.Pattern
class GoogleServicesPlugin implements Plugin<Project> {
public final static String JSON_FILE_NAME = 'google-services.json'
public final static String MODULE_GROUP = "com.google.android.gms"
public final static String MODULE_GROUP_FIREBASE = "com.google.firebase"
public final static String MODULE_CORE = "firebase-core"
public final static String MODULE_VERSION = "11.4.2"
public final static String MINIMUM_VERSION = "9.0.0"
// Some example of things that match this pattern are:
// "aBunchOfFlavors/release"
// "flavor/debug"
// "test"
// And here is an example with the capture groups in [square brackets]
// [a][BunchOfFlavors]/[release]
public final static Pattern VARIANT_PATTERN = ~/(?:([^\p{javaUpperCase}]+)((?:\p{javaUpperCase}[^\p{javaUpperCase}]*)*)\/)?([^\/]*)/
// Some example of things that match this pattern are:
// "TestTheFlavor"
// "FlavorsOfTheRainbow"
// "Test"
// And here is an example with the capture groups in [square brackets]
// "[Flavors][Of][The][Rainbow]"
// Note: Pattern must be applied in a loop, not just once.
public final static Pattern FLAVOR_PATTERN = ~/(\p{javaUpperCase}[^\p{javaUpperCase}]*)/
// These are the plugin types and the set of associated plugins whose presence should be checked for.
private final static enum PluginType{
APPLICATION([
"android",
"com.android.application"
]),
LIBRARY([
"android-library",
"com.android.library"
]),
FEATURE([
"android-feature",
"com.android.feature"
]),
MODEL_APPLICATION([
"com.android.model.application"
]),
MODEL_LIBRARY(["com.android.model.library"])
public PluginType(Collection plugins) {
this.plugins = plugins
}
private final Collection plugins
public Collection plugins() {
return plugins
}
}
@Override
void apply(Project project) {
GoogleServicesPluginConfig config = project.extensions.create('googleServices', GoogleServicesPluginConfig)
project.afterEvaluate {
if (config.disableVersionCheck) {
return
}
DependencyAnalyzer globalDependencies = new DependencyAnalyzer()
project.getGradle().addListener(
new DependencyInspector(globalDependencies, project.getName(),
"This error message came from the google-services Gradle plugin, report" +
" issues at https://github.com/google/play-services-plugins and disable by " +
"adding \"googleServices { disableVersionCheck = false }\" to your build.gradle file."));
}
for (PluginType pluginType : PluginType.values()) {
for (String plugin : pluginType.plugins()) {
if (project.plugins.hasPlugin(plugin)) {
setupPlugin(project, pluginType)
return
}
}
}
// If the google-service plugin is applied before any android plugin.
// We should warn that google service plugin should be applied at
// the bottom of build file.
showWarningForPluginLocation(project)
// Setup google-services plugin after android plugin is applied.
project.plugins.withId("android", {
setupPlugin(project, PluginType.APPLICATION)
})
project.plugins.withId("android-library", {
setupPlugin(project, PluginType.LIBRARY)
})
project.plugins.withId("android-feature", {
setupPlugin(project, PluginType.FEATURE)
})
}
private void showWarningForPluginLocation(Project project) {
project.getLogger().warn(
"Warning: Please apply google-services plugin at the bottom of the build file.")
}
private void setupPlugin(Project project, PluginType pluginType) {
switch (pluginType) {
case PluginType.APPLICATION:
project.android.applicationVariants.all { variant ->
handleVariant(project, variant)
}
break
case PluginType.LIBRARY:
project.android.libraryVariants.all { variant ->
handleVariant(project, variant)
}
break
case PluginType.FEATURE:
project.android.featureVariants.all { variant ->
handleVariant(project, variant)
}
break
case PluginType.MODEL_APPLICATION:
project.model.android.applicationVariants.all { variant ->
handleVariant(project, variant)
}
break
case PluginType.MODEL_LIBRARY:
project.model.android.libraryVariants.all { variant ->
handleVariant(project, variant)
}
break
}
}
private static void handleVariant(Project project,
def variant) {
File quickstartFile = null
List<String> fileLocations = getJsonLocations("$variant.dirName", project)
String searchedLocation = System.lineSeparator()
for (String location : fileLocations) {
File jsonFile = project.file(location + '/' + JSON_FILE_NAME)
searchedLocation = searchedLocation + jsonFile.getPath() + System.lineSeparator()
if (jsonFile.isFile()) {
quickstartFile = jsonFile
break
}
}
if (quickstartFile == null) {
quickstartFile = project.file(JSON_FILE_NAME)
searchedLocation = searchedLocation + quickstartFile.getPath()
}
File outputDir =
project.file("$project.buildDir/generated/res/google-services/$variant.dirName")
GoogleServicesTask task = project.tasks
.create("process${variant.name.capitalize()}GoogleServices",
GoogleServicesTask)
task.quickstartFile = quickstartFile
task.intermediateDir = outputDir
task.searchedLocation = searchedLocation
// This is necessary for backwards compatibility with versions of gradle that do not support
// this new API.
if (variant.respondsTo("applicationIdTextResource")) {
task.packageNameXOR2 = variant.applicationIdTextResource
task.dependsOn(variant.applicationIdTextResource)
} else {
task.packageNameXOR1 = variant.applicationId
}
// This is necessary for backwards compatibility with versions of gradle that do not support
// this new API.
if (variant.respondsTo("registerGeneratedResFolders")) {
task.ext.generatedResFolders = project.files(outputDir).builtBy(task)
variant.registerGeneratedResFolders(task.generatedResFolders)
if (variant.respondsTo("getMergeResourcesProvider")) {
variant.mergeResourcesProvider.configure { dependsOn(task) }
} else {
//noinspection GrDeprecatedAPIUsage
variant.mergeResources.dependsOn(task)
}
} else {
//noinspection GrDeprecatedAPIUsage
variant.registerResGeneratingTask(task, outputDir)
}
}
private static List<String> splitVariantNames(String variant) {
if (variant == null) {
return Collections.emptyList()
}
List<String> flavors = new ArrayList<>()
Matcher flavorMatcher = FLAVOR_PATTERN.matcher(variant)
while (flavorMatcher.find()) {
String match = flavorMatcher.group(1)
if (match != null) {
flavors.add(match.toLowerCase())
}
}
return flavors
}
private static long countSlashes(String input) {
return input.codePoints().filter{x -> x == '/'}.count()
}
static List<String> getJsonLocations(String variantDirname, Project project) {
Matcher variantMatcher = VARIANT_PATTERN.matcher(variantDirname)
List<String> fileLocations = new ArrayList<>()
if (!variantMatcher.matches()) {
project.getLogger().warn("$variantDirname failed to parse into flavors. Please start " +
"all flavors with a lowercase character")
fileLocations.add("src/$variantDirname")
return fileLocations
}
List<String> flavorNames = new ArrayList<>()
if (variantMatcher.group(1) != null) {
flavorNames.add(variantMatcher.group(1).toLowerCase())
}
flavorNames.addAll(splitVariantNames(variantMatcher.group(2)))
String buildType = variantMatcher.group(3)
String flavorName = "${variantMatcher.group(1)}${variantMatcher.group(2)}"
fileLocations.add("src/$flavorName/$buildType")
fileLocations.add("src/$buildType/$flavorName")
fileLocations.add("src/$flavorName")
fileLocations.add("src/$buildType")
fileLocations.add("src/$flavorName${buildType.capitalize()}")
fileLocations.add("src/$buildType")
String fileLocation = "src"
for(String flavor : flavorNames) {
fileLocation += "/$flavor"
fileLocations.add(fileLocation)
fileLocations.add("$fileLocation/$buildType")
fileLocations.add("$fileLocation${buildType.capitalize()}")
}
fileLocations.unique().sort{a,b -> countSlashes(b) <=> countSlashes(a)}
return fileLocations
}
public static class GoogleServicesPluginConfig {
boolean disableVersionCheck = false
}
}