Skip to content

Instantly share code, notes, and snippets.

@jakeouellette
Last active August 29, 2015 14:15
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakeouellette/c776cad24d6eb1ddd0a1 to your computer and use it in GitHub Desktop.
Save jakeouellette/c776cad24d6eb1ddd0a1 to your computer and use it in GitHub Desktop.
Inject XML resources after variants are merged
// Here's a trick to get XML injected into the APK.
// Be careful not to merge-conflict with existing parameters, as this occurs after the mergeResources step, and the behavior is likely undefined.
// was added to app/build.gradle after applying android plugin.
android.applicationVariants.all { variant ->
// Supports both 0.12.+ and 1.+ of Android gradle plugin by getting all processResourceTasks:
def tasks = []
// Multidex has multiple process resource tasks
if (variant.hasProperty('outputs')) {
for (output in variant.outputs) {
tasks.add(output.processResources)
}
} else {
tasks.add(variant.processResources)
}
// FIXME: You would not want to use doFirst.
// Instead, you'd want to add a single task and put it immediately before each of these tasks, (using .dependsOn to inject the task.)
// ALSOFIXME: I'm copying more than once if you're using multidex because I'm doing this for each task which isn't ideal.
// If you just used a fresh task, and .dependsOn, you wouldn't run into this.
tasks.each { it.doFirst({
copy {
// This folder would be project/app/xml, and contain the xml files you want
from 'xml/'
// NOTE: hardcoded path, you'd want to replace debug with ${task.dirName}, I believe.
into 'build/intermediates/res/debug/values'
include '**/*.xml'
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment