<?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 Eslam Basher on Medium]]></title>
        <description><![CDATA[Stories by Eslam Basher on Medium]]></description>
        <link>https://medium.com/@eslambashermohamed?source=rss-05965d43fa3a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*r7fEeFQM_DHp0C_D5EEkfA.png</url>
            <title>Stories by Eslam Basher on Medium</title>
            <link>https://medium.com/@eslambashermohamed?source=rss-05965d43fa3a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 15:54:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eslambashermohamed/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[Gradle in Android]]></title>
            <link>https://medium.com/@eslambashermohamed/gradle-in-android-1cbd7094e611?source=rss-05965d43fa3a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1cbd7094e611</guid>
            <category><![CDATA[toml]]></category>
            <category><![CDATA[gradle-in-android]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[catalog]]></category>
            <category><![CDATA[tasks]]></category>
            <dc:creator><![CDATA[Eslam Basher]]></dc:creator>
            <pubDate>Fri, 07 Nov 2025 03:51:35 GMT</pubDate>
            <atom:updated>2025-11-07T03:55:03.641Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*EFC1Oy5D6H6KXVzC.png" /></figure><p>· <strong>Gradle</strong> is a build automation tool used to manage the build process for Android projects.</p><p>· <strong>Gradle</strong> acts as the maestro that determines when each task starts and exactly what it will do, as we will see later.</p><h4><strong>Structure of gradle files</strong></h4><ul><li>There are many files related to Gradle, and we will discuss all of them, but the most important files are those related to build.gradle, which are of two types.</li></ul><p><strong>1- Project Level → </strong>build.gradle.kts (Project: [project name])</p><p>2- <strong>Module Level →</strong> build.gradle.kts (Module :app)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*evZuQpVwBD1x1EYLEAiqNQ.png" /></figure><p><strong>Now, we will talk about each one of them and the difference between them.</strong></p><h3>What is the difference between the build.gradle for the app and the build.gradle for the project?</h3><p>any module you create has its own build.gradle, and the app is considered a module, which is why it has its own build.gradle.</p><p>On the other hand, the project can contain more than one module, which is why there is a build.gradle for the app module and a build.gradle for the entire project.</p><p>Any code I write in the build.gradle for the app will apply only to that app, which is a module.</p><p>Meanwhile, any code I write in the build.gradle for the project will apply to the entire project, including all the modules, one of which is the app module.</p><h3><strong>First: build.gradle (Module: app)</strong></h3><p><strong>The location of this file is in the module itself: app/build.gradle.</strong></p><p><strong>Its purpose is to specify the configuration for the module, as we will see.</strong></p><p><strong>If you open the build.gradle file for the app module, you will see several blocks that we will discuss now:</strong></p><h3><strong>1- Plugin Block</strong></h3><p><strong>These plugins are specific to the app module only, meaning I cannot use them outside the app module in the rest of the project.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*1ifaKnj_tJacgpt4GelDJg.png" /></figure><h3><strong>2-</strong> <strong>android Block</strong></h3><p><strong>The android block contains several attributes and blocks that we will discuss in detail.</strong></p><h4><strong><em>Namespace</em></strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/784/1*awBibvOFWSRp8wkA_zq8bA.png" /></figure><h3><strong>com.example.myApplication</strong></h3><p><strong>com</strong> → This is considered the top-level domain (TLD) for the company or organization, and it is usually “com” for commercial reasons, similar to many companies.</p><p><strong>example</strong> → This is considered a second-level domain, and it usually is the name of the organization or the company. For example, if I am creating an app for a company named “Example Corp,” I would choose the word “example” to reflect the company’s name.</p><p><strong>myapplication</strong> → This usually refers to the name of the application in the case of the app module, while for the other modules, it refers to the name of the module itself.</p><ul><li>The app module is considered the main module for the entire project; therefore, the namespace is the application ID.</li></ul><h3><strong>What is the benefit of the namespace?</strong></h3><p>The namespace ensures that the resources and classes in the module are uniquely identifiable to avoid conflicts. For example, if I have two modules, and each has a class for a specific feature with the same name, conflicts will occur without the namespace. However, by using the namespace, I can uniquely define which class belongs to which module.</p><h4><strong>compileSdk</strong></h4><p>Here, I will specify the Android SDK version that the app will compile against.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*m5z8TbQgJIcmc_f3Or6u1g.png" /><figcaption><strong>Specifying compileSdk as 35 means this project will be compiled with Android SDK 35, allowing it to use the features of SDK 35.</strong></figcaption></figure><p><strong>defaultconfig Block</strong></p><p>The purpose of this block is to define the default configuration for the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UGBUxwrvNAZaAkPvNcACDw.png" /></figure><h4><strong>applicationId</strong></h4><p>The applicationId is a unique identifier for the app in the project or on Google Play. There may be multiple apps with the same name in the store, but there cannot be more than one app with the same applicationId since it is unique.</p><h4><strong>minSdk</strong></h4><p><strong>Setting minSdk to 24 means the app will not run on any device with an API level lower than 24.</strong></p><h4><strong>targetSdk</strong></h4><p>Setting the targetSdk to 35 indicates that the app is designed to run on devices with an API level of 35 and it’s preferable for compileSdk to match the targetSdk.</p><h4><strong>versionCode</strong></h4><p>This is an integer representing the version number, which is important for updating the app on the app store. It must be incremented each time the app is uploaded to the store.</p><h4><strong>versionName</strong></h4><p>This is a string that can be in any format, such as “14.”, “5”, or “41.9.” Its purpose is to display the app’s version to the user on the app store.</p><h4><strong>testInstrumentationRunner</strong></h4><p>This is related to testing; it specifies the test runner to be used for running instrumented tests.</p><h4><strong>buildFeatures Block</strong></h4><p>I use the buildFeatures block to enable certain features, such as viewBinding, dataBinding, or BuildConfig</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pXHR8Wg274rUcdSVxh51sg.png" /></figure><h4><strong>buildType Block</strong></h4><p>Every app has two default build types: release and debug, which are created with the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YFDombQqqpyNQK1QUD1QyQ.png" /></figure><p>The release is the production version that goes to the client and is uploaded to the Play Store for final use.</p><p>The debug version is used by developers or testers, allowing them to track issues or the code during development.</p><p>I can easily add another build type by going to → File &gt; Project Structure &gt; Build Variants.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*D6_UR9KXXbD9pYetbibeqA.png" /></figure><p>I will notice attributes for the build type, such as the application ID Suffix in debug. If I specify it as .debug, it will append the word “debug” to the base package name.</p><p><strong>If I want the debug version to have a different app name than the release version:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*_ujJ53Gwapbs437Ca29Bng.png" /></figure><p><strong>1.</strong> <strong>First, ensure I am in Project View.</strong></p><p><strong>2.</strong> <strong>Next, open the app module and the src folder.</strong></p><p><strong>3.</strong> <strong>Create a directory and choose Debug/res.</strong></p><p><strong>4.</strong> <strong>Create a resource file named strings.</strong></p><p><strong>5.</strong> <strong>I will find it overriding the strings file, allowing me to change it.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9cdjYUGHunRPliE6mED4ig.png" /></figure><p>If I run the app, I will see that it installs on the device with the new name in debug mode.</p><p>Similarly, I can change any resource, meaning a specific resource can exist in the release version and a different one in the debug version.</p><p><strong>If I have specific code that I want to execute in debug mode and not in release:</strong></p><p><strong>1.</strong> <strong>I will enable BuildConfig like this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k08YXKFwjx77i9oyXrvCGQ.png" /></figure><p><strong>2.</strong> <strong>I will write the code like this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sFiFdXQh_xexhi-UCmqx6g.png" /></figure><p><strong>If I run the app, I will see that in debug mode, it performs the toast, whereas in release mode, it does not execute the toast.</strong></p><h3><strong>productFlavors</strong></h3><p>By default, there are no flavors in the app at the beginning, but there are two build types: debug and release, which we discussed earlier.</p><h3><strong>What is the difference between build types and product flavors?</strong></h3><p>- The build type gives you a different type of build, whether it’s debug or release, while product flavors generate APKs with different characteristics. What does this mean?</p><p>- For example, if I have an app and I want to create a free version and a pro version, each of them will produce two versions, which are the debug and release, allowing me to test both versions from all aspects and eventually release them.</p><h3><strong>How do I add flavors?</strong></h3><p>Suppose I have an app and I want to create a paid version and a free version: → Go to File &gt; Project Structure &gt; Flavors.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b8t3eKEDWV9zfSxweiRNLw.png" /></figure><ul><li><strong>If I click </strong>Add Product Flavor<strong>, it tells me that I need to have a flavor dimension.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*7dICDf3J8HTO5PxKPi9kFQ.png" /></figure><h4><strong>- Therefore, I will choose the first option, Add Flavor Dimension, which will open this dialog where I will specify the flavor dimension name.</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*mXfOfWGkwC_EtfYXDbVKvQ.png" /></figure><ul><li><strong>It will appear like this:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*xKcV9AFbGITRByRI8q9dQQ.png" /></figure><p>- <strong>I will add the first product flavor as </strong>free<strong> and the second as </strong>pro<strong> like this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*P88WueqmUKRIuTAu0f0KNw.png" /></figure><p><strong>I will notice that there are attributes for each flavor that I can define,</strong></p><p><strong>· such as the version code and minSdk, and I can specify these for each flavor.</strong></p><p><strong>· Now, for example, I will set the ApplicationID Suffix like this:</strong></p><ul><li><strong>Free → </strong><strong>.free</strong></li><li><strong>Pro → </strong><strong>.pro</strong></li></ul><p>· <strong>If I go to the build.gradle file for the app, I will find that it has automatically added this code:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*HVZhyz3BSd_Sqz5nurkqTA.png" /></figure><p>· <strong>I can add any resource to any flavor, and I can also add it to a specific flavor while leaving the other flavor without it, in the same way mentioned before:</strong></p><p>Product view &gt; src &gt; generate directory &gt; free/res</p><p>· <strong>Here, I will create any resource I want in the same way as before.</strong></p><p>· <strong>I can control the execution of any code in any flavor in the same way as before, like this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h2MTVUeJ4hynqb0ijl2awg.png" /></figure><p>· This code will execute in free and will not execute in pro.</p><h4><strong>There’s another way to implement different code in each flavor:</strong></h4><p><strong>Step One:</strong></p><p>1. I will display the project in Project View.</p><p>2. After that, if I select the build variant, for example, freeDebug, I will create:</p><p><strong>src &gt; </strong>directory<strong> &gt; freeDebug/kotlin</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jrLmtlGCgQ39XVqz9CY-tg.png" /></figure><p>3. A folder named kotlin will be created where I will create a class or file and define a method, for example, like this, which I will not be able to access unless I am in freeDebug.</p><p><strong>Step Two:</strong></p><p>1. I will do the same as before but select a different build variant, for example, proDebug, and then create : src &gt; directory &gt; proDebug/kotlin</p><p>2. A folder named kotlin will be created again, where I will create a class or file with the same name as before; follow the same System of access, which will only be available while in proDebug.</p><p><strong>Step Three:</strong></p><ul><li>If I run the app in free, it will execute the code I created in freeDebug, and if I run it in pro, it will execute the code in proDebug.</li></ul><h3><strong>BuildConfig file</strong></h3><p><strong>BuildConfig</strong> <strong>is a class that is generated, and the values inside it are static.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*Gd_J8p2pn1lwC0PahnjUpA.png" /></figure><p>· To enable it, I go to the build.gradle file of the module and add this code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/614/1*bc0moHRuKvTJftuJVmnp3A.png" /></figure><ul><li><strong>How to add a parameter to BuildConfig?</strong></li></ul><p>· <strong>I will use the method:</strong></p><p>buildConfigField(“Boolean”,”isHappy”,”false”)</p><p>· <strong>There are three ways I can add parameters to BuildConfig like this:</strong></p><h3>· <strong>Method One: defaultConfig</strong></h3><p>· The advantage of this method is that I can create the parameter and access it anywhere.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ad54IxLlMAxur-vHJKEbmw.png" /></figure><ul><li>After syncing and building, the parameter will be added to the class, and I can easily access it like this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wO3Q3KD_Q7iCMwXKCLZH2g.png" /></figure><ul><li>If I run the app, I will see a toast with the value from the class, which is false.</li></ul><h3>· <strong>Method Two: buildType</strong></h3><ul><li>If I add it only to the release, it means I can only use it in the release, and the same applies to debug.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-lgiGb31VG1ySxAF8glf1w.png" /></figure><ul><li>I can access it the same way as before without any issues.</li></ul><h3>· <strong>Method Three: flavors</strong></h3><ul><li>If I add it to the flavor, for example, the free flavor, it means I can only use it in this flavor, whether it’s debug or release.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G7GY_6zfRv25o9c2bNUdYg.png" /></figure><h4><strong>compileOptions Block</strong></h4><ul><li>Using this block allows me to specify Java compatibility with the source code and bytecode:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LFNCF0aU7urhdWOLt0oHqg.png" /></figure><p>· sourceCompatibility = javaVersion.VERSION_1_8</p><h4>· <strong>This line specifies compatibility with the source code. What does this mean?</strong></h4><p>· It means that the source code will use the features available in Java 8. This allows me to use features such as lambda expressions, stream API, and other Java 8 features without facing compatibility issues.</p><p><strong>· targetCompatibility = javaVersion.VERSION_1_8</strong></p><h4>· <strong>This line specifies compatibility with the bytecode. What does this mean?</strong></h4><ul><li>It means that the bytecode that is compiled must be compatible with Java 8. So if there’s anything above Java 8, I won’t find it in the bytecode. This way, I can ensure that the generated class file can run on a Java 8 runtime environment, even if I’m using a newer version of the SDK for compilation.</li><li>When I choose the JVM, it must be suitable for the minSdk, since, for example, if I choose a low JVM version with a high minSdk, it won’t work.</li></ul><h4><strong>Before moving on to the next block, let’s talk about the difference between</strong></h4><p><strong>Java Version vs. Java Bytecode Version</strong></p><h4>· <strong>First: Java Version</strong></h4><p>The Java version’s role is to specify the Java features that we will use. The same applies to Kotlin (e.g., Kotlin version).</p><h4>· <strong>Second: Java Bytecode Version</strong></h4><p>This specifies the Java bytecode that results from compiling the source code. Each new version includes additions such as capabilities and optimizations.</p><p>The bytecode version does not relate to language features. This means, for example, that I can use Java bytecode version 18, but I cannot use the features in Java 18; these are two separate things.</p><h4><strong>I must choose the Java version and Java bytecode version correctly, because sometimes, for example, I might be using a library that targets a specific JVM. If the library is compiled, it may produce bytecode that is not compatible with the JVM I specified.</strong></h4><h3><strong>kotlinOptions Block</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/908/1*MWPHWo22EQVc1_SkZK2ftg.png" /></figure><p><strong>jvmTarget = “1.8”</strong></p><ul><li><strong>This specifies that the Kotlin code must compile to Java bytecode that is compatible with Java 8, similar to targetCompatibility.</strong></li><li><strong>This ensures that any Kotlin code can utilize the features available in Java 8 and can also run on a Java 8 JVM.</strong></li></ul><h3><strong>3-</strong> <strong>dependencies Block</strong></h3><ul><li>In the dependencies block, we add the libraries we will need in the code.</li></ul><h3><strong>There are several keywords we use:</strong></h3><h4><strong>implementation</strong></h4><ul><li>This is used frequently, and through it, we can add any library or even another module, and we can use it anywhere in the module, whether in the package or in tests.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*l7jV1XGQkhO7kJAVGd_5Tw.png" /></figure><h4><strong>api</strong></h4><ul><li>This functions similarly to implementation, and we discussed the difference between them previously.</li><li>Let’s assume I have three modules, and each module has a class structured as follows:</li><li>After that, I will implement it the way we discussed before, which is → implementation(project( — -))</li><li>The question now is: Does module1 recognize class3?</li><li>The answer is no, and this is what implementation means; it implements the library only for the module itself, not for the other modules that depend on it.</li><li>Can I do the opposite? That is, make module1 recognize class3. Yes, of course, all I need to do is change the word implementation to api in module2 like this: → api(project(“:module3”))</li><li>This way, I can access class3 in module1.</li></ul><h4><strong>debugImplementation</strong></h4><ul><li>This means that these libraries will be used only in debug mode.</li></ul><h4><strong>releaseImplementation</strong></h4><ul><li>This means that these libraries will be used only in release mode.</li></ul><h4><strong>testImplementation</strong></h4><ul><li>This is specific to the implementation related to local unit tests.</li></ul><h4><strong>androidTestImplementation</strong></h4><ul><li>This is specific to the implementation related to tests that require the emulator to be running during the tests, which refers to Android tests.</li></ul><h3><strong>Secondly: build.gradle (project: project name)</strong></h3><ul><li><strong>This file specifies the configuration that I want to apply to the entire project.</strong></li><li><strong>The same code that we discussed in the build.gradle file for the app can be written here and used in the build.gradle(project), but as we said, it will apply to the entire project.</strong></li><li><strong>The location of the build.gradle file is found in the root project.</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OJWoT3EBbLhSKcl2Th6Yow.png" /></figure><ul><li><strong>The plugins here are supposed to apply to the entire project, but since I’ve specified that it’s false, that means it will not apply to either the project or the modules.</strong></li></ul><h3><strong>Thirdly: settings.gradle.kts</strong></h3><ul><li>The settings.gradle is part of the Gradle build system used to configure and manage multi-module projects.</li></ul><p><strong>The settings.gradle file contains several elements for managing the project, which we will discuss now:</strong></p><p>1. <strong>pluginManagement</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*waRtGP9EeToVzQfNtYMEPw.png" /></figure><ul><li><strong>The method pluginManagement() contains a method called repositories(). The purpose of this method is that when I add any plugin, it searches for it in these repositories.</strong></li></ul><p><strong>2- dependencyResolutionManagement</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*UmfroGjaSYLqTMlvC0pxew.png" /></figure><ul><li>The repositories() here is specific to dependencies, meaning any dependencies present in any module will be searched for in these repositories.</li><li>For example, if I have a library structured like this:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*HsAs_ILcq-1wRIJji9md5w.png" /></figure><ul><li>When I run it, I will encounter an error that states:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*FEtbfzO4MyyKgHHT65LUYA.png" /></figure><ul><li>It tells me that it cannot reach the dependency. If I go to the website from which I obtained the dependency, I will find an explanation of how to use the library, and it tells me that I should add this code:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*_YvJ5DxgmfQ7NiPoo-RV0A.png" /></figure><ul><li><strong>I will add it to my project like this:</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*n0DG5Sl8Kh5616I7hq3QGg.png" /></figure><p>· If I run it, I will see that the library has been added, and I can use it easily.</p><h4><strong>3.</strong> <strong>name of the root project</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*iNekyo_9OcMsHrLrFiAFRg.png" /></figure><p>· <strong>This specifies the project name that I defined for the project.</strong></p><h3>· <strong>include function</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*9AlBrxhP2fgetxFk9uuglQ.png" /><figcaption>· This function is very important because I specify the modules that exist in my project.</figcaption></figure><p><strong>Fourthly: gradle.properties</strong></p><p>· This is a configuration file used in Gradle builds to define properties and settings, as we will see shortly.</p><p>· The gradle.properties file allows me to customize many aspects of the build process without having to change the build script.</p><h4><strong>What can I write in the gradle.properties file?</strong></h4><h4><strong>1-</strong> <strong>Define properties</strong></h4><p>· I can define properties that I can access anywhere in the Gradle build scripts, such as in the build.gradle file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/448/1*lPrt6228s6d2L1FZor815w.png" /></figure><p>· After that, I will be able to access them in any build script file like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fh-pI7z4ai5_GyOxaB692w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rVa1x8fBL3zCffj54DNwzA.png" /></figure><h4><strong>1-</strong> <strong>Configuration settings</strong></h4><p>· I specify the build configuration settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yc7wJt_GZoQh1xEAtMEq5w.png" /></figure><p>· Here, for example, I specify the JVM arguments that Gradle uses for running. These arguments allocate memory, performance options, and encoding:</p><p>· -Xmx8192 → This option specifies the maximum heap size that the JVM will use, which is an additional 8 GB.</p><p>· -XX:+HeapDumpOnOutOfMemoryError → This is a flag that creates a heap dump in case an OutOfMemoryError occurs.</p><p>-Dfile.encoding=UTF-8 → This sets the default file encoding for the JVM to UTF-8.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*c43BYTeVupmDYURI-RXJRg.png" /></figure><p>· This enables the Gradle daemon process, which speeds up the build process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*w2UHQID0AQjN076E12XzTg.png" /></figure><p>· This allows Gradle to execute multiple tasks in parallel. If you set this to true, it will execute more than one task at the same time. This certainly increases performance and improves the build speed by using multiple CPU cores.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*nrkwfejCvGk6ZgSrqKOVOw.png" /></figure><p>· When this feature is true, it allows Gradle to configure only the modules required for the tasks currently being executed, instead of configuring all the modules.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*HeCeZwBqAAGMETBjxaYWVA.png" /></figure><p>· This enables AAP2 (Android Asset Packaging Tool 2), which helps package all the files in the project to generate the APK.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*PAqVdLFQJptpYdXVv7drmw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*Erx_XlnTAXf2motiZalxFA.png" /><figcaption>· This enables the Gradle build cache, allowing it to use the output from previous build processes, which increases build speed.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*uwZid2GV8cPxmGMI5rLKiA.png" /><figcaption>· This enables AndroidX dependencies libraries, which are considered better than the old Android libraries.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*sJqgwVvOQNd6dqAkC5dM8g.png" /><figcaption>· This enables the Jetifier tool, which helps ensure compatibility between older Android libraries and AndroidX libraries.</figcaption></figure><p><strong>Fifth: local.properties</strong></p><p>· This is a file among those that exist in my project, and it is a simple text file generated by Android Studio.</p><p>· One important thing here is that by default, this file is included in .gitignore, meaning it is among the files that will be ignored when I upload the project to any version control system.</p><h3><strong>What is the benefit of the </strong>local.properties<strong> file?</strong></h3><h4><strong>1-</strong> <strong>Environment-specific configuration</strong></h4><p>· I can specify configuration settings such as SDK paths.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HR_Wg7DV2ojvJ859h70TaA.png" /></figure><h4><strong>1-</strong> <strong>Sensitive information</strong></h4><p>· If I have sensitive information, such as an api_key, I can store it in local.properties, as we will see shortly.</p><h4><strong>2-</strong> <strong>Convenience</strong></h4><p>· I can clarify or explain the steps for anything I want for new developers.</p><h4><strong>Now let’s store the api_key in local.properties:</strong></h4><h4><strong>Step One:</strong></h4><p>· I will go to local.properties and write the api_key there like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*P9_DC5J1ss9nlwZJ3xvFKA.png" /></figure><h4><strong>Step Two:</strong></h4><p>· I will go to build.gradle(app) in the default config and add this code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZyaJTklbFRqhwRjnd2YgUw.png" /></figure><h4><strong>Step Three:</strong></h4><p>· I will rebuild the project.</p><h4><strong>Step Four:</strong></h4><p>· Now I can use the api_key in the app easily.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*KOtAZjx1_SeCI2wx5Bbt7Q.png" /></figure><h3>Gradle wrapper file</h3><h4><strong>What is the Gradle Wrapper?</strong></h4><p>· The Gradle wrapper (gradlew) is a small application included in the source code that downloads and launches Gradle, making build execution easier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/592/1*oPX0YeA3X7OzY0IKj8jO5Q.png" /></figure><p>· If you open the Gradle folder, you will find a folder for the wrapper. This folder contains two files:</p><h4><strong>First : gradle-wrapper.jar</strong></h4><p>· This is a Java Archive (JAR) file that contains the logic required to download the Gradle distribution.</p><h4>What is Gradle Distribution?</h4><p>· It is a collection of versions of the Gradle build tool that will be downloaded and executed to run Gradle commands.</p><p>· Each distribution contains the files needed during runtime, such as libraries, scripts, and other resources.</p><h3><strong>Second: gradle-wrapper.properties</strong></h3><p>· This is a file that contains a set of properties which we will discuss now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*9DLP0Ev1mdBR7lO5izchTQ.png" /><figcaption>· <strong>The property specifies the path to the base directory where the Gradle distribution will be stored:</strong></figcaption></figure><p><strong>GRADLE_USER_HOME</strong></p><p>· This points to the same path, which in Windows is typically:</p><p><strong>% userprofile % \.gradle → C:\users\username\.gradle</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*RjTsQ1quw0p5vvn8vnNa3g.png" /><figcaption><strong>This is a relative path for the distributionBase, which specifies where the Gradle distribution will be downloaded:</strong></figcaption></figure><p><strong>C:\users\username\.gradle\wrap per\dists</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mM7RSampkh4ocY0eJqRgFg.png" /><figcaption>· <strong>This is the most important property; initially, it checks whether this version of Gradle exists. If it exists, it won’t download it. If it doesn’t exist, it will download it from the URL specified in the property.</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*r4KCCFIOmYK3Qbq3HBy1Tw.png" /><figcaption>· <strong>This is similar to distributionBase as it defines the base directory where the zip files are stored.</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*sGxuUtrqHBQd2UFu0XEBOQ.png" /><figcaption>· <strong>This is similar to distributionPath, as it is relative to the zipStoreBase and also specifies where the Gradle zip files will be downloaded:</strong></figcaption></figure><p><strong>c:\users\username\.gradle\wrap</strong> <strong>per\dists</strong></p><h4><strong>Now, let’s understand what happens when I run the app, in order:</strong></h4><p>1- <strong>Gradle Starts the Build Process</strong></p><p>· Initially, ./gradlew (the Gradle wrapper) is invoked.</p><p>· Gradle reads the configuration from the build.gradle file and determines the tasks to be executed based on the build type (whether it is debug or release).</p><p>2- <strong>Dependency Resolution</strong></p><p>· Gradle checks and resolves the dependencies specified in the build.gradle file, downloading any necessary libraries or modules from the repositories defined in settings.gradle.</p><p>3- <strong>Compilation Tasks</strong></p><p>· Gradle initiates the compile tasks for the project; then, the compiler compiles the source code and converts it to bytecode.</p><p><strong>The remaining steps were discussed in detail earlier until the user can use the app.</strong></p><h3><strong>· How to Migrate Gradle Groovy to Gradle kotlin DSL</strong></h3><p>· There are two versions of Gradle: Groovy and Kotlin.</p><p><strong>Now I want to convert Gradle from Groovy to Kotlin:</strong></p><h4><strong>Step One:</strong> <strong>I will change the names of the Gradle files:</strong></h4><p>- build.gradle(app) → build.gradle.kts</p><p>- build.gradle(project) → build.gradle.kts</p><p>- settings.gradle → settings.gradle.kts</p><p><strong>Step Two:</strong> <strong>Now I will start the migration.</strong></p><p>· <strong>First: </strong>In Groovy, I can use single quotes and double quotes, while in Kotlin, I use only double quotes.</p><p>· <strong>For example, I will change the plugin format from this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*jhHRAXObRY9N9z__FdgW-g.png" /></figure><p>· <strong>To this format:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*e-hmtSeVlx0Lpo0RcXrZgg.png" /><figcaption>· <strong>The same applies to all strings in the Gradle files.</strong></figcaption></figure><p>· <strong>Seocnd: </strong>Convert the attributes written in Groovy to functions by adding parentheses like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*XSDI5xgr7GgpK9DIXYjSIw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o530-3qjLrRogQfDykrJsQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*3S-Wg7vsuNvoLUJL5fMjTg.png" /></figure><h4>· <strong>Third: Convert the fields in Groovy to variables:</strong></h4><p>· <strong>From this format (Groovy):</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*qnxP-3oVfYqK9BUajlW2Pg.png" /></figure><p>· <strong>To this format (Kotlin):</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*6-5OhHVCWcfMiQJiA-boQw.png" /></figure><h4><strong>Forth: For the Build Types, I can’t use the name of the buildType directly; instead, we will use the getByName() method.</strong></h4><p>· <strong>From this format (Groovy):</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nub1DMlZro43D_Rn5NEy0g.png" /></figure><p>· <strong>To this format (Kotlin):</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2Z1OtLO3UbQbKYjXmxT-Lw.png" /></figure><h3><strong>Gradle Catalogs</strong></h3><h4><strong>What is the benefit of Gradle Catalogs?</strong></h4><p>· When I have multiple modules in my project, there are many repeated elements in the Gradle file. If I need to perform an update, such as updating the versions of the libraries, I would have to change each of the modules. That’s why we use Gradle Catalogs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4po6fr3rbnV_Q3VvkXDuIg.png" /></figure><p>Now, if I create a new project, the catalogs will be available, but if I have an old project and want to refactor it to use the catalog, how can I do that?</p><p><strong>· Step One:<br></strong> Make the project visible in the project view.</p><p><strong>Go to Gradle and create a new file named libs.versions.toml.</strong></p><p>· <strong>Step Two:<br></strong> The file will be divided into three sections.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*eNCBgyNP0NwdTBjiRQlNbQ.png" /></figure><p><strong>· Step Three:<br></strong> <strong>Assuming I have a library structured as follows:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*tn2gHbi9gVwEvDT81HhKWQ.png" /></figure><p><strong>In the catalogs file, we’ll write in the relevant section like this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Luo7Gd0oZbykcT8YTyl9Uw.png" /></figure><p>· <strong>Now I can use the library directly via the TOML like this.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*h-pXZoWqoOsOLk79h6T0Gw.png" /></figure><p>· <strong>Step Four:<br></strong> If I have a plugin structured like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*6_mRtApGn4rPOyP5uvykEQ.png" /></figure><p><strong>I will do the same as I did with the library and add the plugin under its corresponding section.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0-nqSnNMWNknaPxllx7_RA.png" /></figure><p><strong>This way, I can use the plugin directly via the TOML.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*tAjhUje-Dhyjl1wT1nYHfA.png" /></figure><p>· <strong>This means that if I need to make updates, instead of updating in every module, I will only do it in one place.</strong></p><h4>· <strong>Step Fifth:</strong></h4><p>If I want to apply the same logic with the variables found in Gradle, I will do it like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*Cwie-EAat-W_PFGi6FJxng.png" /></figure><p>After that, I will use them easily in the build.gradle file like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZiQgL03yROXHVbdpUAYchw.png" /></figure><h3><strong>Gradle script</strong></h3><p>· Most of the files we’ve discussed are Gradle scripts, like for example:</p><p>- build.gradle</p><p>- settings.gradle</p><p>- gradle.properties</p><h4><strong>What are Gradle Scripts?</strong></h4><p>· A Gradle script is a file used in the build configurations of the project. It usually uses a Domain-Specific Language (DSL), either Groovy or Kotlin.</p><h4><strong>What is a Domain-Specific Language (DSL)?</strong></h4><p>· It is a specific programming language designed for particular problems in Android development. It allows programmers to express tasks, concepts, or functionalities in a concise and understandable way instead of using a general-purpose programming language like Java or Kotlin.</p><h4><strong>What is the difference between DSL, like Kotlin DSL, and regular Kotlin?</strong></h4><h4><strong>1-</strong> <strong>Purpose</strong></h4><p>- Kotlin DSL is designed for Gradle to build projects, manage dependencies, and execute tasks.</p><p>- Regular Kotlin is a general-purpose programming language used in various contexts, such as Android app development, desktop applications, or backend services.</p><h4><strong>2-</strong> <strong>Structure &amp; Syntax</strong></h4><p>- Kotlin DSL uses specific keywords for builds, like plugin {}, android {}, dependencies {}, and its syntax is more concise and expressive about build tasks.</p><p>- Regular Kotlin follows the standard syntax to define classes, functions, etc.</p><h4><strong>3-</strong> <strong>Context and Execution Environment</strong></h4><p>- Kotlin DSL is executed within the Gradle build lifecycle, allowing it to reference Gradle-specific APIs, configurations, and tasks, enabling direct interaction with the build system.</p><p>- Regular Kotlin runs in any Kotlin-compatible environment, like JVM or Android, and is not limited to build processes; it can handle a wide range of applications and logic.</p><h4><strong>How to Create a Custom Gradle Script:</strong></h4><p>· <strong>Step One:</strong> Create a new file.<br> Make your project visible in the Project View, and create a new file, say flavors.gradle.</p><p>· <strong>Step Two:</strong> Define your custom task.<br> Add the code you want to execute, for example, the code related to flavors like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*s1PWRgz7pZsQlWlgKK4Chg.png" /></figure><p>· <strong>Step Three:</strong> Include the custom script in your build file.<br> If I have more than one module in the project, instead of writing this code in each module, I can simply apply the Gradle script I created in every module like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wHrPMcImPPPlD12_pId1PA.png" /></figure><p>· This is the root of the project, since I created the Gradle script in the root; I could also place it in the Gradle file itself, but in this case, I would apply it like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U6FfiVq7TdJmXMQxyTuucQ.png" /></figure><h3><strong>Gradle Tasks</strong></h3><h4><strong>What are Gradle Tasks?</strong></h4><p>· A Gradle task is a small unit of work written in the Gradle build script. Each task presents a specific action for execution, like generating an APK from the project, installing the app on the emulator, or checking the app’s code for issues and suggestions on how to resolve them. There are many other tasks as well, and I can make a task depend on another task, as we will see shortly.</p><p>· I do not necessarily have to open the project in Android Studio to execute these tasks; I can run them on the terminal using Bash or any command prompt.</p><h4><strong>There are two types of Gradle tasks:</strong></h4><p>1- Custom task</p><p>2- Built-in tasks</p><p><strong>First: </strong>Built-in Tasks</p><p>· These are ready-made tasks in Android that I can use directly.</p><p>· If I want to see all the available tasks, I will use this command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*6MceEpLEzM2WxUVifF_CQQ.png" /><figcaption><strong>I will find that all available tasks are displayed, and I will notice that they are divided into several types, with each type having its specific tasks as follows:</strong></figcaption></figure><h4><strong>1-</strong> <strong>Build tasks</strong></h4><p>These tasks are responsible for compiling and packaging.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P2ACNDHQE9r9i7amePVgrA.png" /></figure><p>For example, the assemble task has the function of building the APK, and if I’m using flavors, it will generate all available APKs, whether free, pro, or any other existing flavors.</p><h4>2<strong>-</strong> <strong>Build Setup tasks</strong></h4><p>· This usually refers to the tasks that prepare the build environment or configurations before the project’s build process itself. This may include dependencies, configurations, and others.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hq6lM205P-orYMlwN6A5-A.png" /><figcaption><strong>I can create a new project using the command init.</strong></figcaption></figure><h4><strong>3-</strong> <strong>Verification tasks</strong></h4><p><strong>These tasks are responsible for testing, whether unit tests or instrumented tests.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7zkqk_pqpAlxlHChXXlXIA.png" /><figcaption><strong>In this task, lint is responsible for outputting all existing issues in the project, but in the form of HTML files. If I run this command and then open the build file under reports, I will find an HTML file that contains all the issues in the project along with their explanations.</strong></figcaption></figure><h4><strong>4-</strong> <strong>Install tasks</strong></h4><p>These tasks are responsible for deploying and installing the APK.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQVctyjKoWd1fnEjH1XJrw.png" /></figure><h4><strong>5-</strong> <strong>Android tasks</strong></h4><p>These tasks are specific to Android in general, such as the first task that displays all the dependencies in the project, the second task responsible for signing configurations, and the third that displays all the source sets in the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qw0vLUYpSa0edrUmtFdRvw.png" /></figure><h4><strong>6-</strong> <strong>Help tasks</strong></h4><p>These tasks show information about the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mNXrHkVxInPX8OyMJ5XWpA.png" /></figure><h4><strong>Second: Custom Tasks</strong></h4><p>· Now, we can create a specific task that is ours, as we will see shortly. However, because this topic is important, we will start with small things first and then discuss the tasks that we might need while working, which can save us a lot of effort.</p><p>· These tasks can be written in the build.gradle files of the project, and we can make a specific Gradle script for them, then apply it in the build.gradle to execute it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*yPJoohESJN13n5caVwr8Wg.png" /><figcaption>· <strong>Here’s a small task whose only function is to print “hello world.” Now, we want to understand everything in this code.</strong></figcaption></figure><p>· The register function is used to define that a new task will be created and takes two parameters: the first is the task name, which I specified to be task1, and the second is the task itself, meaning the action that will be executed, which is println(“hello world”).</p><p>· <strong>When I run it, I will find the output displayed as follows:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*eX8oR0o1rrjuwMHTZHcjow.png" /></figure><h4><strong>How to Run the Custom Task?</strong></h4><p>· The custom task can be run via the terminal just as I would run built-in tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XMnH1JAT7iw9yGF1htuaDQ.png" /></figure><p>· <strong>I can also run it through Android Studio.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*l-02YQU6Jx-PuTVA-_z40A.png" /></figure><h3><strong>Configuration phase Vs Execution phase</strong></h3><h4><strong>First: Configuration Phase</strong></h4><p>· This occurs when Gradle builds the project. During the build, registration of all tasks happens, and at that registration time, the code inside the task executes.</p><h4><strong>Second: Execution Phase</strong></h4><p>· This happens when I run a specific task. The code executed in this phase is the one that is within the doFirst { } and doLast { }.</p><p><strong>To understand this, let’s take a small example:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YG1YMvZYnZloZrVhhXlO6Q.png" /></figure><p><strong>If I run it, I will find the output appears as follows:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*3pJAW0OuPw4zHrNW_KiXnw.png" /></figure><p>· <strong>First: this is task one</strong><br> This is the main task and is executed when the task is registered, meaning this indicates it’s run as part of the task configuration phase, not during execution time.</p><p>· <strong>Second: this is task one inside first</strong><br> This is because we used doFirst { }. The code written inside doFirst { } is executed after the main task and before any other task. This is executed during the execution time, not during configuration.</p><p>· <strong>Third: this is task one inside last</strong><br> This will execute last because we used doLast { }. It executes after the main task and any other task.</p><h4><strong>dependsOn method:</strong></h4><p>· This method is important in tasks and used widely. Through it, I specify that a particular task should run before another task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/938/1*zfNryg5rO8xx_ZU7wT6pqQ.png" /><figcaption><strong>In this code, I specified that for task2 to execute, it must first execute task1 and then execute task2.</strong></figcaption></figure><p>· <strong>If I run task2, I will see the output as follows:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*W303CzVDht7CIAA5QtoqNg.png" /><figcaption>task1 should execute first, then task2, but the output displayed the opposite:</figcaption></figure><p>· <strong>What happened initially was that task2 was registered but not executed, so it executed the line :</strong></p><p>println(“this is task two”)</p><h4>· <strong>Then it registered task1 but did not execute it, so it executed this line as well :</strong></h4><p>println(“this is task one”)</p><p>· <strong>When it executed the dependsOn { } method, it executed task1 first, and then task2, thus it indeed executed task1 first and then task2.</strong></p><p>· <strong>The “up-to-date” message means that : Gradle considers task1 and task2 and that neither of their outputs has changed.</strong></p><p>· <strong>If there was a change in task1, and I ran task2, since task2 depends on task1, this means that the change would reflect on task2 because it will execute task1 first.</strong></p><p>· <strong>The second task that depends on the first task does not have to be a custom task; it can simply be a built-in task.</strong></p><p>· <strong>Now, I want to create a task to make it so that when I generate the APKs, it places them in a specific folder in the app called, for example, my_apks.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EULS-Tx6eKqE6_UHflAQ-A.png" /></figure><p>· <strong>Here I specified that there is a certain task of the Copy type named moveApk. This type has the following methods:</strong></p><p><strong>from() </strong>→ This is where I specify the path from which to move the files.</p><p><strong>into()</strong> → I specify the path to which the files will be moved.</p><p><strong>- If I run the assemble task, it will first execute the assemble task, and then it will execute the moveApk task, and this is the benefit of the finalizedBy { } method, as I can specify that a certain task will execute last after another task, and I will find the APK moved to a new folder.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1cbd7094e611" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android Runtime]]></title>
            <link>https://medium.com/@eslambashermohamed/android-run-time-c4558dc92da0?source=rss-05965d43fa3a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4558dc92da0</guid>
            <category><![CDATA[android-apps]]></category>
            <category><![CDATA[android-development]]></category>
            <category><![CDATA[bulk-sms-android-apk]]></category>
            <category><![CDATA[android-runtime]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Eslam Basher]]></dc:creator>
            <pubDate>Fri, 29 Aug 2025 01:12:37 GMT</pubDate>
            <atom:updated>2025-08-29T01:29:10.160Z</atom:updated>
            <content:encoded><![CDATA[<p>This guide includes all the steps involved in creating an Android app: from the source code all the way up to a user-ready app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W285jxUmsytVV6dTjxn-Zw.jpeg" /></figure><p><strong>First Step : Compile<br></strong>In the beginning we have the source code for the app, which is either java or kotlin. The first step is to compile the source code to ByteCode by Compiler. This ByteCode is stored with a specific format. For these files, the extension is (.class) and that is why these files are called “class files”.</p><p><strong>Second Step : Dexing or DEX Conversion<br></strong>In this step, the ByteCode is converted to Dalvik code by DEX Compiler.</p><p><strong>What is the difference between ByteCode and Dalvik code ?</strong><br>First let’s take a look at why we need Dalvik code. Java works by the principle “write once, run everywhere”. This means we can type the code just one time and then run it on any operating system, like Windows or Linux, seamlessly. This is possible because these operating systems include JVM (Java Virtual Machine), which converts ByteCode to machine code. After that, the cpu can execute the machine code.<br>In the Android operating system, however, there is no JVM. Instead, there is a similar tool called DVM (Dalvik Virtual Machine). Since DVM does not work with ByteCode, we must convert it to Dalvik code.</p><p><strong>Suppose we have a short java code like this :</strong></p><pre>public class HelloWorld {<br>    public static void main(String[] args) {<br>        System.out.println(&quot;Hello World!&quot;);<br>    }<br>}</pre><p><strong>In the first step it will convert to ByteCode:</strong></p><pre>Compiled from &quot;HelloWorld.java&quot;<br>public class HelloWorld {<br>  public HelloWorld();<br>    Code:<br>       0: aload_0<br>       1: invokespecial #1<br>       4: return<br><br>  public static void main(java.lang.String[]);<br>    Code:<br>       0: getstatic     #2<br>       3: ldc           #3<br>       5: invokevirtual #4<br>       8: return<br>}</pre><p><strong>In the second step it will convert to Dalvik code:</strong></p><pre>.class public LHelloWorld;<br>.super Ljava/lang/Object;<br><br>.method public constructor &lt;init&gt;()V<br>    .registers 1<br>    invoke-direct {p0}, Ljava/lang/Object;-&gt;&lt;init&gt;()V<br>    return-void<br>.end method<br><br>.method public static main([Ljava/lang/String;)V<br>    .registers 2<br>    sget-object v0, Ljava/lang/System;-&gt;out:Ljava/io/PrintStream;<br>    const-string v1, &quot;Hello World!&quot;<br>    invoke-virtual {v0, v1}, Ljava/io/PrintStream;-&gt;println(Ljava/lang/String;)V<br>    return-void<br>.end method</pre><p><strong>Third Step : Packaging<br></strong>In the third step, we use AAPT (Android Asset Packaging Tool). Here the Dalvik code and all associated files (images, XML files, etc,) are packaged into APK/AAB. XML files don’t compile. They will first be converted to binary XML format in order to be packaged within apk.</p><p><strong>Final Step : publish<br></strong>We can now publish apk on Google Play where it will be accessible to all users.<br>Next we will talk about the steps involved when a user installs</p><p>the app on Google Play</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PUH66TdqITuGLJZBlx4orQ.png" /></figure><p><strong>First Step : Decompress<br></strong>In this step apk reverts back to Dalvik code (a Dex file).<br><strong>Second Step : Android Runtime (ART)</strong></p><p><strong>What is the ART?<br></strong>ART (Android Runtime) is the step responsible for compiling DEX files to machine code. Over the years, different Android versions employed distinct strategies for this task.</p><p><strong>So let’s explore each strategy:</strong></p><p><strong>First Strategy : Just In Time (JIT)<br></strong>This strategy was used from Android 1 through Android 4 (Kit Kat).<br>In the past, memory was very limited so it was difficult to compile the whole app to machine code. As a result, we used the principle Just In time Compilation (JIT). JIT means we compile only a specific part of the code while we run the app. Dalvik compiled part of the code so that it would save memory, yet there were a lot of performance problems. Here’s why. Sometimes a certain portion of code is repeated.Instead of compiling this portion of code once, Dalvik was compiling it each time it appeared within the code. In order to make this more efficient, Android made caches for these repeated codes. Due to limited memory, however, the problems persisted.</p><p><strong>Second Strategy : Ahead Of Time (AOT)<br></strong>This strategy was used in Android 5 (lollipop) and Android 6 (marshmallow) In this strategy we used the principle Ahead Of Time (AOT). AOT would compile the whole app during installation. Here, the DEX files compile to native machine code using a special tool called DEX2OAT.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/469/1*sqxuwgagoVkbkLTGLh81Gg.png" /></figure><p><strong>This strategy improved the performance but still had problems:<br></strong>1- it consumed even more memory than the first strategy.<br>2- it prolonged installation time</p><p><strong>Third strategy: Profile Guided Compilation<br></strong>This strategy was used in Android 7 (Nougat) and Android 8 (Oreo).<br>First it compiled using JIT.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/366/1*Zm4FTGXZ5tTXMM68nluzpA.png" /></figure><p>Since there were portions of code that needed to execute rapidly, ART could compile and cache them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*-y1Oq28jOiPoQ6aD0Z2-FA.png" /></figure><p>According to the Profile Guided Compilation strategy, the rapid code (named “profile data”) is stored in the OAT (Optimized Android Application Package) after being compiled. Meanwhile, the rest of the code remains uncompiled until the user needs it.<br>This, fortunately, had no negative effect on installation time. But there remained one small problem : How do we find the profile data?<br>Fourth Strategy : Profile in the Cloud This current strategy has been used since Android 9 (pie).<br>The main idea in this strategy is that most users use the app the same way; so we can get the profile data from the app’s first users. When new users install the app, they are installing apk along with an additional file called core profile (the profile data from previous users).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*PK9MOf149Q5MNnO5DO-Skg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4558dc92da0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>