<?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 Aleksandr Pilgun, PhD on Medium]]></title>
        <description><![CDATA[Stories by Aleksandr Pilgun, PhD on Medium]]></description>
        <link>https://medium.com/@samsancho?source=rss-4c5191b66af2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GXKlkQ32RMNZ04P73MR_1Q.jpeg</url>
            <title>Stories by Aleksandr Pilgun, PhD on Medium</title>
            <link>https://medium.com/@samsancho?source=rss-4c5191b66af2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 14:36:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samsancho/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Android app shrinking beyond R8]]></title>
            <link>https://proandroiddev.com/android-app-shrinking-beyond-r8-ad41934727dc?source=rss-4c5191b66af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad41934727dc</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[code-coverage-tools]]></category>
            <category><![CDATA[smali]]></category>
            <category><![CDATA[android-r8]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <dc:creator><![CDATA[Aleksandr Pilgun, PhD]]></dc:creator>
            <pubDate>Mon, 26 Dec 2022 13:51:02 GMT</pubDate>
            <atom:updated>2022-12-26T13:51:02.414Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello Everyone!</p><p>My name is Aleksandr, and I am working on ACV shrinking — more effective way to shrink Android apps. Through testing!</p><p>In short, ACV shrinking helps to remove bytecode bloat from Android apps by removing unused code. Compared to existing static analysis-based tools it requires thorough testing. But then, only executed code makes it into the APK.</p><p>From this article you will get insights on app internals, how the app changes under R8 shrinking, and how to employ instruction coverage to outperform R8. Feel free to click through our coverage reports in the end of this article!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ywzFkFdKm9-OiG-8uMQgXQ.png" /></figure><h3>Intro</h3><p>App size was always important to app producers since it affects install rates. On the other hand, a rich UI experience is a competitive advantage that increases size.</p><p>Among tools R8 shrinker and ProGuard are most known for size reduction. Additionally, Facebook released its own optimiser called ReDex. They statically identify and remove not reachable code and apply other clever optimisations. These tools help a lot. However, APKs still keep plenty of reachable but never executed code.</p><h3>Minimal App</h3><p>For this demo we produce the smallest WebView app to begin with. Later, we add background notifications to incrementally observe changes. Single activity and a few lines of code to open your webpage, and here we go!</p><pre>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;<br>&lt;WebView android:layout_width=&quot;fill_parent&quot;<br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    android:id=&quot;@+id/web&quot;<br>    android:layout_height=&quot;fill_parent&quot;/&gt;</pre><pre>public class MainActivity extends Activity {<br>    @Override<br>    protected void onCreate(Bundle savedInstanceState) {<br>        super.onCreate(savedInstanceState);<br>        setContentView(R.layout.am);<br>        WebView w = (WebView) findViewById(R.id.web);<br>        w.setWebViewClient(new WebViewClient(){<br>            //older Android still needs this deprecated function<br>            @Override<br>            public boolean shouldOverrideUrlLoading(WebView view, String url) {<br>                return false;<br>            }<br>        });<br>        w.loadUrl(&quot;https://debloat.app&quot;);<br>        w.getSettings().setJavaScriptEnabled(true);<br>        // This line enables notifications (for full version)<br>        FirebaseMessaging.getInstance().subscribeToTopic(&quot;news&quot;);<br>    }<br>}</pre><p>Turned out, Android Studio generates 1–5 MB APK by default for simple apps. Thus, we first remove default dependencies and resources, and clean the AndroidManifest. Another magic trick is to put a single vector XML for an icon.</p><p>Following this receipe, our APK weighs just <strong>10KB</strong> (861<strong>B</strong> for DEX). This doubles to 20KB when published at Google Play. <a href="https://play.google.com/store/apps/details?id=app.debloat">Check it out</a>!</p><h3>Adding background notifications</h3><p>Now, we add the firebase-messaging dependency. Turned out, the firebase dependency requires the supportive AndroidX library. Thus, full app size bounces back to 1.1MB (882KB for DEX).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*86xypIQS9oFM8msOcRVPJg.png" /><figcaption>Full app content (apkanalyzer)</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*iwTrEjw83CCkVlCqXpKfEQ.png" /><figcaption>Full app internals (apktool)</figcaption></figure><p>Apkanalyzer reveals app’s internals, but we also use Apktool to see more. Full app have got additional high level packages (besides the main “app.debloat” package) and a lot of classes in them. Let’s shrink it!</p><h3>R8 shrinking</h3><p>To shrink our app, we enable R8 at full mode and rebuild the app. Here is the comparison table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*gYmiby9qKJwXtuhOY34Xcw.png" /><figcaption>Size comparison before vs. after R8 optimisations</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/657/1*eWvthGyAeUWie43c748VyA.png" /><figcaption>R8 minified class/package names</figcaption></figure><p>The app size improved by 67%. Moreover, code structure changed dramatically too. Visually, instead of readable package/class/method/variable names, we got short names. Yet, the number of code entities has been greatly reduced too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*MpJuDmYOKslfWLOpNQAlnA.png" /><figcaption>Code entities and resources comparison</figcaption></figure><p>One may believe we need all of that. But we will surely find here the redundant code and resources. For example, it was unexpected to find additional 122 XML and 41 PNG files. Turned out, most of XML files keep translations of Google Play services unavailability errors. This functionality goes to almost every app! You may decide yourself on its usefulness :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*oMtOvQ_qcR-bJsiQPbbHgw.png" /><figcaption>APK’s resources folder.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*cn6t7kHt_-pUiQXGdeGkYg.png" /><figcaption>Example of ./res/values/strings.xml</figcaption></figure><p>Worth to note, resources are referenced and used somewhere in the code. There is too much code, but version we could actually recognise packages, classes, methods and fields in the full app. R8 reduces the amount of code, though it’s also harder to get a clue on the minimised code. We can actually see what executes with instruction coverage.</p><h3>Instruction coverage</h3><p>ACVTool is an instruction coverage measurement tool that highlights actually executed code in a JaCoCo-like report, but for the whole app and in smali representation.</p><p>We will skip all the technical details and immediately dive into instruction coverage generated from end-to-end tests. We’ve got one report per each app version — for the initial 1.1 MB app and for the R8-optimised (366KB).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*uo-DcrtkufTJQxzWCIWoxg.png" /><figcaption>Full app (1.1 MB) coverage results</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*Ouc8Xq-uX3YTaXXCzVhwlA.png" /><figcaption>R8-optimised app (366KB) coverage results</figcaption></figure><p>The actually executed code appears to be just 7.6% for the not optimised app. Yet, it grows to 27.5% for the R8-optimised app. Let’s see how the main package changed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*6SuWTA0J8LkDl7Ks9MgnyA.png" /><figcaption>Main package classes before and after R8 shrinking</figcaption></figure><p>Turned out, R8 only left the MainActivity class in the main package. Naturally, this class is referenced in the manifest file. However, the class has changed, too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4elSHV6w1cBCBLWjtA6S6Q.png" /><figcaption>MainActivity.onCreate method before and after R8 shrinking.</figcaption></figure><p>The MainActivity class actually has got more instructions in the onCreate method. Apparently, R8 inlined code from other methods. We can even find here a try-catch structure! Yet, the major part of the app (~73% of instructions) was not executed, despite our testing efforts.</p><h3>ACV shrinking</h3><p>With instruction coverage information we now know exactly what was executing. That code is to stay. However, we carefully cut the not executed instructions. This way the ACV-shrunk version produces close to 100% instruction coverage and weighs 277KB now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*aZgf-7LIG_Lxve7Qa4NKsg.png" /><figcaption>R8 &amp; ACV-shrunk app (277KB), coverage results</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*CqkfgG3whQ7pw2Nv0nfncA.png" /><figcaption>R8 &amp; ACV-shrunk app</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*ZFjHOKPBoinZ0lB1V-bMLg.png" /><figcaption>Total reduction results</figcaption></figure><blockquote>In the end, we got a 277KB app, which is 24% smaller than R8-optimised version. In total, this is 75% less of initial APK (and 91% less for DEX)</blockquote><p>However, bytecode manipulations are quite a challenging task. Our automated approach still keeps some empty classes and definitions of methods where instructions have been removed — stub methods. Stubs are never invoked, but some of them may maintain the code structure through inheritance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*kon9esPySuEh4AlJAFzl8g.png" /><figcaption>Stub methods</figcaption></figure><p>Though in this report we focused on cutting not executed instructions, eventually, stubs get removed. There is quite some room for other optimisations too. This work is in progress.</p><p>Please feel free to share your thoughts, learn more at <a href="https://debloat.app/">Debloat.App</a> project, and check the genuine instruction coverage reports from this article:</p><ol><li><a href="https://report.debloat.app/app.debloat/main_index.html">Initial app</a></li><li><a href="https://report.debloat.app/app.debloat_r8/main_index.html">R8-optimised app</a></li><li><a href="https://report.debloat.app/app.debloat_r8_shrunk/main_index.html">R8 &amp; ACV-shrunk app</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad41934727dc" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/android-app-shrinking-beyond-r8-ad41934727dc">Android app shrinking beyond R8</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>