<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Witalo Benicio</title>
    <description>The latest articles on DEV Community by Witalo Benicio (@witalobenicio).</description>
    <link>https://dev.to/witalobenicio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F306340%2F0ed43618-b3ab-494e-bdde-00dd5a05c012.png</url>
      <title>DEV Community: Witalo Benicio</title>
      <link>https://dev.to/witalobenicio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/witalobenicio"/>
    <language>en</language>
    <item>
      <title>Smaller and faster React Native apk</title>
      <dc:creator>Witalo Benicio</dc:creator>
      <pubDate>Fri, 24 Jul 2020 12:36:23 +0000</pubDate>
      <link>https://dev.to/witalobenicio/smaller-and-faster-react-native-apk-1een</link>
      <guid>https://dev.to/witalobenicio/smaller-and-faster-react-native-apk-1een</guid>
      <description>&lt;h2&gt;
  
  
  With simple (or not) steps
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Hi, everyone
&lt;/h3&gt;

&lt;p&gt;As you may know, Android devices are very different from each other, varying in storage, RAM size, CPU power, and so on. Based on that, we (as developers) always need to focus on a better performance of our application so it will work well on a low-end and high-end device.&lt;/p&gt;

&lt;p&gt;I work at a Brazilian Fintech called &lt;a href="https://meutudo.com.br"&gt;MEU TUDO&lt;/a&gt;. We offer a digital way to get payroll loans from multiple banks.&lt;br&gt;
From meutudo's point of view, we need to be even more careful about low-end devices, since our application is most used on those devices.&lt;/p&gt;

&lt;p&gt;This is not only about how good the experience will be to the final user; it directly impacts the decision to download the app or not.&lt;/p&gt;

&lt;p&gt;Let's take a look at our size before the improvement:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0isp7r8ieqtgrtuyxm1e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0isp7r8ieqtgrtuyxm1e.png" alt="Our application was more than 8Mb bigger than the peers" title="Our application was more than 8Mb bigger than the peers" width="762" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;***Our application was more than 8Mb bigger than the peers***&lt;/center&gt;

&lt;p&gt;And after we improve:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fww5mg82epdykhiwa3dzw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fww5mg82epdykhiwa3dzw.png" alt="We got a 11Mb reduction compared to peers" width="764" height="288"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;***We got a 11Mb reduction compared to peers***&lt;/center&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft2hzqxlvf4j3w7oddi81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft2hzqxlvf4j3w7oddi81.png" alt="Alt Text" width="284" height="109"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbxbpmi8ccnv821on78re.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbxbpmi8ccnv821on78re.png" alt="Alt Text" width="271" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;center&gt;***We went from 45Mb to 25Mb***&lt;/center&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Ok...but how can we reduce our APK size?&lt;/strong&gt;
&lt;/h3&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Newest React Native version&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The very first thing to do is upgrade your application to a recent version of React Native, at least &lt;code&gt;0.60.4&lt;/code&gt;.&lt;br&gt;
Why?&lt;br&gt;
Because they have introduced exciting features like Hermes (that I'll talk about later in this article), and auto-link (which doesn't directly impact size but impacts faster integrations), for iOS, the default way to get libs is with CocoaPods (which is very helpful), and so on. &lt;/p&gt;

&lt;p&gt;Considering the meutudo application, it was easier for our case to create a new project and migrate our code to the new project than trying to upgrade our actual project. So I've just made a fresh new React Native project, and started to add new versions of libs that we use, while trying to run them separately (to ensure that wouldn't crash after adding a bunch of them, and we would get lost), and after that, migrate all the code.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;App Bundle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;This is not such a new feature, but it greatly impacts the apk size.&lt;br&gt;
&lt;a href="https://developer.android.com/guide/app-bundle"&gt;App Bundle&lt;/a&gt; is a new and recommended way of generating and distributing your application.&lt;/p&gt;

&lt;p&gt;AAB will contain all your compiled code and resources, and you only need to upload it to Google Play. After that, PlayStore will handle it and generate an optimized and specific APK for each device. This way, your application will only have the essentials for that specific device and will deliver a much smaller app. As you saw in the above picture, the meutudo APK varies from 16Mb to 26Mb, depending on the device.&lt;/p&gt;

&lt;p&gt;To generate your app bundle, you can simply change your &lt;code&gt;./gradlew assembleRelease&lt;/code&gt; to &lt;code&gt;./gradlew bundleRelease&lt;/code&gt;. It will generate an .aab file that you will send to the Play Store.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Hermes&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://hermesengine.dev"&gt;Hermes&lt;/a&gt; is a JavaScript engine optimiser for React Native developed by Facebook. It will provide a smaller bundle and a faster launch.&lt;br&gt;
Newer versions of React Native are already integrated with Hermes, which makes it really easy to use.&lt;/p&gt;

&lt;p&gt;To start using Hermes, you just need to change the following code to &lt;code&gt;true&lt;/code&gt; in &lt;code&gt;android/app/build.gradle&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project.ext.react = [
    enableHermes: true,  // clean and rebuild if changing
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;But be careful, if you are using &lt;code&gt;react-native-webview&lt;/code&gt; with injectedJavaScript&lt;code&gt;prop and converting your injected function with&lt;/code&gt;String(myFunction)&lt;code&gt;or&lt;/code&gt;myFunction.toString()&lt;code&gt;, it will not work, because Hermes will ignore the&lt;/code&gt;toString()` part and transform your function to bytecode that then will be converted to String and will end-up not working. So if you use this way, you will have to explicitly pass a String as function.&lt;/em&gt;`&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Assets optimization&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you ever stopped to look at your assets, you may have noticed that, together, they are big, really big, most of the time. You put a 120Kb image and think, "This is not so big", and you put another, and another, and you end up with some MBs only from images.&lt;/p&gt;

&lt;p&gt;But not only images are a problem. Fonts too.&lt;br&gt;
Your designer thinks in this beautiful design, with this cool font and when you saw it, just the fonts are like 1.5Mb.&lt;/p&gt;

&lt;p&gt;To just not assume that your images and fonts are big, you can use the &lt;a href="https://developer.android.com/topic/performance/reduce-apk-size#size-analyzer"&gt;Android Size Analyzer&lt;/a&gt; to know exactly which files are bigger.&lt;br&gt;
You just need to run &lt;code&gt;size-analyzer check-bundle -d [BUNDLE].aab&lt;/code&gt; in your previously generated .aab file and get the list with large files and suggestions.&lt;/p&gt;

&lt;p&gt;And one of the suggestions may be to enable...&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Proguard&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A tool to shrink, obfuscate, and optimize your Java bytecode. &lt;br&gt;
To start using proguard in your releases, just change to &lt;code&gt;true&lt;/code&gt; the following line in &lt;code&gt;android/app/build.gradle&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def enableProguardInReleaseBuilds = true&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Bear in mind that some libs require to add some "code" to proguard to keep working&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Large images&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Ok... you run the &lt;code&gt;size-analyzer check-bundle&lt;/code&gt; command and got a list of images and other files.&lt;br&gt;
But what to do with images?&lt;br&gt;
Here is a simple "trick". You can use &lt;a href="https://tinypng.com"&gt;TinyPNG&lt;/a&gt; to reduce your images sizes by almost 50-70%.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Fonts&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;That can be a problem depending on your font and if you use all the weights. And this can be a problem since some fonts have lots of special characters that you will never use or maybe languages that you will never use.&lt;/p&gt;

&lt;p&gt;To improve your font size, you can use some tools to remove all non ISO-8859-15 (Latin0) characters.&lt;br&gt;
Tools like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://fontforge.org/en-US/"&gt;FontForge&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/fonttools/fonttools"&gt;FontTools&lt;/a&gt; with &lt;code&gt;pyftsubset&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or any other tool that you may find.&lt;/p&gt;

&lt;p&gt;After removing those unused chars, you can reduce 80-90% of the font size! That's really awesome!&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Bundle analyzer&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;During the development of an application, you always at some moment will try some libs to solve a problem, or you may use a lib to solve a problem that later you may not need anymore, or even use a lib for simple things that don't require a lib to.&lt;/p&gt;

&lt;p&gt;To analyze a bundle and see what is taking up the space, we can use &lt;a href="https://github.com/IjzerenHein/react-native-bundle-visualizer"&gt;react-native-bundle-visualizer&lt;/a&gt;.&lt;br&gt;
Running it on your project will give an image that looks like the following one:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffz4zdbprfoy4zhpaw3v8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffz4zdbprfoy4zhpaw3v8.png" alt="Example of bundle analyzer" width="800" height="486"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From that you can get a visualization of every folder of the application, and analyze which one is costing you more.&lt;/p&gt;

&lt;p&gt;From meutudo project, I've checked that &lt;code&gt;lodash&lt;/code&gt; and &lt;code&gt;aws-pinpoint/aws-sdk&lt;/code&gt; were the main libs to work on.&lt;br&gt;
I started looking at the project the places where I used lodash and, for my surprise (or not), I was using in only ONE place. To solve this, I removed lodash, and implemented the method myself because it was a really simple method.&lt;br&gt;
After that, I started to look for a solution for aws libs. Initially we were using them as a single package, and this comes with problems, because we had all the packages together, but we were only using one of them. So I searched for a most recent version of the libs, and notice that they already had separated projects as &lt;code&gt;@aws/core&lt;/code&gt; and &lt;code&gt;@aws/analytics&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is one of the examples that can be improved, but this is really relative to individual projects, and you will have to analyze your specific cases.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Thats it&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;With those steps, we were able to reduce our .apk size from 45Mb to 16-25Mb, with really fast load, and a better performance and feel in use.&lt;/p&gt;

&lt;p&gt;When working in a fast-growing startup such as meutudo, we always focus on the client and business needs, and sometimes this impacts the accumulation of technical debts so the product can grow faster. At the beginning, this can be put aside, but as soon as the company starts to increase its number of users and interactions, you need to start paying those debts.&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>android</category>
      <category>apk</category>
      <category>react</category>
    </item>
  </channel>
</rss>
