<?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 Mariya Zharyonova on Medium]]></title>
        <description><![CDATA[Stories by Mariya Zharyonova on Medium]]></description>
        <link>https://medium.com/@norphyra?source=rss-e9a2ce4dc96------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6tSLAOYjbpyVeiFV0tWVRA.png</url>
            <title>Stories by Mariya Zharyonova on Medium</title>
            <link>https://medium.com/@norphyra?source=rss-e9a2ce4dc96------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 09:14:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@norphyra/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[Script for auto-build Android applications]]></title>
            <link>https://medium.com/@norphyra/script-for-auto-build-android-applications-efbb8b7e1bae?source=rss-e9a2ce4dc96------2</link>
            <guid isPermaLink="false">https://medium.com/p/efbb8b7e1bae</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Mariya Zharyonova]]></dc:creator>
            <pubDate>Mon, 14 Aug 2023 17:15:45 GMT</pubDate>
            <atom:updated>2023-08-25T12:24:26.806Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*i2p4im1zdapwlom_4FIHfA.png" /></figure><p>This article will cover the creation of an automated app build using a signature key for Google Play. Automated build will help reduce the time spent manually adding signature key data and choosing what to generate (APK or bundle), especially advantageous when you need to build multiple product flavor at the same time. In the end we only need to run the script from the command line and wait for it to execute. The APK or bundle will be built and added to the created build folder.</p><h3>Creating application signature keys</h3><p>To publish and later update the application in Google Play, you need to create a signature key. Let’s do it. To do this, select build in the top toolbar of Android Studio and choose Generate Signed Bundle/APK in the pop-up menu:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*81PgZofcgZvlltkZPz6rMg.png" /></figure><p>In the window that opens, select <strong>Android App Bundle.</strong> This is the file with the <strong>.aab</strong> extension, which must be uploaded to the <strong>Google</strong> <strong>Play Console</strong> for publishing. Click on the “next” button and go to the window for entering data about the already created signature key. So far we don’t have such a key, so we go to create a new one <strong>“Create new…”</strong> and enter the necessary data in the opened window (the password from the file where the key is stored and from the key itself must be the same). I suggest saving the keys in the project folder, so that you can add them to a remote Git repository later and not lose them:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5T2kLNT9kdZ52MJi8fFN-A.png" /></figure><p>For convenience, name the key file (the very first line when filling in the key data) the same as the key itself (key alias). Create as many keys as you will have applications on this code base. To create several keys, just go to the form of creating a key several times by clicking <strong><em>“Create new…”</em></strong> and repeat all the same steps. Once the key is created, you don’t need to go further to generate the signed bundle. Our task was to create the signature key. The window can be closed.</p><h3>Creating gradle.build.kts without product flavors</h3><p>Let’s start modifying our <em>build.gradle.kts(build.gradle)</em> application level, specifically the <em>android block</em> inside this file. Check if the SDK values, version number, code version are correct. Check if <em>sourceCompatibility</em> and <em>targetCompatibility</em> Java versions in compileOptions are correct — they should be the same and the same as the version for the generated Java jvmTarget bytecode in the <em>kotlinOptions block</em>. All other blocks (buildFeatures, composeOptions if you are using Compose and dependencies) must be already filled in, because without them you would not be able to build the application.</p><h4>Adding key data to build.gradle.kts</h4><p>Now we need to write a block in the <em>android block</em> that is responsible for automatically adding a signature when creating a signed <em>.aab/APK</em>, so that we don’t have to enter the key data manually. This block is called <em>signingConfig</em>. In it you need to specify all the same data that are filled in the window when creating a signed .aab/APK: the path where the file with the key is located, passwords to the file and to the key itself, and its alias (name):</p><pre>android {<br>  ....<br><br>  defaultConfig {<br>    ....<br>  }<br><br>  signingConfigs {<br><br>    create(&quot;key1&quot;) {<br>      keyAlias = &quot;key1&quot;<br>      keyPassword = &quot;PyYkAdn6hfnz&quot;<br>      storeFile = &quot;../jks/key1.jks&quot;<br>      storePassword = &quot;PyYkAdn6hfnz&quot;<br>    }<br>  }<br>  ....    <br>}</pre><p>This step can still be optimized by using the <em>java.util.Properties</em> class. This class represents a persistent set of properties. A property can be saved in a stream or loaded from a stream. To use this class, we will need to create a text file with the key parameters and save it in the project folder. I save in the key folder:</p><pre>storePassword=PyYkAdn6hfnz<br>keyPassword=PyYkAdn6hfnz<br>keyAlias=key1<br>storeFile=../jks/key1.jks</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Dl_a9QfD6NxtPwo45MHYTA.png" /></figure><p>Now let’s describe how to create a <em>Properties</em> object and read a text file using an input stream:</p><pre>import java.util.Properties<br>import java.io.FileInputStream<br><br>plugins {<br>  id(&quot;com.android.application&quot;)<br>  id(&quot;org.jetbrains.kotlin.android&quot;)<br>}<br><br>val keystorePropertiesFile = rootProject.file(&quot;jks/key1store.properties.txt&quot;)<br>val keystoreProperties = Properties()<br>keystoreProperties.load(FileInputStream(keystorePropertiesFile))<br><br>android {<br>  ....<br>}</pre><p>Let’s rewrite the <em>signingConfig</em> block:</p><pre>val keystorePropertiesFile = rootProject.file(&quot;jks/key1store.properties.txt&quot;)<br>val keystoreProperties = Properties()<br>keystoreProperties.load(FileInputStream(keystorePropertiesFile))<br><br>android {<br>  ....<br><br>  defaultConfig {<br>    ....<br>  }<br><br>  signingConfigs {<br><br>    create(&quot;key1&quot;) {<br>      keyAlias = keystoreProperties[&quot;keyAlias&quot;] as String<br>        keyPassword = keystoreProperties[&quot;keyPassword&quot;] as String<br>        storeFile = file(keystoreProperties[&quot;storeFile&quot;] as String)<br>        storePassword = keystoreProperties[&quot;storePassword&quot;] as String<br>    }<br>  }<br>  ....    <br>}</pre><h4>Creating build types</h4><p>Let’s move on to the buildType block. At this stage, you need to determine what types of builds you need: debug, release and some others, for example, a separate build for a specific application store, for which you need to specify additional parameters, or separate paid and free versions of the application. The variant with a different store we will just consider in the article, so that we have a complete understanding of what to do and how to do it, and then there were no problems with the creation of a new build type. In each release version of the build we need to specify its parameters: whether to create a signed <em>.aab/APK</em>, if yes — information about the key, whether to do code shrinking, resource shrinking, obfuscation, optimization, proguardFiles (more information about these 5 methods <a href="https://developer.android.com/build/shrink-code"><strong><em>here</em></strong></a>) and additional parameters (properties) passed to the buildConfigField function, which in turn creates properties in the BuildConfig class. With this we can create separate properties for different build types. In build type for debug, there should be no optimization of code and resources, which were described above.</p><p>In our case, we will create three types of builds: <em>debug, release</em> and a separate build for the Russian <strong>RuStore app store</strong>. We needed to make a separate build for it because of the implementation of the transition to the store to update the application. This build will differ from the release build by an additional added parameter in BuildConfig. We will add a link to the RuStore, which the user will go to when pressing the <em>“Update”</em> button in the pop-up notification:</p><pre>android {<br><br>  ....<br><br>  signingConfigs {<br><br>    create(&quot;key1&quot;) {<br>      keyAlias = keystoreProperties[&quot;keyAlias1&quot;] as String<br>      keyPassword = keystoreProperties[&quot;keyPassword1&quot;] as String<br>      storeFile = file(keystoreProperties[&quot;storeFile1&quot;] as String)<br>      storePassword = keystoreProperties[&quot;storePassword1&quot;] as String<br>    }<br>  }<br><br>  buildTypes {<br><br>    debug {<br>      signingConfig = null<br>    }<br><br>    release {<br>      isMinifyEnabled = false<br>      proguardFiles(<br>          getDefaultProguardFile(&quot;proguard-android-optimize.txt&quot;),<br>          &quot;proguard-rules.pro&quot;<br>      )<br>      signingConfig = signingConfigs.getByName(&quot;key1&quot;)<br>    }<br><br>    create(&quot;rustore&quot;) {<br>      initWith(getByName(&quot;release&quot;))<br>      buildConfigField(&quot;String&quot;, &quot;APP_SOURCE_BASE_URI&quot;, &quot;\&quot;https://apps.rustore.ru/app/\&quot;&quot;)<br>    }<br>  }<br><br>  compileOptions {<br>    ....<br>  }<br><br>  ....<br>}</pre><p>For convenience, we will not configure code and resource optimization, and for proguardFiles we will specify the path to the proguards-rules.pro file created initially when creating the project. Optimization of code, resources and description of rules in proguards-rules.pro is the topic of a separate article and we will not consider it here.<br> To be able to use the <em>BuildConfig</em> class we need to write it in the <em>buildFeatures</em> block:</p><pre>android {<br><br>  ....<br><br>  compileOptions {<br>    ....<br>  }<br>  <br>  buildFeatures {<br>    ....<br>    buildConfig = true<br>  }<br><br>  ....<br>}</pre><p>This completes the <em>build.gradle.kts(build.gradle)</em> application level addition.</p><h3>Creating gradle.build.kts with product flavors</h3><p>You need to perform all the items from the section <strong>“Creating gradle.build.kts without product flavors”</strong>, but with some modifications. If you choose to use these keys by creating an object of the Properties class, then specify the data of all three keys in the text file:</p><pre>storePassword1=PyYkAdn6hfnz<br>keyPassword1=PyYkAdn6hfnz<br>keyAlias1=key1<br>storeFile1=../jks/key1.jks<br><br>storePassword2=0kwuLphc677B<br>keyPassword2=0kwuLphc677B<br>keyAlias2=key2<br>storeFile2=../jks/key2.jks<br><br>storePassword3=2w91D8SC7SJk<br>keyPassword3=2w91D8SC7SJk<br>keyAlias3=key3<br>storeFile3=../jks/key3.jks</pre><p>Then in the<em> signingConfig</em> block we describe the parameters for all created keys, using a different name for each:</p><pre>android {<br>  ....<br>  signingConfigs {<br><br>    create(&quot;key1&quot;) {<br>      keyAlias = keystoreProperties[&quot;keyAlias1&quot;] as String<br>      keyPassword = keystoreProperties[&quot;keyPassword1&quot;] as String<br>      storeFile = file(keystoreProperties[&quot;storeFile1&quot;] as String)<br>      storePassword = keystoreProperties[&quot;storePassword1&quot;] as String<br>    }<br><br>    create(&quot;key2&quot;) {<br>      keyAlias = keystoreProperties[&quot;keyAlias2&quot;] as String<br>      keyPassword = keystoreProperties[&quot;keyPassword2&quot;] as String<br>      storeFile = file(keystoreProperties[&quot;storeFile2&quot;] as String)<br>      storePassword = keystoreProperties[&quot;storePassword2&quot;] as String<br>    }<br><br>    create(&quot;key3&quot;) {<br>      keyAlias = keystoreProperties[&quot;keyAlias3&quot;] as String<br>      keyPassword = keystoreProperties[&quot;keyPassword3&quot;] as String<br>      storeFile = file(keystoreProperties[&quot;storeFile3&quot;] as String)<br>      storePassword = keystoreProperties[&quot;storePassword3&quot;] as String<br>    }<br>  }<br>  ....<br>}</pre><p>And we don’t need to specify the key data in the <em>buildType</em> block in the release build type, we will specify them in the <em>productFlavors</em> block.</p><p>Before creating product flavors, you must add a value for the<strong> </strong><em>dimensions parameter</em>(these are the first letters before applicationId: <em>“app. ..applicationId”</em>):</p><p>Before creating product flavors, you must add a value for the dimensions parameter(<em>these are the first letters before applicationId: “app. ..applicationId”</em>). If each product flavor has a different value for the dimensions parameter, then add the value <em>“default”</em> to the common array, and then for each product flavor you must add your own value:</p><pre>android {<br>  ....<br>  <br>  signingConfigs {<br>    ....<br>  }<br>  <br>  flavorDimensions.add(&quot;default&quot;)<br>  <br>  ....<br>}</pre><p>Let’s create three product flavors to demonstrate the process of building multiple applications on the same code base. The productFlavor block is used for this purpose. This block specifies the parameters for each of the applications:</p><ul><li><strong><em>name</em></strong> — name of product flavor</li><li><strong><em>applicationId</em></strong> (ex. «com.example. ….»)</li><li><strong><em>applicationIdSuffix</em></strong> (ex. «.first») — matches the <strong>name</strong></li><li><strong><em>versionName</em></strong></li><li><strong><em>versionCode</em></strong></li><li>additional fields such as <strong><em>buildConfigField</em></strong> and <strong><em>signingConfig</em></strong> (the signature from the signingConfigs block corresponding to the created product flavor is specified)</li></ul><p>If the <strong><em>applicationId</em></strong> field is the same as the main fields, they can be omitted. In fact, all these parameters are a repetition of the <em>defaultConfig</em> block. We just specify, if necessary, those parameters that are different in each of the created applications. For example, let’s fill all these fields regardless of whether the first one matches the main defaultConfig block or not:</p><pre>android {<br><br>  flavorDimensions.add(&quot;default&quot;)<br><br>  productFlavors {<br>    create(&quot;first&quot;) {<br>      applicationId = &quot;com.example.application_for_article&quot;<br>      applicationIdSuffix = &quot;.first&quot;<br>      versionName = &quot;1&quot;<br>      versionCode = 1<br>      signingConfig = signingConfigs.getByName(&quot;key1&quot;)<br>    }<br><br>    create(&quot;second&quot;) {<br>      applicationId = &quot;com.example.application_for_article&quot;<br>      applicationIdSuffix = &quot;.second&quot;<br>      versionName = &quot;1&quot;<br>      versionCode = 1<br>      signingConfig = signingConfigs.getByName(&quot;key2&quot;)<br>    }<br><br>    create(&quot;third&quot;) {<br>      applicationId = &quot;com.example.application_for_article&quot;<br>      applicationIdSuffix = &quot;.third&quot;<br>      versionName = &quot;1&quot;<br>      versionCode = 1<br>      signingConfig = signingConfigs.getByName(&quot;key3&quot;)<br>    }<br>  }<br>}</pre><p>This example shows that you can set values separately for each product flavor, but you can also use <em>variables</em>:</p><pre>android {<br>  ....<br><br>  flavorDimensions.add(&quot;default&quot;)<br><br>  val _versionName = &quot;1&quot;<br>  val _versionCode = 1<br><br>  productFlavors {<br>    create(&quot;first&quot;) {<br>      applicationId = &quot;com.example.application_for_article&quot;<br>      applicationIdSuffix = &quot;.first&quot;<br>      versionName = _versionName<br>      versionCode = _versionCode<br>      signingConfig = signingConfigs.getByName(&quot;key1&quot;)<br>    }<br><br>    create(&quot;second&quot;) {<br>      applicationId = &quot;com.example.application_for_article&quot;<br>      applicationIdSuffix = &quot;.second&quot;<br>      versionName = _versionName<br>      versionCode = _versionCode<br>      signingConfig = signingConfigs.getByName(&quot;key2&quot;)<br>    }<br><br>    create(&quot;third&quot;) {<br>      applicationId = &quot;com.example.application_for_article&quot;<br>      applicationIdSuffix = &quot;.third&quot;<br>      versionName = _versionName<br>      versionCode = _versionCode<br>      signingConfig = signingConfigs.getByName(&quot;key3&quot;)<br>    }<br>  }<br>  ....<br>}</pre><p>Sync gradle, open the <strong><em>build variants tab</em></strong> and check that all product flavors are displayed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/966/1*T1oGHxfoaZLE9yD5jOPJ9w.png" /></figure><h3>Creating and running the script for auto-build application(s)</h3><p>Here our task is to create a script that we will run from the command line to automatically build all created <em>product flavors</em> and their <em>build types</em>. This script is a sequential execution of <em>Gradle tasks</em>, which will build <em>bundle/APKs</em><strong><em> </em></strong>and separate them into bundels and apks folders.</p><p>A Gradle task is a set of sequential Action objects<strong> </strong>(functions). When the task is executed, each of these actions is executed in turn, calling the Action.execute(T) method. Thus we get a sequence of actions that can be combined into an atomic operation, i.e. all actions from our point of view will be executed as one. Such atomic operation is a gradle task.</p><p>In our case we will build only release builds, for all other builds the algorithm is the same. But we must somehow define the names of the gradle tasks to be run. Here everything is simple — first in the name goes the type to be built (bundle for .aab and assemble for APK), then the name of product flavor if there is such and build type. The name of the task is written in camel case.</p><h4>Creating the script without product flavor</h4><p>Our script will have the extension .sh (shell). We will consider creating a script for Linux/MacOs. For convenience, place the script file in the project folder.</p><p>To execute gardle tasks we will use <strong><em>gradle wrapper</em></strong> (short just <strong>Wrapper</strong>). Therefore, the script that we will run to execute our build has the extension <em>.gradlew</em>. Wrapper is a script that calls our version of Gradle and installs it if necessary, which allows the programmer to quickly start working with the build without first installing the correct version of Gradle. Wrapper also standardizes the project for our version of Gradle, which makes builds more reliable.</p><p>In Linux/MacOs operating systems there is no concept of executable files (extension .exe). We will have to specify that the file is executable. Therefore, the first command will be to specify that the gradlew script file is executable:</p><pre>chmod +x ./gradlew</pre><p>Let’s consider the written command. The <strong><em>chmod</em></strong> command is intended for changing access rights to files and directories. The <strong><em>“+”</em></strong> sign means that we want to give permission, and <strong><em>the letter “x” means “execute”</em></strong>. This is how we define that the gradlew file can be executed. Now we can use this wrapper to execute gradle tastks.<br> With the next command <strong><em>cd(change directory)</em></strong>, we will go to the app folder to access the application level build.gradle.kts:</p><pre>chmod +x ./gradlew<br>cd ./app</pre><p>For the convenience of tracking the stages of the build, I suggest to output the stage of the build to the terminal. To do this, we will use the command <strong><em>echo</em></strong> (output a line of text to the terminal):</p><pre>chmod +x ./gradlew<br>cd ./app<br><br>echo &quot;AAB build&quot;</pre><p>Let’s write down the command for the release bundle build:</p><pre>chmod +x ./gradlew<br>cd ./app<br><br>echo &quot;AAB build&quot;<br>../gradlew bundleRelease</pre><p>Let’s parse the recorded command. After the command <strong><em>cd ./app</em></strong> we are in the app folder, and the wrapper script is in the root of the project folder. In order to access this script we need to go to the directory above (“../”), then access the wrapper script to execute it (“../gradlew”) and record the task that gradle should execute (bundleRelease). Recall that the name of the task is written in the camel case.</p><p>Next, for convenience, we will create a build folder in the root of the project with two folders in it — budles and apks, in which we will put the created bundle and APK. To do this, write three commands:</p><pre>chmod +x ./gradlew<br>cd ./app<br><br>echo &quot;AAB build&quot;<br>../gradlew bundleRelease<br><br>mkdir -p ../build/bundles<br>find ./build/outputs/bundle -name &quot;*.aab&quot; | xargs -I % cp % ../build/bundles<br>rm -rf ./build</pre><ul><li><strong><em>mkdir</em></strong> — to make directory</li><li><strong><em>../build/bundles — </em></strong>create a build folder in the root of the project (if there is no such folder) and a bundles folder in it (if there is no such folder)</li><li><strong><em>find — </em></strong>to<strong><em> </em></strong>find all files in the specified folder</li><li><strong><em>./build/outputs/bundle — </em></strong>folder where the build with the .aab type is saved by default</li><li><strong><em>-name — </em></strong>find command parameter, meaning the name of the files we are looking for</li><li><strong><em>*.aab — </em></strong>look for all files whose name ends in .aab</li><li><strong>| — </strong>is a bash wildcard that redirects the output of the command on the left to the input of the command on the right</li><li><strong><em>xargs — </em></strong>this command reads the data it receives for input (in our case from the command to the left of the wildcard), analyzes it and executes the specified command one or more times with the analyzed data</li><li><strong><em>-I — </em></strong>the insert parameter of the xargs command, indicating that it is necessary to insert the data that came to the input of the xargs command instead of the filler %</li><li><strong><em>% — </em></strong>placeholder for the xargs -I command</li><li><strong><em>cp</em></strong> — to copy from this directory to another</li><li><strong><em>../build/bundles — </em></strong>the directory to which we will copy the found files</li><li><strong><em>rm</em></strong> — to remove files or a directory</li><li><strong><em>-rf — </em></strong>rm command parameter specifying to forcibly delete the directory with all its contents, including files and subdirectories</li><li><strong><em>./build — </em></strong>the directory that we want to completely delete for uselessness — we moved all the built bundles to ../build/bundles (remember that this directory is created in the app folder)</li></ul><p>Now let’s do all the same steps after moving to the app directory to create a build for build type “rustore”. We will build APK for it, so gradle task will be recorded:</p><pre>../gradlew assembleRustore</pre><p>When selecting all files to move to the build folder, we will look for all files with the .apk extension. In the end we will have a script:</p><pre>chmod +x ./gradlew<br>cd ./app<br><br>echo &quot;AAB build&quot;<br>../gradlew bundleRelease<br><br>mkdir -p ../build/bundles<br>find ./build/outputs/bundle -name &quot;*.aab&quot; | xargs -I % cp % ../build/bundles<br>rm -rf ./build<br><br>echo &quot;APK build&quot;<br>../gradlew assembleRustore<br><br>mkdir -p ../build/apk<br>find ./build/outputs -name &quot;*.apk&quot; | xargs -I % cp % ../build/apk<br>rm -rf ./build</pre><h4>Creating the script with product flavors</h4><p>Let’s rewrite the .sh script we wrote for gradle without product flavor, for multiple product flavor. The only difference will be in the gradle tasks entry. The name of the product flavor will be added:</p><pre>chmod +x ./gradlew<br>cd ./app<br><br>echo &quot;AAB build&quot;<br>../gradlew bundleFirstRelease<br>../gradlew bundleSecondRelease<br>../gradlew bundleThirdRelease<br><br>mkdir -p ../build/bundles<br>find ./build/outputs/bundle -name &quot;*.aab&quot; | xargs -I % cp % ../build/bundles<br>rm -rf ./build<br><br>echo &quot;APK build&quot;<br>../gradlew assembleFirstRustore<br>../gradlew assembleSecondRustore<br>../gradlew assembleThirdRustore<br><br>mkdir -p ../build/apk<br>find ./build/outputs -name &quot;*.apk&quot; | xargs -I % cp % ../build/apk<br>rm -rf ./build</pre><h4>Running the script</h4><p>To run the script, we need to specify that our script is executable, just like for gradlew. Let’s check what modes it has by default:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5qSM8okyxQK2NSjRMgDNGA.png" /></figure><p>Now let’s execute the already known <strong><em>chmod</em></strong> command and check what has changed in the script modes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CPVETULPwJEP4KIRm45fAQ.png" /></figure><p>Great! The letter <strong>“x”</strong> appears, so the execution mode is set correctly. Now we need to run it. First we synchronize gradle to make sure that what we have written is correct. Now I recommend that you build a debug build once to make sure that everything works as it should. Now run the script just by accessing it:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FNElJ8BJrNXM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DNElJ8BJrNXM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FNElJ8BJrNXM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/031fce43b9e8ee26916ba99774f4336f/href">https://medium.com/media/031fce43b9e8ee26916ba99774f4336f/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FMMHRQ4vaAaY&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DMMHRQ4vaAaY&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/495a8472e41bb9df32b020c0f7301d56/href">https://medium.com/media/495a8472e41bb9df32b020c0f7301d56/href</a></iframe><h4>The project folder:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n2vbmPV9VWermovjVBXmlA.png" /></figure><h4>The folder with signature keys:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E81npQ2uS4aVB7a5Jy8Edg.png" /></figure><h4>The resulting build.gradle.kts file:</h4><pre>import java.util.Properties<br>import java.io.FileInputStream<br><br>plugins {<br>    id(&quot;com.android.application&quot;)<br>    id(&quot;org.jetbrains.kotlin.android&quot;)<br>}<br><br><br>val keystorePropertiesFile = rootProject.file(&quot;jks/keystore.properties.txt&quot;)<br>val keystoreProperties = Properties()<br>keystoreProperties.load(FileInputStream(keystorePropertiesFile))<br><br>android {<br>    namespace = &quot;com.example.application_for_article&quot;<br>    compileSdk = 33<br><br>    defaultConfig {<br>        applicationId = &quot;com.example.application_for_article&quot;<br>        minSdk = 24<br>        targetSdk = 33<br>        versionCode = 1<br>        versionName = &quot;1&quot;<br><br>        testInstrumentationRunner = &quot;androidx.test.runner.AndroidJUnitRunner&quot;<br>        vectorDrawables {<br>            useSupportLibrary = true<br>        }<br>    }<br><br>    signingConfigs {<br><br>        create(&quot;key1&quot;) {<br>            keyAlias = keystoreProperties[&quot;keyAlias1&quot;] as String<br>            keyPassword = keystoreProperties[&quot;keyPassword1&quot;] as String<br>            storeFile = file(keystoreProperties[&quot;storeFile1&quot;] as String)<br>            storePassword = keystoreProperties[&quot;storePassword1&quot;] as String<br>        }<br><br>        create(&quot;key2&quot;) {<br>            keyAlias = keystoreProperties[&quot;keyAlias2&quot;] as String<br>            keyPassword = keystoreProperties[&quot;keyPassword2&quot;] as String<br>            storeFile = file(keystoreProperties[&quot;storeFile2&quot;] as String)<br>            storePassword = keystoreProperties[&quot;storePassword2&quot;] as String<br>        }<br><br>        create(&quot;key3&quot;) {<br>            keyAlias = keystoreProperties[&quot;keyAlias3&quot;] as String<br>            keyPassword = keystoreProperties[&quot;keyPassword3&quot;] as String<br>            storeFile = file(keystoreProperties[&quot;storeFile3&quot;] as String)<br>            storePassword = keystoreProperties[&quot;storePassword3&quot;] as String<br>        }<br>    }<br><br>    val _versionName = &quot;1&quot;<br>    val _versionCode = 1<br><br>    flavorDimensions.add(&quot;default&quot;)<br><br>    productFlavors {<br>        create(&quot;first&quot;) {<br>            applicationId = &quot;com.example.application_for_article&quot;<br>            applicationIdSuffix = &quot;.first&quot;<br>            versionName = _versionName<br>            versionCode = _versionCode<br>            signingConfig = signingConfigs.getByName(&quot;key1&quot;)<br>        }<br><br>        create(&quot;second&quot;) {<br>            applicationId = &quot;com.example.application_for_article&quot;<br>            applicationIdSuffix = &quot;.second&quot;<br>            versionName = _versionName<br>            versionCode = 1<br>            signingConfig = signingConfigs.getByName(&quot;key2&quot;)<br>        }<br><br>        create(&quot;third&quot;) {<br>            applicationId = &quot;com.example.application_for_article&quot;<br>            applicationIdSuffix = &quot;.third&quot;<br>            versionName = _versionName<br>            versionCode = _versionCode<br>            signingConfig = signingConfigs.getByName(&quot;key3&quot;)<br>        }<br>    }<br><br>    buildTypes {<br><br>        debug {<br>            signingConfig = null<br>        }<br><br>        release {<br>            isMinifyEnabled = false<br>            proguardFiles(<br>                getDefaultProguardFile(&quot;proguard-android-optimize.txt&quot;),<br>                &quot;proguard-rules.pro&quot;<br>            )<br>            signingConfig = signingConfigs.getByName(&quot;key1&quot;)<br>        }<br><br>        create(&quot;rustore&quot;) {<br>            initWith(getByName(&quot;release&quot;))<br>            buildConfigField(&quot;String&quot;, &quot;APP_SOURCE_BASE_URI&quot;, &quot;\&quot;https://apps.rustore.ru/app/\&quot;&quot;)<br>        }<br>    }<br>    compileOptions {<br>        sourceCompatibility = JavaVersion.VERSION_17<br>        targetCompatibility = JavaVersion.VERSION_17<br>    }<br>    kotlinOptions {<br>        jvmTarget = &quot;17&quot;<br>    }<br>    buildFeatures {<br>        compose = true<br>        buildConfig = true<br>    }<br>    composeOptions {<br>        kotlinCompilerExtensionVersion = &quot;1.4.3&quot;<br>    }<br>    packaging {<br>        resources {<br>            excludes += &quot;/META-INF/{AL2.0,LGPL2.1}&quot;<br>        }<br>    }<br>}<br><br>dependencies {<br><br>    ....<br>}</pre><blockquote><em>If you found article useful then please start following me. You can also find me</em> <em>on </em><a href="https://medium.com/@norphyra"><strong><em>Medium</em></strong></a><strong><em> </em></strong><em>or </em><a href="http://www.linkedin.com/in/norphyra"><strong><em>LinkedIn</em></strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=efbb8b7e1bae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to add dependencies in Android Studio with an internal plugin]]></title>
            <link>https://medium.com/@norphyra/how-to-add-dependencies-in-android-studio-with-an-internal-plugin-4d507c0e94b3?source=rss-e9a2ce4dc96------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d507c0e94b3</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Mariya Zharyonova]]></dc:creator>
            <pubDate>Thu, 10 Aug 2023 09:50:52 GMT</pubDate>
            <atom:updated>2023-08-25T12:37:54.760Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*TYIhiohnbEqrZYOa3FVoQg.png" /></figure><p>In this article, we will be looking at managing dependencies using a separate module that includes a plugin. This approach will help to add new dependencies and manage their versions more conveniently.</p><p>I will omit a migration Gradle files from Groovy to KTS, since in new version of Android Studio you can now choose the language of Gradle files. If it necessary, I will make a separate article about migration.</p><p>There are advantages and disadvantages to this method. The main advantage is that everything related to plug-in libraries and versions is in one place and their manage becomes more convenient. The main disadvantage — creation additional module and its structure. This requires more time and may seems complicated for a beginner.</p><h3>Plugin packaging</h3><p>There are several place where you can put the source for the plugin:</p><p><strong><em>Build script</em></strong></p><p>You can include the souce of the plugin directly in build script of the entire project. The advantage of this is that the plugin is automatically compiled and include in the classpath of build script without you having to do anything. However, the plugin is not visible outside the build script, and so you cannot reuse this plugin outside the build script it is defined in.</p><p><strong><em>buildSrc project</em></strong></p><p>You can put the source of the plugin to main directory <strong><em>rootProjectDir</em>/buildSrc/src/main/java. </strong>In this case, Gradle will take cera about compiling and testing the plugin and make it available on the classpath of the build script. Such a plugin will be visible to every build script, however it is still cannot be used outside the build which it is define in.</p><p><strong><em>Standalone project</em></strong></p><p>In this case we create a separate project for this plugin type. This project produced and publish JAR that will be available outside the build script and can be shared with others. Usually this JAR include some plugins or combine several related tasks classes into a single library. Or a combination of both.</p><p>In our case, we will choose a standalone project options.</p><h3>Customisation plugin</h3><p>So, let’s get started. In an already created project in which as a build script language KTS is used, in project tree select the display of all project files:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bgknhFMhk9uTkDsW6P2JAg.png" /></figure><p>Select an our project directory and create a new directory called <strong>InternalPlugin :</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qxffQj4-rJ8OhsMGJZKq8A.png" /></figure><p>In directory <em>InternalPlugin</em> create build scritp <em>build.gradle.kts</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0xQ6kp69XTP_avoClLxZjg.png" /></figure><p>In this build script we define the java-gradle-plugin and kotlin-dslplugins. The <em>java-gradle-plugin</em><strong> </strong>plugin is a helper for development Gradle plugin. It automatically applies the Java Library ( java — library) plugin, adding <em>gradleApi()</em> dependency to the API configuration and performs plugin metadata validations during JAR task execution. Plugin <em>kotlin-dsl</em><strong> </strong>defines that our build script will use DSL language. We also define a mavenCentral()repository from which the define plugins will be loaded:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EPo5tDetrKhdV813mEYiiQ.png" /></figure><p>Now, using gradlePluginblock, which define the plugins are created by project, let’s describe a scenario of plugin creation. In our case, we will only need a plugin name, its ID and a class that will implement the Plugin&lt;Project&gt;<strong> </strong>interface:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*huy40uq7u4MFzE5Qaf8_IA.png" /></figure><h4><strong>The plugin ID must meet the following criteria:</strong></h4><ul><li>May contain any alphanumeric character, ‘.’, and ‘-’.</li><li>Must contain at least one ‘.’ character separating the namespace from the name of the plugin.</li><li>Conventionally use a lowercase reverse domain name convention for the namespace.</li><li>Conventionally use only lowercase characters in the name.</li><li><strong><em>org.gradle</em></strong> and <strong><em>com.gradleware</em></strong> namespaces may NOT BE USED.</li><li>Cannot start or end with a ‘.’ character.</li><li>Cannot contain consecutive ‘.’ characters (i.e. ‘..’).</li></ul><p>Let’s choose <em>buildPlugin</em><strong> </strong>as a name, an ID will be the same as the name and in the implementationClassfield we will specify the path to the class that implements the <em>Plugin&lt;Project&gt;</em><strong> </strong>interface. For now this path is only specified, after include this plugin to our build it will be converted into a separate module, and we will be able to create this class. To match the namespace in the main project, the path to implementationClass<strong> </strong>will look like this com.example.buildPlugin.BuildPlugin .</p><p>Now we need to include the created project with our plugin in the build script of the source project. To do this, add includeBuild(&quot;InternalPlugin&quot;) in the <em>settings.gradle.kts</em><strong> </strong>file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HW8JbmqOxS8CtV-p7hv8Ew.png" /></figure><p>Synchronise all Gradle files and see that our new <strong>InternalPlugin</strong> directory is now defined as a separate module. It now contains two directories <strong>main</strong> and <strong>test:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OTjAutSzJYfNkVot-5V6QQ.png" /></figure><p>Go to the project tree and select to display all project directories. In <em>InternalPlugin</em><strong> </strong>directory, create a new directory that we declared for implementationClass<strong>. </strong>The path to the new directory will look like src/main/java/com.example.buildPlugin:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ts9YMuhmGcAa1X0PIozj4Q.png" /></figure><p>In this directory, create the <em>BuildPlugin</em> class that implements the <em>Plugin&lt;Project &gt;</em> interface. In the overridden method apply(target: Project) delete the TODO(Not yet implemented)<em>line</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YzsSGI0EH0iMBLXEisFbCQ.png" /></figure><h3>Description of dependencies and their versions</h3><p>Let’s create the <strong><em>Config</em> </strong>class that describes the SDK versions, code version and version name:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fnFS8KEjJ5mE9Yg7I4fJFA.png" /></figure><p>Let’s create the Config<strong> </strong>class that contains the versions of the pluggable dependencies:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9uvpwojV6anpOvD9jiJ_Wg.png" /></figure><p>Now, let’s create <strong><em>dependencies</em> </strong>directory containing all dependencies:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Xy1oA0BDAP1Cycf94R5JdA.png" /></figure><p>Let’s create classes for the dependencies we need (Compose, DI, Retrofit, Room, Test и Other):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v8vH638FhlzXMesV_QFomg.png" /><figcaption>Dependencies for Compose</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LPAATiOGGwDkaZakLJvUMg.png" /><figcaption>Dependencies for DI</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4pdgDsOh9KGnZnxilym--w.png" /><figcaption>Dependencies for Retrofit</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VDISEpE1DP73lY2ZbRP2Ug.png" /><figcaption>Dependencies for Room</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fx-5JTD4Bq512yWcmK1EXA.png" /><figcaption>Dependencies for Tests</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bu5qIkKiqRA3KjTjw0PnlA.png" /><figcaption>Other required dependencies</figcaption></figure><p>For convenient access to all created dependency classes let’s create a common <em>Dependencies</em> class outside the directory with dependencies <em>dependencies</em>, but in the same directory where <strong><em>BuildPlugin</em></strong>, <strong><em>Versions</em></strong> and <strong><em>Config</em> </strong>classes are located:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LcdvZj1FHiHSUhoiRPnYJw.png" /></figure><h3>Modifying the application level build.gradle.kts file</h3><p>For easy of use, let’s create aliases for <em>Dependencies</em> and <em>Config</em><strong> </strong>classes and put them on top of the file. Connect our plugin using the created <strong><em>ID</em> </strong>in<strong> </strong><em>build.gradle.kts</em><strong> </strong>of new our project. Let’s place the values of SDK versions, code version and version name with the values from the <em>Config</em> class. And don’t forget to connect the <em>Kapt</em> annotation processor:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4Z-Ygf4tLuuu1F1k4bdA3g.png" /></figure><p>Let’s move on to dependencies block. In order to implement all dependencies we need to pass each property to <em>implementation</em> function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6iltfP_nS-9fDc9DfTmKpw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jzoP3NxhlcgmAVwa4U7E0w.png" /></figure><p>After that we synchronise the project. If everything is done correctly, the synchronisation will be successful and we will get a ready project with an additional module where all dependencies are managed.</p><blockquote>If you found article useful then please start following me. You can also find me<em> </em>on <a href="https://medium.com/@norphyra"><strong>Medium</strong></a><strong> </strong>or <a href="http://www.linkedin.com/in/norphyra"><strong>LinkedIn</strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d507c0e94b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>