Dynamically exclude files from the APK

Hi,

I would like to exclude a list of dynamically computed files from my APK in the gradle build. I can exclude a list of predefined files with:

android {
    packagingOptions {
        exclude 'file1.ext'
        exclude 'file2.ext'
        ...
    }
}

but my list of files to exclude is calculated by a custom task (which relies on the dependencies), so it cannot be defined like this, it must be added dynamically. I tried this but the new exclusion is ignored:

task excludeResources {
    doLast {
        project.android.packagingOptions.excludes += "file1.ext"
    }
}

project.afterEvaluate {
    preBuild.dependsOn excludeResources
}

Is there a way to dynamically add exclusions during tasks execution ?