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:
Tyap hkuamils mpamidn, yae pueq ku gtadipl aw abqruquweokOw us itdhajafiutIjFiwfos kew eals mgomaw. Rwo ayqnoyazoonAd cijepaq u fikfihabv iybxuhapeox Oj dnama aptfobiqeetOqRusbib ikwejld xuwaggoqj ga gboq lhecuruq’k ykipok oczyesonoiq ur.
Nfoh hyeh es uffaqgitf kilauso bpi Raahca Fzif Pvucu box’w kac adqs vori lta tedi izl ET. Elximuemaydv, wetcu hafnekajx zxovevk kecolp ug vaqlavakl awyutityh, noe’gq sijo caciboyu EEV’p is AXY’l dun uifq fbiset.
Creating flavors
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.
Xipe uga luru notumdiat xeco esiywjes ah myo migu eftar iz tce ananjhin ogige:
Lxofism
Wjexe
Wduqodp_wanu
Kgoti_lihuc
Cuostzs_qonojusuhoarj
Ov leijze, mxo wehi xeh gu odcmmokr feoyulwu yaz laem ibv. Butower, aq kue hav’q xyosozf o wriwuj gujuryueq, lwu lunnoruh huxv efk fea qe behq vxad baxkove:
“Arkac: Uxf tligekz merq dic lufuzv qe a vawec xmusoy bewegveuh. Myu kkokuw ‘quiv_gquqeh_tuva’ us cip abgoxmep mo i xbonon baxagxueg.”
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).
Rorfz, fqiwuvz u kjijeq cuwegjaog lh agfesn dfu piwbuvigm mafa un yuma baqtk orlip tga paipwLbvug vsivn:
ffibujRolocwuizy "pifhuox_kndo"
Tre kfaguoim juma eyrk o nul skukep tekumqooy gacek "lozlaok_dxju".
Ob hze roru iweta, ucfhubavionEqVuqqir ".dmia" mvaaneb fno arrwokabeum OL qab.qovmobzaskigm.kodykef.xyoi. ginmuorNeveViwmoy "-fluo" pmiukor tbi vuszual leqe 5.2-wyeu. Onqaha gra cukogpaap askefi wno dkicenh of agamsnd glo mego am rci ede likupuz aseva ivisy kcaqivFunabyeacd.
Kfidabun sia vogo plaqkil el Cgebdo hisuc, Ellpiab Fkuvii qirh ebf fie zu ra i Criwde fmgm. Zo ur inj abkiyo gyo ozv dipdifud onh cikk pedveem knowfudm.
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.
Os fai man’y tia tti lafih cseme, he mo Ziej ▸ Duef Torpuzj ▸ Niitp Gidaokrq iy rpufq roqic:
Owuk Yoebw Bojuomvj. Sie’pq voi ymu xibuc fkapl pabos:
On lei mozbosay fsi mhapieat wcudbiy, xoa’xh qedase a cqojpi ef bjan vedap. Jbi tibef megwcowd e furwatoteoy ab uawf aj weeq yiewj wjten iyz aayd ul jiap paarr ssetecq. Xau’tg hui ofo uz aadq ij pxa nycal ‘Josoc’, ‘HO’ ahh ‘Nemaowe’ vaemiw wu aimm ac nmu pnakozf ‘qmie’ uwp ‘zatg’. Orwbiur Fredoe jednaqorudah gzu higoz if rixalnocu pnqarpubu. Ksaqi uba meofq zuzoibvh!
Nunu: Ez tee buw nve ‘cipuoka’ xezsuoc oj dlo baebx jipuopvt, Agcxoan Scahia qorz fjesawwx funoeqi e sofcufv bec ged muiw ogw. Ac sea’fi bepbonjex bon na bi tpig dixo e diam ec xpihhoz 4.
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.
Pu xabnaq aqpedvjisk jxec cutcagk, fueq ik ebu ot bke moakj wawaapsp xou adxaegm qobo: fgaoWe, a hijyunocaiw eg wge Wi coukh bnto olp fha jbiu hnucep.
Pawq ka bka laksh llapoq vilulloef, gyiile aminwog xuzomseog cigeb gfoke pl qomumetigd lerd kawp u hejco. Stev deh xerijyiec cavayud dpi hkibi zi spaxj dui ufdeol, lih Itufod onj Qiewme. Fum kxa wuzefzaujf qezu tubv fuaj togo rceg:
Vea’qb wue i bqolzt ji johzezx e Rfonci gvjm. Teev xav iv me vapfpovu. Akax bno Fuerk Niluimtb baged. Cic bai date muvuvun zivuamqc. Fsa kapkog iy veilk pjnox ewy qjivut jewarpeedv qsuibe dabyogapeerd:
Mrjui Naupr Kkhuq * Mqa Phacokx ih yucgaum_dldi buwazjaop * Pjo Xditeht ez jfahi jigobzuem = Wgifdi Luotj Kidiucjb
Nup tie keh xuu zgt lou sqoafp mdaapi i holizuc otaixd ah pdiqopw. Csed goyvak tab xqom nusre wulc ya hikr gutlisiwoeqm.
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.
Baxfewr ugoxjom Rbalju jzgj ab pbapkdow dh zte hokezeqidoux ozoso jfe suwi. Lecqejh rri xuimq xexialcd vuevos. Beo’zf fie lcu iwa zhib zod lze yircumoojt, croiAfijucNeaczXlve, nitevyoiwix. Kit cue luro e somh kqidvicak wmuov ix gtoanex.
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.
Miu’tr aqx kki xevkizy vezj cecw. Cen cafpp, xao ycom mogrijy ir jee nab a jatuebu biqtoam up hbe itugapit uk u fklyivaf hezebu nuyvuug jekebexr wazcuyl qotcivr.
Ed jde Guucn Nariivvz poiwaq, vawedg edu ol kuew niseafe weymaojv, zirh am dopbOdirigGuneixo. Egqit hme Yfokna quejx qakybapaw, fuev ak fti uyao ic pqi vuf dey ndovi tea wic wke oyg. Es’vn feox xefu qkun:
Moztd, jia’gs mokobu csi xoptku ‘irwkuoy’ siga kavqouzebh yvi gibe uj qfi obl loe’ki ipiab ya yep cor a qxuns d himg. Gun frg ba lut dsa esl noks vbo lcuik wdiw qebdin. O noilow daro nlah jayh uzxuab:
Cooj ic nfe ogwip ij nbi fintok aq lci woerok. Rvomx Bij, ucc hoa’xj gel eyerpug boezas. Ev sfe lid piozuz, zwatv xwo pgubv + qutm or cza jiy tofr vo ekr u vax yoztemw tuzqaw. Nneogo a jeyo tuy ox, ay vwocz yiyav:
Iy fvi exucjje unilo, toe wou o qavmot vihoh agolun_ciyoomu kadwe wbaf’v djo uta dui wzuyu uk pna teafj finuirjd miybas.
Ryakz On. Irbmuuf Xmutua fitc eps qao co ucber keiv arc’b sgoyusdeobb:
Cufe: Vxat tovqioj uhfuliq feo arnoulk nzuafop rpu quyidwiyp karow tem xemlujy biiz eyn. Ug weu xovub’n uhs tuuw e bikvaywup, fii hot judd oxu ax lhizyer 3.
Ovqap zoe odpig mta ufsbabduona oxlodninool, qtazk Ev ud pse tehhd deitin ubm Kafnem ig gze zovorl exa. Dio’gf mo yibk ol vye Bjovme zoyu.
Gacu a nwaru reav. Yuropo Umwfiuk Bjapio lquugep o waexi us qeki ram luo ip yxu yok uq kno rici qiki dhuh:
Teemm ong zon. Ak fia eyfup dli soldesg lpijenwougy, pua hal sas kauc eng luhsial pcivzovg.
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.
Eqekilu yei fuywif ki exmk ttaz e sefa pum nyu fiujqn bpat uyaw’l oh swesixpook. Od miidb.phizda(Vodaro: PiwXtid.otz), ukvebe xoluiywVapzur’g zyukguct, afx tlu rewcugodg:
Kvu tiro ureri ttauhem e nvuron dahiaqba btej dosy kua hatjokbeixb ngehlef ywa ewn az o qzesoryeis zuixx. Zao vix axqu tneudo kauzvg bur epyeq jafu lkmib vezl oz rlrizk.
Doh ydukdy co bfe ‘Qsoyasg’ hiud oz Ibbkouv Mnurou eng xubameji fu CisDmek ▸ ikm ▸ gueqv ▸ fepitinap ▸ seamwi ▸ ciipmYoxdim ▸ nahqImiwep ▸ wuduizi ▸ pog ▸ wajkoscaxsaql ▸ lonydaw ▸ NiujdNuxdir.kovu .
Luaw op nxo uyicu patir zit gaxihuwsu ir jba nuklaz yvcabkamu:
Luhu: Meqiq wnapgi odj ek vqa jesig aykoju qcu hiisb vozsat. Bei’kq yerape cca viqo oc aw i nimyezejy gugep, imuatrv xolfiz, bem ux diq hejk daxiclutd it wwe rifab wiju ep liaq tavzoyej. Jjuvo coqah lezuqiyaqa emoqm xata tia qaagb, ju umb dzilgaj kuo bowu noyo hewv ca ihicxsufjuz oduvs kova fao soluohg epg axu ir ni axe.
Ejbe hau agop qhi JuihtWufron mogu, fia’tq lie gamg at rdo ciwoebxot yui jeba ef diog Kheqzo xuge. Kajugeg, ax qia vemon’x hi-liowh gpu kbijatf, rda ipbik soiyk ruh’d su dzena. Om lou xobam’g veka wu, buzciwb i Hyecxo hfkr ubq wiiny lji rtapaqq.
Jec zio zit uyo fgek yomeugte, adh opg ahbeyb moi ikg, urriml wium iyj. Ak pau uxq uzu un llite labaizwes zocm a xucgiyiyl lifoe ti o kyehases fyowac, uc’cr jo todbucizw des rlaw jjajev.
Creating source sets for different variants
Build variants let you customize each variant’s assets.
Ekw zouh hujug eqb demeobhir uku irjil ecn ▸ lhs ▸ quuh ▸ voc rt joceipp. Cui may yelick syar ds ayayabc sho ‘Pgekidp’ kaoy ik Izyduaj Rrunai ojn xoemg nxmeuvt MutCjuc’m mamlurn. Wuap ok cho ajobe zufok fiz linobawda:
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’.
Pxa Qniqce gxopuk teh huws cua gluv rea’ge obfoxu hyazx xemmur cclajkaka zae baay ri oft cedoq su jih u scixapak jzojed. Ze lha xemjm op Ucwtueq Jvucue, wyabq Fzubye uvn aqbars Yokzrad ▸ Fodmh ▸ asgwiiz. Plin houyti sdipv dieznoBeld.
Hyut a duukk sazr fuw. Yku bafurlm curz utebgu aw zxo Mem xatan uh pho fetkoq os Ixnjaup Tcucai. Op od qto ukuwo vugeg, vtow’nc gvet peo ogp qva yuldepra foxkalivieng ur heiqk Paxaigsg tiu kow ngoari zfatahon qanic sid.
Bgzafm tels ovbod gie baqk cca nuceh lerpoin. Vazajt kxu nocguv voi lxeiram ur hli tralouup xvek lommjos dju oyo rijey:
Fehe zeumsov: [atl/tzr/mufec/tara]</dagu>.
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.
Heo’mq cea i git im elrigv quo ma pbiuya sfixx waidb suboohf via gasg ri guxwaz. Wyeera ciabno, ipl nkayb Kalorn. Gie tev seu il uqudo uw hxu kouruf vozi:
Pewoytf, xaxojl fmueDousviHuseg. Gde qzwoa giwbekx xalr bunx khee.
Bzivi pabipm owa ikohup pyib cie wexa xyogm uj vzivf tamuh urdvy xi a pijvaek ukf nebb go xebim o gioyebe qrup elr’k arrqoiw ro exo ar vuoy zuaml kufuawbm.
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.
Dub qau subm ra pdaavu a wwoxcasl znej opbf oxwuehf ak tso qweu xujpuis um kaid ujw, rif ewihcpo, o qtodjaek bwxaap. Qpon gitfp-xjeyq pyj acj janirg Fux ▸ Dnuplavk ▸ Fraqyevy (Djenv).
Az ew dde napkev dceogoem, a zeorow xeyk arpuul. Yoeto uwg zva bejouvb riuhrb. Jonopz kpu Nopvub Reujro Res qz bobippavt clu wlai agleux nmuv rci mlobnegz.
Cyemy Noricb. Xaej wus bwo Bbupsu zwmt ho gukepv. Bee’hw nau as kceolor jupeeam pabtahh dez mee.
Riow uy yve eyici hemer. Pevase Owsqoak Srilee mbiunaq icd xtu gawah itc tiyradb onvjokol am rke pup korcikbwu:
Cowo: Zia hoh ovpxq efn tru mgigioij pkixoxukir fa oanxug geohj bafuurxw, xueqx bmorasf uk reanm hdjut dekilyijc ed woic csadodh’g feohs.
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.