Home > Blockchain >  Set noCompress for only one productFlavor
Set noCompress for only one productFlavor

Time:06-10

I'm trying to set androidResources.noCompress for only one product flavor

When I try this

flavorDimensions "ring"
productFlavors {
    innerRing {
        dimension "ring"
    }
    outerRing {
        dimension "ring"
        androidResources {
            noCompress 'so'
        }
    }
}

Both innerRing and outerRing end up with .so files uncompressed. I believe this is due to gradle configuring all the product flavors statically. (see here)

But when I try to change noCompress afterEvaluate

afterEvaluate {
    android.applicationVariants.all { variant ->
        def ring = variant.getProductFlavors().get(0).name
        if (ring == "outerRing") {
            println("Don't compress .so files for outer ring build")
            android.androidResources.noCompress = ['so']
        }
    }
}

I get this error

com.android.build.gradle.internal.dsl.AgpDslLockedException: 
It is too late to modify internalNoCompressList
It has already been read to configure this project.
Consider either moving this call to be during evaluation,
or using the variant API.

How can I use the variant API to fix this? Any help is appreciated!

CodePudding user response:

If you are on AGP 7.2

android {
...
    androidComponents {
        onVariants(selector().all()) { variant ->
            if(variant.name.startsWith("outerRing")) {
                variant.androidResources.noCompress.add('so')
            }
        }
    }
...
}

If AGP is below 7.2 then from the documentation theoretically the following code should work, but I've had no success with it

android {
...
    androidComponents {
        onVariants(selector().all()) { variant ->
            if(variant.name.startsWith("outerRing")) {
                def aaptParams = variant.androidResources.aaptAdditionalParameters
                aaptParams.add("-0")
                aaptParams.add("so")
            }
        }
    }
...
}
  • Related