<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Android on Haseeb Majid</title>
    <link>https://haseebmajid.dev/tags/android/</link>
    <description>Recent content in Android on Haseeb Majid</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Sat, 02 Mar 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://haseebmajid.dev/tags/android/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>TIL: How to Run Android Emulator Using avd on Hyprland</title>
      <link>https://haseebmajid.dev/posts/2024-03-02-til-how-to-run-android-emulator-using-avd-on-hyprland/</link>
      <pubDate>Sat, 02 Mar 2024 00:00:00 +0000</pubDate>
      <author>Me</author>
      <guid>https://haseebmajid.dev/posts/2024-03-02-til-how-to-run-android-emulator-using-avd-on-hyprland/</guid>
      <description>&lt;p&gt;&lt;strong&gt;TIL: How to Run Android Emulator Using avd on Hyprland&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Recently, I was having issues when trying to run an Android Emulator using Android Studio (avd). When I would try to
run an Emulator from the device manager, I would get the following &lt;code&gt;The emulator process for avd xxx has terminated error&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Looking deeper into the logs at &lt;code&gt;~/.local/cache/Google/AndroidStudio2023.1/log/idea.log&lt;/code&gt;, I noticed the following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:45,859 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781674&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Android emulator version 33.1.24.0 &lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;build_id 11237101&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;CL:N/A&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:45,859 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781674&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Found systemPath /home/haseebmajid/Android/Sdk/system-images/android-34/google_apis_playstore/x86_64/
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,037 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781852&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   WARN - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Failed to process .ini file /home/haseebmajid/.config/.android/avd/../avd/Pixel_7_Pro_API_34.avd/quickbootChoice.ini &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; reading.
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,053 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781868&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Storing crashdata in: , detection is enabled &lt;span class=&#34;k&#34;&gt;for&lt;/span&gt; process: &lt;span class=&#34;m&#34;&gt;194632&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,054 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781869&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,054 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781869&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Duplicate loglines will be removed, &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; you wish to see each individual line launch with the -log-nofilter flag.
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,054 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781869&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,054 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781869&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Increasing RAM size to 3072MB
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,054 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781869&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Available platform plugins are: xcb.
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,054 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781869&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Warning: Could not find the Qt platform plugin &lt;span class=&#34;s2&#34;&gt;&amp;#34;wayland&amp;#34;&lt;/span&gt; in &lt;span class=&#34;s2&#34;&gt;&amp;#34;/home/haseebmajid/Android/Sdk/emulator/lib64/qt/plugins&amp;#34;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;((&lt;/span&gt;null&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;:0, &lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;null&lt;span class=&#34;o&#34;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,054 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781869&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - &lt;span class=&#34;o&#34;&gt;((&lt;/span&gt;null&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;:0, &lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;null&lt;span class=&#34;o&#34;&gt;))&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;2024-02-20 12:01:46,055 &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt; 781870&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;   INFO - Emulator: Pixel &lt;span class=&#34;m&#34;&gt;7&lt;/span&gt; Pro API &lt;span class=&#34;m&#34;&gt;34&lt;/span&gt; - Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note, I did have qtwayland5 installed on my Ubuntu machine.
So get it working based on this log line &lt;code&gt;Available platform plugins are: xcb.&lt;/code&gt; The simplest thing I could think of was
to run&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p><strong>TIL: How to Run Android Emulator Using avd on Hyprland</strong></p>
<p>Recently, I was having issues when trying to run an Android Emulator using Android Studio (avd). When I would try to
run an Emulator from the device manager, I would get the following <code>The emulator process for avd xxx has terminated error</code>.</p>
<p>Looking deeper into the logs at <code>~/.local/cache/Google/AndroidStudio2023.1/log/idea.log</code>, I noticed the following:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">2024-02-20 12:01:45,859 <span class="o">[</span> 781674<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Android emulator version 33.1.24.0 <span class="o">(</span>build_id 11237101<span class="o">)</span> <span class="o">(</span>CL:N/A<span class="o">)</span>
</span></span><span class="line"><span class="cl">2024-02-20 12:01:45,859 <span class="o">[</span> 781674<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Found systemPath /home/haseebmajid/Android/Sdk/system-images/android-34/google_apis_playstore/x86_64/
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,037 <span class="o">[</span> 781852<span class="o">]</span>   WARN - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Failed to process .ini file /home/haseebmajid/.config/.android/avd/../avd/Pixel_7_Pro_API_34.avd/quickbootChoice.ini <span class="k">for</span> reading.
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,053 <span class="o">[</span> 781868<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Storing crashdata in: , detection is enabled <span class="k">for</span> process: <span class="m">194632</span>
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,054 <span class="o">[</span> 781869<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,054 <span class="o">[</span> 781869<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Duplicate loglines will be removed, <span class="k">if</span> you wish to see each individual line launch with the -log-nofilter flag.
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,054 <span class="o">[</span> 781869<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - 
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,054 <span class="o">[</span> 781869<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Increasing RAM size to 3072MB
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,054 <span class="o">[</span> 781869<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Available platform plugins are: xcb.
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,054 <span class="o">[</span> 781869<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Warning: Could not find the Qt platform plugin <span class="s2">&#34;wayland&#34;</span> in <span class="s2">&#34;/home/haseebmajid/Android/Sdk/emulator/lib64/qt/plugins&#34;</span> <span class="o">((</span>null<span class="o">)</span>:0, <span class="o">(</span>null<span class="o">))</span>
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,054 <span class="o">[</span> 781869<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - <span class="o">((</span>null<span class="o">)</span>:0, <span class="o">(</span>null<span class="o">))</span>
</span></span><span class="line"><span class="cl">2024-02-20 12:01:46,055 <span class="o">[</span> 781870<span class="o">]</span>   INFO - Emulator: Pixel <span class="m">7</span> Pro API <span class="m">34</span> - Fatal: This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
</span></span></code></pre></div><p>Note, I did have qtwayland5 installed on my Ubuntu machine.
So get it working based on this log line <code>Available platform plugins are: xcb.</code> The simplest thing I could think of was
to run</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nv">QT_QPA_PLATFORM</span><span class="o">=</span>xcb ~/Android/Sdk/emulator/emulator -avd Pixel_XL_API_33
</span></span></code></pre></div><p>Forcing it to use the platform it can find. I don&rsquo;t need to do much Android development, so this was fine for now.
For a better solution, you can look at this <a href="https://github.com/hyprwm/Hyprland/issues/3417">issue</a>, to add both
in your Hyprland config itself.</p>
]]></content:encoded>
    </item>
    
    <item>
      <title>How to Remote Debug your SvelteKit on an Android Device in Firefox</title>
      <link>https://haseebmajid.dev/posts/2022-12-30-how-to-remote-debug-your-sveltekit-on-an-android-device-in-firefox/</link>
      <pubDate>Fri, 30 Dec 2022 00:00:00 +0000</pubDate>
      <author>Me</author>
      <guid>https://haseebmajid.dev/posts/2022-12-30-how-to-remote-debug-your-sveltekit-on-an-android-device-in-firefox/</guid>
      <description>&lt;p&gt;In this post, we will go over how you can test your SvelteKit app on your Android device in Firefox.
If you&amp;rsquo;re like me you are building an app that you want users to use on both normal PCs (laptops, Desktop) and
their smartphones.&lt;/p&gt;
&lt;p&gt;But the question arises how can we debug our app on a smartphone?
One way to achieve this is to use Firefox and remote debugging, you will need to connect your
smartphone via USB to your device running the SvelteKit app i.e. your Laptop.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>In this post, we will go over how you can test your SvelteKit app on your Android device in Firefox.
If you&rsquo;re like me you are building an app that you want users to use on both normal PCs (laptops, Desktop) and
their smartphones.</p>
<p>But the question arises how can we debug our app on a smartphone?
One way to achieve this is to use Firefox and remote debugging, you will need to connect your
smartphone via USB to your device running the SvelteKit app i.e. your Laptop.</p>
<h2 id="smartphone">SmartPhone</h2>
<p>On your phone make sure you have developer mode on and have switched on USB debugging <sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup>.
Open up Firefox, then click on the 3 dots to open up the context menu go to <code>Settings</code>,
scroll down and check <code>Remote debugging via USB</code> to &ldquo;on&rdquo;.</p>
<p><img
        loading="lazy"
        src="/posts/2022-12-30-how-to-remote-debug-your-sveltekit-on-an-android-device-in-firefox/images/firefox_usb_debugging.jpeg"
        type=""
        alt="Firefox USB Debugging"
        
      /></p>
<p>Next, connect your smartphone to your PC via USB.</p>
<h2 id="pc">PC</h2>
<p>On your PC start up your SvelteKit application for example <code>npm run dev -- --host --port 5173 --open</code>.
Where <code>dev</code> is <code>vite dev</code> in my <code>package.json</code>. This will start the app but <code>--host</code> will mean it is accessible
to devices on the same private network:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">&gt; bookmarkey-gui@0.0.1 dev
</span></span><span class="line"><span class="cl">&gt; vite dev --host --port <span class="m">5173</span> --open
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  VITE v4.0.1  ready in <span class="m">701</span> ms
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  ➜  Local:   http://localhost:5173/
</span></span><span class="line"><span class="cl">  ➜  Network: http://192.168.0.26:5173/
</span></span><span class="line"><span class="cl">  ➜  Network: http://10.121.2.243:5173/
</span></span><span class="line"><span class="cl">  ➜  Network: http://172.24.0.1:5173/
</span></span><span class="line"><span class="cl">  ➜  press h to show <span class="nb">help</span>
</span></span></code></pre></div><p>This means we can connect to our PC on <code>http://192.168.0.26:5173/</code> from our smartphone and access the SvelteKit site.
You may want to set up your firewall such that only your smartphone can connect to your PC. In my case, since I am only
developing at home I have allowed all traffic on port 5173.</p>
<p><img
        loading="lazy"
        src="/posts/2022-12-30-how-to-remote-debug-your-sveltekit-on-an-android-device-in-firefox/images/ufw_rules.png"
        type=""
        alt="Firewall Rules"
        
      /></p>
<details
  class="notice warning"
  open="true"
>
    <summary class="notice-title">Firewall</summary>
  
  Make sure you know what you are doing when playing around with your firewall rules!
You don&rsquo;t want to allow more access than is necessary. Especially if you working in say
a coffee shop on public WIFI.
</details>

<h3 id="firefox">Firefox</h3>
<p></p>
<p>Now on your PC still:</p>
<ul>
<li>Open Firefox</li>
<li>Go to <code>about:debugging</code></li>
<li>You should see your device under <code>USB enabled</code>.</li>
<li>Select connect</li>
<li>Click on your phone</li>
<li>Then select your SvelteKit app to inspect</li>
</ul>
<p>
<img
        loading="lazy"
        src="/posts/2022-12-30-how-to-remote-debug-your-sveltekit-on-an-android-device-in-firefox/images/firefox_inspect.png"
        type=""
        alt="Firefox Inspect"
        
      />
<img
        loading="lazy"
        src="/posts/2022-12-30-how-to-remote-debug-your-sveltekit-on-an-android-device-in-firefox/images/firefox_console.png"
        type=""
        alt="Firefox Console"
        
      /></p>
<details
  class="notice warning"
  open="true"
>
    <summary class="notice-title">Cannot See Smartphone</summary>
  
  If you don&rsquo;t see your device it may be because your USB preferences are set to only charge your device.
On my phone, I have to change it to <code>File Transfer/Android Auto</code>. Then Firefox can see the phone.
</details>

<p></p>
<div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>How to setup USB debugging, <a href="https://www.youtube.com/watch?v=nYoH5B2GXXY">https://www.youtube.com/watch?v=nYoH5B2GXXY</a>&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>
]]></content:encoded>
    </item>
    
    <item>
      <title>Add adaptive icons to your Android app</title>
      <link>https://haseebmajid.dev/posts/2019-11-09-add-adaptive-icons-to-your-android-app/</link>
      <pubDate>Sat, 09 Nov 2019 00:00:00 +0000</pubDate>
      <author>Me</author>
      <guid>https://haseebmajid.dev/posts/2019-11-09-add-adaptive-icons-to-your-android-app/</guid>
      <description>&lt;p&gt;In this article, we will go over how you can use add the new adaptive app icons to your Android app.
In his article I will be using a React Native project, so the structure of your Android app may vary.&lt;/p&gt;
&lt;p&gt;&lt;img
    loading=&#34;lazy&#34;
    src=&#34;https://developer.android.com/guide/practices/ui_guidelines/images/NB_Icon_Mask_Shapes_Ext_02.gif&#34;
    alt=&#34;Mask&#34;
    
  /&gt;&lt;/p&gt;
&lt;h2 id=&#34;adaptive-icons&#34;&gt;Adaptive Icons&lt;/h2&gt;
&lt;p&gt;Adaptive icons are a new feature introduced in Android 8.0 (API level 26). It allows your app icon
to be displayed using multiple shapes across different devices and launchers, more
information available &lt;a href=&#34;https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive&#34;&gt;here&lt;/a&gt;.
The main advantage of using adaptive icons is so that we don&amp;rsquo;t have to have to create multiple icons, i.e. round and square
versions of your app icon. You provide two layers, a foreground and the background which is usually just a colour.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>In this article, we will go over how you can use add the new adaptive app icons to your Android app.
In his article I will be using a React Native project, so the structure of your Android app may vary.</p>
<p><img
    loading="lazy"
    src="https://developer.android.com/guide/practices/ui_guidelines/images/NB_Icon_Mask_Shapes_Ext_02.gif"
    alt="Mask"
    
  /></p>
<h2 id="adaptive-icons">Adaptive Icons</h2>
<p>Adaptive icons are a new feature introduced in Android 8.0 (API level 26). It allows your app icon
to be displayed using multiple shapes across different devices and launchers, more
information available <a href="https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive">here</a>.
The main advantage of using adaptive icons is so that we don&rsquo;t have to have to create multiple icons, i.e. round and square
versions of your app icon. You provide two layers, a foreground and the background which is usually just a colour.</p>
<h3 id="android-studio">Android Studio</h3>
<p>To add adaptive icons to your application do the following;</p>
<ul>
<li>Open your application in Android Studio (If it&rsquo;s a React Native application make sure to open the <code>android</code> folder in Android Studio)</li>
<li>Right-click on the <code>app</code> folder</li>
<li>File &gt; New &gt; Image Asset</li>
<li>On the first tab &ldquo;Foreground Layer&rdquo; click on <code>path</code> and select your icon (resize the icon as required)</li>
<li>Select the second tab &ldquo;Background Layer&rdquo; select <code>Asset Type: Color</code> and select the colour you would like to use</li>
<li>Select <code>Next</code> (bottom right)</li>
<li>Select <code>Finish</code></li>
</ul>
<p><img
        loading="lazy"
        src="/posts/2019-11-09-add-adaptive-icons-to-your-android-app/images/android-studio.gif"
        type=""
        alt="Android Studio"
        
      /></p>
<p>You can find the new files in <code>android/app/src/main/res</code> sub-folders</p>
<h3 id="test">Test</h3>
<p>You can test it works, if you&rsquo;re using the example source code found below.</p>
<p>Run the following commands</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">adb connect xxx.xx.xxx.xx <span class="c1"># IP Address of your emulator</span>
</span></span><span class="line"><span class="cl">yarn
</span></span><span class="line"><span class="cl">yarn run start
</span></span><span class="line"><span class="cl">yarn run android <span class="c1"># In another terminal</span>
</span></span></code></pre></div><p>Then you should see something like the image below instead of the default green Android robot.</p>
<p><img
        loading="lazy"
        src="/posts/2019-11-09-add-adaptive-icons-to-your-android-app/images/cover.png"
        type=""
        alt="New Icon"
        
      /></p>
<h2 id="appendix">Appendix</h2>
<ul>
<li><a href="https://gitlab.com/hmajid2301/blog/-/tree/main/content/posts/2019-11-09-add-adaptive-icons-to-your-android-app/source_code">Example source code</a></li>
<li><a href="https://romannurik.github.io/AndroidAssetStudio/index.html">Android Asset Studio</a></li>
<li><a href="https://www.uplabs.com/posts/adaptive-icons-for-android-o">Cover Photo from UpLabs</a></li>
</ul>
]]></content:encoded>
    </item>
    
    <item>
      <title>Auto Publish React Native App to Android Play Store using GitLab CI</title>
      <link>https://haseebmajid.dev/posts/2019-10-09-auto-publish-react-native-app-to-android-play-store-using-gitlab-ci/</link>
      <pubDate>Wed, 09 Oct 2019 00:00:00 +0000</pubDate>
      <author>Me</author>
      <guid>https://haseebmajid.dev/posts/2019-10-09-auto-publish-react-native-app-to-android-play-store-using-gitlab-ci/</guid>
      <description>&lt;p&gt;In this article, I will show you how can automate the publishing of your AAB/APK to the &lt;code&gt;Google Play Console&lt;/code&gt;.
We will be using the &lt;a href=&#34;https://github.com/Triple-T/gradle-play-publisher&#34;&gt;Gradle Play Publisher&lt;/a&gt; (GPP) plugin to do
automate this process for us. Using this plugin we cannot only automate the publishing and release of our app,
we can also update the release notes, store listing (including photos) all from GitLab CI.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; In this article I will assume that you are using Linux and React Native version &amp;gt;= 0.60.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>In this article, I will show you how can automate the publishing of your AAB/APK to the <code>Google Play Console</code>.
We will be using the <a href="https://github.com/Triple-T/gradle-play-publisher">Gradle Play Publisher</a> (GPP) plugin to do
automate this process for us. Using this plugin we cannot only automate the publishing and release of our app,
we can also update the release notes, store listing (including photos) all from GitLab CI.</p>
<p><strong>Note:</strong> In this article I will assume that you are using Linux and React Native version &gt;= 0.60.</p>
<h2 id="prerequisites">Prerequisites</h2>
<ul>
<li><a href="https://play.google.com/apps/publish/signup/#EnterDetailsPlace">Google Developers Account</a></li>
<li>A working React Native Android project.</li>
</ul>
<h2 id="google-play-console">Google Play Console</h2>
<p>First, we need to create a service account, this account will be used to make changes to our app
automatically, such as publish it or change the store listing.</p>
<ul>
<li>Go to the <a href="https://play.google.com/apps/publish">Google Play Console</a></li>
<li>Go to &ldquo;Settings&rdquo;</li>
<li>Go to &ldquo;Developer Account&rdquo;</li>
<li>Select &ldquo;API access&rdquo;</li>
<li>Click the &ldquo;Create Service Account&rdquo; button</li>
<li>Click the &ldquo;Google API Console&rdquo; button, this will take you to the <code>Google Cloud Platform</code></li>
<li>Click &ldquo;Create Service Account&rdquo;, give your service account a <code>name</code> and a <code>description</code></li>
<li>Select the <code>Role</code> as <code>Owner</code>, select the &ldquo;Continue&rdquo; button and then finally select &ldquo;Done&rdquo;</li>
<li>After creating your service account you should have a file automatically downloading <code>api&lt;...&gt;.json</code>, if not click on &ldquo;Actions (3 Dots) &gt; Create Key&rdquo;, as shown in <code>Image 1</code></li>
<li>Now go back to &ldquo;Play Console&rdquo;</li>
<li>Go to &ldquo;Service Accounts&rdquo; (on the current page), and select &ldquo;Grant Access&rdquo;</li>
<li>Next set the permission as shown in <code>Image 2</code></li>
</ul>
<p>If you would like, you can set the permissions either globally so you can use this account for all your apps,
or you can specify an app so that you will create a new account each time you want to auto-publish a new app.
Each approach has its own advantages, the first approach is more convenient. However, the second approach is
more safe, for example, if your credentials are leaked, only one of your apps is affected.</p>
<p><img
        loading="lazy"
        src="/posts/2019-10-09-auto-publish-react-native-app-to-android-play-store-using-gitlab-ci/images/json-key.png"
        type=""
        alt="Image 1: Permissions"
        
      /></p>
<p><img
        loading="lazy"
        src="/posts/2019-10-09-auto-publish-react-native-app-to-android-play-store-using-gitlab-ci/images/permissions.png"
        type=""
        alt="Image 2: Permissions"
        
      /></p>
<h2 id="gitlab">GitLab</h2>
<p>Now we need the JSON file we just downloaded to be accessible by our GitLab CI jobs, where the JSON file should
look something like this;</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;type&#34;</span><span class="p">:</span> <span class="s2">&#34;service_account&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;project_id&#34;</span><span class="p">:</span> <span class="s2">&#34;xxx&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;private_key_id&#34;</span><span class="p">:</span> <span class="s2">&#34;xxx&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;private_key&#34;</span><span class="p">:</span> <span class="s2">&#34;xxx&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;client_email&#34;</span><span class="p">:</span> <span class="s2">&#34;xxx&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;client_id&#34;</span><span class="p">:</span> <span class="s2">&#34;xxx&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;auth_uri&#34;</span><span class="p">:</span> <span class="s2">&#34;https://accounts.google.com/o/oauth2/auth&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;token_uri&#34;</span><span class="p">:</span> <span class="s2">&#34;https://oauth2.googleapis.com/token&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;auth_provider_x509_cert_url&#34;</span><span class="p">:</span> <span class="s2">&#34;https://www.googleapis.com/oauth2/v1/certs&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;client_x509_cert_url&#34;</span><span class="p">:</span> <span class="s2">&#34;xxx&#34;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>Now, let&rsquo;s move the relevant keystore information to GitLab CI variables, so we can access them during our CI jobs.
First, go to your GitLab project;</p>
<ul>
<li>Settings &gt; CI/CD &gt; Variables</li>
<li>Add Type: File, Key: PLAY_STORE_JSON, Value: (the contents of your JSON file)</li>
</ul>
<p><strong>Note:</strong> When I&rsquo;m testing locally sometimes I store this JSON file locally, so I include it in my <code>.gitignore</code> (I call mine <code>play-store.json</code>) file
so it doesn&rsquo;t accidentally get published online.</p>
<h2 id="buildgradle">build.gradle</h2>
<p>We need to edit the <code>android/build.gradle</code> file first. The Maven URL is a place where we can download
the GPP plugin. The dependencies list the plugins we want to download and their versions.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-groovy" data-lang="groovy"><span class="line"><span class="cl"><span class="n">buildscript</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="o">...</span>
</span></span><span class="line"><span class="cl">    <span class="n">repositories</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="o">...</span>
</span></span><span class="line"><span class="cl">        <span class="n">maven</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">url</span> <span class="s2">&#34;https://plugins.gradle.org/m2/&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="n">dependencies</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">classpath</span> <span class="s2">&#34;com.android.tools.build:gradle:3.5.0&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="n">classpath</span> <span class="s2">&#34;com.github.triplet.gradle:play-publisher:2.4.1&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">...</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></div><h3 id="gradle-wrapperproperties">gradle-wrapper.properties</h3>
<p>To use GPP version <code>2.4.1</code> we need to use gradle version &gt;= 5.6.1. To do this we open
<code>android/gradle/wrapper/gradle-wrapper.properties</code> and edit the line <code>distributionUrl</code> so that the gradle version match
the required version for example <code>distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.1-all.zip</code>.</p>
<h2 id="appbuildgradle">app/build.gradle</h2>
<p>We also need to edit our <code>android/app/build.gradle</code>.
Add the <code>apply plugin: &quot;com.github.triplet.play&quot;</code> to the top of your file after <code>apply plugin: &quot;com.android.application&quot;</code>.
Then at the bottom of the file add the code below. This is what the GPP plugin uses to determine how to publish your app.
In this case, it will</p>
<ul>
<li>defaultToAppBundles: It will generate an AAB (Android App Bundle) instead of APK</li>
<li>track: Which track we should deploy the new AAB to, you can chose for example production or beta, in this case it&rsquo;s the internal track</li>
<li>serviceAccountCredentials: The location of the Play Store JSON we stored in our CI/CD variables in this example it will be kept in <code>android/app/play-store.json</code></li>
<li>resolutionStrategy: Automatically updates the app&rsquo;s version code to matches the next available number required so it&rsquo;s published</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-groovy" data-lang="groovy"><span class="line"><span class="cl"><span class="n">play</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">defaultToAppBundles</span> <span class="o">=</span> <span class="kc">true</span>
</span></span><span class="line"><span class="cl">    <span class="n">track</span> <span class="o">=</span> <span class="s2">&#34;internal&#34;</span> <span class="c1">// beta, alpha, production.
</span></span></span><span class="line"><span class="cl">    <span class="n">serviceAccountCredentials</span> <span class="o">=</span> <span class="n">file</span><span class="o">(</span><span class="s2">&#34;./play-store.json&#34;</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">    <span class="n">resolutionStrategy</span> <span class="o">=</span> <span class="s2">&#34;auto&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="n">outputProcessor</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">versionNameOverride</span> <span class="o">=</span> <span class="s2">&#34;$versionNameOverride.$versionCode&#34;</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></div><p>There are numerous more options you can enable/use, such as generating a draft which you can manually publish yourself from the Play store console.
You can find more details <a href="https://github.com/Triple-T/gradle-play-publisher">here</a>, in the GPPs very well documented README.</p>
<h2 id="meta-data">Meta Data</h2>
<p>You can also use the GPP to publish meta-data about our app such as the listing, photos etc. To do this you can either
run <code>cd android &amp;&amp; ./gradlew bootstrap</code> which will initialise the directory layout you need and also pull the
information from the play store if you already have it. If you do not want to run this command, you can create a
structure similar to the one shown below in your <code>android/app/src/main/play/</code> folder. You can find more information
about meta-data <a href="https://github.com/Triple-T/gradle-play-publisher#managing-play-store-metadata">here</a>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">├── contact-email.txt
</span></span><span class="line"><span class="cl">├── contact-website.txt
</span></span><span class="line"><span class="cl">├── default-language.txt
</span></span><span class="line"><span class="cl">├── listings
</span></span><span class="line"><span class="cl">│   └── en-GB
</span></span><span class="line"><span class="cl">│       ├── full-description.txt
</span></span><span class="line"><span class="cl">│       ├── graphics
</span></span><span class="line"><span class="cl">│       │   ├── feature-graphic
</span></span><span class="line"><span class="cl">│       │   │   └── 1.png
</span></span><span class="line"><span class="cl">│       │   ├── icon
</span></span><span class="line"><span class="cl">│       │   │   └── 1.png
</span></span><span class="line"><span class="cl">│       │   └── phone-screenshots
</span></span><span class="line"><span class="cl">│       │       ├── 1.png
</span></span><span class="line"><span class="cl">│       │       ├── 2.png
</span></span><span class="line"><span class="cl">│       │       ├── 3.png
</span></span><span class="line"><span class="cl">│       │       ├── 4.png
</span></span><span class="line"><span class="cl">│       │       ├── 5.png
</span></span><span class="line"><span class="cl">│       │       └── 6.png
</span></span><span class="line"><span class="cl">│       ├── short-description.txt
</span></span><span class="line"><span class="cl">│       ├── title.txt
</span></span><span class="line"><span class="cl">│       └── video-url.txt
</span></span><span class="line"><span class="cl">├── release-names
</span></span><span class="line"><span class="cl">│   └── default.txt
</span></span><span class="line"><span class="cl">└── release-notes
</span></span><span class="line"><span class="cl">    └── en-GB
</span></span><span class="line"><span class="cl">        └── default.txt
</span></span></code></pre></div><h2 id="packagejson">package.json</h2>
<p>Add the following script to your <code>package.json</code>, this will be used within our <code>.gitlab-ci.yml</code>. This is so that we can
simply use yarn run bundle, for example instead of having to write the whole command, in our GitLab CI. Also, the other
advantage is that the command is used multiple times in our GitLab CI jobs, we only have to edit in a single location. It also
saves us having to type out the same (very long) command again.</p>
<ul>
<li>bundle: Bundles all of our react native code into a single file</li>
<li>publish-package: Will build our AAB and also publish it along with all the meta-data we include</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// ...
</span></span></span><span class="line"><span class="cl">  <span class="nt">&#34;scripts&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;bundle&#34;</span><span class="p">:</span> <span class="s2">&#34;react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.bundle --sourcemap-output android/app/src/main/assets/index.map --assets-dest android/app/src/main/res&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;publish-package&#34;</span><span class="p">:</span> <span class="s2">&#34;cd android &amp;&amp; ./gradlew publishRelease&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// ...
</span></span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl">  <span class="c1">// ...
</span></span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><h2 id="gitlab-ciyml">.gitlab-ci.yml</h2>
<p>Now finally we can add our job to our GitLab CI file.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">stages</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">publish</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="nt">publish:android:package</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">stage</span><span class="p">:</span><span class="w"> </span><span class="l">publish</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">reactnativecommunity/react-native-android</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">script</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">echo fs.inotify.max_user_watches=524288 | tee -a /etc/sysctl.conf &amp;&amp; sysctl -p</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">mv $PLAY_STORE_JSON android/app/play-store.json</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">yarn bundle</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">yarn publish-package --no-daemon</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">artifacts</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="nt">paths</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span>- <span class="l">./android/app/build/outputs/</span><span class="w">
</span></span></span></code></pre></div><p>Let&rsquo;s break this file down line by line;
We need a Docker image which contains all the prerequisites for building our APK. A good one
to use is the <a href="https://hub.docker.com/r/reactnativecommunity/react-native-android">reactnativecommunity/react-native-android</a>.
It has everything we need for React Native/Android (Java, Android SDK etc).</p>
<p>Depending on your exact project, you may need to increase the inoitfy file watcher limit. You can do this by using
<code>echo fs.inotify.max_user_watches=524288 | tee -a /etc/sysctl.conf &amp;&amp; sysctl -p</code>. Essentially file watchers are used
to monitor changes in the file system. You can find more information about
Linux&rsquo;s inotify <a href="https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers">here</a>.</p>
<p><code>yarn bundle</code>: We create a bundle this is where all of our (JavaScript) React Native file are bundled into a single Javascript
file.</p>
<p><code>yarn android-publish --no-daemon</code>: This builds our AAB locally and then publish it to the <code>Android Play Store</code>, to the internal testing track in this case.
It will also publish all of our meta-data for us such as the photos, store listing, contact information and release notes (etc).
Since this is a CI job we don&rsquo;t need to start a daemon, to speed up future builds, hence the <code>--no-daemon</code> argument.</p>
<p>Finally we create artifacts <code>./android/app/build/outputs/</code> so that we can download the AAB after the job has been completed. I also like to include
my artifacts (AAB) with releases of my apps within GitLab. It makes it easier to track what version of the app was released when. But if you don&rsquo;t
want the AAB/APK you can simply remove the artifacts part from the job.</p>
<p>That&rsquo;s it, we&rsquo;re done :).</p>
<p><img
    loading="lazy"
    src="https://giphy.com/gifs/13Csc1WMn9sXSM/html5"
    alt="More automation is good right?"
    
  /></p>
<h2 id="appendix">Appendix</h2>
<ul>
<li><a href="https://gitlab.com/hmajid2301/stegappasaurus/tree/cde1afd6fbb9d882bccb9e05693824587ce1b77e">Example project</a></li>
<li><a href="https://github.com/Triple-T/gradle-play-publisher">Gradle Play Publisher</a></li>
</ul>
]]></content:encoded>
    </item>
    
    <item>
      <title>Using Gitlab CI to Publish an Android React Native App</title>
      <link>https://haseebmajid.dev/posts/2019-09-23-using-gitlab-ci-to-publish-an-android-react-native-app/</link>
      <pubDate>Mon, 23 Sep 2019 00:00:00 +0000</pubDate>
      <author>Me</author>
      <guid>https://haseebmajid.dev/posts/2019-09-23-using-gitlab-ci-to-publish-an-android-react-native-app/</guid>
      <description>&lt;p&gt;In this article I will show how you can use the GitLab CI with React Native to create a binary which can be published to the
Google Play Store.&lt;/p&gt;
&lt;h2 id=&#34;prerequisites&#34;&gt;Prerequisites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://play.google.com/apps/publish/signup/#EnterDetailsPlace&#34;&gt;Google Developers Account&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;A working React Native Android project&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;h2 id=&#34;keystore&#34;&gt;Keystore&lt;/h2&gt;
&lt;p&gt;First, we have to generate a keystore which we will use to sign our APK. To do this run the commands below,
follow all the instructions and keep the file safe.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>In this article I will show how you can use the GitLab CI with React Native to create a binary which can be published to the
Google Play Store.</p>
<h2 id="prerequisites">Prerequisites</h2>
<ul>
<li><a href="https://play.google.com/apps/publish/signup/#EnterDetailsPlace">Google Developers Account</a></li>
<li>A working React Native Android project</li>
</ul>
<hr>
<h2 id="keystore">Keystore</h2>
<p>First, we have to generate a keystore which we will use to sign our APK. To do this run the commands below,
follow all the instructions and keep the file safe.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1"># Used to generate our keystore</span>
</span></span><span class="line"><span class="cl">keytool -genkeypair -v -keystore my-key.keystore -alias my-key-alias -keyalg RSA -keysize <span class="m">2048</span> -validity <span class="m">10000</span>
</span></span><span class="line"><span class="cl"><span class="c1"># Used to encode our keystore in base64</span>
</span></span><span class="line"><span class="cl">base64 my-key.keystore &gt; base64-keystore.txt
</span></span></code></pre></div><h3 id="app-signing">App signing</h3>
<p>We no longer need to upload a certificate manually, as long as you &ldquo;Opt In&rdquo;, to let Google Sign your applications for you.
It will keep track of the key you used to upload your very first APK file, then it will expect you to sign all new releases
of that application using that same key, so don&rsquo;t lose your keystore. To allow Google to sign our app for us do the following;</p>
<ul>
<li>Login to <a href="https://play.google.com/apps/publish">Google Play Console</a></li>
<li>Select your application from the list</li>
<li>Select &ldquo;App Releases&rdquo;</li>
<li>Select a track, for example &ldquo;Internal test&rdquo; &gt; &ldquo;Manage&rdquo;</li>
<li>Select &ldquo;Create Release&rdquo;</li>
<li>Then where it says <code>Let Google manage and protect your app signing key (recommended)</code>, Select &ldquo;Continue&rdquo;</li>
</ul>
<p>The signing process works as follows;</p>
<ul>
<li>You digitally sign each release using your upload key (upload key being the keystore we just generated) before publishing it on the Play Console.</li>
<li>Google Play uses the upload certificate to verify your identity and then re-signs your release using the app signing key for distribution.</li>
</ul>
<h3 id="gitlab">GitLab</h3>
<p>Now, let&rsquo;s move on to the relevant keystore information to GitLab CI variables, so we can access them during our CI jobs.
First, go to your GitLab project;</p>
<ul>
<li>Settings (side menu) &gt; CI/CD &gt; Variables</li>
<li>Add Type: Variable, key: <code>ANDROID_KEYSTORE_ALIAS</code> , value: <code>my-key-alias</code></li>
<li>Add Type: Variable, key: <code>ANDROID_KEYSTORE_PASSWORD</code>, value: (whatever password you used)</li>
<li>Add Type: Variable, key: <code>ANDROID_KEYSTORE_KEY_PASSWORD</code>, value: (whatever password you used, by default it&rsquo;s the same as the <code>ANDROID_KEYSTORE_PASSWORD</code>)</li>
<li>Add Type: <code>File</code>, key: <code>ANDROID_KEYSTORE</code>, value: (copy the contents of <code>base64-keystore.txt</code>)</li>
</ul>
<p>Now we have all our keystore values/files on GitLab CI.</p>
<p><strong>Note</strong> Check the project permissions so only the relevant users can see/edit these values.
You should keep the keystore file/passwords private, make sure only the relevant users can access them.</p>
<hr>
<h2 id="appbuildgradle">app/build.gradle</h2>
<p>Next open the <code>android/app/build.gradle</code>, then add the following to the <code>android{}</code> section.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-groovy" data-lang="groovy"><span class="line"><span class="cl"><span class="n">android</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    <span class="o">...</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">signingConfigs</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">release</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="o">(</span><span class="n">project</span><span class="o">.</span><span class="na">hasProperty</span><span class="o">(</span><span class="s1">&#39;MYAPP_RELEASE_STORE_FILE&#39;</span><span class="o">))</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">storeFile</span> <span class="nf">file</span><span class="o">(</span><span class="n">MYAPP_RELEASE_STORE_FILE</span><span class="o">)</span>
</span></span><span class="line"><span class="cl">                <span class="n">storePassword</span> <span class="n">MYAPP_RELEASE_STORE_PASSWORD</span>
</span></span><span class="line"><span class="cl">                <span class="n">keyAlias</span> <span class="n">MYAPP_RELEASE_KEY_ALIAS</span>
</span></span><span class="line"><span class="cl">                <span class="n">keyPassword</span> <span class="n">MYAPP_RELEASE_KEY_PASSWORD</span>
</span></span><span class="line"><span class="cl">            <span class="o">}</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">buildTypes</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">release</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">minifyEnabled</span> <span class="n">enableProguardInReleaseBuilds</span>
</span></span><span class="line"><span class="cl">            <span class="n">proguardFiles</span> <span class="nf">getDefaultProguardFile</span><span class="o">(</span><span class="s2">&#34;proguard-android.txt&#34;</span><span class="o">),</span> <span class="s2">&#34;proguard-rules.pro&#34;</span>
</span></span><span class="line"><span class="cl">            <span class="n">signingConfig</span> <span class="n">signingConfigs</span><span class="o">.</span><span class="na">release</span>
</span></span><span class="line"><span class="cl">        <span class="o">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="o">...</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></div><p>All this does is generate a signed APK so we can upload it to the Android Play Store. It will use the
values from the keystore we just generated, to sign our application. We will create a <code>gradle.propeties</code>
file so we don&rsquo;t have to store our keystore values in plain-text within the <code>app/build.gradle</code>
(The <code>gradle.properties</code> file will be generated during our CI job, using the values we stored earlier on GitLab)</p>
<p><strong>Note</strong>: I have added my <code>gradle.properties</code> file to my <code>.gitignore</code> file so it doesn&rsquo;t accidentally get committed
when I am testing out the build process locally. I recommend you do the same.</p>
<hr>
<h2 id="packagejson">package.json</h2>
<p>Add the following three scripts to your <code>package.json</code> file. This is so that we can simply use <code>yarn run bundle</code>
for example instead of having to write out the whole command, in our GitLab CI. Also, the other advantage is
if the command is used multiple times in our GitLab CI jobs, we only have to edit in a single place.</p>
<ul>
<li>build-package: Builds our APK file</li>
<li>bundle: Bundles all of our react native code into a single file</li>
<li>generate-gradle-properties: Creates a gradle.properties file for us in the <code>android folder</code>.</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;scripts&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;android-package&#34;</span><span class="p">:</span> <span class="s2">&#34;cd android &amp;&amp; ./gradlew assembleRelease&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;bundle&#34;</span><span class="p">:</span> <span class="s2">&#34;react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.bundle --sourcemap-output android/app/src/main/assets/index.map --assets-dest android/app/src/main/res&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;generate-gradle-properties&#34;</span><span class="p">:</span> <span class="s2">&#34;sh generate-gradle-properties.sh &gt; android/gradle.properties&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="err">...</span>
</span></span><span class="line"><span class="cl">  <span class="p">}</span>
</span></span><span class="line"><span class="cl">  <span class="err">...</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>Where the <code>generate-gradle-propeties.sh</code> file looks something like, the code below.
The file is essentially a template file, where the <code>${variable}</code> are determined by
the environment variables set (the values we set earlier on GitLab). So, in this case,
the GitLab CI will pass in our keystore variables as environment variables and this
file will simply fill them in and will create our <code>gradle.properties</code> file.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="cp">#!/usr/bin/env bash
</span></span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">cat  <span class="s">&lt;&lt; EOF
</span></span></span><span class="line"><span class="cl"><span class="s">android.useAndroidX=true
</span></span></span><span class="line"><span class="cl"><span class="s">android.enableJetifier=true
</span></span></span><span class="line"><span class="cl"><span class="s">MYAPP_RELEASE_STORE_FILE=my-key.keystore
</span></span></span><span class="line"><span class="cl"><span class="s">MYAPP_RELEASE_STORE_PASSWORD=${ANDROID_KEYSTORE_PASSWORD}
</span></span></span><span class="line"><span class="cl"><span class="s">MYAPP_RELEASE_KEY_ALIAS=${ANDROID_KEYSTORE_ALIAS}
</span></span></span><span class="line"><span class="cl"><span class="s">MYAPP_RELEASE_KEY_PASSWORD=${ANDROID_KEYSTORE_KEY_PASSWORD}
</span></span></span><span class="line"><span class="cl"><span class="s">EOF</span>
</span></span></code></pre></div><hr>
<h2 id="gitlab-ciyml">.gitlab-ci.yml</h2>
<p>Add the following job to your <code>.gitlab-ci.yml</code> file, this job will create our APK.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-yaml" data-lang="yaml"><span class="line"><span class="cl"><span class="nt">stages</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span>- <span class="l">build</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="nn">---</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="nt">build:android:package</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">stage</span><span class="p">:</span><span class="w"> </span><span class="l">build</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">image</span><span class="p">:</span><span class="w"> </span><span class="l">reactnativecommunity/react-native-android</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">script</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">echo fs.inotify.max_user_watches=524288 | tee -a /etc/sysctl.conf &amp;&amp; sysctl -p</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">yarn install</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">base64 -d $ANDROID_KEYSTORE &gt; android/app/my-key.keystore</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">yarn generate-gradle-properties</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">yarn bundle</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span>- <span class="l">yarn android-package --no-daemon</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="nt">artifacts</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">    </span><span class="nt">paths</span><span class="p">:</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">      </span>- <span class="l">./android/app/build/outputs/</span><span class="w">
</span></span></span></code></pre></div><p>Let&rsquo;s break this job down line by line;
First we need a Docker image which contains all the prerequisites for building our APK. I think the
<a href="https://hub.docker.com/r/reactnativecommunity/react-native-android">reactnativecommunity/react-native-android</a>
has everything we need for our React Native/Android build (Java, Android SDK etc).</p>
<p>Depending on your exact project, you may need to increase the <code>inoitfy file watcher</code> limit, you can do this using
<code>echo fs.inotify.max_user_watches=524288 | tee -a /etc/sysctl.conf &amp;&amp; sysctl -p</code>. Essentially, file watchers are used
to monitor changes in the file system. You can find more information about
Linux&rsquo;s inotify <a href="https://github.com/guard/listen/wiki/Increasing-the-amount-of-inotify-watchers">here</a>.</p>
<p>We then install our project dependencies using <code>yarn install</code>.</p>
<p>We then decode the keystore file <code>base64 -d $ANDROID_KEYSTORE &gt; android/app/my-key.keystore</code> the file need to be saved in
<code>android/app</code> folder so it can be used during the building of the APK.</p>
<p>We then generate our <code>gradle.properties</code> file using <code>yarn generate-gradle-properties</code>, where we store variables required during the
build process such as the keystore passsword.</p>
<p>Then we run <code>yarn bundle</code>, which creates the bundle, where all of our (JavaScript) React Native file are bundled into a single Javascript
file.</p>
<p>Finally we run the command that will actually build our APK <code>yarn android-build-apk --no-daemon</code>. Since this is a <code>CI</code> job we
don&rsquo;t need to start a daemon, to speed up future builds hence the <code>--no-daemon</code> argument.</p>
<p>Also we make some build artifacts available so everything with in this folder, <code>./android/app/build/outputs/</code> can be accessed/downloaded
the APK after the job has completed, so we can then upload our APK manually.</p>
<h3 id="aab">AAB</h3>
<p>An AAB is the <a href="https://developer.android.com/guide/app-bundle">Android App Bundle</a>, which is now the recommended
way to upload our app to the Play Store. It has a few advantages over the APK, the main one being it usually
makes your app slimmer and takes fewer bytes on your users device&rsquo;s. Luckily for us the change in code required to
create an AAB instead of an AAB is very small.</p>
<p>All we have to do is open our <code>package.json</code> and edit the <code>android-package</code> script so that it contains the following
<code>&quot;android-package&quot;: &quot;cd android &amp;&amp; ./gradlew bundleRelease&quot;</code>. So essentially we change the Gradle target from
<code>assembleRelease</code> to <code>bundleRelease</code> and that it.</p>
<p><img
        loading="lazy"
        src="/posts/2019-09-23-using-gitlab-ci-to-publish-an-android-react-native-app/images/gitlab.gif"
        type=""
        alt="Example GitLab CI job"
        
      /></p>
<hr>
<h2 id="appendix">Appendix</h2>
<ul>
<li><a href="https://gitlab.com/hmajid2301/stegappasaurus/blob/4eb78b18e5677b9f6956750d50ea50725c4d099f">Example project</a></li>
</ul>
]]></content:encoded>
    </item>
    
    <item>
      <title>React Native/Expo with VirtualBox and Genymotion</title>
      <link>https://haseebmajid.dev/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/</link>
      <pubDate>Mon, 03 Sep 2018 00:00:00 +0000</pubDate>
      <author>Me</author>
      <guid>https://haseebmajid.dev/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/</guid>
      <description>&lt;p&gt;My home PC runs Windows for various conveniences, such as gaming. However, for development, I run an Ubuntu virtual
machine (VM) and Genymotion (on Windows) for testing my app. Genymotion also uses VirtualBox to run its Android
emulators. So we work out how to let two VMs running on the same host communicate with each other
(Ubuntu and Android Emulator).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Please Note:&lt;/strong&gt; This will also work for VMWare Player.&lt;/p&gt;
&lt;h2 id=&#34;solution&#34;&gt;Solution&lt;/h2&gt;
&lt;p&gt;There are a few networking options we can choose from when setting up a VM.&lt;/p&gt;</description>
      <content:encoded><![CDATA[<p>My home PC runs Windows for various conveniences, such as gaming. However, for development, I run an Ubuntu virtual
machine (VM) and Genymotion (on Windows) for testing my app. Genymotion also uses VirtualBox to run its Android
emulators. So we work out how to let two VMs running on the same host communicate with each other
(Ubuntu and Android Emulator).</p>
<p><strong>Please Note:</strong> This will also work for VMWare Player.</p>
<h2 id="solution">Solution</h2>
<p>There are a few networking options we can choose from when setting up a VM.</p>
<ul>
<li>
<p><strong>NAT</strong>: Allows your VMs so communicate with the outside world (outbound network), but your host machine (Windows)has no way to access this network</p>
</li>
<li>
<p><strong>Bridged</strong>: Allows your VMs to access a network using the host computer&rsquo;s Ethernet adapter. However this adds a bit of complexity, as your VM needs it own identity on the network as essentially its treated like another physical machine.</p>
</li>
<li>
<p><strong>Host-only</strong>: Acts a hybrid between the other two options. It allows your VM to communicate with your host machine. So essentially all we need for multiple VMs to communicate with each is other is to turn on Host-only network (on all VMs). This allow them to communicate with the host machine which will act as a &ldquo;bridge&rdquo; (no pun intended).</p>
</li>
</ul>
<p><img
        loading="lazy"
        src="/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/images/network.png"
        type=""
        alt="Network Diagaram"
        
      /></p>
<h2 id="prerequisites">Prerequisites</h2>
<ul>
<li><a href="https://www.virtualbox.org/wiki/Downloads">VirtualBox</a> Installed on the host machine</li>
<li>An <a href="https://docs.expo.io/versions/latest/workflow/create-react-native-app">Expo</a> app in the (Ubuntu) development VM</li>
<li><a href="https://www.genymotion.com/desktop/">Genymotion</a> Installed and setup on the host machine</li>
</ul>
<h2 id="virtualbox">VirtualBox</h2>
<ul>
<li>Open up VirtualBox</li>
<li>Click on the development VM and open the &ldquo;Settings&rdquo;.</li>
<li>Go to &ldquo;Network&rdquo; and Set the following two adapters, as shown in Figure 1</li>
<li>Adapter 1: &ldquo;Host-only Adapter&rdquo;</li>
<li>Adapter 2: &ldquo;NAT&rdquo;</li>
</ul>
<p>NAT allows the Ubuntu VM to use the host machines internet access, it allows it to communicate with the outside world.
The Host-only Adapter allows the Ubuntu VM to communicate with other VM, such as the Genymotion Android emulator.</p>
<p><img
        loading="lazy"
        src="/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/images/ubuntu_network1.png"
        type=""
        alt="Figure 1: Ubuntu VM network settings"
        
      />
<img
        loading="lazy"
        src="/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/images/ubuntu_network2.png"
        type=""
        alt="Figure 1: Ubuntu VM network settings"
        
      /></p>
<h2 id="genymotion">Genymotion</h2>
<p>Genymotion network settings are pretty much the same as the Ubuntu VM, as shown in Figure 2. Please note that the
Host-only adapters can be the same (i.e. #7).</p>
<p><img
        loading="lazy"
        src="/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/images/phone_network1.png"
        type=""
        alt="Figure 2: Android emulator VM network settings"
        
      />
<img
        loading="lazy"
        src="/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/images/phone_network2.png"
        type=""
        alt="Figure 2: Android emulator VM network settings"
        
      /></p>
<h2 id="expo">Expo</h2>
<ul>
<li>Start your Genymotion Android emulator.</li>
<li>Start your Ubuntu VM and open a terminal.</li>
<li>Replace the IP Address with your own one, you can find your IP address on the Android emulator. As shown in Figure 3.</li>
</ul>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">adb connect 192.168.112.101
</span></span><span class="line"><span class="cl">exp start
</span></span><span class="line"><span class="cl">exp android
</span></span></code></pre></div><p><img
        loading="lazy"
        src="/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/images/genymotion_ip.png"
        type=""
        alt="Figure 3: Genymotion Android emulator&rsquo;s IP address"
        
      /></p>
<p>That&rsquo;s it you should see your app running on the Genymotion Android emulator now. You can see how to start the
application below in Figure 4.</p>
<p><img
        loading="lazy"
        src="/posts/2018-09-03-react-native-expo-with-virtualbox-and-genymotion/images/connecting.gif"
        type=""
        alt="Figure 4: Starting Expo application on Android emulator"
        
      /></p>
<h2 id="appendix">Appendix</h2>
<ul>
<li>GIFs created with <a href="https://www.screentogif.com/">screentogif</a></li>
<li><a href="http://bertvv.github.io/notes-to-self/2015/09/29/virtualbox-networking-an-overview/">Detailed post about VirtualBox Networking</a></li>
<li>Drawing made with <a href="https://www.draw.io/">draw.io</a></li>
</ul>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
