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