<?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 Jamie Adkins on Medium]]></title>
        <description><![CDATA[Stories by Jamie Adkins on Medium]]></description>
        <link>https://medium.com/@jamieadkins95?source=rss-cd1456167f45------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WIxVvWYMqgMryaUk.jpg</url>
            <title>Stories by Jamie Adkins on Medium</title>
            <link>https://medium.com/@jamieadkins95?source=rss-cd1456167f45------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 14:10:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jamieadkins95/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[Everything They Don’t Tell You About Instant Apps #2 — Android Manifest…]]></title>
            <link>https://medium.com/pulselive/everything-they-dont-tell-you-about-instant-apps-2-android-manifest-b0b57b78f4b8?source=rss-cd1456167f45------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0b57b78f4b8</guid>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android-instant-apps]]></category>
            <dc:creator><![CDATA[Jamie Adkins]]></dc:creator>
            <pubDate>Thu, 02 Apr 2020 17:08:06 GMT</pubDate>
            <atom:updated>2020-04-23T10:30:02.609Z</atom:updated>
            <content:encoded><![CDATA[<h3>Everything They Don’t Tell You About Instant Apps and Dynamic Features #2 — Android Manifest Merging</h3><p><em>Note: Just before I was about to publish this, Google released an update for bundletool, which fixes the very issue that this blog post talks in length about. Still, this series of posts was designed to tell the story of the Cricket World Cup instant app, so I’m going to leave this post as it was. I’ve included some discussion at the end of this post about the fix in bundletool. Enjoy!</em></p><p>This is part 2 in a series detailing how we transformed the Cricket World Cup 2019 app from a 56MB APK to a ~15MB app bundle with a 4MB instant app, aiming to show every single problem we hit along the way.</p><p>In <a href="https://medium.com/@jamieadkins95/everything-they-dont-tell-you-about-instant-apps-problems-with-dagger-android-af5b61f5a419">part 1</a>, we started with a huge monolithic :app module, and created a :base module that is now home to our Application class. We are now ready to make :base our Application module, and turn :app into a dynamic feature module.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*FHpYiUF3gqTwPMvGzaYfcA.png" /><figcaption>This is where we were at the end of Part 1. We have a :base module that is now ready to become our application module.</figcaption></figure><p>There is nothing out of the ordinary for how we configured these, you can follow the documentation on how to setup those modules. For :base, you can use <a href="https://developer.android.com/studio/projects/dynamic-delivery/configure-base">the base module guide</a>. Since you want :app to be a dynamic feature that is always installed when the user installs your app from the Play Store, you should also use <a href="https://developer.android.com/studio/projects/dynamic-delivery/at-install-delivery">the at-install guide</a>.</p><p>Your Gradle dependency diagram will now look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*oiPkt4A3SM9-BUb5AFAX6g.png" /><figcaption>:app is now a (very large) dynamic feature module</figcaption></figure><p>But your project probably won’t build, since the Android manifest merge will almost certainly fail.</p><h4>Android Manifest Merging in Dynamic Feature Projects</h4><p>The error you see will look something like this:</p><pre>* What went wrong:Execution failed for task ‘:app:processDebugResources’.<br>&gt; Android resource linking failed<br>~/plaid/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml:177: AAPT:<br>error: resource style/Plaid.Translucent.About (aka io.plaidapp:style/Plaid.Translucent.About) not found.<br>error: failed processing manifest.</pre><p><em>Note: I’m gonna use a lot of examples from the Plaid in this blog post. It’s a great sample app. Ben Weiss discusses how Plaid deals with this problem in </em><a href="https://medium.com/androiddevelopers/a-patchwork-plaid-monolith-to-modularized-app-60235d9f212e"><em>this post</em></a><em>, and we’ll be building off of that solution in this post.</em></p><p>When the Android Gradle Plugin builds your dynamic feature project, it will merge the Android Manifest from your dynamic feature module into the base module’s Android Manifest. Even though :app depends on :base from a Gradle point of view, the :base module will grab the Manifest from :app and merge it with its own, like it would with a library module.</p><p>This means that your merged Android Manifest will look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fcd0ce05393fa40af0bb75fe29e3d0ce/href">https://medium.com/media/fcd0ce05393fa40af0bb75fe29e3d0ce/href</a></iframe><p>So the Manifest from dynamic feature :about has been merged into the base module, and the Activity declared in :about says it wants to use a theme.</p><pre>android:theme=&quot;@style/Plaid.Translucent.About&quot;</pre><p>The important detail here is that<strong> only the Manifest</strong> has been merged down to the Application module, the style resource Plaid.Translucent.About still only exists in :about. So the manifest gets merged, Android Gradle Plugin then tries to evaluate the Manifest and it can’t find the Plaid.Translucent.About style resource. How can we deal with this?</p><p>You could just move all your styles to the :base module. This unfortunately has a knock on effect if you use styles/themes heavily. Do you reference some color resources in your style definition? They need to be moved to :base. Fonts? Those .ttf files need to come into :base. Got some drawables that you reference in your styles? Into :base they go.</p><p>Remember, in part 1 that we said we want to keep our :base module as small as possible. It’s going to become part of our instant app, so anything in :base will contribute to the 4MB instant app limit. You really want to avoid having anything in there that doesn’t need to be there.</p><p>The ICC/Cricket World Cup app uses styles/themes extensively. Since different tournaments have different branding, it’s not uncommon for us to use different fonts per tournament, and our apps contain lots of images/drawables related to branding. Our instant app is only going to need the Cricket World Cup theme, but if we include all our styles in :base, then the instant app will include fonts and drawables from tournaments that it doesn’t use. That’s going to make our life difficult when we try to get the instant app under 4MB.</p><h4>How does Plaid solve it?</h4><p><a href="https://medium.com/androiddevelopers/a-patchwork-plaid-monolith-to-modularized-app-60235d9f212e">In this post about Plaid</a>, Ben Weiss suggests the following solution. Since the Android Manifest only needs to know about the id of the style resource, not the implementation, you can create stub styles in :base to make the Manifest happy, and then override them in the dynamic feature module.</p><pre>&lt;!-— Placeholders. Implementations in feature modules. --&gt;<br>&lt;style name=”Plaid.Translucent.About” /&gt;<br>&lt;style name=”Plaid.Translucent.DesignerNewsStory” /&gt;<br>&lt;style name=”Plaid.Translucent.DesignerNewsLogin” /&gt;<br>&lt;style name=”Plaid.Translucent.PostDesignerNewsStory” /&gt;<br>&lt;style name=”Plaid.Translucent.Dribbble” /&gt;<br>&lt;style name=”Plaid.Translucent.Dribbble.Shot” /&gt;<br>&lt;style name=”Plaid.Translucent.Search” /&gt;</pre><p>These stub styles in :base satisfy the Manifest merge, and then you can provide the real implementations in the Dynamic Feature module:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1ead869e835831abe796e44d64db266/href">https://medium.com/media/f1ead869e835831abe796e44d64db266/href</a></iframe><h4>Why didn’t this solution work for us?</h4><p>Plaid’s solution is really useful, and keeps you from having to put all your styles in :base. However, there are 2 situations where this doesn’t work:</p><ul><li>You deploy as an app bundle to a device below SDK 21</li><li>You deploy as a universal APK to any device</li></ul><p>There is a quirk in the Android Gradle Plugin where styles are overridden differently in single APK installs vs split APK installs. Since devices below SDK 21 don’t support split APK installs, <a href="https://developer.android.com/studio/command-line/bundletool">bundletool</a> (the tool that builds APKs for a device when given an Android App Bundle) will build a single, universal APK for that device. When you run the app, you’ll get a RuntimeException that says your theme doesn’t extend Theme.AppCompat. This is because it’s using the empty stub style in :base, and not the style you declared in your dynamic feature module.</p><p>The Cricket World Cup app is min SDK 19. We have a large user base in India, which means we can’t bump to 21 just yet. If we were to use stub styles, then users on devices running &lt; SDK 21 would see a crash as soon as they opened the app.</p><p>It also causes problems when giving builds to QA. For convenience, we would normally just give QA a universal APK to test, but they would see the same issue with this setup.</p><p>If your project is min SDK 21+, then you may be able to get away with using stub styles by changing your QA workflow. Instead of giving them universal APK builds, you can use <a href="https://play.google.com/apps/publish/internalappsharing/">Google Play’s new internal app sharing</a> to give them split APK installs that are built from app bundles.</p><p>If you are like us and still have a min SDK &lt; 21, then you unfortunately will have to move all your styles to :base. For us, this meant we had to move an additional 300KB set of .ttf font files. Despite the fact that this font is only used in our installed app, it now was included in our instant app download, and it pushed us over the 4MB limit.</p><h4>Our solution for CWC</h4><p>We did manage to find a way to remove those font files from :base, but it involved sacrificing the experience a little bit for users on SDK &lt; 21. Instead of using a completely empty stub style like Plaid does, you can put most of your theme in :base, and let your dynamic feature overwrite that theme and include any large resources, such as fonts.</p><p>Here’s how this looks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/904b9c5aeb2d2b24c75bd525a44c73a7/href">https://medium.com/media/904b9c5aeb2d2b24c75bd525a44c73a7/href</a></iframe><p>In our :base module, we have BaseIccTheme which contains all of our theme except the fonts, and then we create an empty IccTheme that inherits from this theme.</p><p>Then in our dynamic feature module, we overwrite IccTheme and add the fonts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46c12e6242a92579a47bb63fdb4d4700/href">https://medium.com/media/46c12e6242a92579a47bb63fdb4d4700/href</a></iframe><p>What does this achieve? Well for all users on SDK 21 and above, they will receive a split APK install, and the theme in the dynamic feature module will be used. They will get everything from BaseIccTheme, but they’ll also get the font that is applied in IccTheme. For users on SDK &lt; 21, they will receive a single APK install, and will receive the wrong theme, they will receive the empty IccTheme that is in the :base module. However, since we’ve moved most of the theme to :base, they will still have all the correct colours and other theme attributes, but they will have the incorrect font. This was a compromise we were willing to make in order to release an instant app. It is not ideal, but we decided that a font issue for &lt;3% users was worth it for an instant app for ~94% of users.</p><p>Like in part 1, the solution we ended up using is absolutely not a best practice, but it is a solution that allowed us to get an instant app out of the door. I hope that this helps you make an informed decision about how to deal with the problem in your project. <em>Note: Now that this Android Manifest merging issue has been fixed in bundletool 0.13.4, my suggestion now would be to use Plaid’s solution, see footnote.</em></p><p>In the next post we’ll look some more bugs we encountered in the Android tooling. See you then!</p><h4>Post Index:</h4><ol><li><a href="https://medium.com/@jamieadkins95/everything-they-dont-tell-you-about-instant-apps-problems-with-dagger-android-af5b61f5a419">Problems with your Dagger setup</a></li><li>Android Manifest merging in Dynamic Feature projects (this post)</li><li>Android tooling bugs (coming soon)</li><li>The 4MB download size limit (coming soon)</li><li>Permissions in Instant apps (coming soon)</li><li>Resisting the urge to make everything a Dynamic Feature (coming soon)</li></ol><h3><strong>April 2020 — Bundletool fix</strong></h3><p>After we tried Plaid’s solution to this problem, and encountering the issue of the incorrect style being used on universal APK installs, <a href="https://github.com/google/bundletool/issues/68">I raised an issue on Github with bundletool</a>. With incredible timing 1 year later, now that I am about to publish this blog post that discusses the problem in great detail, Google have just released a fix for this issue!</p><p>If you are using bundletool 0.13.4 or above, bundletool will merge your manifests slightly differently, using a new <a href="https://github.com/google/bundletool/blob/24e8e6efa7a23860fb84903c39eeb44628b3878b/src/main/java/com/android/tools/build/bundletool/mergers/FusingAndroidManifestMerger.java">FusingAndroidManifestMerger</a>, and as a result, the Android Manifest in universal APKs will now point to the correct style resource; the one in your dynamic feature module.</p><p>To use this fix in your project, you need to tell AGP to use the new version of bundletool (until an update to AGP is released that uses bundletool 0.13.4 by default). You can do this by adding bundletool as a dependency in your top level build.gradle:</p><pre>buildscript {<br>    repositories {<br>        google()<br>        jcenter()<br>        ...<br>    }<br>    dependencies {<br>        classpath &quot;com.android.tools.build:bundletool:0.13.4&quot;<br>        ...<br>    }<br>    ...<br>}</pre><p>As described in <a href="https://github.com/google/bundletool/issues/68#issuecomment-606123310">this comment on Github</a>.</p><p>You will also need to wait for Google Play to be updated to use the new version of bundletool. Remember that that when a user installs your app from the Play Store, Google Play uses bundletool to build APKs for that user’s device. In order for this fix to work, those APKs need to be built using bundletool 0.13.4. <a href="https://github.com/google/bundletool/issues/68#issuecomment-606123310">Google Play will be updated this month to use bundletool 0.13.4.</a></p><p>Once you are using bundletool 0.13.4, you can use Plaid’s stub styles solution in your project, and both universal APK and split APKs installs will behave the same, they will both take the style from your dynamic feature module.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0b57b78f4b8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pulselive/everything-they-dont-tell-you-about-instant-apps-2-android-manifest-b0b57b78f4b8">Everything They Don’t Tell You About Instant Apps #2 — Android Manifest…</a> was originally published in <a href="https://medium.com/pulselive">Pulselive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Quick Look at Feature-on-Feature Dependencies in Android Gradle Plugin 4.0.0]]></title>
            <link>https://medium.com/pulselive/a-quick-look-at-feature-on-feature-dependencies-in-android-gradle-plugin-4-0-0-5828915d02d3?source=rss-cd1456167f45------2</link>
            <guid isPermaLink="false">https://medium.com/p/5828915d02d3</guid>
            <category><![CDATA[dynamic-feature-module]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[instant-apps]]></category>
            <dc:creator><![CDATA[Jamie Adkins]]></dc:creator>
            <pubDate>Mon, 11 Nov 2019 11:58:52 GMT</pubDate>
            <atom:updated>2020-04-27T11:59:07.176Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O6dpE98N8RvZuRnCQv-s3A.png" /></figure><p>At first glance, Feature-on-Feature dependencies appears to be a change meant for niche dynamic features use cases. However, in this post we’ll explore how this change will make it much easier to build and ship instant apps.</p><p>In Android Gradle Plugin 4.0.0, dynamic features can now depend on other dynamic features. Now, when your app downloads a dynamic feature, it also downloads any dynamic features that it depends on.</p><p><a href="https://developer.android.com/studio/preview/features#feature-on-feature">New features in Android Studio Preview | Android Developers</a></p><p>Your Gradle dependency graph can now look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*tqzWpWx3sGSseJ0YIKZluA.png" /><figcaption>Feature-on-Feature example from <a href="https://developer.android.com/studio/preview/features">https://developer.android.com/studio/preview/features</a></figcaption></figure><p>At first you may think that this only serves some niche use cases of dynamic features, however this actually helps solves a very real problem that dynamic feature projects currently face in Android Gradle Plugin 3.5.</p><p>Suppose you have a project with 3 dynamic features; :video-list, :video-player and :news. Both :video-list and :video-player depend on a common library; :video-data. You may try to set up your Gradle dependencies like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/619/1*O3iHXyDWngl3NtaRSfd9gA.png" /><figcaption>Example project with 3 dynamic features. 2 of which depend on the same library module.</figcaption></figure><p>If you now try to build this, you’ll get an error that says that 2 dynamic features both package the same library.</p><blockquote>[:video-list, :video-player] all package the same library [:video-data].<br> <br> Multiple APKs packaging the same library can cause runtime errors.<br> Adding the above library as a dependency of the base module will resolve this<br> issue by packaging the library with the base APK instead.</blockquote><p>In Android Gradle Plugin 3.5, common dependencies have to be provided by base. So instead, your Gradle dependencies will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*iiMn8ZGOM4pAzFHjjoE3wQ.png" /><figcaption>:video-data has to be provided by :base, because 2 or more dynamic features depend on it</figcaption></figure><p>This has produced an interesting side effect, :news now implicitly depends on :video-data . This becomes a huge problem if you want to make :news an instant app. Instant apps have a 4MB download size limit, and if you depend on libraries that you don’t actually need, it is going to make it very difficult to get the download size below 4MB. For example, :video-data may depend on the Cast library from Google Play Services, which is a big dependency if your project doesn’t already use Google Play Services.</p><p>Feature-on-Feature dependencies in Android Gradle Plugin 4.0 solves this issue, and you have a couple of different options.</p><p>You can make :video-list depend on :video-player like so:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*cb0M7ztZIjPSGrq4K4hLYw.png" /><figcaption>This also works vice versa, i.e :video-player depending on :video-list</figcaption></figure><p>Now, you no longer have 2 completely separate dynamic features that depend on :video-data, and AGP is happy.</p><p>You can also make :video-data itself a dynamic feature, and have :video-list and :video-player depend on :video-data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*EkbnDdvtTXFXUzt9atb8Og.png" /></figure><p>Either of these solutions will avoid the common dependency build error, and ensure that :news doesn’t depend on :video-data, so that :news can live happily as a 4MB instant app!</p><h4>Further Reading</h4><ul><li><a href="https://developer.android.com/studio/preview/features#feature-on-feature">https://developer.android.com/studio/preview/features#feature-on-feature</a></li><li><a href="https://issuetracker.google.com/issues/116852261">https://issuetracker.google.com/issues/116852261</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5828915d02d3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pulselive/a-quick-look-at-feature-on-feature-dependencies-in-android-gradle-plugin-4-0-0-5828915d02d3">A Quick Look at Feature-on-Feature Dependencies in Android Gradle Plugin 4.0.0</a> was originally published in <a href="https://medium.com/pulselive">Pulselive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Everything They Don’t Tell You About Instant Apps — Problems with Dagger Android]]></title>
            <link>https://medium.com/pulselive/everything-they-dont-tell-you-about-instant-apps-problems-with-dagger-android-af5b61f5a419?source=rss-cd1456167f45------2</link>
            <guid isPermaLink="false">https://medium.com/p/af5b61f5a419</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-instant-apps]]></category>
            <dc:creator><![CDATA[Jamie Adkins]]></dc:creator>
            <pubDate>Mon, 04 Nov 2019 22:29:02 GMT</pubDate>
            <atom:updated>2020-04-23T10:28:50.908Z</atom:updated>
            <content:encoded><![CDATA[<h3>Everything They Don’t Tell You About Instant Apps and Dynamic Features #1 — Problems with your Dagger setup</h3><p>Creating an Android project that is decoupled enough to be able to release a feature as an instant app is a tough ask. It becomes even more daunting when your starting point is a monolithic, single module project that is 4 years old.</p><p>At <a href="https://www.pulselive.com/">Pulselive</a>, we built the official Cricket World Cup 2019 app. We published the Cricket World Cup Quiz feature as an instant app, but it turned out to be much more difficult than we first thought, and it ended up taking longer than we expected.</p><p>In this series we’ll go over how the Cricket World Cup 2019 app went from a 56MB download to a ~15MB download for the full installed app and 4MB for the instant app, and we’ll show every single problem we hit along the way.</p><p><em>Note: I’ll refer to the project interchangeably as ICC or CWC19. The app changes name based on what major cricket tournament is currently occurring.</em></p><p>This the first of many posts on this subject. Here’s an outline about what you can expect to see in the coming weeks:</p><ul><li>Problems with your Dagger setup</li><li>Android Manifest merging in Dynamic Feature projects</li><li>Android Styles and Themes in Dynamic Feature projects</li><li>Android Gradle Plugin bugs</li><li>Constraint Layout bugs</li><li>Resisting the urge to make everything a Dynamic Feature</li><li>Navigation in multi module projects</li><li>The 4MB instant app limit</li><li>Permissions in Instant apps</li><li>Misc. instant apps tips and tricks</li></ul><blockquote>“You said you released an instant app. Can we see it?”</blockquote><p>We released the Quiz feature in the Cricket World Cup 2019 app as an instant app. If you are reading this on an Android device, you can try it out by clicking here:</p><p><a href="http://www.cricketworldcup.com/quiz?id=60">www.cricketworldcup.com/quiz?id=60</a></p><p>(Warning: This quiz is <strong>very</strong> hard, even for huge cricket fans)</p><p><em>2020 edit: Our instant app has now moved for the T20 World Cup </em><a href="https://www.t20worldcup.com/quiz?id=64"><em>https://www.t20worldcup.com/quiz?id=64</em></a></p><h3>Preparing your project for Dynamic Features</h3><p>This series does assume that you know a little bit about what dynamic features are, and how that affects your gradle dependency graph (i.e. features can now depend on your application module). If you are not sure about what dynamic features are, you can read about it in the documentation <a href="https://developer.android.com/studio/projects/dynamic-delivery">here</a>.</p><h4>Where We Started</h4><p>In the start of this post I said our starting point was a single module, monolithic project. That’s not quite true, by January 2019, every feature in the app had been migrated to clean architecture and the project was modularised horizontally, by architectural layer:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*y_iN9q3TkX-A_hv_-ZtNDg.png" /><figcaption>January 2019 — Modularised by architectural layer</figcaption></figure><p>While this was slightly better than a single module, as most features were pretty clean and decoupled, it wasn’t helping our build times. :app was still a huge module and it was taking forever to build thanks to a pretty large Dagger graph being non-incrementally processed by kapt.</p><p>Our initial feature modularisation had a primary goal of reducing build times, and if we could get an instant app out of that, that’d be a huge bonus.</p><p><a href="https://github.com/android/plaid">Google’s open source Plaid app</a> was of particular interest to us, it had just gone through a re-architecture to dynamic features modules, and served as very good source for how to achieve the same in our project.</p><p><a href="https://twitter.com/keyboardsurfer">Ben Weiss</a>, one of the collaborators on Plaid, has written two very useful articles about Plaid’s re-architecture, <a href="https://medium.com/androiddevelopers/a-patchwork-plaid-monolith-to-modularized-app-60235d9f212e">one about migrating to dynamic features</a> and the other about <a href="https://medium.com/androiddevelopers/dependency-injection-in-a-multi-module-project-1a09511c14b7">DI in a multi module project</a>. The latter is what we will be focusing on in this post. CWC19 already had a very large and established Dagger setup, and it quickly became clear that it wasn’t going to work in a project with dynamic feature modules. What follows is the steps we took to migrate the project from a huge monolithic Dagger Android setup to something much closer to Plaid’s DI setup.</p><h3>Step 1 — Creating a base application module</h3><p>The first thing we realised is that we couldn’t use :app as our base application module for dynamic features. We could have just moved the Quiz feature into a dynamic feature module that depended on :app, and this would have let us do on demand delivery for the Quiz feature pretty easily. However, that wasn’t going to help us deliver an instant app. At this point we were still shipping ICC as an APK, and it was 56MB. There was zero chance of shipping Quiz as a 4MB instant app if it depended on the :app module.</p><p>The next step was pretty obvious then, we would create a new :base module that would become our application module, both :quiz and :app would depend on that, and :app would also become a dynamic feature module that was always installed at install time. The plan was to end up with a gradle dependency graph that looked something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*8p7EE2dLbGC5ak40J4kptw.png" /><figcaption>Initial plan for dynamic feature module setup. :app and :quiz are dynamic features that depend on a much smaller :base application module.</figcaption></figure><p><em>Note: Google’s samples suggest that your ‘core’ app experience should live in your base module. This is very difficult to do when you are chasing the 4MB instant app limit. As of October 2019, the only feature we have in our base module is the splash screen.</em></p><p>We realised that turning :app into a dynamic feature module would probably cause its own problems, so before we committed to that, we made :base an android library module, and started moving stuff that we knew that would have to live in :base.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*5V1lLJZciNFfwOfq6jheRA.png" /><figcaption>First step towards migrating to dynamic features, creating a :base module that will eventually become the application module.</figcaption></figure><p>At the very minimum, the application class would have to be moved to :base. Both :app and :quiz were going to need it, so it made sense to move that to base first. This is where we encountered our first big hurdle.</p><h3>Step 2— Moving your application class to :base</h3><p>At this point our custom application class looked something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10a6a05a40dddb86e9c9c66743985540/href">https://medium.com/media/10a6a05a40dddb86e9c9c66743985540/href</a></iframe><p>The problem isn’t obvious at first glance. Both Timber and Fabric will be useful in dynamic feature modules anyway, so those dependencies can go in to the :base module with no issue. The problem lies with the fact that IccApplication is a DaggerApplication and it is the one creating our AppComponent. ICC at this point relied heavily on Dagger Android, and Dagger Android encourages you to set up an AppComponent that knows about pretty much everything.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b71d4e13273357dfac292081f95fbfc/href">https://medium.com/media/3b71d4e13273357dfac292081f95fbfc/href</a></iframe><p>AppComponent included ~90 modules of its own, and even more with all the features using @ContributesAndroidInjector to add to SubComponents to this Dagger graph.</p><p>This meant that if we were to move the Application class to :base, we’d also have to move AppComponent, which would mean we have to move pretty much everything into :base, completely defeating the point of creating a small :base module in the first place!</p><p>Adopting Plaid’s DI setup would be ideal here. :base should provide a much smaller CoreComponent instead of the mammoth AppComponent, and each feature should have it’s own Component. Unfortunately, it wasn’t that straight forward in our project. Everything in :app is setup with Dagger Android, so features rely on AppComponent to be able add their Subcomponents to the graph using @ContributesAndroidInjector. We were 3 months out from the World Cup starting, migrating every feature away from AppComponent to use their own Component (like in Plaid) was not feasible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*5BeYTvyKQibCUsXyYF27uQ.png" /><figcaption>Example Feature Component setup in Plaid. Each feature has its own Component that depends on CoreComponent. Source: <a href="https://medium.com/androiddevelopers/dependency-injection-in-a-multi-module-project-1a09511c14b7">https://medium.com/androiddevelopers/dependency-injection-in-a-multi-module-project-1a09511c14b7</a></figcaption></figure><p>After a few days of internal discussion, we came to a few conclusions:</p><ul><li>We shouldn’t try to redo to Dagger graph for the entire app, any solution that changed as little as possible in the :app module would be a good solution.</li><li>There are very few dependencies in our Dagger graph that actually need to be Singletons. Mostly everything was stateless.</li><li>Extending DaggerApplication was unnecessary, we weren’t injecting anything into the Application class, and if we needed to do that, we could do that the normal Dagger way, without DaggerApplication.</li></ul><p>Therefore we proposed this solution:</p><ul><li>AppComponent would stay in :app.</li><li>IccApplication would move to :base, and it would provide a CoreComponent that was home to anything that really needed to be a Singleton.</li><li>AppComponent would depend on CoreComponent for those Singleton dependencies.</li><li>Dagger Android would no longer be present at the Application level, and instead AppComponent would be built by the Activities that needed it.</li></ul><p>In theory, we would have to change very little in the :app module. Features that used Dagger Android could continue to add their Subcomponents to the graph, and since most things were stateless, it didn’t matter that AppComponent was now scoped to the Activities rather than the Application.</p><p>In practice, this worked very well! The final set of changes (broken up into a few different PRs) ended up touching a lot of files, but most of those were single line changes that removed the @Singleton annotation from dependencies that didn’t need them. Much fewer changes than having to change the Dagger setup in the entire project!</p><h4>Removing DaggerApplication and DaggerAppCompatActivity</h4><blockquote>Dagger Android would no longer be present at the Application level, and instead AppComponent would be built by the Activities that needed it.</blockquote><p>There is one key change we had to make here to achieve this.</p><p>In a normal Dagger Android setup, the Dagger graph is initialised by your Application class that extends DaggerApplication. Your Activities then extend DaggerAppCompatActivity, which calls AndroidInjection.inject(this) to inject itself. Under the hood, this call looks for an AndroidInjector from the Application class. DaggerApplication provides this by implementing HasAndroidInjector. However, since our Application class no longer extends DaggerApplication, DaggerAppCompatActivity will be unable to find an AndroidInjector.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5eb4667377138093b958e005a71b9e7b/href">https://medium.com/media/5eb4667377138093b958e005a71b9e7b/href</a></iframe><p>To solve this, we came up with our own abstract Activity class, which looks very similar to DaggerAppCompatActivity, but importantly it doesn’t call AndroidInjection.inject(this):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6754abe90916451e6a0b2002b3383fac/href">https://medium.com/media/6754abe90916451e6a0b2002b3383fac/href</a></iframe><p>Then in our Activities, we can override onInject and instantiate the AppComponent, like the Application class was doing before:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ed7b16ead5e46ecd2fc5b9878470620d/href">https://medium.com/media/ed7b16ead5e46ecd2fc5b9878470620d/href</a></iframe><p>Here’s what the other key classes looked like after these changes, notice that they are very similar to the Plaid setup:</p><p>IccApplication now lives in :base and (lazily) builds CoreComponent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87b23c0bc8534e1500f31766564fbce0/href">https://medium.com/media/87b23c0bc8534e1500f31766564fbce0/href</a></iframe><p>CoreComponent also lives in :base and knows very little compared to AppComponent:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10016fba6f50759cd3a22de44f6f72fa/href">https://medium.com/media/10016fba6f50759cd3a22de44f6f72fa/href</a></iframe><p>AppComponent is still in :app and a few things have changed:</p><ul><li>It is scoped with @PerActivity rather than @Singleton</li><li>It depends on CoreComponent</li><li>It injects into MainActivity and CwcActivity rather than IccApplication. Remember that the remaining dependencies in AppComponent are stateless, so this doesn’t result in any behaviour changes.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad9b6fc953b8be8e1b91b0724bb41764/href">https://medium.com/media/ad9b6fc953b8be8e1b91b0724bb41764/href</a></iframe><h3>Conclusion</h3><p>We’ve now successfully decoupled our application class from that big nasty AppComponent, and we can continue working on our instant app without disturbing the current implementation of :app too much. In addition to this, we can also slowly start refactoring features in :app away from being dependent on AppComponent.</p><p>What we’ve achieved in this post may not be recommended Dagger practice, but it is a neat solution to a problem that was blocking us migrating to dynamic feature modules. AppComponent is still problematic, it still knows far too much about every feature in :app, but now we can develop new features that don’t need to use AppComponent, and we can migrate existing features away from AppComponent in small chunks. We don’t have to do everything at once.</p><h4>Next Steps</h4><p>Back to our instant app journey, here’s how our gradle module graph looked at this point:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*5V1lLJZciNFfwOfq6jheRA.png" /><figcaption>Same diagram as before, but now :base contains out CoreComponent and our Application class. We are ready to make :base our application module.</figcaption></figure><p>Now that we have a :base module, we’re ready to make that our application module, and turn :app into a dynamic feature module. This will bring on its own set of problems, and we’ll talk about them next time.</p><h4>Further Reading</h4><ul><li>Everything They Don’t Tell You About Instant Apps and Dynamic Features #2 — Android Manifest merging in Dynamic Feature projects (coming soon)</li><li><a href="https://medium.com/androiddevelopers/a-patchwork-plaid-monolith-to-modularized-app-60235d9f212e">Patchwork Plaid — A modularization story — Ben Weiss</a></li><li><a href="https://medium.com/androiddevelopers/dependency-injection-in-a-multi-module-project-1a09511c14b7">Dependency injection in a multi module project — Ben Weiss</a></li><li><a href="https://developer.android.com/studio/projects/dynamic-delivery">About Dynamic Delivery — Android Docs</a></li><li><a href="https://github.com/android/plaid">https://github.com/android/plaid</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af5b61f5a419" width="1" height="1" alt=""><hr><p><a href="https://medium.com/pulselive/everything-they-dont-tell-you-about-instant-apps-problems-with-dagger-android-af5b61f5a419">Everything They Don’t Tell You About Instant Apps — Problems with Dagger Android</a> was originally published in <a href="https://medium.com/pulselive">Pulselive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>