<?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 Neil MacMillan on Medium]]></title>
        <description><![CDATA[Stories by Neil MacMillan on Medium]]></description>
        <link>https://medium.com/@neilmacmillan?source=rss-34a3c2423d13------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5m-NTYEKYk5uplxyFiB9uw.jpeg</url>
            <title>Stories by Neil MacMillan on Medium</title>
            <link>https://medium.com/@neilmacmillan?source=rss-34a3c2423d13------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 01:00:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@neilmacmillan/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 like it’s 2014]]></title>
            <link>https://medium.com/@neilmacmillan/android-like-its-2014-8a1e401f152e?source=rss-34a3c2423d13------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a1e401f152e</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Neil MacMillan]]></dc:creator>
            <pubDate>Fri, 20 May 2016 16:58:40 GMT</pubDate>
            <atom:updated>2016-05-26T23:54:56.284Z</atom:updated>
            <content:encoded><![CDATA[<p>Java 8 came out in the early part of 2014 bringing Java developers around the world powerful new language features. Some of which have been long awaited and loved in other languages for some time.</p><p>Unless you were an Android developer.</p><p>But Android basically <em>is</em> Java… right? So why can’t I use the latest version of Java?? It’s more like Java as a snapshot in time in the Android platform. Hell, we don’t even get all of Java 7’s language features before KitKat and that version of Java came out in 2011.</p><p>Developing Android in 2016 is like time traveling to 2014. Every. Day.</p><p>Like many of us Android developers, unless we’re actively doing full-stack development on a daily basis, many (or all) of the new Java capabilities have gone unnoticed.</p><blockquote>“If Oracle releases a language update in the forest and Google is too busy being sued by them to notice, did they really release it at all?” -me</blockquote><p>I’ll give you some code snippets from a simple application that displays a Chuck Norris joke from an API requested via Volley. It should be pretty straight forward even if you aren’t familiar with Volley or Chuck Norris.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*id5Vc7K4i-mU2Vx4ypNQeA.png" /></figure><p>Let’s look at <em>some</em> of the things we’ve been missing shall we?</p><h4>Default Methods and Static Method</h4><p>You can actually add implementation to interfaces now!</p><p>An interface for our jokes might look like this, with the default implementation providing a random joke</p><pre>public interface JokeResolver {<br>    static String getBaseUrl() {<br>        return &quot;http://api.icndb.com/jokes&quot;;<br>    }<br><br>    default String getPath() {<br>        return &quot;/random&quot;;<br>    }<br><br>    default String getJokeURL() {<br>        return <em>getBaseUrl</em>() + getPath();<br>    }<br>}</pre><p>However, if you wanted to implement a different type of JokeResolver you can provide your implementation</p><pre>public class BestJokeResolver implements JokeResolver {<br><br>    @Override<br>    public String getPath() {<br>        return &quot;/412&quot;;<br>    }<br>}</pre><h4>Method References</h4><p>Here we have a really common pattern for making network calls. This queues a request for a new Chuck Norris joke and has a success and a failure callback. These are often provided as anonymous inner classes that are going to simply call out to another method. Here we call displayNewJoke() if it was successful or displayError() if it was a failure.</p><pre>private void getNewJokeOld() {<br>    JokeRequest request = new JokeRequest(getJokeResolver(), new Response.Listener&lt;Joke&gt;() {<br>        @Override<br>        public void onResponse(Joke response) {<br>            displayNewJoke(response);<br>        }<br>    }, new Response.ErrorListener() {<br>        @Override<br>        public void onErrorResponse(VolleyError error) {<br>            displayError(error);<br>        }<br>    });<br><br>    mRequestQueue.add(request);<br>}</pre><p>If the entire purpose of all that ugly anonymous inner class setup code is just to get to the point of executing a method, let’s shortcut the whole mess and just provide a reference to the method!</p><pre>private void getNewJokeMethodRef() {<br>    JokeRequest request = new JokeRequest(getJokeResolver(), this::displayNewJoke, this::displayError);<br><br>    mRequestQueue.add(request);<br>}</pre><h4>Lambda Expressions</h4><p>I’ll always think of them as closures, but Oracle says Lambda Expression, so we’ll go with that. Lambda it is.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AMoZmz9jVwONsfTnvsjGIw.jpeg" /><figcaption>Lambda Lambda Lambda!</figcaption></figure><p>Similar to Method References, Lambdas help reduce the eye sore of anonymous inner classes used all over your code when you want to pass implementation to a method. If your method is doing more than just calling a method (where you would just use a Method Reference), say like this</p><pre>private void getNewJokeOld() {<br>    JokeRequest request = new JokeRequest(getJokeResolver(), new Response.Listener&lt;Joke&gt;() {<br>        @Override<br>        public void onResponse(Joke response) {<br>            Log.<em>d</em>(&quot;joke&quot;, &quot;success!&quot;);<br>            displayNewJoke(response);<br>        }<br>    }, new Response.ErrorListener() {<br>        @Override<br>        public void onErrorResponse(VolleyError error) {<br>            Log.<em>d</em>(&quot;joke&quot;, &quot;failure!&quot;);<br>            displayError(error);<br>        }<br>    });<br><br>    mRequestQueue.add(request);<br>}</pre><p>We can apply lambdas to clean it up nicely</p><pre>private void getNewJokeLambdaMultiline() {<br>    JokeRequest request = new JokeRequest(getJokeResolver(), response -&gt; {<br>        Log.<em>d</em>(&quot;joke&quot;, &quot;success!&quot;);<br>        displayNewJoke(response);<br>    }, error -&gt; {<br>        Log.<em>d</em>(&quot;joke&quot;, &quot;failure!&quot;);<br>        displayError(error);<br>    });<br><br>    mRequestQueue.add(request);<br>}</pre><h4>Functional Interfaces</h4><p>This is an annotation (@FunctionalInterface) you can add to an interface which only has one method.</p><p>How many times have you had to make an anonymous inner class of a Runnable just to pass the implementation of run()? With functional interfaces you get to treat that interface with a lambda</p><pre>new Thread(new Runnable() {<br>    @Override<br>    public void run() {<br>        Log.<em>d</em>(&quot;joke&quot;, &quot;multi-threaded logging!&quot;);<br>    }<br>}).start();</pre><p>becomes</p><pre>new Thread(() -&gt; Log.d(&quot;joke&quot;, &quot;multi-threaded logging!&quot;)).start();</pre><h4>Streams</h4><p>This might warrant an article all on its own. The basic idea behind Streams is COMPLETELY overhauling the way you interact with the Java Collections library. It’s amazing and long overdue.</p><p>Here’s a teaser of what you can do from a <a href="http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/">great introduction article</a></p><pre>List<strong>&lt;</strong>String<strong>&gt;</strong> myList <strong>=</strong><br>    Arrays<strong>.</strong>asList<strong>(</strong>&quot;a1&quot;<strong>,</strong> &quot;a2&quot;<strong>,</strong> &quot;b1&quot;<strong>,</strong> &quot;c2&quot;<strong>,</strong> &quot;c1&quot;<strong>);</strong><br><br>myList<br>    <strong>.</strong>stream<strong>()</strong><br>    <strong>.</strong>filter<strong>(</strong>s <strong>-&gt;</strong> s<strong>.</strong>startsWith<strong>(</strong>&quot;c&quot;<strong>))</strong><br>    <strong>.</strong>map<strong>(</strong>String:<strong>:</strong>toUpperCase<strong>)</strong><br>    <strong>.</strong>sorted<strong>()</strong><br>    <strong>.</strong>forEach<strong>(</strong>System<strong>.</strong>out<strong>::</strong>println<strong>);</strong></pre><p>Now you’re sold right? So when do you get to use these in Android?</p><p>NOW.</p><p>sort of.</p><p>it’s complicated.</p><p>You can have all of the Java 8 features above <strong>if</strong> you are developing on a Marshmallow or N device (all are available on N, some are available on M). You also <strong>have</strong> to use a new experimental compiler called <a href="https://source.android.com/source/jack.html">Jack</a>.</p><p>So if you can cut off most of your users and also use an unproven and extremely alpha compiler that (oh btw) doesn’t support <a href="http://tools.android.com/tech-docs/instant-run">Instant Run</a>… this can all be yours!</p><p>Way to sell it Google.</p><h3>Java 8 Retro</h3><p>Now we’re all excited and depressed at the same time and you kind of want to throw it in the bin and become an iOS developer.</p><p>Not so fast.</p><p>Let’s jump ahead to some good news. You can actually do a bunch of these cool things right now! No catch, no experimental compilers, you can actually use many of those new features in your app today.</p><p>With an easy drop-in <a href="https://github.com/evant/gradle-retrolambda">gradle plugin</a> and some quick gradle script setup, you can be rolling with Java 8 on any version of Android.</p><p>Here’s what it can do:</p><ul><li>Lambda Expressions</li><li>Method References</li><li>Default Interface Methods</li><li>Static Interface Methods</li><li>Try-With-Resources statements (Bonus from Java 7!)</li><li>requireNonNull (Bonus from Java 7!)</li></ul><p>Here’s what it can’t do:</p><ul><li>streams</li><li>functional interfaces</li></ul><p>As a note of caveats, the static/default methods are disabled by default as it is “limited support” for the native Java 8 feature and could theoretically break if Java 8 changed how it exposed some of this internally. It works great, but use with caution.</p><h3>Setting up Java 8 Retro:</h3><p>1. Download and install <a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java 8</a></p><p>2. Set Java 8 in Android Studio</p><p><em>File -&gt; Project Structure -&gt; SDK Location</em></p><p>3. Apply retrolambda plugin to gradle</p><p>Add this to your project <em>build.gradle dependencies {}</em> block</p><pre>classpath &#39;me.tatarka:gradle-retrolambda:3.3.0-beta4&#39;</pre><p>Or if you’re uncomfortable running the latest beta, you can also use the latest stable version but you’ll have to forego some Instant Run optimizations.</p><pre>classpath &#39;me.tatarka:gradle-retrolambda:3.2.5&#39;</pre><p>Also make sure you’re using maven in addition to jcenter by adding this to the <em>repositories {}</em> block</p><pre>mavenCentral()</pre><p>4. Tell android studio to run with Java 8</p><p>Add this in <em>build.gradle</em> inside the <em>android {}</em> block</p><pre>compileOptions {<br>    sourceCompatibility JavaVersion.<em>VERSION_1_8<br>    </em>targetCompatibility JavaVersion.<em>VERSION_1_8<br>}</em></pre><p>5. Setup proguard if you use it</p><p>Add this to your proguard rules</p><pre>-dontwarn java.lang.invoke.*</pre><p>6. Optionally enable static/default methods</p><p>Add this to your module <em>build.gradle</em> after <em>android {}</em> block</p><pre>retrolambda {<br>    defaultMethods true<br>}</pre><p>Boom. You’ve got new language features.</p><h4>References</h4><ul><li><a href="https://github.com/neilmofo/javaeightretro">neilmofo/javaeightretro</a></li><li><a href="http://developer.android.com/preview/setup-sdk.html">Set Up the Preview</a></li><li><a href="https://github.com/orfjackal/retrolambda">orfjackal/retrolambda</a></li><li><a href="https://github.com/evant/gradle-retrolambda">evant/gradle-retrolambda</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a1e401f152e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>