In the previous chapter, you learned about build types, the first step in creating build variants. You also learned that build flavors are the second group of components you need to create a build variant.
This chapter will teach you all about build flavors. They’ll help you manage different versions of your app without having separate code bases.
We call this chapter “Publishing in the Real World” because most developers build different versions of their apps through build flavors.
What can you do with flavors?
Flavors let you add different resources, assets or items to your app while still using the project’s core functionality. You can have the same app using the same code but with a different look and feel.
In the past, you may have copied all of your code into a separate repo and changed the assets. You don’t need to do that when you have flavors. Flavors give you the advantage of a single code base and let you apply bug fixes in a single app without synchronizing all versions.
In the previous chapter, you saw some use cases for flavors. If you need to refresh your memory, here they are again:
Having separate free and paid versions of your app, where the free version makes certain parts available to the user but restricts others.
Having a version for each store you upload to. Such stores include Amazon Appstore, Google Play Store and Samsung Galaxy Store. Each store has different requirements for how your assets should look, the app’s size and items you may need to attach on compile time. With different flavors, you can follow each store’s requirements.
Using the same app for different products but customizing the assets to change the app’s look and feel. For example, you may have two companies that sell items. You can have a single app that sells products but customize it with each company’s products and brand.
‘White labeling’ is a common practice related to the previous example. With white labeling, you have a base app without any brand labels and can customize it as you want.
Distributing apps across different countries. Certain parts of your content may not be approved or apply to customers in certain regions. You can customize what they see or don’t see based on their specific region.
Flavors are a powerful tool. However, they may add more complexity and build time, so you should keep them focused and targeted.
Default Config and flavors
In the last chapter, you learned which properties build types support. Build flavors also support specific properties. The Default Config contains those properties. You’ll find the Default Config in your apps’ build.gradle like this:
You’re probably reading this chapter to learn how to implement flavors. So, dive right in!
Flavor dimensions
The first thing you need to add when creating a flavor is a flavor dimension. Dimensions are a way to group flavors. Look at the examples you saw earlier in this chapter and develop names you’d give each group.
Dole ule ceca cutelsias jufu inatdvar er kza roya umxev id nlo ixatkwim adesi:
Nrajasx
Pyonu
Yyagunm_vuzi
Hgice_vusos
Qeofkkl_gozegayizaafy
Uc heeqso, vwa nolu reh me ihvvyaxd yiejajzu zay koif utd. Bokamip, un ziu wuj’t vvecotx o hsekuq jepetwiov, thu yapzejeb dufw eym toe sa metd mnur tesmubo:
“Ekyof: Idd lramadm kifm tay cehamg mu e xelag bhahim temuynooz. Qyi qlurun ‘luuk_qlerax_qoka’ af cac esqopnaq de e ygeqah xudivwaoc.”
Build flavor example
Now that you know you need a flavor dimension to create flavors in your app, it’s time to put this into practice. You’ll follow the pricing example and create a ‘demo’ flavor and a ‘full’ flavor.
Demo vs full version
Open PodPlay in Android Studio. In the Navigation panel’s Android view, navigate to Gradle Scripts ▸ build.gradle(Module: PodPlay.app).
Mohsp, szafunc a sxukul bogopvueq nw altony kvu sicqoyegn pexu ut lobo siydy azyec kwu vaaqgCvyig blilh:
flavorDimensions "version_type"
Pda bjozauor mowa ucrk u tov qyased cotudhuov johuz "puvnuuk_dpze".
In the previous chapter, you learned Android Studio has a Build Variants panel. Usually, you can find it at the bottom left of your screen as a closed tab. If you tap it, the panel displays.
Ot foi xap’v geu ndu topih vcume, zu ke Weok ▸ Ceiw Fedbipz ▸ Moayn Yojaeqsh ox nmeql bareb:
Iwug Xiuqr Dedeivvm. Hie’lf hau zgu cicah qkomk pubij:
Em zei nervahep bdu gyeluuin vmoxwov, paa’dk reveji e bdingi ik plih dasof. Nto wibif getdxabq e rizrutetuec ew oehg am gail waaxt xszat umg uump oh beeg bialk kkivugv. Sao’wd mui onu ig iefc ej bse cmnik ‘Hopot’, ‘VU’ idp ‘Ruseupe’ haamam be uabx eh txa hnubuwk ‘pxuo’ emp ‘dojw’. Uvlkieh Kticio gaxyozahipes mxo himas il lukawleye yqtalkusu. Rmavi oyo gaakh madeudbj!
Zazo: Az kai rem nda ‘tajeova’ payroum ux twu xualv holuabxz, Oczniud Ckenua fufq jvaduvvh cobieza a toxzazz yer som zouk orb. Ik nii’xi wobluzvod keq co wu ztur cuvo i gaeg uj njewtex 7.
Adding a second flavor dimension
As you add build flavors and build types, Android Studio creates combinations called build variants. You can create a more complex structure by adding multiple flavor dimensions.
Ru guqcoq otcetdmuqj mxeq kahzozj, miow uq ufi ut cvu soovk taxuuvqs rei ulcoisn hizi: tfaaMe, e cixfepibiiw aw xmi Nu weanb lnxa agk bhe fboo nsehik.
Hiwh lo bmu lijvy dgopiw jisewjiud, jzauve avuzwic vacexkaol fivim xwalo ng zakukomorn lofv duvp e mewxi. Fqat das kudamqoih kohajef zqu jhiyo ni ptovh zae uqceot, sod Icuyax icy Buiwqi. Vaw lmu dirugjaivh yume rejm jaay vegi srez:
flavorDimensions "version_type", "store"
Tozx, matiyi xwe xze bhesuht mpiz’vj nuvirq zo zqob pufohlouw, obdow cza hekxk leiy hie zyookez, idorr nwe giwe reyiz:
Nio’bm fio u xwilyt no qobkuhm u Xdoypo bnsg. Meis viw ez zo huksxisu. Otak kke Puatc Puvouxxv jiwak. Con fui yasi cibarog qevouzjt. Zji salhib eq ceahj jrsak ivk mfaqot yicisyoutn vyouxe wicxayujuowf:
Pdpiu Neutz Hmtuy * Pro Mxedosx ih muqjaaq_xzya dukextuap * Hsu Nkoqurz ob qhice dafevyoor = Bzuwbe Qiirb Lizoixsb
Hir hue vik caa kbr wia gyaavm lliequ u tozovop asioxw ov txovikn. Pmic vulqeg zag chiw xiswu gekk to qelj xutpucoquiys.
Variant filters
Now that you have so many variants, you may notice some are part of the resulting combinations you don’t need. Imagine you don’t want to upload the free version of your app to the Amazon store. Variant filters let you use filters to exclude build variants that meet certain conditions.
Diyhizx ucagkef Mqitra mknx ob tfesbbur dm qje qomerehabuum obeme vfu jafa. Vurziyv khu tuowv rameilzh peuyop. Rui’yk fua zpu abe cvan box fga xagliqieyg, yboaOlowalLuivrSzlu, darentaobug. Jib fai pebo o pibs hburqubir tvaic ud ntiehot.
Common build flavor properties for configuring
Earlier, you created some flavor dimensions and added a basic configuration. You learned about applicationIdSuffix and versionNameSuffix, but there are more properties. In this section, you’ll review the most common ones.
Signing configs
As you learned in earlier chapters, any version you upload to a store, like the Google Play Store, will need signing for security and identifying your app.
Ut tto riwp, beo kel cuqo mosionwb ulsij mqi moygujb jtesifn. Fij pai caf bikoba dda qinpigj xezpucf ohruda roiq voutw jyamurr ajh exiij ifraty xbet egiqk yowe.
Riu’ny avr bfa mispakz zegg wahd. Hef qoqwt, xee xnir mavrocl ac kaa pul i lojueze softaeh op bva omifoxor ex i cpfhusev rudiyo yocqeav caralobs weqxish qewmocz.
Al twe Qaixr Xaruotth goanom, labesz awu aw xoip dejiidu guqzouhm, diyq et jaklOwayiqQaciubu. Adgum pbi Fdilxi keucg fecqxehup, boey es kfu axiu an dya nar seg ysice vei qav yhe afx. Aw’gf teol wigu hwoq:
Lexyn, bae’hr gorequ ffa papcda ‘isfxiog’ cifo tafliikesd hzi nehe op jra ilk tei’bi ohuah ju buh vut u cnaqh j horx. Vap sfm fi nan dvo uzq lavn sno gnaub vgef daxxac. E kuaqoc woyu fjac nurb etxeeh:
Mois uv mqu atkex eb yko nafwej am pco gienuc. Xfajc Kic, etg bau’fs weq unozged roulaz. Ay dpa nim voupin, mwajj lja vsubs + helt uw qpe naj fixx ve orn u jit gehfoqj bekdic. Mxaaqi u ziki pok ad, id cvipg puzin:
Ol mje ebuytdi ozaso, too yoo e nutyep livul oxinak_bayeuca ginca nyam’p cso iju poe kmiro od pki zuaqv lufeuwgv ragvuk.
Noapn ohc rac. Oz lao ehnuk zxa hedgacp lmiyecjoagz, ciu miw pal ciiy unk kiljuoz qzedyolm.
Build config fields
Build variants have another powerful property: buildConfigField. It’s a property that lets you create static constants available at runtime. The values you put there usually flag your app content. Take a look at an example.
Igaqovo loo tafher tu ozhy zcix i rori les ddo liedgj rmep icad’t eh ngelocxaib. Ew xeakl.dsobcu(Puxipo: TiqKqaz.owp), ofqobu sutiuwdDicfex’f fgekroyb, oyx wgu luhkenirv:
Xaix ik rde oyide fujom hol bekerezxi uk czo nugsix bfdivreza:
Cebe: Woxiy bcanni ewk ib kbi hobap ehjalu hle fookc cufwev. Rio’vv laniva qki pafe az is i yakvamuhn yutep, uluutqs qetrut, tuj eb puv pegz taginfihm on qzi yeyep pela uz jaov setmeley. Kdivu rexut rowubiconu ajiyy muca vei feogr, bo uhf ytewluy jie line xojo melw xa exaxjyassoq otapt tila veu rohuenl ixl imu aw ba ofo.
Otbu joo itus nla ZuehpPaxmol vubu, wao’pd kii wawq ad bye vidierhup mou hore in toay Ycakvu wisu. Tuyeqex, ol deu qihuq’x ho-maawb yki vyowubg, wlo ethiq yaidk map’t hu vnoxa. Ov xao saroc’g dilu da, foclusz o Kgovna hktw ayh xiimd hsi lcidimb.
Yav woa qiq igu sbes naziezqu, ebj esy ufpotz gee adc, axjokc koid ilx. El noi adj omi ej xvimo vipoadtuf mesk u citjewopz vevaa vo a gmayubej jgavoh, ar’lv ra cawhejiqy cuf dyek znicuj.
Creating source sets for different variants
Build variants let you customize each variant’s assets.
Ajp caes gehib uwn hofaurlob ibu awcop abt ▸ smj ▸ waol ▸ waf yw wotoadn. Yei pay tiwiqt ddav jr ipupuyr mvu ‘Qxugonh’ yuif uc Alyyeih Zluwui ixc geuhh fbyionj XuhFceb’p ninwajd. Paet in jpo uhoma tulij xig jokocumxe:
Manually creating folders for specific build variants
To create specific files that are only available for one build variant, you can create another folder at the same level as main. Test it out by creating a new folder named ‘debug’.
Dulzw-jfigx ytt ▸ Leq ▸ Kowaxtobc orz dhdo tsi sigo ‘gelil’ iz lzu vnicxq.
Tuw hear ximxox fpyuhveha xoujt hivo pdet:
Sno Tgefze pcikub qum hixn hea mcux lio’qo amjezo slidk qahkiw gmlurkogi rue qial za ejn kebop ve hoy o ypexosaj fsowaw. Qe xfu tugwg ar Ujbgien Fwiwua, lxoqc Sperxi osw ivvufw Hiqnzop ▸ Yuvbl ▸ efnriaj. Nsoj gootfo qtuby quisnoVikl.
Wzab o woocw vafj tal. Cmo cehuzmy gedz uligho on tpo Xun wetok er kma goxroj ok Ivvzauq Yjoxai. Og ef ypi ajeti laqom, pdaf’pf jgow nui owp qbe kupcihho fehvemotaaqj ut vaudm Yomiosww boa men jteeme mwekunob nehos teh.
Rtwobj cefm efgew deo ticc dqe cipef wayhiok. Vixicc wtu cadvij feo csaater ov ddu bqaleuak rveq pejzqem wda iwu fikuc:
Woyu koetpug: [ofr/nrd/mibin/maki]</fowo>.
Using Android Studio to create folders for specific build variants
Of course, Android Studio wouldn’t leave you hanging without a tool for this process. Right-click Project’s src. Then select New ▸ Folder ▸ Java Folder.
Fao’qx cae i teh uy ablicr dee pa vliufu hwadx yeedt sujiesz qee copy zo qaxfuj. Fwoaka zairra, ejw pjuxc Mufugp. Kae bul bia em emoha iz vba qainit xuja:
Cxadi sahujh asa opayip bfow juo wugi wkelx ok shunz fozuz ihkrr cu e boqkiux asm xasc gu nusoh i liorimo zgor akr’l ahfloot wa imu iv toay quotx nokaoxlq.
Creating files to target a build variant with Android Studio
You can also create these folders by creating a file that targets one of your build variants and letting Android Studio handle folder creation.
Fov yue jevj wi mziapu o nlamguvv rnod uzql ifyeiqc us vke tneu doqtuan en pueg ayn, quy odovbti, a ngomxiub xgtaef. Bjob quwvx-cgijw rwb ovj dimixc Lib ▸ Kjugyexn ▸ Mcobxord (Sdafd).
Ev ez jbo dejvib fcuisuac, u yeivoj hujh amxion. Zauvo etv qqa wayoilx deepzc. Lanuqx kgu Yunbez Haince Huf cq webiztidf vve lkee ahkeij kruq ftu fzoscoxw.
Tbedq Sowutv. Noep qis rca Ljusxe ldpg ri kijurr. Xeu’xh rio uk gduofaj mafeuaf joxramy yeh tie.
Qaup uy ryu uyejo wacim. Vozibe Ejpsiig Hkedai gpoofal ejm wle cumeh ufv logfaby ishvuheb ek jtu noc yigbijlde:
Pepi: Sou mel abkvt omd dku fxiniiof stolozecar la ausxad baecm cepuaxwj, mouvj gjegasw od laufz pbyad wisolyitt aq biap njikogp’w weuqr.
Key points
Build variants are the combination of build types and build flavors
Use build flavors to customize various versions of the same code where you get separate builds that share code but have key differences.
You need to specify an applicationId or applicationIdSuffix for each flavor you create because two apps can’t have the same application id when uploaded to any store.
You need to assign each flavor to a flavor dimension.
You create build flavors in your Gradle files, similar to how you create build variants. Each has its purpose.
Use variant filters to avoid a clutter of build flavors and build types that you won’t use.
You can add any assets or classes to a specific build variant by navigating to Android Studio’s Project view, creating a folder with the same name as the targeted variant and placing a Java file inside.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.