3

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.

AutonomousApps
  • 4,229
  • 4
  • 32
  • 42

2 Answers2

3

Wouldn't this work?

Detect the debug build, reset the configurations and add your desired debug configuration.

applicationVariants.all { variant ->
    if (variant.buildType.name == "debug") {   
        variant.mergedFlavor.resourceConfigurations.clear()
        variant.mergedFlavor.resourceConfigurations.add("en")
    }
}
darken
  • 807
  • 12
  • 25
2

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.

Community
  • 1
  • 1
AutonomousApps
  • 4,229
  • 4
  • 32
  • 42
  • 2
    The solution didn't work for me, were there breaking changes with gradle? `variant.mergedFlavor.resourceConfigurations.clear()` did work though. – Henning Mar 15 '18 at 16:13
  • I used this successfully on an older version of the Android Gradle Plugin (AS 2.x). I haven't tried it with AS 3.x and build tools 3.x. – AutonomousApps Mar 15 '18 at 22:39