Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate assets in android project with gradle

I need to pack some text files in a zip file, and put it in the assets folder of my Android project. I am using gradle and android studio. This is what I have in my build.gradle at the moment:

task textsToZip(type: Zip, description: 'create a zip files with all txts') {
    destinationDir file("$buildDir/txts")
    baseName 'txts'
    extension 'zip'
    from fileTree(dir: 'text-files', include: '**/*.txt')
    into 'assets'
}

android.applicationVariants.all { variant ->
    variant.mergeAssets.dependsOn(textsToZip)
}

This doesn't seem to work, I don't get the txts.zip in the right place. I am quite new to gradle, can someone give me a hint about what I am doing wrong? Thanks!

like image 810
Enrico Avatar asked Sep 16 '14 06:09

Enrico


1 Answers

Ok, at the end I figured it out. I created a task for every variant, this way:

android.applicationVariants.all { variant ->
    def zipTask = project.tasks.create "textsToZip${variant.name.capitalize()}", Zip
    zipTask.destinationDir = file(variant.processResources.assetsDir)
    zipTask.baseName = "txts"
    zipTask.extension = "zip"
    zipTask.from fileTree(dir: 'text-files', include: '**/*.txt')
    variant.processResources.dependsOn(zipTask)
}

I don't know if it's the best way to go, but it worked for me, and I find it also quite elegant.

like image 66
Enrico Avatar answered Oct 18 '22 01:10

Enrico