11

How can I override the resConfigs per build types? I read that flavors would allow that, but I don't use them. I just want for my debug build another set of supported languges.

Here is what I tried:

buildTypes {
    debug {
        resConfigs "de", "en" // allow also german in debug builds
    }
    release {
        signingConfig signingConfigs.release
        resConfigs "en"       // english only releases
    }
}

Any simple idea how I can achieve that?

rekire
  • 47,260
  • 30
  • 167
  • 264
  • This ticket might been related: https://code.google.com/p/android/issues/detail?id=66133 – rekire Aug 10 '15 at 11:34
  • resConfigs isn't heeded for debug builds anyway, so just leaving resConfigs "en" in your release build should achieve what you want – Nilzor Feb 01 '17 at 07:44
  • 1
    No this is wrong @Nilzor this also applies for debug versions. I had to add `"en-RXA", "ar-RXB"` in order to test the pseudo locales. – rekire Feb 01 '17 at 07:57
  • Ah you're right I did a mistake in my testing. – Nilzor Feb 01 '17 at 08:18

2 Answers2

12

For some reason the individual buildType configs doesn't support the resConfigs command as you point out, but defaultConfig does and then you can use this trick to manipulate it per build type even without flavors configured:

android {
    defaultConfig {
        resConfigs "en"
    }
    applicationVariants.all { variant ->
        if (variant.buildType.name.equals("debug")) {
            variant.mergedFlavor.resourceConfigurations.add("de")
        }
    }
}
Nilzor
  • 18,082
  • 22
  • 100
  • 167
  • 1
    Man why didn't I think about to manipulate it on demand. Brilliant idea! – rekire Feb 01 '17 at 09:58
  • I just posted this answer to a related question. Might be useful for people who find this question/answer first: http://stackoverflow.com/questions/42259173/android-how-to-reset-resconfigs-for-release-variant/42259174#42259174 – AutonomousApps Feb 15 '17 at 20:17
  • Can I use `variant.mergedFlavor.resourceConfigurations.remove("de")` ? I tried it but not working @AutonomousApps – sam_k Apr 26 '18 at 05:38
  • No, I'm actually pretty that doesn't work, although I can't recall why. As an alternative, you could try getting the whole list, manually removing de from this list, clear the resConfigs list (which does work) and then setting your non-de list on it. – AutonomousApps Apr 26 '18 at 05:42
  • 1
    For some reasons when `resConfigs` is defined explicitly updating `resourceConfigurations` didn't work for me. However, if you omit `resConfigs` all together it works – tokudu Jan 26 '19 at 18:01
  • Pay attention, that Lint rule "MissingTranslation" won't consider "de" locale in that case and won't warn you about missing translation. – MyDogTom Apr 28 '20 at 15:28
0

The accepted answer did not work for me. de wasn't succesfully added. Doing everything inside the applicationVariants.all { ... } lambda works though:

android {
    defaultConfig {
        // No resConfigs here!
        // resConfigs "en"
    }
    applicationVariants.all { variant ->
        if (variant.buildType.name.equals("debug")) {
            variant.mergedFlavor.resourceConfigurations.add("de")
        } else {
            variant.mergedFlavor.resourceConfigurations.add("en", "de")
        }
    }
}

or in Kotlinscript:

android {
    defaultConfig {
        // no resConfigs here!
    }
    android.applicationVariants.all {
        val resConfigs = when {
            name.equals("debug") -> listOf("en", "de")
            else -> "en"
        }
        (mergedFlavor as DefaultProductFlavor).addResourceConfigurations(resConfigs)
    }
}
Henning
  • 2,202
  • 1
  • 17
  • 38
  • I am getting `Unresolved reference: DefaultProductFlavor` error. I am using the Android Gradle plugin version 4.0.1. Any idea how to fix it? – Ponsuyambu Oct 13 '20 at 19:25
  • @PonsuyambuVelladurai try using `AbstractProductFlavor` instead of `DefaultProductFlavor`. This helped me me when I updated to AGP 4.0.0. See here: https://stackoverflow.com/questions/60103603/how-to-set-different-applicationid-for-each-flavor-combination-using-flavordimen – Henning Oct 26 '20 at 15:12