<?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 Pedro Okawa on Medium]]></title>
        <description><![CDATA[Stories by Pedro Okawa on Medium]]></description>
        <link>https://medium.com/@pedrookawa?source=rss-916826c3297b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gNxKZUGaKcv6SQUCIuRp7g.jpeg</url>
            <title>Stories by Pedro Okawa on Medium</title>
            <link>https://medium.com/@pedrookawa?source=rss-916826c3297b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:55:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pedrookawa/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 — Clean architecture with Dynamic-features and Hilt/Dagger2 (Pt.3)]]></title>
            <link>https://medium.com/swlh/android-clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-3-ca40d2316fc8?source=rss-916826c3297b------2</link>
            <guid isPermaLink="false">https://medium.com/p/ca40d2316fc8</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[dagger]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <dc:creator><![CDATA[Pedro Okawa]]></dc:creator>
            <pubDate>Fri, 05 Mar 2021 00:54:53 GMT</pubDate>
            <atom:updated>2021-03-05T13:12:13.691Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android — Clean architecture with Dynamic-features and Hilt/Dagger2 (Pt.3)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W86JemFAqZfPpfpt8e6rMw.jpeg" /><figcaption>Barcelona — Spain (2019)</figcaption></figure><h3>Introduction</h3><p>As you noticed by the title, this is the third part of the series of articles explaining and describing my process of creating an application using dynamic features with Hilt/Dagger. You may find the previous posts here:</p><ul><li><a href="https://pedrookawa.medium.com/clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-1-d1be41a44b12">Part 1</a></li><li><a href="https://pedrookawa.medium.com/clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-2-af801f162b0d">Part 2</a></li></ul><p><a href="https://github.com/PedroOkawa/app-blockchain-mkt">PedroOkawa/app-blockchain-mkt</a></p><h4>Hilt / Dagger</h4><p>The proper setup to use Hilt in your application is well-described on <a href="https://developer.android.com/training/dependency-injection/hilt-android">Google’s webpage</a>, but I’ll quickly show an example based on the code I implemented.</p><p>In order to use Hilt, we must annotate our Application class with @HiltAndroidApp.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8736b0f944d738cfb73a6fbc81df5c1/href">https://medium.com/media/b8736b0f944d738cfb73a6fbc81df5c1/href</a></iframe><p>If we are going to manage dependency injection on our app module, we can still use Hilt by adding the @AndroidEntryPoint on one of the following Android components:</p><ul><li>Application (by using @HiltAndroidApp)</li><li>ViewModel (by using @HiltViewModel)</li><li>Activity</li><li>Fragment</li><li>View</li><li>Service</li><li>BroadcastReceiver</li></ul><p>To exemplify, we are going to inject an instance of a “ColorProvider” inside our view model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b6d0802a7c07e9b9ee9b86a5e997ccd/href">https://medium.com/media/7b6d0802a7c07e9b9ee9b86a5e997ccd/href</a></iframe><p>First, we need to annotate the activity and fragment holding this fragment with the @AndroidEntryPoint</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f69414e9b7c5713a360d1d18177bee83/href">https://medium.com/media/f69414e9b7c5713a360d1d18177bee83/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1105a1510e9299e671750f8d292f7d92/href">https://medium.com/media/1105a1510e9299e671750f8d292f7d92/href</a></iframe><p>With that done, we are now able to inject our “ColorProvider” inside our view model by using a binding injection on our constructor</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16f2e35feac9548fde4d30173bdddad0/href">https://medium.com/media/16f2e35feac9548fde4d30173bdddad0/href</a></iframe><p>Great, that’s enough to have our dependency injection working on our app module, but what about our dynamic features?</p><h4>Dagger2</h4><p>Well, Hilt is not supported yet on dynamic features, and the workaround is to use Dagger2 instead, and now here comes the trick. Hilt provides an annotation to help with that called @EntryPoint, so we must declare an interface in our app module annotated with this and provide the function signatures of the dependencies that will be used by our dynamic features. In our example, the only dependency that is being used by the other modules is Retrofit.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1540d3cbf7cb62ceb75c2c05c3d63587/href">https://medium.com/media/1540d3cbf7cb62ceb75c2c05c3d63587/href</a></iframe><p>The dependencies declared must be installed in a defined scope by using the @InstallIn annotation, which in our case is defined into SingletonComponent</p><h4>Feature module</h4><p>Now, in our feature module, we must create a component that depends on the DaggerDependencies interface we created and also add all the needed Dagger modules.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9173b88312330a8c8caa29b56a98259e/href">https://medium.com/media/9173b88312330a8c8caa29b56a98259e/href</a></iframe><p>In order to make our dependencies work, we must define an inject function that receives as a parameter the target class to inject the dependencies, which will be in our case our fragment.</p><p>For those who already worked with Dagger and multi-modules, this is not new, the only difference is the appDependencies function defined inside the Builder, which is the key to connect our dependencies.</p><p>Right after declaring the component and rebuild your project, you will be able to locate the generated Dagger component that will be named “DaggerPoolsComponent”</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4596f573a16c5bdc4eb05d32e4730135/href">https://medium.com/media/4596f573a16c5bdc4eb05d32e4730135/href</a></iframe><p>Do you remember the appDependencies function we defined in our component? It is time for it to shine. We must call this function and pass the EntryPoint we defined using the “EntryPointAccessors”.</p><p><strong>Note:</strong> I defined the inject function using extension functions to reduce the amount of code inside my fragment, but it could be defined inside the fragment without any problems.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/37e7b4703be0a144acacf19fd4b96be2/href">https://medium.com/media/37e7b4703be0a144acacf19fd4b96be2/href</a></iframe><p>Now the only thing we must do to fulfill our mission is to call the inject function, and according to <a href="https://developer.android.com/training/dependency-injection/dagger-android">Google’s documentation</a>, we must do it inside the onAttach function.</p><blockquote>When using activities, inject Dagger in the activity’s onCreate() method before calling super.onCreate() to avoid issues with fragment restoration. During the restore phase in super.onCreate(), an activity attaches fragments that might want to access activity bindings.</blockquote><blockquote>When using fragments, inject Dagger in the fragment’s onAttach() method. In this case, it can be done before or after calling super.onAttach().</blockquote><p>We now have finished the setup to use dynamic-features along with Hilt/Dagger.</p><p>Congratulations and thanks for accompanying me on that journey!!! 🎉</p><p>Any thoughts ideas or suggestions, please add some comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ca40d2316fc8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/android-clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-3-ca40d2316fc8">Android — Clean architecture with Dynamic-features and Hilt/Dagger2 (Pt.3)</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android — Clean Architecture With Dynamic-Features and Hilt/Dagger2 (Pt.2)]]></title>
            <link>https://medium.com/geekculture/clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-2-af801f162b0d?source=rss-916826c3297b------2</link>
            <guid isPermaLink="false">https://medium.com/p/af801f162b0d</guid>
            <category><![CDATA[navigation]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[jetpack]]></category>
            <category><![CDATA[dynamic-feature-module]]></category>
            <dc:creator><![CDATA[Pedro Okawa]]></dc:creator>
            <pubDate>Thu, 04 Mar 2021 23:36:08 GMT</pubDate>
            <atom:updated>2021-03-08T09:40:44.656Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android — Clean Architecture With Dynamic-Features and Hilt/Dagger2 (Pt.2)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EFy3_z65YWWK_WxahTchZQ.jpeg" /><figcaption>Bologna — Italy (2019)</figcaption></figure><h3>Introduction</h3><p>In our previous article we setup our Gradle file and now we are going to start working on our Navigation. If you haven’t checked yet, take a look at the first part of this series.</p><ul><li><a href="https://pedrookawa.medium.com/clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-1-d1be41a44b12">Part 1.</a></li></ul><p><a href="https://github.com/PedroOkawa/app-blockchain-mkt">PedroOkawa/app-blockchain-mkt</a></p><h3>Navigation</h3><p>Our navigation file is going to include our dynamic features and here we must focus on few details otherwise, you’re going to face some issues (Trust me, I had so many issues with that).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c4b786c57b97aa32e6adb832cec3d1a/href">https://medium.com/media/0c4b786c57b97aa32e6adb832cec3d1a/href</a></iframe><p>As you can see, by adding the dynamic navigation library we have access to the “include-dynamic” object, which requires some parameters to make it work, you may take a look at those parameters on <a href="https://developer.android.com/guide/navigation/navigation-dynamic">Google’s documentation page</a>, but a good hint for you is that you can use the “${applicationId}” to avoid mistyping the path otherwise, you must define it as:</p><pre>app:graphPackage=&quot;com.okawa.blockchain.mkt.stats&quot;</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17edbbe730580266e28eb5bc72d4da90/href">https://medium.com/media/17edbbe730580266e28eb5bc72d4da90/href</a></iframe><p>Inside our feature module, we must define our navigation file, and here’s one of the most important details, the id defined on our “nav_pools” must be the same created on the “nav_home”. In other words, <strong>DO NOT</strong> add the plus sign here:</p><pre>&lt;navigation xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br><strong>    android:id=&quot;@id/nav_pools&quot; &lt;--- DO NOT ADD PLUS SIGN HERE</strong><br>    app:startDestination=&quot;@id/fragment_pools&quot;&gt;</pre><p>If you add the plus sign here you are going to declare a new id for the navigation file and it won’t match, and the result is this one:</p><pre>2021-03-04 18:21:27.913 22314-22314/com.okawa.blockchain.mkt E/AndroidRuntime: FATAL EXCEPTION: main<br>    Process: com.okawa.blockchain.mkt, PID: 22314<br>    java.lang.IllegalStateException: The included &lt;navigation&gt;&#39;s id com.okawa.blockchain.mkt.pools:id/nav_pools is different from the destination id com.okawa.blockchain.mkt:id/nav_pools. Either remove the &lt;navigation&gt; id or make them match.</pre><p>Now we must connect our features and in order to do so, we are going to use the bottom navigation view, first define the menu that is going to be used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4042e12ba4dd7496729ac739d2e001bc/href">https://medium.com/media/4042e12ba4dd7496729ac739d2e001bc/href</a></iframe><p>The ids must also match here, the app will not crash if it doesn’t, but the navigation won’t take any action, as you can see in the next animation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*owtm5UtIbzLTzUMLD_xX7w.gif" /><figcaption>Non-matching ids error</figcaption></figure><p>Now we add the menu to our activity file and programmatically setup the bottom navigation menu using the <strong>setupWithNavController</strong> function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/193f5c1297e08761c1835a398796038e/href">https://medium.com/media/193f5c1297e08761c1835a398796038e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac30db04f3726069e1cd0edf5469fdf1/href">https://medium.com/media/ac30db04f3726069e1cd0edf5469fdf1/href</a></iframe><p>Voilá!! 🎉<br>We now have the dynamic navigation working and for this post that’s all.<br>In our next article, we are going to setup Hilt and Dagger to use the dependency injection.</p><p>Any thoughts ideas or suggestions, please add some comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af801f162b0d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/geekculture/clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-2-af801f162b0d">Android — Clean Architecture With Dynamic-Features and Hilt/Dagger2 (Pt.2)</a> was originally published in <a href="https://medium.com/geekculture">Geek Culture</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android — Clean architecture with Dynamic-features and Hilt/Dagger2 (Pt.1)]]></title>
            <link>https://medium.com/swlh/clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-1-d1be41a44b12?source=rss-916826c3297b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1be41a44b12</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[dagger]]></category>
            <category><![CDATA[multi-module-project]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[dynamic-feature-module]]></category>
            <dc:creator><![CDATA[Pedro Okawa]]></dc:creator>
            <pubDate>Thu, 04 Mar 2021 23:33:43 GMT</pubDate>
            <atom:updated>2021-03-05T22:59:17.076Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android — Clean architecture with Dynamic-features and Hilt/Dagger2 (Pt.1)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3JYkXlluiY0KrEoP5zKw9g.jpeg" /><figcaption>Paris — France (2019)</figcaption></figure><h3>Introduction</h3><p>Dynamic-feature is a well-known approach that Google developed to reduce the APK size by using bundles, it was released in 2018 and it is going to be used in this article to separate our project into feature modules, but there’s something “new” despite the dynamic-feature that we are going to talk about, if you are an android developer, you probably have worked with some kind of Dependency Injection or Service Locator library, like <a href="https://insert-koin.io/">Koin</a>, <a href="https://kodein.org/di/">Kodein</a>, and the most popular… <a href="https://dagger.dev/">Dagger</a>.<br>But last year, Google came up with a new player, announcing a better and easier way to handle dependency injection… <strong>*drum roll*</strong>… <a href="https://developer.android.com/training/dependency-injection/hilt-android">Hilt</a>.</p><p>Hilt is a dependency injection library based on Dagger that reduces the boilerplate of manually defining dependencies in your project. It is not that hard to set up in your project, and Google describes pretty well how to do so, even if your project is <a href="https://developer.android.com/training/dependency-injection/hilt-multi-module">using dynamic features</a>. So, I’m going to show and describe in this article how was my journey to implement an application using Hilt and dynamic features on top of Clean Architecture.</p><p>This will be the first of a series of articles and for now, we are going to tackle the clean architecture and Gradle setup to use dynamic-feature navigation and Hilt.</p><h3>The subject…</h3><p>The application I implemented was a Bitcoin market app that allows the user to access stats, charts, and pools related to updated Bitcoin data. To make the requests, the API used was the <a href="https://www.blockchain.com/api/charts_api">Blockchain Charts API</a>. It is pretty simple and easy to use, and there’s no authorization required (Easy peasy).</p><h4>Github</h4><p><a href="https://github.com/PedroOkawa/app-blockchain-mkt">PedroOkawa/app-blockchain-mkt</a></p><h4>Preview</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*XRkBXDfYEczFbHWiL8lZgQ.gif" /><figcaption>Successful flow</figcaption></figure><p>PS: The GIF quality is not the best, and the colors are looking a little bit different.</p><h3>The structure…</h3><p>The package structure is described on my Github page, and here I’m going to explain a little bit the relationship and structure of the modules:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SgizrAChlTM7s_e2OUuBKg.png" /><figcaption>Architecture diagram</figcaption></figure><p>Each tab on the bottom navigation view represents one feature, so charts, pools, and stats are all dynamic features defined on the app “build.gradle”. Each feature has its own “clean architecture” structure that applies the following approach:</p><ul><li>UI module implements the domain layer to have access to the necessary use cases and implements also the data layer to provide the dependency injection for the repositories (Otherwise we will not be able to provide the repository instances);</li><li>The Data Layer depends only on the domain layer to implements the repository interface defined on the domain layer;</li><li>And finally, the Domain Layer is independent and doesn’t implement any other module.</li></ul><p>The “testutils” module is added using the api keyword to provide classes used on the other features’ unit tests which in my case it is only the Coroutine test rule.</p><h3>And the code…</h3><p>So without further ado, let’s discuss some code and how to implement it. Let’s start with the Gradle dependencies used on our project.</p><p>The first Gradle file we are going to work on is the root build.gradle. This one is fairly simple, we are going to add the hilt classpath to use the hilt plugin.</p><pre>classpath &quot;com.google.dagger:hilt-android-gradle-plugin:$versions.hilt&quot;</pre><p><strong>PS:</strong> My versions are defined inside the libraries.gradle</p><p>Inside our app Gradle file, we are going to apply some changes, so I’m going to describe it step by step (I’m going over the necessary changes to use dynamic-features with Hilt and Dagger, so I’m going to skip things that are not related).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c054727eda4cfedb9facb114b59697c4/href">https://medium.com/media/c054727eda4cfedb9facb114b59697c4/href</a></iframe><p>We need to add the Hilt plugin to declare the dependency.</p><pre>id &#39;dagger.hilt.android.plugin&#39;</pre><pre>applicationVariants.all { variant -&gt;          variant.getCompileConfiguration().exclude module: &#39;testutils&#39;        variant.getRuntimeConfiguration().exclude module: &#39;testutils&#39;<br>}</pre><p>If you are declaring the test utils module into your app build gradle dependencies, it is necessary to remove the “testutils” module from the final APK to avoid build failures due to merge resources, because inside my testutils I’m implementing “kotlinx-coroutines-test” and “junit-jupiter-api” that causes conflicts:</p><pre>Execution failed for task &#39;:app:app:mergeDebugJavaResource&#39;.<br>&gt; A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade<br>   &gt; More than one file was found with OS independent path &#39;META-INF/AL2.0&#39;.</pre><p>The dynamic-features are automatically defined whenever a new dynamic feature is added using AS, but the following code snippet shows how to define dynamic features manually.</p><pre>dynamicFeatures = [&#39;:charts:charts&#39;, &#39;:stats:stats&#39;, &#39;:pools:pools&#39;]</pre><p>Here are all the dependencies that we will need to use dynamic features (including dynamic navigation) with Hilt and Dagger.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9efaf3f894ba909dc8336159fdd3f8e1/href">https://medium.com/media/9efaf3f894ba909dc8336159fdd3f8e1/href</a></iframe><p>As you can see, we also define the new “navigation-dynamic-features-fragment”, this is pretty important to declare our navigation between modules.</p><p>Now you might be asking yourself why am I defining the dagger library since we already have Hilt. 💭</p><p>According to Google, it is still not possible to use Hilt on dynamic feature modules and the suggested approach is to use Dagger. For more info, check the documentation <a href="https://developer.android.com/training/dependency-injection/hilt-multi-module">here</a>.</p><p><strong>PS:</strong> All the dependencies are defined as api to be used by the children features (Including hilt-android because of the EntryPointAccessors), except the “hilt-lifecycle-viewmodel”, which is not going to be used on other modules because we are going to use another approach to inject view models.</p><h4>Feature Gradle</h4><p>Whenever a new dynamic feature is created using Android Studio it already applies the appropriate plugin:</p><pre>id &#39;com.android.dynamic-feature&#39;</pre><p>Now we must define the Kapt Dagger dependencies in our feature Gradle file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f3f262733214eb053fffda1cdb4ad94/href">https://medium.com/media/6f3f262733214eb053fffda1cdb4ad94/href</a></iframe><p><strong>PS: </strong>The dynamic features MUST implement the “app” project</p><p>Great, our Gradle files are all set and ready to help us implement the dynamic feature navigation and the Hilt / Dagger dependency injection. 🎉<br>That’s all for now and in our next post, we are going to talk about navigation.</p><p>Any thoughts ideas or suggestions, please add some comments.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1be41a44b12" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/clean-architecture-with-dynamic-features-and-hilt-dagger2-pt-1-d1be41a44b12">Android — Clean architecture with Dynamic-features and Hilt/Dagger2 (Pt.1)</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android — Retrieving Google Location using RxJava and Clean Architecture]]></title>
            <link>https://pedrookawa.medium.com/android-retrieving-google-location-using-rxjava-and-clean-architecture-36c1017c6529?source=rss-916826c3297b------2</link>
            <guid isPermaLink="false">https://medium.com/p/36c1017c6529</guid>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[google-maps]]></category>
            <category><![CDATA[location]]></category>
            <category><![CDATA[clean-architecture]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Pedro Okawa]]></dc:creator>
            <pubDate>Sun, 17 May 2020 23:31:18 GMT</pubDate>
            <atom:updated>2021-03-09T02:06:48.996Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android — Retrieving Google Location using RxJava and Clean Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iNCh_-3WPmr_06UZ" /><figcaption>Photo by <a href="https://unsplash.com/@andrewtneel?utm_source=medium&amp;utm_medium=referral">Andrew Neel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Working with Google location is not exactly a simple task, but the <a href="https://developers.google.com/location-context/fused-location-provider">FusedLocationProvider API</a> helps and simplifies a lot for us.<br>In this post, I’m going to cover an approach I implemented to retrieve data from Google Location services and show it to the user through a clean architecture project.</p><p>Since we’re following clean architecture, let’s start with our architecture and explain how will it work.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*S4E-UdY6vDdVDoMO93Ugxw.png" /><figcaption>Architecture / Dependencies</figcaption></figure><p>Instead of spending time here explaining the clean architecture concept, I’m going to assume you are already familiarized with that and explain how it works on our scenario.<br>If you do not know how clean architecture works, you could have a look at this nice post about it:<br><a href="https://www.raywenderlich.com/3595916-clean-architecture-tutorial-for-android-getting-started">https://www.raywenderlich.com/3595916-clean-architecture-tutorial-for-android-getting-started</a></p><p><strong>Domain Layer: </strong>The domain layer will take care of the use case (GetLocationUseCase) that will consume the LocationRepository data.<br>This layer will only be implemented by other layers.</p><p><strong>Data layer: </strong>This layer will be responsible for requesting the Google location data itself by using the Fused Location Provider API (<a href="https://developers.google.com/location-context/fused-location-provider">https://developers.google.com/location-context/fused-location-provider</a>), it will be encapsulated inside the GoogleLocationDataSource class.<br>This layer will implement the domain layer.</p><p><strong>App Layer: </strong>Furthermore, we will need to define a bridge from the business logic (Domain) to the UI (Application), which will be handled by the use case injected inside our TrackerViewModel.<br>This layer will implement both layers (Data and Domain) in order to provide the dependency injection we need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*5ghJXuX2LP2fx8iCiDqq0g.png" /><figcaption>Relation between modules</figcaption></figure><p>Without further ado, let’s check how it will be from the code side, starting by the GoogleLocationDataSource class</p><h3>Data source</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff04e15f072c3b29e83b57da0d0b3d08/href">https://medium.com/media/ff04e15f072c3b29e83b57da0d0b3d08/href</a></iframe><p>First, we set up the Google Location API by defining the FusedLocationProviderClient, LocationRequest, and the LocationCallback.</p><p><strong>FusedLocationProviderClient: </strong>Will take care of requesting the location updates using the <em>requestLocationUpdates</em> function and the <em>lastLocation </em>through a listener.<br>PS1: It will allow us to remove the location updates callback also (<em>removeLocationUpdates </em>function).<br><a href="https://developer.android.com/training/location/request-updates">https://developer.android.com/training/location/request-updates</a></p><p><strong>LocationRequest: </strong>Defines the necessary quality of service parameters that will be used by our location provider client, such as interval and priority, you could find more info here: <a href="https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html">https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html</a></p><p><strong>LocationCallback: </strong>It’s pretty straight forward, it will be the callback that will receive the locations data provided by Google Location.<br><a href="https://developers.google.com/android/reference/com/google/android/gms/location/LocationCallback.html">https://developers.google.com/android/reference/com/google/android/gms/location/LocationCallback.html</a></p><p><strong>LocationSubject:</strong> This variable will be in charge of posting the result we receive on <strong>LocationCallback</strong> and will be exposed as a Flowable to the repository.</p><p><strong>LocationObservable: </strong>That’s the bridge that we will define between our data source and the repository. Note that we associated the location request functions to the flowable using the <strong>doOnSubscribe </strong>to start requesting location updates and <strong>doOnCancel </strong>to stop requesting location updates.</p><p><strong>LocationEntity: </strong>It is the object that wraps the latitude and longitude of the location, which should only be accessed from the data layer.</p><h3>Repository</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72a1c78446b0ec8e002455f58126831d/href">https://medium.com/media/72a1c78446b0ec8e002455f58126831d/href</a></iframe><p>The location repository interface will be defined on the domain layer to be used as a protocol by the use case.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c97da94ddfe8af88502b23888f9d6db/href">https://medium.com/media/8c97da94ddfe8af88502b23888f9d6db/href</a></iframe><p>And the repository implementation will be the actual implementation of the repository defined on the data layer and will be created with an injected instance of our <strong>GoogleLocationDataSource </strong>already defined. It must map to a domain layer object (<strong>LocationDomainModel</strong>) since the domain layer is not aware of the data layer and doesn’t have access to the <strong>LocationEntity.</strong></p><h3>UseCase</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a48c24a3f17661fca85e1c3bc566e4c9/href">https://medium.com/media/a48c24a3f17661fca85e1c3bc566e4c9/href</a></iframe><p>The use case will inject the repository interface we defined to be used by our view model.</p><h3>ViewModel</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8b799f349e810bc6667621f0aff0273/href">https://medium.com/media/e8b799f349e810bc6667621f0aff0273/href</a></iframe><p>In our view model, whenever we have granted permission to the location, we use the injected use case to observe the location data, we must map the result to our UI model and post the value using the <strong>_locationModel </strong>livedata.<br><strong>PS:</strong> The baseSubscribe is a function defined on my BaseViewModel, it will be available on my <a href="https://github.com/PedroOkawa/google-location-rx">Github repo</a>.</p><h3>Fragment</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88b30c6014de8b3e2de25f03a02099b4/href">https://medium.com/media/88b30c6014de8b3e2de25f03a02099b4/href</a></iframe><p>And finally, we have our fragment that will handle the Google Maps interface and the location permission. To receive the location data, the only thing we need to do is to observe the <strong>locationModel </strong>observable.</p><h3>Result</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*5uW1nwZh9pUXfL2bRfsUMw.gif" /><figcaption>Application result</figcaption></figure><p><strong>PS:</strong> The location was changed using the emulator location feature.</p><h3>Conclusion</h3><p>The Fused Location API is pretty simple and straightforward, but we must define a proper way to request this data, and using a clean architecture approach could help us on this task by making it more robust and organized.</p><p>For more details, please, check my Github project with this implementation:</p><p><a href="https://github.com/PedroOkawa/google-location-rx">PedroOkawa/google-location-rx</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36c1017c6529" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android — Migrating to Kotlin Script]]></title>
            <link>https://pedrookawa.medium.com/android-experiment-pt-1-kotlin-script-116e7b34da9e?source=rss-916826c3297b------2</link>
            <guid isPermaLink="false">https://medium.com/p/116e7b34da9e</guid>
            <category><![CDATA[gradle-kotlin-dsl]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[kotlin-dsl]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[gradle]]></category>
            <dc:creator><![CDATA[Pedro Okawa]]></dc:creator>
            <pubDate>Tue, 14 Jan 2020 22:28:00 GMT</pubDate>
            <atom:updated>2021-03-06T20:26:22.430Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android — Migrating to Kotlin Script</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7RJ7NNHLmZ8P-VwTTcZk9w.jpeg" /><figcaption>Rudbeckia hirta</figcaption></figure><h3>Introduction</h3><p>This article will cover my experience on migrating one simple project from Groovy Gradle to Kotlin Script.<br>It will cover the setup and process having it working fine to start doing stuff over Kotlin Script in your project. 🎉</p><h3>Android Experiment</h3><p>The project I’m going to use for this post will be a simple Android app that uses the SpaceX API (<a href="https://github.com/r-spacex/SpaceX-API">https://github.com/r-spacex/SpaceX-API</a>). I’m going to split this project into modules to make it a little bit more complex.</p><p>So for the first part of our experiment, I’m going to give an overview of how I set up Kotlin Script in an Android project, for more details please take a look at these references:</p><p><strong>Documentation:</strong> <a href="https://bit.ly/2TjDEwO">https://bit.ly/2TjDEwO</a><br><strong>The New Way of Writing Build Gradle with Kotlin DSL:</strong> <a href="https://bit.ly/388T1fs">https://bit.ly/388T1fs</a><br><strong>I hated Gradle! Kotlin and the buildSrc Plugin made me love it: </strong><a href="https://bit.ly/2tlMM9B">https://bit.ly/2tlMM9B</a></p><p><strong>Github Project link:</strong></p><p><a href="https://github.com/PedroOkawa/android-experiment">PedroOkawa/android-experiment</a></p><p><strong>Disclaimer: </strong>I used the Android Studio 4.0 Preview for this project to use the Jetpack Compose.</p><h3>Kotlin Script</h3><p>A few years ago, Gradle announced support for Kotlin, which I truly believe made almost all Android developers excited about it. Why?! Because it allows us to code on Gradle using the same language we use on our Android codebase.</p><h3>The “amazing” buildSrc</h3><p>One of the most useful things about using Gradle with Kotlin is the <strong>buildSrc</strong> folder. Long short story, everything inside this folder will be built first and will be available to be used on <strong>build.gradle.kts</strong>. You will find a great and detailed post about it on the already mentioned <strong>“I hated Gradle! Kotlin and the buildSrc Plugin made me love it”</strong></p><h3>Setup</h3><p>Before we proceed, we must rename our gradle files to use the <strong>.kts</strong> extension. So, for this example, we’re going to modify the following files:</p><ul><li>settings.gradle ===&gt; settings.gradle<strong>.kts</strong></li><li>build.gradle ===&gt; build.gradle<strong>.kts</strong></li></ul><h3>Settings Gradle</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba80df146f037c1dfcca3a75ca1c35fe/href">https://medium.com/media/ba80df146f037c1dfcca3a75ca1c35fe/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58f225dab779160cde97805285a17aaa/href">https://medium.com/media/58f225dab779160cde97805285a17aaa/href</a></iframe><p>The settings Gradle file is not that different, the main difference is that when using Kotlin Script we must replace all single quote signs <strong>(‘)</strong> by double quote signs <strong>(“) </strong>and the function calls must use parenthesis.</p><h3>Top-Level Build Gradle</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a26119ecbd92a4bb5ce56c1d28ad9b7/href">https://medium.com/media/1a26119ecbd92a4bb5ce56c1d28ad9b7/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da246009bb5ddeaa0b4c88655f382cea/href">https://medium.com/media/da246009bb5ddeaa0b4c88655f382cea/href</a></iframe><p>So, let’s point the differences between these Gradle files. The first thing is that the maven call is an extension function declared on <strong>RepositoryHandlerExtensions</strong> for the <strong>RepositoryHandler</strong> Java Interface</p><pre>fun RepositoryHandler.maven(url: Any) =<br>    maven <strong>{ it</strong>.setUrl(url) <strong>}</strong></pre><p>Also, the classpath must be defined as a function, since it’s using an extension function declared on <strong>ScriptHandlerScope</strong> class for the <strong>DependencyHandler</strong></p><pre>fun DependencyHandler.classpath(dependencyNotation: Any): Dependency? =<br>    add(<em>CLASSPATH_CONFIGURATION</em>, dependencyNotation)</pre><p>Another interesting point is that we can use the kotlin function on <strong>KotlinDependencyExtensions</strong> to declare dependencies defined under <strong>“org.jetbrains.kotlin:kotlin-”</strong></p><pre>fun DependencyHandler.kotlin(module: String, version: String? = null): Any =<br>    &quot;org.jetbrains.kotlin:kotlin-$module${version?.<em>let </em><strong>{ </strong>&quot;:$version&quot; <strong>} </strong>?: &quot;&quot;}&quot;</pre><p>Finally, the clean task definition, in case of Kotlin Script we use the <strong>register </strong>extension function from the <strong>tasks </strong>declared on ProjectDelegate class</p><pre>inline fun &lt;T : org.gradle.api.Task&gt; org.gradle.api.tasks.TaskContainer.`register`(`name`: String, `type`: kotlin.reflect.KClass&lt;T&gt;, `configurationAction`: org.gradle.api.Action&lt;in T&gt;): org.gradle.api.tasks.TaskProvider&lt;T&gt; =<br>    `register`(`name`, `type`.<em>java</em>, `configurationAction`)</pre><h3>Application Build Gradle</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3d33ba55d59dbbb9ac7573bd8f3df2e8/href">https://medium.com/media/3d33ba55d59dbbb9ac7573bd8f3df2e8/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8039bf2619056a1f8522333fd73f7c83/href">https://medium.com/media/8039bf2619056a1f8522333fd73f7c83/href</a></iframe><p>Despite the basic modifications like swapping quotation marks and call functions using parenthesis (In case of attributes must use the equal sign, it’s hard to know which are functions and which are attributes, I did it empirically), we must apply the following changes:</p><ul><li>Apply plugins using the <strong>plugins </strong>closure, it’s possible to declare plugins using convention objects, but according to the Gradle documentation (<a href="https://bit.ly/2tjJIKX">https://bit.ly/2tjJIKX</a>):</li></ul><blockquote>Please avoid using convention objects when writing new plugins. The long term plan is to migrate all Gradle core plugins to use extensions and remove the convention objects altogether.</blockquote><ul><li>The Build types declaration which needs to use the <strong>getByName </strong>function to retrieve types according to the build type name provided</li><li>The <strong>dynamicFeatures</strong> attribution, which needs to declare a mutable set to assign</li><li>For the dependencies, I declared a separate kotlin file and split into <strong>api</strong>,<strong> implementation</strong>,<strong> testImplementation</strong> and <strong>androidTestImplementation</strong>. These dependencies are defined under buildSrc folder (we could do the same by applying Groovy Gradle files).</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94775b3fc33a44271ec3a55982202f95/href">https://medium.com/media/94775b3fc33a44271ec3a55982202f95/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e869b04e4a449dbf6306b45e0f1aef0/href">https://medium.com/media/2e869b04e4a449dbf6306b45e0f1aef0/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9bd3ed96e54f4822ca24b4b0054353af/href">https://medium.com/media/9bd3ed96e54f4822ca24b4b0054353af/href</a></iframe><p>After all these modifications you will be able to build your project using Kotlin Script 🎉 I hope it helped you and see you next time.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=116e7b34da9e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Android — Content Providers + Dependency Injection (Dagger 2)]]></title>
            <link>https://pedrookawa.medium.com/content-providers-dependency-injection-dagger-2-4ee3e49777b?source=rss-916826c3297b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ee3e49777b</guid>
            <category><![CDATA[dagger-2]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <dc:creator><![CDATA[Pedro Okawa]]></dc:creator>
            <pubDate>Wed, 12 Sep 2018 13:08:39 GMT</pubDate>
            <atom:updated>2021-03-06T20:27:28.972Z</atom:updated>
            <content:encoded><![CDATA[<h3>Android — Content Providers + Dependency Injection (Dagger 2)</h3><p>Are you an Android Developer? Do you use Dagger for Dependency Injection? Do you need to implement a Content Provider and don’t want to create a Singleton?<br>Well, that was my situation a few days ago and I didn’t have any idea on how to do so.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*r0DD69bllYd1dJ7n" /><figcaption>My “How can I do it?” reaction</figcaption></figure><p>The project that I worked on consisted on a VoIP mock application, using the phone contacts requested by a CursorLoader and allowing the user to make a “call” to a contact by clicking them. So, I used a ContentProvider and SQLite scripts to store these fake calls on database (Yeah, I know that I could store it using Room, Realm, GreenDAO etc, but I wanted to make it using SQLite). You may check the project here <a href="https://github.com/PedroOkawa/voip-app">https://github.com/PedroOkawa/voip-app</a>.</p><p>The point is, how can I create a ContentProvider in a dependency Injection context? How can I provide a database instance to my ContentProvider? The answer is simple… Dagger 2.</p><p>Dagger has some new amazing features that can help us. These features include the power to allow you inject dependencies into Services, BroadcastReceivers and ContentProviders without making a reference to it in your application Component. You may find these new types described here: <a href="https://google.github.io/dagger/android">https://google.github.io/dagger/android</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*U3z2WvatnocvcSiD" /><figcaption>My “Is that possible?” reaction</figcaption></figure><p>Ok, let’s show some code now. What are the steps that I need to implement it?</p><p>First, you need to include Dagger Android dependency in your gradle file</p><pre>// In my case I used 2.16 version<br>implementation &#39;com.google.dagger:dagger-android:[DaggerVersion]&#39;</pre><p>Second, it’s time to implements the interface HasContentProviderInjector in your custom application class. It will return an instance of an AndroidInjector based on ContentProvider type.</p><p>PS: Note that I called my injection method into attachBaseContext method, because the Provider onCreate method is called before Application onCreate and I got this by this StackOverflow post: <a href="https://stackoverflow.com/a/44413873/2200209">https://stackoverflow.com/a/44413873/2200209</a></p><pre>class App : Application(), HasActivityInjector, <strong>HasContentProviderInjector</strong> {<br><br>    @Inject<br>    lateinit var androidInjector: DispatchingAndroidInjector&lt;Activity&gt;<br><br>    <strong>@Inject<br>    lateinit var contentProviderInjector: DispatchingAndroidInjector&lt;ContentProvider&gt;</strong><br><br>    override fun activityInjector() = androidInjector<br><br>    <strong>override fun contentProviderInjector() = contentProviderInjecto</strong>r<br><br>    override fun attachBaseContext(base: Context?) {<br>        super.attachBaseContext(base)<br>        setupDependencyInjection()<br>    }<br><br>    <em>/**<br>     * Injects the app component<br>     */<br>    </em>private fun setupDependencyInjection() {<br>        DaggerAppComponent.builder().application(this).build().inject(this)<br>    }<br><br>}</pre><p>Third and finally you just have to inject the database into your ContentProvider class by doing:</p><pre>class VoIPAppProvider: ContentProvider() {<br><br>    <strong>@Inject<br>    lateinit var databaseHelper: DatabaseHelper</strong><br><br>    override fun onCreate(): Boolean {<br>        <strong>AndroidInjection.inject(this)</strong><br>        return true<br>    }<br><br>    override fun insert(uri: Uri?, contentValues: ContentValues?): Uri? {<br>        ...<br>        return uri<br>    }<br><br>    override fun query(uri: Uri?, projection: Array&lt;out String&gt;?, selection: String?, selectionArgs: Array&lt;out String&gt;?, sortOrder: String?): Cursor {<br>        ...<br>        return cursor<br>    }<br><br>    override fun getType(uri: Uri?): String {<br>        ...<br>        return type<br>    }</pre><pre>    override fun update(uri: Uri?, contentValues: ContentValues?, selection: String?, selectionArgs: Array&lt;out String&gt;?): Int {<br>        ...<br>        return rowsUpdated<br>    }<br><br>    override fun delete(uri: Uri?, selection: String?, selectionArgs: Array&lt;out String&gt;?): Int {<br>        ...<br>        return rowsDeleted<br>    }<br>}</pre><p>Afterwards, you’ll be fine to use your <strong>databaseHelper</strong> instance along your ContentProvider class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*bVLIOOiTiEDWQmwAmnQ4gQ.gif" /><figcaption>Woooow! We did it!</figcaption></figure><p>I hope it helped anyone that is reading and if you have any suggestion about this post, just tell me. Maybe there are better ways to solve this.</p><p>Thanks to Mick (<a href="https://stackoverflow.com/users/1384374/mick">https://stackoverflow.com/users/1384374/mick</a>) for the answer on StackOverflow and LeEnot (<a href="https://toster.ru/user/LeEnot">https://toster.ru/user/LeEnot</a>) for the answer on <a href="https://toster.ru/q/287891">https://toster.ru/q/287891</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ee3e49777b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>