<?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 Minh Diep on Medium]]></title>
        <description><![CDATA[Stories by Minh Diep on Medium]]></description>
        <link>https://medium.com/@minhdiep?source=rss-3da607b3ebc0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*r0NVKaWfdh4DqMxR.jpg</url>
            <title>Stories by Minh Diep on Medium</title>
            <link>https://medium.com/@minhdiep?source=rss-3da607b3ebc0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 10:15:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@minhdiep/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[Gradle for Android]]></title>
            <link>https://medium.com/@minhdiep/gradle-for-android-4e749050b02e?source=rss-3da607b3ebc0------2</link>
            <guid isPermaLink="false">https://medium.com/p/4e749050b02e</guid>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Minh Diep]]></dc:creator>
            <pubDate>Tue, 29 Nov 2016 01:43:47 GMT</pubDate>
            <atom:updated>2016-11-29T01:52:23.825Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is Gradle?</h3><p>Gradle is an open source build automation system released in 2007 and primary focuses on Java, Groovy and Scala. It was based on the concept of Apache Ant and Apache Maven; however, it uses domain specific language (DSL), similar to Groovy, instead of XML to configure the project.</p><h3>Gradle for Android</h3><p>Android Studio uses Gradle as a dedicated automation system to build Android Applications; meanwhile, the building task can also be easily executed with Gradle via command line or terminal. Furthermore, developers do not need to install Gradle on their machine to build Android projects since the IDE does come with a Gradle distribution. In fact, Gradle doesn’t include Android functionality by default, but Google stepped in and developed an Android plugin for Gradle. This plugin adds a numerous features specific to Android project such as build types, flavors, signing configurations, library projects… To add the Android plugin in order to the build system, simply insert</p><pre>apply plugin: &#39;com.android.application&#39;</pre><p>into the build.gradle file.</p><h3>Gradle Wrapper</h3><p>Also known as gradlew, Gradle Wrapper allows any client to run Gradle without installing it first. This wrapper uses 2 filesgradle-wrapper.jar and gradlewrapper.properties under the folder gradle/wrapper in the application root in order to start the process. Developers can begin the building process by executing this command:</p><pre>./gradlew build</pre><p>The first run can take few minutes depending on the internet connection; then, any following build will significantly take less time since the build will take cached version.</p><h3>Gradle Tasks</h3><p>With Gradle Wrapper, developers can easily run any supported or custom task in the command line.</p><ul><li>To list all of the available tasks:</li></ul><pre>./gradlew tasks -all </pre><ul><li>To execute a simple task, such as assembling all Debug builds:</li></ul><pre>./gradlew assembleDebug</pre><ul><li>To run all JUnit tests:</li></ul><pre>./gradlew test</pre><blockquote>This single run will execute all of the JUnit tests for all the variants of the application; then, Gradle will generate reports in HTML formats under the app/build/reports/tests folder.</blockquote><ul><li>To run JUnit tests in only one test class:</li></ul><pre>./gradlew &lt;test-name&gt; --tests=&#39;*.&lt;test-class-name&gt;&#39;</pre><ul><li>In case, we don’t use build.gradle as the build file, we can use the -b flag to specify the build file name.</li></ul><pre>./gradlew -b &lt;build-file-name&gt;</pre><h3>Gradle — Adding Library Dependencies</h3><p>Adding other Java Libraries into Android is done in a very sophisticated and clean way with Gradle. There is a dependencies code block in build.gradle file, and developers basically insert any library that they want into the code block in a certain formatted string. The formatted string either starts with compile for developing libraries or testCompile for libraries involved in testing. Then, it will immediately be followed by the library’ group name, library name and version number, and all these variables are separated by colons. For example,</p><ul><li>To add android support library appcompat-v7:</li></ul><pre>compile &#39;com.android.support:appcompat-v7:23.3.0&#39;</pre><ul><li>To add JUnit library for testing:</li></ul><pre>testCompile &#39;junit:junit:4.12&#39;</pre><ul><li>To add library with a jar file in the libs folder:</li></ul><pre>compile files(&#39;libs/&lt;file-name&gt;.jar&#39;)</pre><ul><li>To add all of the libraries under the libs folder:</li></ul><pre>compile fileTree(dir:&#39;libs&#39;, include:&#39;*.jar&#39;)</pre><ul><li>Developers can also list all of the app dependencies tree via gradlew command:</li></ul><pre>./gradlew anDep</pre><h3>Gradle Custom Tasks</h3><p>In Gradle, there is a task block which developers can use to write their own custom task; moreover, Gradle includes a number of existing tasks for us to use out of the box, just by setting some properties. For example, you can copy all of signed apk file to a certain folder by adding this task block:</p><pre>task copySignedApks(type: Copy) {<br>    from(&quot;$buildDir/outputs/apk) {<br>        exclude &#39;**/*unsigned.apk&#39;, &#39;**/*unaligned.apk&#39;</pre><pre>    }<br>    into &#39;../signedApks&#39;</pre><pre>}</pre><p>If we want to add a custom task to a build process, we can use dependsOn property to insert the task. For example, developers can add the above copySignedApks task into their build process so that each time a build is complete, the signed apks can be properly moved to designated folder.</p><pre>task copySignedApks(type: Copy, dependsOn: assemble) {<br>    from(&quot;$buildDir/outputs/apk) {<br>        exclude &#39;**/*unsigned.apk&#39;, &#39;**/*unaligned.apk&#39;</pre><pre>    }<br>    into &#39;../signedApks&#39;</pre><pre>}</pre><p>What it does is making copySignedApks to become dependent on the assemble task. Thus, copySignedApks will only run after assemble task has finished building its apks.</p><p>We can also exclude certain tasks from the building process by using the -x flag</p><pre>./gradlew build -x copySignedApks</pre><h3>Conclusion</h3><p>In fact, this writing can only touches a small portion of functionalities or features in Gradle. As an Android developer, it is our responsibility to gain much knowledge about Gradle so that we can make the developing-testing-releasing cycle to be easier, more efficient and effective.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4e749050b02e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reactive Programming — RxJava 101]]></title>
            <link>https://medium.com/@minhdiep/reactive-programming-rxjava-101-d71454e83752?source=rss-3da607b3ebc0------2</link>
            <guid isPermaLink="false">https://medium.com/p/d71454e83752</guid>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <dc:creator><![CDATA[Minh Diep]]></dc:creator>
            <pubDate>Tue, 20 Sep 2016 00:27:50 GMT</pubDate>
            <atom:updated>2016-09-20T22:56:38.078Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>What is Reactive Programming</strong></h3><p>Reactive Programming is a programming paradigm which involves in composing and consuming a stream of values.</p><h3>Why we need Reactive Programming</h3><p>Nowadays, modern web and mobile applications often require a responsive system which quickly reacts to its users in order to provide the most positive user experience. Think of Google Sheets Web/Mobile app as an example; in fact, each time a cell value get updated, all other cells related to it can be updated simultaneously as well.</p><p>Reactive Programing is based on the principle of pushing, rather than pulling the data; meanwhile, it can perform workloads in parallel, rather than serial. Reactive Programing also has a toolbox of features that can help developers to construct/transform and chain their stream of data in any possible way. This programming paradigm is indeed proven to be very efficient in speed and performance while providing flexibility to developers.</p><h3>What is RxJava</h3><p>RxJava is a Java implementation of Reactive Programming and follows the Observer pattern. It mainly consists of:</p><ul><li><strong>Observable</strong> object which will emit the data or notify certain events.</li><li><strong>Observer</strong> or <strong>Subscriber</strong> object which subscribes and then listens to the data stream or event notification from the <strong>Observable</strong> object.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o3Ey7kPECe_AQZ8_tpkm3Q.png" /></figure><p>In order to understand the fundamental of RxJava, let walk through a simple example of it. First, we need to create an <strong>Observable</strong> object by using the <strong>“create”</strong><em> </em>operator. The <strong>Observable</strong> object will emit a stream of <strong>String</strong> values via <strong>onNext()</strong> method, then notify the subscriber when it’s done with <strong>onCompleted()</strong>.</p><pre>Observable&lt;String&gt; stringObservable = Observable.<em>create</em>(<em>new </em>Observable.OnSubscribe&lt;String&gt;() {<br>    @Override<br>    <em>public void </em>call(Subscriber&lt;? <em>super </em>String&gt; <em>subscriber</em>) {<br>        <em>subscriber</em>.onNext(&quot;I&#39;m Batman&quot;);<br>        <em>subscriber</em>.onNext(&quot;Superman is coming&quot;);<br>        <em>subscriber</em>.onCompleted(); <em>//Notify subscriber this is end of the stream<br>    </em>}<br>});</pre><p>Next, let’s have a <strong>Subscriber</strong> to listen to all these events</p><pre>Subscriber&lt;String&gt; subscriber = <em>new </em>Subscriber&lt;String&gt;() {<br>    @Override<br>    <em>public void </em>onCompleted() {<br>        <em>//This get called when the observable object get done with its streaming<br>    </em>}<br><br>    @Override<br>    <em>public void </em>onError(Throwable <em>e</em>) {<br>        <em>//This get called when an error occurs and the whole stream will stop.<br>    </em>}<br><br>    @Override<br>    <em>public void </em>onNext(String <em>s</em>) {<br>        System.out.println(<em>s</em>);<br>    }<br>};<br>stringObservable.subscribe(subscriber);</pre><p>And that’s it, it will print “I’m Batman”, then “Superman is coming” right when the <strong>Observable</strong> calls <strong>subscribe(subscriber)</strong>. In fact, we often don’t care about the event of stream completion or error; thus, we can simplify this a bit by not having a <strong>Subscriber</strong>, but rather using an <strong>Action1</strong> object.</p><pre>stringObservable.subscribe(<em>new </em>Action1&lt;String&gt;() {<br>    @Override<br>    <em>public void </em>call(String <em>s</em>) {<br>        System.out.println(<em>s</em>);<br>    }<br>});</pre><h3>RetroLambda</h3><p>Do we need RetroLambda for RxJava? No, but it will be very nice to have it since it helps to remove a lot of unnecessary code, and makes our code look better/cleaner/nicer. With the simple example above, we can rewrite it with RetroLambda</p><pre>stringObservable.subscribe(<em>s </em>-&gt; {System.out.println(<em>s</em>);});</pre><p>Or perhaps, we can also take the advantage of Method Reference:</p><pre>stringObservable.subscribe(System.out::println);</pre><p>You can find more information about RetroLambda at <a href="https://github.com/orfjackal/retrolambda">https://github.com/orfjackal/retrolambda</a></p><h3>Operators</h3><p>Up till now, we have been discussing about emitting and receiving data, but what about modifying or transforming these data values before they ultimately got delivered to the <strong>Subscriber</strong>. This is where RxJava operators fit perfectly in the whole big picture. In fact, RxJava offers a wide range of operators, but we would rather focus on these two: <strong>map()</strong> and <strong>flatmap()</strong>.</p><p><strong>map(): </strong>is used to transform one value to another value regardless of type. For example, the <strong>Observable</strong> object below will emit a stream of values, which are “Batman”, “Superman”, “Wonder Woman” (Type <strong>String</strong>) and the map operator will map each value to its character length (Type <strong>Integer</strong>); then, these integer values will be pushed to the <strong>Subscriber</strong> to consume.</p><pre>Observable.<em>just</em>(&quot;Batman&quot;, &quot;Superman&quot;, &quot;Wonder Woman&quot;)<br>        .map(<em>new </em>Func1&lt;String, Integer&gt;() {<br>            @Override<br>            <em>public </em>Integer call(String <em>s</em>) {<br>                <em>return s</em>.length();<br>            }<br>        })<br>        .subscribe(<em>new </em>Action1&lt;Integer&gt;() {<br>            @Override<br>            <em>public void </em>call(Integer <em>integer</em>) {<br>                System.out.println(&quot;Number of characters = &quot; + <em>integer</em>);     <br>            }<br>        });</pre><p>Again, we can just use RetroLambda to simplify all these</p><pre>Observable.<em>just</em>(&quot;Batman&quot;, &quot;Superman&quot;, &quot;Wonder Woman&quot;)<br>        .map(<em>s </em>-&gt; <em>s</em>.length())<br>        .subscribe(<em>integer </em>-&gt; {<br>            System.out.println(&quot;Number of characters = &quot; + <em>integer</em>);     <br>        });</pre><p><strong>flatmap():</strong> the <strong>map()</strong> operator is indeed powerful in helping us to manipulate stream of data, but it limits in just transforming from one value to another value of type <strong>T</strong>. Meanwhile, the <strong>flatmap()</strong> can take on one value of the stream and return an <strong>Observable&lt;T&gt;</strong>, meaning each values from the new <strong>Observable</strong> can be separately pushed further down the pipeline. Therefore, <strong>flatmap()</strong> is perfectly suitable to make asynchronous call inside the method.</p><p>Let take a dive into <strong>flatmap()</strong> with the below example, in which, a list of url strings are given to us, and we have to find out what server each url belongs to. The example takes 5 simple steps to do; thus, demonstrate how powerful and flexible RxJava can be:</p><pre><strong>- First, we just wants a list of url strings for the input:</strong><br>Observable.<em>fromCallable</em>(() -&gt; Arrays.<em>asList</em>(“https://www.google.com/&quot;, “https://www.yahoo.com/&quot;, “https://www.microsoft.com/en-us/&quot;))</pre><pre><strong>- Second, transform the list of url strings to an Observable object so that it can emit the values one by one:<br></strong>flatMap(<em>urls </em>-&gt; Observable.<em>from</em>(<em>urls</em>))</pre><pre><strong>- Third, for each url, we use the method HttpUrl.getServerName(String url) to retrieve the server name, and construct the output string:<br></strong>flatMap(<em>url </em>-&gt; Observable.<em>just</em>(<em>url </em>+ &quot; is hosted at: &quot; + HttpUrl.<em>getServerName</em>(<em>url</em>)))</pre><pre><strong>- Fourth, set up the call to run on IO thread:</strong><br>subscribeOn(Schedulers.<em>io</em>())</pre><pre><strong>- Finally, create a subscriber to listen to the event and output the results:<br></strong>subscribe(<em>s </em>-&gt; {System.out.println(<em>s</em>);})</pre><p>At last, we can chain all these method calls all in one to achieve our mission.</p><pre>Observable.<em>fromCallable</em>(() -&gt; Arrays.<em>asList</em>(&quot;https://www.google.com/&quot;, &quot;https://www.yahoo.com/&quot;, &quot;https://www.microsoft.com/en-us/&quot;))<br>        .flatMap(<em>urls </em>-&gt; Observable.<em>from</em>(<em>urls</em>))<br>        .flatMap(<em>url </em>-&gt; Observable.<em>just</em>(<em>url </em>+ &quot; is hosted at: &quot; + HttpUrl.<em>getServerName</em>(<em>url</em>)))<br>        .subscribeOn(Schedulers.<em>io</em>())<br>        .subscribe(<em>s </em>-&gt; {System.out.println(<em>s</em>);});</pre><h3>Schedulers</h3><p>Multi-threading development has never been an easy task; especially with modern web or mobile application. In fact, RxJava can make developing multi-threads application so simple by instructing its <strong>Observable</strong> objects and operators to run on particular <strong>Schedulers</strong> (or in other words, Threads).</p><p>By default, the <strong>Observable</strong> object and its operators get executed on the same thread as the <strong>subscribe()</strong> method get called. However, we can use the <strong>subscribeOn()</strong> operator to tell the <strong>Observable</strong> object where to execute its operations. For example, let’s say an Android app needs to do an intensive data computation, it can use <strong>subscribeOn(Schedulers.computation())</strong> so that all of the heavy computational work will be done on a dedicated thread; then, all of the results will be notified to its observers on the android main UI thread via <strong>observeOn(AndroidSchedulers.mainThread())</strong>. Or with the above <strong>flatmap()</strong> example, we indeed used <strong>Schedulers.io()</strong> to schedule those operations to be done on an IO-bound work thread.</p><h3>Subscription</h3><p>When a <strong>Subscriber</strong> subscribes to an <strong>Observable</strong> object, it will return a <strong>Subscription</strong>.</p><pre>Subscription subscription = Observable.<em>just</em>(&quot;Batman&quot;, &quot;Superman&quot;, &quot;Wonder Woman&quot;)<br>        .map(<em>s </em>-&gt; <em>s</em>.length())<br>        .subscribe(<em>integer </em>-&gt; {<br>            System.out.println(&quot;Number of characters = &quot; + <em>integer</em>);     <br>        });</pre><p>This <strong>Subscription</strong> can be used to unsubscribe or unlink between the <strong>Subscriber</strong> and the <strong>Observable</strong>. Furthermore, when the <strong>unsubscribe()</strong> method get called from a <strong>Subscription</strong>, it will immediately terminate the chain of operations; thus, making the app to be efficient and not wasteful on its power resource. Specially with Android app, <strong>subscription.unsubscribe()</strong> can be easily called in <strong>onStop()</strong> so that we don’t have to worry about any memory-leaks or crashes.</p><h3>Few Extra Words</h3><p>RxJava is indeed proven to be extremely powerful while giving developers a great flexibility to get their development done right. Some other well-known libraries such as: Retrofit, RxAndroid and RxRelay can integrate very well and make RxJava to become super useful in developing Android application. However, RxJava does require a steep learning curve and often needs developers to stop thinking in their imperative programming world, but instead immerse themselves in the world of Reactive Programing to make their imagination perhaps runs wild and comes true.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d71454e83752" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>