Search code examples
androidandroid-studiokotlingradlebuild

How does resConfigs work in android (gradle)?


Lack of proper documentation on gradle method resConfigs made me ask this question.

Official documentation:

  • In this example, google gives a specific example of how to use it with Languages.
  • In this example, google mixed Languages and Drawables
  • I was unable to find any javadoc like gradle doc for the same

My (mostly wrong) understanding:

If I specify something in front of resConfigs, only that gets included in the final build, everything else does not get bundled into the apk.


Android app setup

The test app has

  • Multiple languages (values-en , values-fr, ...)
  • Multiple drawable folders (drawable-xxhdpi, drawable-xxxhdpi, ...)
  • Multiple android versions (values-v21 , values-v7, ...)
  • Multiple smallest widths(sw800, sw1000, ...)
  • Multiple orientations (port,land)
  • etc

My Test Observations

Note: I used android studios' apk analyzer to deduce the following results. before every test, full clean, rebuild, and prod build was generated with proguard(shrink) enabled.


Test 1: I specified only languages resConfigs="en", "fr"

Result 1: In the apk,

  • all languages vanished except en and fr
  • all other resources were present (drawables, dimens, sw, orientation ...)

Test 2: I specified only drawables resConfigs="xxhdpi"

Result 2: In the apk,

  • all languages were present
  • all other resources were present (dimens, sw, orientation ...)
  • all drawables vanished except xxhdpi. (all other drawable folders were present, but had 0 byte files)

Test 3: I specified languages and drawable resConfigs="en","fr","xxhdpi"

Result 3: In the apk,

  • all languages vanished except en and fr
  • all other resources were present (dimens, sw, orientation ...)
  • all drawables vanished except xxhdpi.

Questions:

  1. When I only specify languages resConfigs="en", "fr", why are other resources getting bundled into the apk (for eg: drawables)
  2. Similary, when I only specify resConfigs="xxhdpi" , why languages and other resources get bundled
  3. In case of gradle is using some intelligence to figure out what to exclude and what not to exclude, does there exist a list of group of resources which fall under same category, is there any documentation on it?
    For eg
    • Language is a group, if gradle finds at least one language tag, it excludes all other languages. otherwise, includes all languages?
    • Drawable is a group, if gradle finds at least one drawable tag, it excludes all other drawables. otherwise, includes all drawables?

Related Questions:

  1. resConfigs only worked for me on app level gradle only. When I tried to use it on a dependent module, it ignored it completely. How to make it work on a dependent module. (I was trying to integrate firebaseUI for auth)
  2. is resConfigs in early stages of development, or is it meant only for development purposes, and not to be used in prod setups?

Solution

  • here are a few things to keep in mind

    • resConfigs works with resource qualifiers groups
    • resource qualifiers are grouped in different buckets like language , dpi , platform version etc .

    When you provide a resource qualifier to resConfigs then all other resources for qualifiers in that bucket will be removed. Resources in other buckets are not affected .

    Please refer to the following link for all the available grouping/buckets . https://developer.android.com/guide/topics/resources/providing-resources#AlternativeResources

    hope this makes sense.