packagingOptions per flavors

Hello
I am trying to take different so files from an aar as per a flavor when it’s packaging.

This is project structure.

+ src
    + lib
        test.aar  // there are many so files in aar
    + main
        + java
        + res

I tried this and it’s working when I fire build with a specific flavor like “gradle assembleChipARelease”.

android {
    
    ...

    flavorDimensions "chip"
    productFlavors {
        chipA {
        }
        chipB {
        }
    }


    gradle.startParameter.getTaskNames().each { task ->
        if (task.contains('assemble')) {
            if (task.contains('ChipA')) {
                packagingOptions.exclude('/lib/arm64-v8a/libXXX1.so')
                packagingOptions.exclude('/lib/arm64-v8a/libXXX2.so')
            } else if (task.contains('ChipB')) {
                packagingOptions.exclude('/lib/arm64-v8a/libXXX3.so')
                packagingOptions.exclude('/lib/arm64-v8a/libXXX4.so')
            }
        }
    }
}

But, this doesn’t work for “gradle build”
So, I tried various ways but all doesn’t work

Trying#1. all so file has excluded in each flavor

android {
    ...
    productFlavors {
        chipA {
            dimension "chip"
            packagingOptions {
                packagingOptions.exclude('/lib/arm64-v8a/libXXX1.so')
                packagingOptions.exclude('/lib/arm64-v8a/libXXX2.so')
            }
        }
        chipB {
            dimension "chip"
            packagingOptions {
                packagingOptions.exclude('/lib/arm64-v8a/libXXX3.so')
                packagingOptions.exclude('/lib/arm64-v8a/libXXX4.so')
            }
        }
    }

Trying#2. beforeEvaluate doesn’t get called

android {
    ...
    beforeEvaluate {
        tasks.configureEach {
            if (task.name.contains('ChipA')) {
                packagingOptions.exclude('/lib/arm64-v8a/libXXX1.so')
                packagingOptions.exclude('/lib/arm64-v8a/libXXX2.so')
            } else if (task.name.contains('ChipB')) {
                packagingOptions.exclude('/lib/arm64-v8a/libXXX3.so')
                packagingOptions.exclude('/lib/arm64-v8a/libXXX4.so')
            }
        }
    }
}

Trying#3. this says “It is too late to modify excludes”

android {
    ...
    afterEvaluate {
        tasks.configureEach { task ->
            if (task.name.contains('ChipA')) {
                packagingOptions.exclude('/lib/arm64-v8a/libXXX1.so')
                packagingOptions.exclude('/lib/arm64-v8a/libXXX2.so')
            } else if (task.name.contains('ChipB')) {
                packagingOptions.exclude('/lib/arm64-v8a/libXXX3.so')
                packagingOptions.exclude('/lib/arm64-v8a/libXXX4.so')
            }
        }
    }
}

Please help how to selectively exclude so files based on falvor.

As this is highly Android Gradle Plugin specific, you might have more luck asking in an Android community. :slight_smile:

1 Like

Thank you for reply :slight_smile:

1 Like