Last active
March 1, 2024 15:47
-
-
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
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
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') | |
} |
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
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very useful function, thanks!
I had to change two things to make it work (I'm running Gradle 2.1, Groovy 2.3.6):
manifestFile
:"$buildDir/intermediates/manifests/full/${flavor.name}/${buildtype.name}/AndroidManifest.xml"
instead of"$buildDir/manifests/${flavor.name}/${buildtype.name}/AndroidManifest.xml"
def
keyword to thereplaceInManifest
function