Skip to content

Instantly share code, notes, and snippets.

@tprochazka
Created April 3, 2024 22:55
Show Gist options
  • Save tprochazka/f90511eeee83cacad719027cf916c1cc to your computer and use it in GitHub Desktop.
Save tprochazka/f90511eeee83cacad719027cf916c1cc to your computer and use it in GitHub Desktop.
Android Library module: Copy one localication to another during build time
androidComponents.onVariants { variant ->
generateCopyValuesTask(variant, "values-sr-rRS", "values-b+sr+Latn", "copySerbianValuesTaskFor")
generateCopyValuesTask(variant, "values-es", "values-ca", "copySpanishValuesTaskFor")
generateCopyValuesTask(variant, "values-iw", "values-he", "copyHebrewValuesTaskFor")
}
fun generateCopyValuesTask(variant: LibraryVariant, sourceLocale: String, targetLocale: String, taskName: String) {
variant.sources.res?.let { source: SourceDirectories.Layered ->
val assetCreationTask =
project.tasks.register<CopyLocalizedResTask>("$taskName${variant.name}") {
res.set(project.layout.projectDirectory.dir("src/main/res/$sourceLocale/"))
this.targetLocale = targetLocale
}
source.addGeneratedSourceDirectory(
assetCreationTask,
CopyLocalizedResTask::outputDirectory
)
}
}
abstract class CopyLocalizedResTask: DefaultTask() {
@get:InputDirectory
@get:Optional
abstract val res: DirectoryProperty
@get:OutputDirectory
abstract val outputDirectory: DirectoryProperty
@get:Input
abstract var targetLocale: String
@get:Inject abstract val fs: FileSystemOperations
@TaskAction
fun taskAction() {
val target = File(outputDirectory.get().asFile, targetLocale)
target.mkdir()
println("Copying values from ${res.get()} to $target")
fs.copy {
from(res.get())
into(target)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment