Last active
May 15, 2021 07:11
-
-
Save rovkinmax/d63a1fcae43c35a8b75f0a8d43846d8e to your computer and use it in GitHub Desktop.
Google xml generator without google services gradle plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
android { | |
productFlavors { // or build types | |
dev { | |
project.ext.setProperty("googleAppId", "your google app id") // for task in google_service_xml_generator.gradle | |
} | |
prod { | |
project.ext.setProperty("googleAppId", "your google app id") // for task in google_service_xml_generator.gradle | |
} | |
} | |
apply from: "${rootDir}/gradle_helper/google_service_xml_generator.gradle" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.xml.MarkupBuilder | |
import javax.inject.Inject | |
class GoogleXmlGenerator extends DefaultTask { | |
private File outputDir | |
private File outputFile | |
private final String googleAppId | |
@Inject | |
GoogleXmlGenerator(String buildType, String flavor, String googleAppId) { | |
this.googleAppId = googleAppId | |
outputDir = new File(project.getBuildDir(), "generated/res/google-services/$flavor/$buildType/values/") | |
outputFile = new File(outputDir, "values.xml") | |
} | |
@OutputFile | |
File getOutputFile() { | |
return outputFile | |
} | |
@TaskAction | |
void letsDoTheShit() { | |
def appId = googleAppId | |
if (!outputFile.exists()) | |
outputFile.createNewFile() | |
outputFile.withWriter { writer -> | |
def xml = new MarkupBuilder(writer) | |
xml.doubleQuotes = true | |
xml.mkp.xmlDeclaration(version: '1.0', encoding: 'utf-8') | |
xml.resources { | |
string(name: "google_app_id", appId) | |
mkp.yield('\n ') | |
mkp.comment("AUTO-GENERATED FILE. DO NOT MODIFY. ${outputFile.name} is generated by the $name gradle task") | |
} | |
} | |
} | |
} | |
afterEvaluate { | |
android.applicationVariants.all { variant -> | |
def variantName = variant.name.capitalize() | |
def taskName = "generateGoogleXml${variantName}" | |
if (variant.buildType.name == "release") { | |
def projectId = project.ext.getProperty("googleAppId") | |
def xmlGenerator = task "$taskName"(type: GoogleXmlGenerator, constructorArgs: [variant.buildType.name, variant.flavorName, projectId]) { | |
group = "Osome" | |
description = "Generate xml with google project id and oter properties for Crashlytics" | |
} | |
tasks.named("uploadCrashlyticsMappingFile$variantName").configure { | |
dependsOn xmlGenerator | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment