Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class GoogleServicesPlugin implements Plugin<Project> {
return
}
DependencyAnalyzer globalDependencies = new DependencyAnalyzer()
project.getGradle().addListener(
new DependencyInspector(globalDependencies, project.getName(),
project.gradle.addListener(
new DependencyInspector(globalDependencies, project.name,
"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 = true }\" to your build.gradle file."));
Expand All @@ -87,19 +87,19 @@ class GoogleServicesPlugin implements Plugin<Project> {
showWarningForPluginLocation(project)

// Setup google-services plugin after android plugin is applied.
project.plugins.withId("android", {
project.plugins.withId("android") {
setupPlugin(project, PluginType.APPLICATION)
})
project.plugins.withId("android-library", {
}
project.plugins.withId("android-library") {
setupPlugin(project, PluginType.LIBRARY)
})
project.plugins.withId("android-feature", {
}
project.plugins.withId("android-feature") {
setupPlugin(project, PluginType.FEATURE)
})
}
}

private void showWarningForPluginLocation(Project project) {
project.getLogger().warn(
project.logger.warn(
"Warning: Please apply google-services plugin at the bottom of the build file.")
}

Expand Down Expand Up @@ -145,7 +145,8 @@ class GoogleServicesPlugin implements Plugin<Project> {
GoogleServicesTask)

task.setIntermediateDir(outputDir)
task.setVariantDir(variant.dirName)
task.setBuildType(variant.buildType.name)
task.setProductFlavors(variant.productFlavors.collect { it.name })

// This is necessary for backwards compatibility with versions of gradle that do not support
// this new API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,15 @@
/** */
public class GoogleServicesTask extends DefaultTask {
public final static String JSON_FILE_NAME = "google-services.json";
// 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 = Pattern.compile("(?:([^\\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 = Pattern.compile("(\\p{javaUpperCase}[^\\p{javaUpperCase}]*)");

private static final String STATUS_DISABLED = "1";
private static final String STATUS_ENABLED = "2";

private static final String OAUTH_CLIENT_TYPE_WEB = "3";

private File intermediateDir;
private String variantDir;
private String buildType;
private List<String> productFlavors;
private String packageNameXOR1;
private TextResource packageNameXOR2;

Expand All @@ -83,8 +69,13 @@ public File getIntermediateDir() {
}

@Input
public String getVariantDir() {
return variantDir;
public String getBuildType() {
return buildType;
}

@Input
public List<String> getProductFlavors() {
return productFlavors;
}

/**
Expand All @@ -105,8 +96,12 @@ public void setIntermediateDir(File intermediateDir) {
this.intermediateDir = intermediateDir;
}

public void setVariantDir(String variantDir){
this.variantDir = variantDir;
public void setBuildType(String buildType) {
this.buildType = buildType;
}

public void setProductFlavors(List<String> productFlavors) {
this.productFlavors = productFlavors;
}

public void setPackageNameXOR1(String packageNameXOR1) {
Expand All @@ -117,11 +112,10 @@ public void setPackageNameXOR2(TextResource packageNameXOR2) {
this.packageNameXOR2 = packageNameXOR2;
}


@TaskAction
public void action() throws IOException {
File quickstartFile = null;
List<String> fileLocations = getJsonLocations(variantDir);
List<String> fileLocations = getJsonLocations(buildType, productFlavors);
String searchedLocation = System.lineSeparator();
for (String location : fileLocations) {
File jsonFile = getProject().file(location + '/' + JSON_FILE_NAME);
Expand Down Expand Up @@ -510,38 +504,14 @@ private String getPackageName() {
return packageNameXOR1;
}

private static List<String> splitVariantNames(String variant) {
if (variant == null) {
return new ArrayList<>();
}
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) {
Matcher variantMatcher = VARIANT_PATTERN.matcher(variantDirname);
static List<String> getJsonLocations(String buildType, List<String> flavorNames) {
List<String> fileLocations = new ArrayList<>();
if (!variantMatcher.matches()) {
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);

String flavorName = flavorNames.stream().reduce("", (a,b) -> a + (a.length() == 0 ? b : capitalize(b)));
fileLocations.add("src/" + flavorName + "/" + buildType);
fileLocations.add("src/" + buildType + "/" + flavorName);
fileLocations.add("src/" + flavorName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.google.common.truth.Truth.assertThat;
Expand All @@ -27,14 +29,14 @@ public class GoogleServicesPluginTest {

@Test
public void testNoFlavor() {
List<String> output = toStringList(GoogleServicesTask.getJsonLocations("release"));
List<String> output = toStringList(GoogleServicesTask.getJsonLocations("release", Collections.emptyList()));
assertThat(output).contains("src/release");
}

@Test
public void testOneFlavor() {
List<String> output =
toStringList(GoogleServicesTask.getJsonLocations("flavor/release"));
toStringList(GoogleServicesTask.getJsonLocations("release", Collections.singletonList("flavor")));
assertThat(output)
.containsAllOf(
"src/release",
Expand All @@ -47,7 +49,7 @@ public void testOneFlavor() {
@Test
public void testMultipleFlavors() {
List<String> output =
toStringList(GoogleServicesTask.getJsonLocations("flavorTest/release"));
toStringList(GoogleServicesTask.getJsonLocations("release", Arrays.asList("flavor", "test")));
assertThat(output)
.containsAllOf(
"src/release",
Expand Down