Search code examples
androidgradleandroid-build-type

Android: How to reset resConfigs for release variant?


To make development faster, I want to do the following:

android {
    defaultConfig {
        resConfigs "en"
    }
}

My app has a lot of languages, and doing this saves significant time while developing. However, I do NOT want to release a version with this set. Unfortunately, resConfigs is not available on product flavors or build types, so I can't set it in debug {}, for example.

How can I automatically exclude resConfigs from release variants? I do not want to have to remember comment out that line of code when I'm building for release.


Solution

  • My solution was inspired by this answer to a related question. Here's how you do it:

    in app/build.gradle

    // Reset `resConfigs` for release
    afterEvaluate {
        android.applicationVariants.all { variant ->
            if (variant.buildType.name.equals('release')) {
                variant.mergedFlavor.@mResourceConfiguration = null
            }
        }
    }
    

    This works because mResourceConfiguration is the backing field for resConfigs. Unfortunately, The Android Gradle DSL does not currently expose a method to reset resConfigs, so we're forced to access the field directly using the groovy @<fieldName> syntax. This works, even though mResourceConfiguration is private.

    WARNING: this solution is a little fragile, as the Android Gradle build tools team could change the name of that field at any time, since it is not part of the public API.