Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Gradle product flavor variants

samystudio edited this page Feb 10, 2016 · 3 revisions

Additionally to build types you may need to have different app versions. For example a free/paid version or an armv7/x86 version with Android. You may create these variants using product flavors and adding any Flair properties:

flair {

  appId "com.hello.world"
  
  productFlavors {
    free {
      appId "com.hello.free"
    }
    paid {
      appId "com.hello.paid"
    }
  }
}

As with build types these product flavors will automatically create new tasks, this means for example if you targeting ios, you'll have now:

  • assembleIosFree
  • assembleIosPaid
  • compileIosFree
  • compileIosPaid
  • ...

If you have set a debug and release build types, you'll have:

  • assembleIosFreeDebug
  • assembleIosFreeRelease
  • assembleIosPaidDebug
  • assembleIosPaidRelease
  • compileIosFreeDebug
  • compileIosFreeRelease
  • compileIosPaidDebug
  • compileIosPaidRelease
  • ...

You may add product flavors to specific platform as well

flair {

  appId "com.hello.world"
  
  android {
    productFlavors {
      armv7 {
      }
      x86 {
        packageX86 true
      }
    }
  }
}

If you want to use multiple product flavors level you'll do as following:

flair {

  appId "com.hello.world"
  
  flavorDimensions "version", "pet"
  
  productFlavors {
    free {
      dimension "version"
    }
    paid {
      dimension "version"
    }
    dog {
      dimension "pet"
    }
    cat {
      dimension "pet"
    }
  }
}

This will create tasks:

  • assembleIosFreeDog
  • assembleIosFreeCat
  • assembleIosPaidDog
  • assembleIosPaidCat
  • compileIosFreeDog
  • compileIosFreeCat
  • compileIosPaidDog
  • compileIosPaidCat
  • ...

Example with Android armv7 and x86:

flair {

  appId "com.hello.world"
  
  productFlavors {
    free {

    }
    paid {

    }
  }
  
  android {
  
    flavorDimensions "version", "pet"
  
    productFlavors {
      
      free {
        dimension "version"
      }
      paid {
        dimension "version"
      }
      armv7 {
        dimension "pet"
      }
      x86 {
        dimension "pet"
        packageX86 true
      }
    }
  }
}

This means all platform but android will have single flavor (free/paid) and Android will have multiple flavors (free_armv7, free_x86, paid_armv7, paid_x86).

In your code you may access which product flavor is executing using compilation constants:

  • PRODUCT_FLAVOR::FREE
  • PRODUCT_FLAVOR::PAID
  • PRODUCT_FLAVOR::XXX
  • ...

Theses constants will be set to true or false depending which version you are running.

...
if( PRODUCT_FLAVOR::FREE )
{
  // do stuff only if free version
}

PRODUCT_FLAVOR::FREE
{
public function myFunction(){}
}

PRODUCT_FLAVOR::PAID
{
public function myFunction(){}
}

Learn more about compilation constants from Adobe documentation.