Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 621
yajnesh Avatar asked Mar 16 '21 18:03

yajnesh


People also ask

How does Android gradle work?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.

What is a Buildtype in gradle and what can you use it for?

Discussion. A build type determines how an app is packaged. By default, the Android plug-in for Gradle supports two different types of builds: debug and release . Both can be configured inside the buildTypes block inside of the module build file.

How do I enable R8 on my Android?

To enable R8, open build. gradle module app file and add this piece of code inside the buildTypes . The code inside the release{} block means that this will be applied to the release build version of your application. If you launch the app in the emulator, this code is not executed.

What does Minifyenabled do?

minify is an Android tool that will decrease the size of your application when you go to build it. It's extremely useful as it means smaller apk files! It detects any code or libraries that aren't being used and ignores them from your final apk.


1 Answers

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.

like image 188
Amsanjeev Avatar answered Oct 18 '22 00:10

Amsanjeev