Skip to content

Instantly share code, notes, and snippets.

@kibotu
Last active July 18, 2022 12:41
Show Gist options
  • Save kibotu/a2bc6f154518fba633a87273cf024dcd to your computer and use it in GitHub Desktop.
Save kibotu/a2bc6f154518fba633a87273cf024dcd to your computer and use it in GitHub Desktop.
Generating Dependencies for different build variants.
/**
* Generates a text file containing dependencies for different build variants
* and saves them into the respective variant asset folder.
* Also deletes files on clean.
*
* Can be safely used with <code>org.gradle.unsafe.configuration-cache=true</code>
*/
android.applicationVariants.all { variant ->
def name = variant.name
def nameCapitalized = name.capitalize()
preBuild.dependsOn tasks.register("list${nameCapitalized}Dependencies") {
outputs.file(project.rootProject.file("app/src/$name/assets/dependencies.txt"))
def dependencies = configurations
.getByName("${name}RuntimeClasspath")
.getResolvedConfiguration()
.getFirstLevelModuleDependencies()
.collect {
"${it.moduleGroup}:${it.moduleName}:${it.moduleVersion}\n"
}
.sort()
doLast {
description = "Generate ${outputs.files.singleFile}"
println(description)
dependencies.each {
outputs.files.singleFile.append(it)
}
}
}
clean {
delete project.rootProject.file("app/src/$name/assets/dependencies.txt")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment