<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aly Abd El-shafy on Medium]]></title>
        <description><![CDATA[Stories by Aly Abd El-shafy on Medium]]></description>
        <link>https://medium.com/@alyabdelshafy?source=rss-bf35d0fc547a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*REWm0LEq67_YfB1-b0I3-w.jpeg</url>
            <title>Stories by Aly Abd El-shafy on Medium</title>
            <link>https://medium.com/@alyabdelshafy?source=rss-bf35d0fc547a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 10:08:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alyabdelshafy/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android Flavors]]></title>
            <link>https://alyabdelshafy.medium.com/android-flavors-6b802705b289?source=rss-bf35d0fc547a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b802705b289</guid>
            <dc:creator><![CDATA[Aly Abd El-shafy]]></dc:creator>
            <pubDate>Tue, 04 May 2021 11:09:19 GMT</pubDate>
            <atom:updated>2021-05-04T12:57:31.921Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XW_flzIELOoOlzSODwF3pg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@masakaze">Masakaze Kawakami</a> on <a href="https://unsplash.com/">Unsplash</a></figcaption></figure><p>A typical normal day at work… The QA team has asked me to upload a version of one of the apps we are working on to start testing it. When it comes to development and testing apps, we use Firebase App distribution. Moreover, we let the apps point to the corresponding Staging-Backend Endpoints Base URL. It is worth mentioning that we display some test values in the app for development and testing purposes to ensure a smooth testing process. Okay… Lucky us, the QA team approved the app and gave us the green light to release it to the Playstore.Yaay! It’s a happy story, yes? Hmm. Not really! Here comes the nightmare. To prepare a production build of the app, we have to let the app point to the Production-Backend Endpoints Base URL. Moreover, we have to hide all of the aforementioned unwanted values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Tx6bQNDAo3iPDThA-0n54w.gif" /></figure><p>That how I used to spend my working days up until I stumbled upon the Product Flavors. Product flavors are a powerful feature of the Gradle plugin from Android Studio to create customized versions of products. They form part of what we call Build Variants.</p><h4>Build Variants</h4><p>Simply, it is exposing your app in different versions or flavors by keeping only a single project.</p><p>By default, in every project, there are two options debug and release, as shown in <strong>Figure 1.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0ov8djkBinhxPShg0sUrXw.png" /><figcaption>Figure 1</figcaption></figure><h3>Configuring Android Builds in your application</h3><p>In this article, I will demystify how to configure builds in Android. By the end of the article, we will be having:</p><p>1- Three different environments, namely Dev, Stg, Production</p><p>2- Three different values for the same Base API URL variable</p><p>3- Three different values for the same ApplicationID reflecting each environment.</p><p>4- A unique Version code and A Version name for every build.</p><p><strong>Figure 2</strong>. shows how to create a new directory named Config in the project root directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fSj8f8SlfHXKlHJEV7iLrw.png" /><figcaption>Figure 2</figcaption></figure><p>In the Config directory, we create three files. dev. properties, stg. properties and prod.properties.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ee5b8d522e59845037304876341cbd9/href">https://medium.com/media/4ee5b8d522e59845037304876341cbd9/href</a></iframe><p>The config files are plain texts used for supplying build settings and other configurable properties to the Gradle build systems without changing the source code.</p><p>Please note that the config files’ content is in the form of key-value and supports different types such as boolean, string, integer.</p><p>The three subsequent figures show how we added four variables in each file of the three; APPLICATION_ID, BASE_URL, VERSION_CODE, and VERSION_NAME.</p><p><strong>stg.properties</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6071e41d19e7b3004ae2d2ed6b80f011/href">https://medium.com/media/6071e41d19e7b3004ae2d2ed6b80f011/href</a></iframe><p><strong>dev.properties</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/019de177c933309c2799e09449fa278b/href">https://medium.com/media/019de177c933309c2799e09449fa278b/href</a></iframe><p><strong>prod.properties</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6071e41d19e7b3004ae2d2ed6b80f011/href">https://medium.com/media/6071e41d19e7b3004ae2d2ed6b80f011/href</a></iframe><p><strong>Read configuration based on .properties file</strong></p><p>After adding the config files, let see how to let Gradle read those configurations. For example, the APPLICATION_ID</p><p>Furthermore, let me highlight that values like VERSION_CODE, VERSION_NAME can be changed in our build.gradle file at the build time.</p><p><strong>Figure 7</strong>. shows how to add an ext (ExtraPropertiesExtension) entry.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8903a43b1bd16e7062f2226a06aa29f8/href">https://medium.com/media/8903a43b1bd16e7062f2226a06aa29f8/href</a></iframe><p>Let’s pay a closer look at the previous code. Yeah, we are getting the current build flavor and read the corresponding .properties file located in the config directory and save it in the config variable. We also have two methods in the previous code <br>1- getCurrentFlavor()<br>2- getProps(Path)</p><p><strong>getCurrentFlavor()</strong> extracts the flavor name from one of the Gradle tasks and returns. It uses a regex to do this job.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cac7185b3c3b8f72fc56ed17ba954c75/href">https://medium.com/media/cac7185b3c3b8f72fc56ed17ba954c75/href</a></iframe><p><strong>getProps(Path) </strong>reads a properties file from a given path and returns its contents.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d1fb1f1861f9fb4448f11429b9619f1/href">https://medium.com/media/8d1fb1f1861f9fb4448f11429b9619f1/href</a></iframe><p><strong>Changing the properties in Build.gradle file</strong></p><p>The following step highlights how we can change the properties like <strong>APPLICATION_ID</strong>, <strong>BASE_URL</strong> in our build.gradle file at the build time.</p><p>Let’s open the app (module) level build.gradle as shown in <strong>Figure 7.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*mWP0Vg3Y6iPgtz3gHwluKQ.png" /><figcaption>Figure 7</figcaption></figure><p>Under the android block, we add <strong>flavorDimensions</strong> and <strong>productFlavors</strong> properties. Here, I am specifying our build flavors to contain three flavors as the following <strong>Figure 8</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9aa3a65819c4b7e665bb174f29dbb916/href">https://medium.com/media/9aa3a65819c4b7e665bb174f29dbb916/href</a></iframe><p>In the previous code, we also read the .properties file in the respective <strong>flavor</strong> and assign the value to applicationId, versionCode, and versionName fields. Let’s examine the above lines a bit. We are substituting applicationId using envConfig.APPLICATION_ID.replace(“””, “”). Here we are using the app-id specified in the respective config file. All our config property values are specified as a string within double-quotes. Gradle generates the BuildConfig.java file on the fly while building and uses the values specified in the substitutions. Hence, we make sure to strip double-quotes using replace(“””, “”).</p><p>versionCode is an integer and is substituted with the value of VERSION_CODE by converting the string value to an integer.</p><p><strong>Figure 9 </strong>shows how we can change the properties under <strong>defaultConfig.</strong></p><p>We are iterating over the Config key-value pairs read from the properties file and adding them as build config fields.</p><p>Please note that the if condition prevents us from adding duplicate entries into BuildConfig.java, as versionCode, versionName and applicationId are already part of build.gradle.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/743b03870828d45f6f3f1f7ab9daf7a5/href">https://medium.com/media/743b03870828d45f6f3f1f7ab9daf7a5/href</a></iframe><p>Now that our configs are added as build configs</p><p><strong>Figure 10</strong> shows how we can access them in the code. Open MainActivity.kt file and add the below lines in the onCreate method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b13ac9498ba6ee4da0e69179c0f4f2e/href">https://medium.com/media/4b13ac9498ba6ee4da0e69179c0f4f2e/href</a></iframe><p>Select Build Variants tab from sidebar and select devDebug as the variant as shown in <strong>Figure 11</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*d0_Omljl291zJgfCgxmg-w.png" /><figcaption>Figure 11</figcaption></figure><p>Run the app to see the logs as shown in <strong>Figure 12</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZKbfXX-ZEuZ00RlFsGN4hw.png" /><figcaption>Figure 12</figcaption></figure><p>Finally 🎉🎉, we are changing the config variables values depend on each <strong>flavor</strong>, and you can use it wherever you want in your code</p><p><strong>Renaming the app according to each flavor</strong></p><p>By default, in Android, the app name is specified as a string resource in AndroidManifest.xml file like android:label=”@string/app_name” under the application tag.</p><p>We can specify the app name as a string resource in the build.gradle file. We do this in the productFlavors block we defined earlier, as shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cALVv5Ke87Hp_Dh22kW8Cg.png" /><figcaption>Figure 13</figcaption></figure><p>Note: To avoid duplicate values for the app_name, make sure to remove the default app name inside the main/res/values/strings.xml file.</p><p>If we now run all the <strong>flavors</strong>, below is how it would look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZIr7g6gT0_p3b2cTGoxSXg.jpeg" /></figure><p>Interesting, yes! We finally did it.</p><h3>Where to Go From Here?</h3><p>The above article is just an example of how you can use <strong>flavors</strong>, but there are many more. For example, you can also use them to ease up implementing your free and paid versions of your app.</p><p>If you want to learn more, have a look at the link below:</p><ul><li><a href="https://developer.android.com/studio/build/build-variants"><strong>Configuring Build Variants</strong></a></li></ul><p>This article was only the first of a series of upcoming articles on optimizing our day-to-day coding situations. Please, stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b802705b289" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>