Building assets in android project using gradle

I need to pack some text files into a zip file and put it in assets

my Android project folder . I am using gradle and android studio. This is what I have in mine 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)
}

      

It doesn't work, I am not getting txts.zip in the right place. I'm completely new to gradle, can someone please tell me what I am doing wrong? Thank!

+3


source to share


1 answer


Ok, in the end I figured it out. I created a task for each option, 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 this is better, but it worked for me and I find it to be quite elegant too.

+4


source







All Articles