Skip to content

Instantly share code, notes, and snippets.

@saadfarooq
Last active March 1, 2024 15:47
Show Gist options
  • Save saadfarooq/9651367 to your computer and use it in GitHub Desktop.
Save saadfarooq/9651367 to your computer and use it in GitHub Desktop.
Gradle function to replace a placeholder string in AndroidManifest.xml with productFlavor package name
replaceInManifest = {variant, fromString, toString ->
def flavor = variant.productFlavors.get(0)
def buildtype = variant.buildType
def manifestFile = "$buildDir/manifests/${flavor.name}/${buildtype.name}/AndroidManifest.xml"
def updatedContent = new File(manifestFile).getText('UTF-8').replaceAll(fromString, toString)
new File(manifestFile).write(updatedContent, 'UTF-8')
}
applicationVariants.all { variant ->
def flavor = variant.productFlavors.get(0)
def buildType = variant.buildType
variant.processManifest.doLast {
println '################# Adding Package Names to Manifest #######################'
replaceInManifest(variant,
'PACKAGE_NAME',
[flavor.packageName, buildType.packageNameSuffix].findAll().join()) // ignores null
}
}
@espinchi
Copy link

espinchi commented Nov 6, 2014

Very useful function, thanks!

I had to change two things to make it work (I'm running Gradle 2.1, Groovy 2.3.6):

  • For the manifestFile: "$buildDir/intermediates/manifests/full/${flavor.name}/${buildtype.name}/AndroidManifest.xml" instead of "$buildDir/manifests/${flavor.name}/${buildtype.name}/AndroidManifest.xml"
  • Add the def keyword to the replaceInManifest function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment