How to configure build types vs. product flavors?

Fahim picture Fahim · Feb 3, 2017 · Viewed 7.1k times · Source

Based on this answer https://stackoverflow.com/a/27908019/5156317 I have a follow up question: What makes an app different that speaks for product flavours? I am trying to compare this with my XCode setup which is as follows:

  • Dev App that uses test backend
  • Dev App that uses production backend
  • Test App that uses test backend (enterprise distribution)
  • Test App that uses production backend (enterprise distribution
  • Live App that uses production backend (app store distribution)

My thoughts for the android setup:

buildTypes: debug_test debug_production // no need of enterprise apps since it is possible unsigned apps on any device release

flavors: myApp

Thanks for your support!

Answer

Andrzej Zabost picture Andrzej Zabost · Feb 3, 2017

Well, I wouldn't specify more build types than debug and release in order to use different backend. Instead, I would use some of these techniques:

  • more flavors,
  • custom build config fields (documentation here),
  • combine multiple product flavors (documentation here).

You can access build types, build flavors and custom fields in the application code using BuildConfig class.

Approach with simple flavors

  • Build types:

    • debug
    • release
  • Flavors:

    • dev
    • test
    • live

Which would result in these build variants (you don't have to use all of them):

  • devDebug
  • devRelease
  • testDebug
  • testRelease
  • liveDebug
  • liveRelease

Approach with combining multiple flavors using dimensions

  • Flavor dimensions:

    • backend
    • target
  • Build types:

    • debug
    • release
  • Flavors:

    • target dimension:
      • dev
      • test
      • live
    • backend dimension:
      • production
      • test

Which would result in these build variants (again, you don't have to use all of them):

  • productionDevDebug
  • productionDevRelease
  • productionTestDebug
  • productionTestRelease
  • productionLiveDebug
  • productionLiveRelease
  • testDevDebug
  • testDevRelease
  • testTestDebug
  • testTestRelease
  • testLiveDebug
  • testLiveRelease

Using build field

Use additional value in build types and build flavors declarations, for example:

buildConfigField "boolean", "production_backend", "false"

or

buildConfigField "String", "backend", "\"production\""