<?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 Łukasz Wasylkowski on Medium]]></title>
        <description><![CDATA[Stories by Łukasz Wasylkowski on Medium]]></description>
        <link>https://medium.com/@wasyl?source=rss-c635d6fd0ed4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IaKwMFOeTCf5flVDl18lXw.jpeg</url>
            <title>Stories by Łukasz Wasylkowski on Medium</title>
            <link>https://medium.com/@wasyl?source=rss-c635d6fd0ed4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 20:47:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@wasyl/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[What happens when you subscribe to a stream? A look inside RxJava]]></title>
            <link>https://medium.com/@wasyl/what-happens-when-we-subscribe-to-rxjava-stream-d2d430febfd4?source=rss-c635d6fd0ed4------2</link>
            <guid isPermaLink="false">https://medium.com/p/d2d430febfd4</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[reactive-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Łukasz Wasylkowski]]></dc:creator>
            <pubDate>Mon, 02 Sep 2019 15:27:32 GMT</pubDate>
            <atom:updated>2019-09-03T08:05:08.503Z</atom:updated>
            <content:encoded><![CDATA[<h3>What happens when we subscribe to a stream? A look inside RxJava</h3><p>Using RxJava seems rather simple: we create a stream, apply some operators, and then subscribe. But as our streams get more and more complex and we start using more advanced operators, sometimes we end up with behavior we don’t quite understand. This is one of several posts I have planned, that aim to clarify some of the inner workings of RxJava.</p><p>The goal of this post is not to explain how RxJava works in fine detail, neither to teach how to write custom operators. The flow presented is a simplified model which ignores some advanced elements like operator fusion (optimizing specific pairs of operators) or thread safety (and threading in general). Also even though in this post I only talk about Observables, other stream classes — Completable, Single and Maybe — use the same concept.</p><h3>RxJava chains</h3><p>A typical example of RxJava usage would look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9b873c70194da507f66502625d78611/href">https://medium.com/media/d9b873c70194da507f66502625d78611/href</a></iframe><p>We can distinguish four key parts:</p><ol><li>An observable <strong>source</strong> — some function or factory method that provides us with an initial Observable instance</li><li>A number of chained <strong>operators</strong> which manipulate the stream values and its behavior</li><li>The <strong>subscription*</strong></li><li>Events <strong>callbacks</strong>*</li></ol><p><em>*It’s an important distinction, as we’ll see — the subscription happens immediately after calling </em><em>subscribe, while events callbacks can happen anytime after that. But we’ll get to that.</em></p><p>While this is a pretty simple example, there’s quite a lot going on behind the scenes.</p><h3>Creating an observable</h3><p>First, Observable.fromArray(1, 2, 3, 4, 5) creates and returns an Observable instance, in this case ObservableFromArray specifically. This is an Observable subclass that, when subscribed to, will immediately emit given values one by one. Not much is happening here, but we’ll get back to this class later. The point is, this is one of many ways how we can acquire the initial Observable instance, which will be the source of our stream. Also even though this particular observable will emit values immediately, most will emit at some later point in time (or never).</p><h3>Applying operators</h3><p>Then we have the operators. RxJava applies all of them in a very similar fashion, so let’s look at map and take as examples:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67a8af3ecce44b088de4872fe59ecd4d/href">https://medium.com/media/67a8af3ecce44b088de4872fe59ecd4d/href</a></iframe><p>We can see that the structure of both is similar. If we discard annotations with metadata and onAssembly call (used by plugins to track where operators are applied), we’re left with the following template (in Kotlin, so we define an extension function instead of instance method):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11ae99bd160fefb678fdec147b44d1a8/href">https://medium.com/media/11ae99bd160fefb678fdec147b44d1a8/href</a></iframe><p>This is how most of the operators calls will look like. We can see that:</p><ul><li>each operator will have a corresponding Observable subclass, e.g. ObservableTake, ObservableMap, ObservableFlatMap and so on;</li><li>to create an instance of the operator’s observable, we need to pass the necessary parameters and the <em>source</em> observable (this);</li><li>this new observable is returned from the operator function. This is because observables are <em>immutable — </em>applying an operator doesn’t <em>change</em> any already existing observable<em>;</em></li><li>operator function declares its return value as an Observable, so the specific observable class of the operator we just applied is hidden;</li><li>since all operators are declared on the Observable class, we’re free to apply subsequent operators right away.</li></ul><p>The most important thing here is that<strong> each operator will wrap current observable in the newly created one, </strong>so we effectively end up with a<strong> nested tree of observables.</strong> Each observable has a <em>parent </em>source observable and some (or no) parameters. Here’s how we would create our observable chain if we were creating the objects manually:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/397935697c07cc76e330a7be0fa7bcc3/href">https://medium.com/media/397935697c07cc76e330a7be0fa7bcc3/href</a></iframe><p>Fortunately we don’t have to do that 😬. But this is the structure we get when applying operators. We can make couple of interesting observations here:</p><ul><li>in the end we only have reference to the observable returned by the <em>last</em> chained call — take operator was the last in chain, so its observable is the outermost one in the final tree;</li><li>each operator observable only has reference to its direct parent (source) observable;</li><li>ObservableFromArray doesn’t receive a source — it doesn’t need one, since it <em>is</em> the source of the events.</li></ul><p>So how do we subscribe to the entire chain given that we only have reference to the outermost observable? How does the data flow through the stream if every observable only contains reference to one parent observable?</p><h3>Subscription</h3><p>Calling subscribe method is the key point of all RxJava-based code. It establishes a subscription and allows actual flow of events to which we can react. We usually call subscribe with handlers for events like onNext or onError, or with an Observer instance. But we should know that even if we pass only some handlers (or none), RxJava will still wrap them in a LambdaObserver instance. This means that <strong>each operator’s </strong><strong>subscribe method always receives a single </strong><strong>Observer instance</strong>, regardless of what permutation of onNext, onError and onComplete consumers we&#39;ve passed when subscribing initially.</p><h4>Subscribing to a chain</h4><p>As we already know, we’re always dealing with <em>chains</em> of observables. So how can we subscribe to just one of them? The answer is: we can’t. That is, even though we explicitly only subscribe to one observable (the last one in the chain), that observable will, in turn, subscribe to its parent. Its parent will subscribe to <em>its</em> parent, and so on, until the beginning of the stream is reached.</p><h4>Upstream and downstream</h4><p>This is good time to start naming things right. Before the subscription happens, all we have is a chain of immutable observables. There’s nowhere the events could come from, and nowhere they could go anyway — just Observables.</p><p>But as subscribe methods are executed, one by one, they each create a new subscription: one that links that observable’s source with an observer it received. This is how the actual <em>stream</em> is established — events flow from observer to observer, starting with the source observable, and ending at the observer we pass to the initial subscribe method.</p><p>So now, for every observable, <strong><em>upstream</em> is where the events come from</strong> — parent observable’s subscription. <strong><em>Downstream</em> is where the events should be delivered to</strong>, and it’s the observer received in subscribe.</p><h4>Operator logic</h4><p>But it wouldn’t make much sense if the operators simply took values from upstream and passed them downstream. This is why in between, there’s observable’s custom logic, which acts as a middle-man between the <em>upstream </em>and <em>downstream. </em>Where does it come from?</p><p>We know that each subscription requires an observer, and that each observable will subscribe to its parent. So what observer will it use? Typically each observable will have its own custom Observer subclass, which receives emissions from upstream, acts on them, and sends them downstream.</p><p>Let’s see how a <em>very simplified</em> ObservableTake class would look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0956d91cc4c796a6a2b558a72e70773/href">https://medium.com/media/a0956d91cc4c796a6a2b558a72e70773/href</a></iframe><p>So much going on! Let’s break down what happens when we call subscribe on the ObservableTake:</p><ul><li>inside subscribe method, the observable will subscribe to its parent;</li><li>the new subscription will use custom TakeObserver that wraps observer passed to subscribe method;</li><li>TakeObserver will execute logic to fulfill take operator’s purpose: every time <em>upstream</em> emits an event, TakeObserver will consider whether to pass it to the <em>downstream</em> or not, and whether the <em>upstream</em> subscription still makes sense or should be disposed.</li></ul><p>Similarly, onErrorReturn operator, which can be used to <em>map</em> errors to onNext events, will provide an onError implementation that doesn’t signal downstream.onError — instead it would call downstream.onNext(value). map observer will have custom onNext implementation, which calls onNext but with value returned by the map function. Some operators can also have <em>two</em> upstreams, like withLatestFrom or merge— they would subscribe to both upstreams and combine their events accordingly.</p><p>This is how each observable installs custom logic in its place in the stream — it subscribes to the source observable using a <strong>custom observer, </strong>which<strong> receives events from upstream, modifies them and sends them downstream</strong>.</p><h4>Wrapping observers</h4><p>We saw that each observable uses custom observer to wrap the one it received in subscribe call, but let’s see why this works.</p><p>Since we call subscribe on the last (outermost) observable in the chain, it will be the first one to create a custom observer that wraps the one we pass. This new observer will then in turn be passed to its parent, which will wrap it in its own custom observer, and so on. This means that <strong>the last observable to wrap some observer is the <em>first</em> operator in the chain</strong>. In our example, the wrapped observers would look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/679cf807a6c2e90b2e60205be3dc7077/href">https://medium.com/media/679cf807a6c2e90b2e60205be3dc7077/href</a></iframe><p>MapObservable was the deepest one in the observables tree, but its MapObserver is the outermost one. And this of course makes perfect sense — we want operators to be applied in the order we write them. Map was first in the chain, so it should be the first to apply its logic.</p><h4>Emissions</h4><p>Now when FromArrayObservable emits values, it passes them to its downstream — the MapObserver, which will execute its logic, and in turn pass mapped values further to <em>its</em> downstream — DoOnNextObserver. That one will perform an action on each doOnNext call, and pass all events down to TakeObserver, and so on. <strong>Each observer only receives values from its upstream, and only emits values directly to its downstream.</strong></p><p>This is why we sometimes read that that in RxJava the subscription happens upstream, and emissions downstream. First we subscribe, from the bottom up, establishing a stream. Then that stream — connected observers — will be used to pass values from the source to our observer. Whenever an operator swallows an event, it simply disappears — but only downstream of the operator that swallows it.</p><p>Also notice that the source observable can hold onto the observer it was given. This way it can emit events at <em>any</em> point in time after it was subscribed to (and until it’s been disposed). But any time observer.onXXX method is called by the source observable, that event travels through all observers in the stream — until it’s swallowed or emitted to the initial observer we used to subscribe.</p><h3>That’s it</h3><p>At least for now — there’s still a lot more to uncover. But I hope this post was helpful in understanding the basics how RxJava’s streams work under the hood. While it doesn’t seem important when simply using the library in day-to-day development, it’s crucial to understand some of the more advanced topics: like how and why subscribeOn and observerOn work? How to distribute emitted values among several threads? Why stream with flatMap sometimes emits on the “<em>wrong”</em> thread? How to avoid creating a subscription when we don’t need it? I’ll try to answer these and other questions in future posts 🙂</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d2d430febfd4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RxJava threading: when subscribeOn doesn’t work]]></title>
            <link>https://medium.com/@wasyl/rxjava-threading-when-subscribeon-doesnt-work-ee467a21935b?source=rss-c635d6fd0ed4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee467a21935b</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[rxjava]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[threading]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Łukasz Wasylkowski]]></dc:creator>
            <pubDate>Thu, 30 May 2019 12:37:05 GMT</pubDate>
            <atom:updated>2019-12-16T14:56:04.882Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*CPCAI_DQLK6A3TE3" /></figure><p>As we know, one of RxJava’s important features is that we can easily manage threads by using subscribeOn and observeOn operators. In fact, it&#39;s been a go-to solution for handling background operations on Android for some time now. However, this use case of RxJava comes with an easy to miss caveat: sometimes subscribeOn doesn&#39;t work as you expect! Let&#39;s see <em>when</em> and <em>why</em> on a couple of examples.</p><h4>Observable#create and emitters</h4><p>When using RxJava we can pass a scheduler to a subscribeOn operator, which will cause the subscription to run on a thread provided by that scheduler. Normally the subscription will emit items on the same thread it&#39;s called. But nothing prevents us from emitting items from a different thread! Let&#39;s try it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/222369d1ad450fc24026d6b6c162a423/href">https://medium.com/media/222369d1ad450fc24026d6b6c162a423/href</a></iframe><p>And the result:</p><pre>[RxComputationThreadPool-1] Subscribing<br>[Main thread] 1 - emitting<br>[Main thread] 1 - performing an expensive computation<br>[Main thread] 2 - emitting<br>[Main thread] 2 - performing an expensive computation<br>[Main thread] 3 - emitting<br>[Main thread] 3 - performing an expensive computation</pre><p>As we see in the output, both the <em>emission</em> and the <em>mapping</em> are performed on the newly created thread, and not one from the computation scheduler. This means that yes, RxJava <em>will</em> manage threading, but it will do it only as long as you <strong><em>don&#39;t change the threads manually</em></strong>.</p><p>In this case, the subscription does happen on the thread from computation scheduler, as we can read in the output. But once we emit values — even from a new thread — RxJava will happily take those emissions and pass them further in the stream. It won’t change the thread. Because why would it? The subscribeOn operator did its work — the subscription already happened on a different thread. There&#39;s no reason RxJava would change the thread again.</p><p>The example above might’ve been obvious, because we’ve clearly created a new thread. A straightforward conclusion is to simply <em>not do that</em>, we rely on RxJava to do the threading after all! But what about third party libraries? A good example is Google Play Services that we can use on Android to get user’s location. Let’s have a look at how we’d wrap callback-based location emissions in an Observable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7bc40e123720f12f4a86272e1897d07d/href">https://medium.com/media/7bc40e123720f12f4a86272e1897d07d/href</a></iframe><p>Pretty standard — we create a callback instance, set cancellable to unregister it when necessary, start listening to updates and push them to the stream. When we receive new location, we want to fetch some data for it from the network, so we&#39;re using map operator for that. Finally, we want everything to happen on an io thread, so we subscribe on it, and should be fine, right?</p><p>But what is this Looper.getMainLooper()? Seems like the location client uses the main thread somewhere, no big deal, eh? Only as you probably expect, it is pretty big deal if it affects the thread on which the emissions happen.</p><p>If we have a look at the location client’s documentation, we can read:</p><blockquote>Callbacks for LocationCallback will be made on the specified thread, which must already be a prepared looper thread.</blockquote><p>Which means, our onLocationResult method is called on the main thread. Thus no matter what thread we subscribe on, the emissions always happen on the main thread, <strong>because that&#39;s where the library executes the callback</strong>. The result is we’re emitting location on the main thread, and so the network call is also happening on the main thread. Fortunately, the above code would crash at that point, but sometimes this behavior is subtly blocking UI or causing difficult to debug bugs.</p><p>To achieve the behavior we want, we have to change the thread after the emission, using observeOn operator. This case is especially tricky if you want to encapsulate your code and always emit values on the subscription scheduler. Because an observable doesn&#39;t have explicit access to the subscription scheduler, it&#39;s difficult to emit values on it. There&#39;s a <a href="https://github.com/ReactiveX/RxJava/issues/941">somewhat related issue</a> that&#39;s unfortunately closed for some time now.</p><h4>PublishSubject</h4><p>There’s another case when the thread change and subscribeOn behavior isn&#39;t obvious: subjects. With subjects we first establish a stream, to which we can later emit values. It&#39;s not difficult to figure out what can go wrong here: <strong>if we call </strong><strong>onNext on the subject from a certain thread,</strong> <strong>that thread is used for the emission</strong>, regardless of the scheduler we used to subscribe. This is also stated <a href="https://github.com/ReactiveX/RxJava/blob/d7d0a33e04e4b409789fc67ec94883bf0e828c64/src/main/java/io/reactivex/subjects/PublishSubject.java#L65"><em>right there in the documentation</em></a>.</p><p>Let’s consider an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1bb0d00f6028db245342ed155eb13bd/href">https://medium.com/media/b1bb0d00f6028db245342ed155eb13bd/href</a></iframe><p>The result aren’t surprising by now:</p><pre>[Test worker] 1 - I want this happen on an IO thread<br>[Test worker] 1 - I want this happen on a new thread<br>[Test worker] 2 - I want this happen on an IO thread<br>[Test worker] 2 - I want this happen on a new thread<br>[Test worker] 3 - I want this happen on an IO thread<br>[Test worker] 3 - I want this happen on a new thread</pre><p>To achieve what we want, which is handling the emissions on a separate thread, we need to use observeOn in place of the subscribeOn calls. This makes RxJava change the thread after the item was emitted, regardless of the emission thread:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fb594f0724ba3b41473e50d1b1b4f2d/href">https://medium.com/media/5fb594f0724ba3b41473e50d1b1b4f2d/href</a></iframe><p>The result then is as expected:</p><pre>[RxNewThreadScheduler-1] 1 - I want this happen on a new thread<br>[RxCachedThreadScheduler-1] 1 - I want this happen on an IO thread<br>[RxNewThreadScheduler-1] 2 - I want this happen on a new thread<br>[RxCachedThreadScheduler-1] 2 - I want this happen on an IO thread<br>[RxNewThreadScheduler-1] 3 - I want this happen on a new thread<br>[RxCachedThreadScheduler-1] 3 - I want this happen on an IO thread</pre><h4>BehaviorSubject</h4><p>Things are slightly bit different with BehaviorSubjects, where subscribeOn <em>can</em> sometimes affect the emission thread. Let&#39;s recall how a BehaviorSubject works: for each onNext called on the subject, it will notify all current subscribers. At the same time it will cache this last emission, and replay it for the <em>new</em> subscribers. So, there are two scenarios in which an emission can happen: when we explicitly call onNext, and when someone subscribes to the subject.</p><p>Let’s see this on an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70fcc31869da3a3d1ff0c62ad9019ba0/href">https://medium.com/media/70fcc31869da3a3d1ff0c62ad9019ba0/href</a></iframe><p>and the output:</p><pre>[Test worker] 1 - First observer<br>[RxCachedThreadScheduler-1] 1 - Second observer<br>[RxNewThreadScheduler-2] 1 - Third observer<br>[Test worker] 2 - First observer<br>[Test worker] 2 - Second observer<br>[Test worker] 2 - Third observer<br>[Test worker] 3 - First observer<br>[Test worker] 3 - Second observer<br>[Test worker] 3 - Third observer</pre><p>Now that’s interesting! 1 was emitted on 3 different threads! Why? Let&#39;s go one by one:</p><p>First observer was already subscribed when we emitted first value. Since the subscription code has already completed by the time we called onNext, there&#39;s no reason for subscribe scheduler to have any effect. When we call onNext in this scenario, it&#39;s analogous to how PublishSubject works — observers are notified on the same thread that called onNext.</p><p>Second and third observers only subscribed <em>after</em> the initial onNext. This is where BehaviorSubject&#39;s behavior kicks in — for any <em>new</em> subscriber, it will <em>replay the latest emission</em>. So for these two observers it&#39;s no longer <em>our</em> onNext call that emits 1. Rather, the BehaviorSubject that notices it has some value cached, and <strong>emits it as part of the subscription</strong>. As such, the subscription scheduler is respected, and the observers are notified on a thread that it provides.</p><p>All subsequent emissions happen outside of subscription, so the values are emitted on the same thread as onNext, again, similarly to how PublishSubject works. Also the same rules apply — specifiying scheduler using observeOn will ensure you receive the value on the appropriate thread.</p><h4>Conclusion</h4><p>I hope this clears up some of RxJava’s behavior and helps avoid subtle threading bugs. While they won’t necessarily crash your app, they may silently crowd your main thread or cause unexpected results. When using streams spanning across multiple app layers, subjects and third party libraries, it’s quite easy to miss that the thread isn’t necessarily what you expect it to be. So keep an eye out, don’t change the thread yourself (unless you know exactly what you’re doing), and pay attention to third party libraries’ threading behavior!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee467a21935b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pretty tests summary in Gradle]]></title>
            <link>https://medium.com/@wasyl/pretty-tests-summary-in-gradle-744804dd676c?source=rss-c635d6fd0ed4------2</link>
            <guid isPermaLink="false">https://medium.com/p/744804dd676c</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[report]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[summary]]></category>
            <category><![CDATA[logging]]></category>
            <dc:creator><![CDATA[Łukasz Wasylkowski]]></dc:creator>
            <pubDate>Sun, 16 Sep 2018 20:25:39 GMT</pubDate>
            <atom:updated>2018-09-16T20:25:39.510Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to collect and print out tests summary in Gradle (*not the popular version)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aQFJQDoa9hJTjFa8" /><figcaption>Irrelevant but pretty image to grab attention (by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> from <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>There’s this <a href="https://stackoverflow.com/a/36130467/1349855">popular answer on Stack Overflow</a> that makes Gradle show tests output during build execution. Here’s the default output when running tests:</p><pre>▶ ./gradlew test</pre><pre>[...]<br>BUILD SUCCESSFUL in 1s<br>13 actionable tasks: 2 executed, 11 up-to-date</pre><p>And here’s the output with the mentioned snippet somewhere in our build.gradle:</p><pre>▶ ./gradlew test</pre><pre>&gt; Task app:test</pre><pre>com.example.tests.AnotherTestClass &gt; anotherPassingTest1 PASSED</pre><pre>com.example.tests.AnotherTestClass &gt; anotherPassingTest2 PASSED<br>.<br>. (lots of tests here)<br>.<br>com.example.tests.AnotherTestClass &gt; anotherPassingTest13 PASSED</pre><pre>com.example.tests.AnotherTestClass &gt; anotherPassingTest14 PASSED</pre><pre>com.example.tests.SomeTestClass &gt; aPassingTest1 PASSED<br>.<br>. (lots of tests here)<br>.<br>com.example.tests.SomeTestClass &gt; aPassingTest13 PASSED</pre><pre>com.example.tests.SomeTestClass &gt; aPassingTest14 PASSED</pre><pre>--------------------------------------------------------------------<br>| Results: SUCCESS (28 tests, 28 successes, 0 failures, 0 skipped) |<br>--------------------------------------------------------------------<br></pre><pre>BUILD SUCCESSFUL in 1s<br>13 actionable tasks: 2 executed, 11 up-to-date</pre><p>Overall it’s pretty neat, but it shows tests result for each single test, cluttering the console if you have hundreds of unit tests. Additionally the tests summaries show for each test task, right after it has completed. When you have several modules and run tests for all of them in single Gradle build, the summaries are scattered all over the output, interleaved with other print statements from each module.</p><p>Let’s see if we address those points :)</p><p>TL;DR: we can! The final snippet can be found <a href="https://gist.github.com/f5b2b4ebe9e348ebbd8ee4cb995f8362#file-gradle_tests_report-gradle">here</a>, and the output looks like <a href="https://gist.github.com/19fa336b55c09733e4051312eac1b9ad#file-output">this</a>. Read on if you want to know the details.</p><h3>Original snippet</h3><p>First let’s read the original snippet and understand (more or less) what’s going on. If you’re new to Gradle, I invite you to check out my <a href="https://medium.com/@wasyl/understanding-android-gradle-build-files-e4b45b73cc4c">post</a> about how to read Gradle scripts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/747aadf7688c586cf357e6c938b021a2/href">https://medium.com/media/747aadf7688c586cf357e6c938b021a2/href</a></iframe><h3>What’s going on here?</h3><p>The tasks.withType(Test) { /* closure */ } will execute the given closure against each test task found in current project (tasks is a property declared in Project class). Test tasks are represented by <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html">Test class</a>, and we see in the documentation that testLogging method will configure <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/logging/TestLoggingContainer.html">TestLoggingContainer</a>. Its documentation states that by default we&#39;re configuring logging for lifecycle log level (so the default Gradle logging level), but we can also configure what we log for info, debug, error etc.</p><p>We have couple of settings here — for example events will configure which events will be logged to console for each test, and showStackTraces sets whether stack traces will show.</p><p>We also see afterSuite method (although the above snippet got it wrong — afterSuite is declared in AbstractTestTask, so it can be written outside of the testLogging closure). afterSuite closure is called after every test, but also after groups of tests. Whenever <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestDescriptor.html">TestDescriptor</a> doesn&#39;t have a parent, it&#39;s the biggest group of tests we can report.</p><h3>Adjusting the output</h3><p>Let’s try and simplify the snippet a little bit. For start, we can remove debug and info settings, as we most likely only run Gradle with the default settings. We can also safely remove PASSED status, since passed tests don&#39;t require attention.</p><h4>Logging options</h4><p>Let’s log FAILED events (though they are logged by default) and SKIPPED, to have a reminder that we have tests to fix. STANDARD_OUT may be useful or not, depending on whether your tests print something, and I&#39;ll also log STANDARD_ERROR that&#39;s not in the original snippet, but I feel it&#39;s important — it may bring attention to tests that pass, but perhaps have some problem. Let&#39;s also show all the details for failed tests that we can. This leaves us with the following test logging options:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6755144eae1c79dd61e4d994c2af5837/href">https://medium.com/media/6755144eae1c79dd61e4d994c2af5837/href</a></iframe><p>Now onto the summary details, there’s a bit more changes we can apply:</p><ul><li>we can show time it took to execute the tests, just because we get this information from result,</li><li>we can show url to the HTML report for the tests,</li><li>we can print all the summaries together, after the build, and</li><li>we can print it prettier.</li></ul><p>Let’s do all of those one by one:</p><h4>Time it took to execute the tests</h4><p>This is fairly straightforward to do, since result parameter has startTime and endTime properties. We can get the duration of the tests by writing:</p><pre>def duration = TimeCategory.minus(new Date(result.endTime), new Date(result.startTime)).toString()</pre><p>and placing duration somewhere in the output string. We’ll need to import groovy.time.TimeCategory to access TimeCategory.minus method.</p><h4>HTML report link</h4><p>Since Task class has reports, and we&#39;re within withType(Test) closure, we can access the html report directly by calling reports.html.entryPoint. We found it out by exploring <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/Test.html">Test</a> class documentation, which lead us to <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/testing/TestTaskReports.html">TestTaskReports</a>class, which in turn holds html report as a <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/reporting/DirectoryReport.html">DirectoryReport</a> which has an entryPoint — usually index.html file.</p><h4>Print all summaries after the build</h4><p>To do that, we have to collect all the summary information somewhere, and only print it after all the tasks have executed. We’ll store the summaries in root project’s ext property (since all modules have access to that) as soon as they&#39;re available. You can read more about Gradle properties and how they work <a href="https://medium.com/tooploox/where-do-gradle-properties-come-from-bf77e43ee572">here</a>. Then we&#39;ll use gradle.buildFinished hook that executes given closure <em>right before</em> Gradle finishes. This means all our summaries will already have been stored, so we can print them out one by one.</p><p>Since we’re storing the summaries before printing, we can now group them so that those with failed tests are at the bottom. To make sure we handle <em>all</em> the modules, we’ll also do everything within the root project. This gives us more or less this structure:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8274f95480610d6e60bc00da26ffac4d/href">https://medium.com/media/8274f95480610d6e60bc00da26ffac4d/href</a></iframe><h3>Pretty print the results</h3><p>Finally we’d like to print the results in a bit prettier fashion. Since the summary I’ll create contains two lines (statistics and report URL), I had to play a bit with Groovy to:</p><ul><li>calculate the width of the longest line, to know how wide the table will be,</li><li>print top border,</li><li>print each two-line summary, with left and right borders</li><li>between each two summaries, print a separator,</li><li>print bottom border.</li></ul><p>And to make the output fancier, we’ll use UTF characters, like ┌, ┐, │, └, ┘, used to build</p><pre>┌─────────────┐<br>│Pretty tables│<br>└─────────────┘</pre><p>Here’s the code (allResults is a list of possibly multiline strings):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be432b4ec790c1e6815e976933dd0e49/href">https://medium.com/media/be432b4ec790c1e6815e976933dd0e49/href</a></iframe><h4>Always try to run all tests</h4><p>Finally, let’s add ignoreFailures = true so that our tests continue running even if one of them fails. This does not mean your Gradle build will finish successfully when a test fails — it&#39;ll still report error, but only after running rest of the tests too. This is useful to know all the places to fix at once.</p><h3>Final snippet</h3><p>Here’s the final snippet, available also on Github (let me know if I forgot). In order to have it affect all of your multiple modules, you have to place it in your root project’s build.gradle file. You can paste the snippet into another file (e.g. setupTestsReports.gradleand apply it using apply from: file(&#39;setupTestsReports.gradle&#39;) to keep your build.gradle files short.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/344498bb28c4f10a369ecc03dd4a8ff9/href">https://medium.com/media/344498bb28c4f10a369ecc03dd4a8ff9/href</a></iframe><h3>Showing summary for each project</h3><p>And one final thing — we certainly don’t want to copy such a useful snippet into each of the projects we’re working on! Also sometimes we’d like to not modify the project’s code at all. This is where Gradle’s <a href="https://docs.gradle.org/current/userguide/init_scripts.html"><strong>init script</strong></a> comes in handy! In essence, init scripts will be executed when you call any Gradle build. You can use it to create tasks or set logic that you’d like to have common for all your builds — just what we want!</p><p>To create an init script, create a file called init.gradle in Gradle&#39;s home directory (~/.gradle/ for mac users). Inside, we can inject the above script by placing it in closure passed to gradle.projectsLoaded:</p><pre>gradle.projectsLoaded {<br> // The above script<br>}</pre><p>Make sure the imports are at the top, outside of any closures, et voilà — we have a pretty, useful tests summary, that runs for every build, without additional effort:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/411e151bff83ba4db15de3bd64c6497d/href">https://medium.com/media/411e151bff83ba4db15de3bd64c6497d/href</a></iframe><p>That’s it, leave a comment if you have any questions or improvement suggestions!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=744804dd676c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Kotlin to Dart - an Android developer's perspective]]></title>
            <link>https://medium.com/@wasyl/kotlin-developers-thoughts-on-dart-1f60c4ad21ad?source=rss-c635d6fd0ed4------2</link>
            <guid isPermaLink="false">https://medium.com/p/1f60c4ad21ad</guid>
            <category><![CDATA[dart]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[flutter]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Łukasz Wasylkowski]]></dc:creator>
            <pubDate>Sun, 05 Aug 2018 17:48:50 GMT</pubDate>
            <atom:updated>2019-05-03T19:57:17.869Z</atom:updated>
            <content:encoded><![CDATA[<h3>From Kotlin to Dart — an Android developer’s perspective</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*MFjVEPb_ZdMouK_C" /><figcaption>Photo by <a href="https://unsplash.com/@artmatters?utm_source=medium&amp;utm_medium=referral">Artur Matosyan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>People are asking how easy it is so switch to Dart for Flutter development, what are the differences between Dart, Java and Kotlin, and what can an Android developer expect when learning Flutter. Here are some of my thoughts on the topic — I’ve been using (and loving) Kotlin for over 1.5 years now, and I’ve been exploring Flutter and Dart in my free time for couple of months now. As always, take everything with a grain of salt, and your mileage will most definitely vary.</p><h3>The language</h3><p>If you come from Java, Dart’s syntax is fine. You don’t lose much, while getting some syntactic sugar like fields assignment in constructor arguments, named parameters or proper getters/setters. Functions as first-class citizens also feel a bit better (you don’t have to hack around with interfaces). If you’re coming from Java you’re already used to having to generate equals &amp; hashCode, which you also have to do in Dart. And if you&#39;re a fan of immutable classes — to using something that helps with that, like AutoValue or Immutables — in Dart there&#39;s BuiltValue for example.</p><p>Now if you come from Kotlin, then Dart as language is borderline terrible — you don’t get data classes, extension functions, null-safe types, if/when-as-expression, sealed classes, function literals with receivers, and more. Lack of any of those is already a step back, lack of all of them — it’s like you went back several years. Dart does have some features that don’t exist in Kotlin, like cascade operator, but I’d gladly give up cascade operator for an apply function.</p><p>It’s not that I’m a Kotlin fanboy (I am though), but let’s see:</p><ul><li><strong>no data classes</strong> mean you’re back to generating equals and hashCode for every single data class you have. For immutability you also have to either manually write copy method or write hefty boilerplate to let BuiltValue generates those for you. And boy it feels like forever to do either, while in Kotlin you only add data keyword to your class.</li><li><strong>no extension functions</strong> makes you unable to extend somewhat limited standard library. Who doesn’t like some string.isBlank() method?</li><li><strong>lack of null-safe types</strong> is killing me. I’m not a great fan of nulls, but I do use them — when it makes sense. Kotlin makes you document whether you expect something to be nullable or not — Dart doesn’t, and it simply adds a lot of mental overhead to deduce the nullability.</li><li><strong>sealed classes</strong> — once I started using those, there’s no coming back. Now in Dart (same as Java) if you contain everything in a single type, you have to juggle boolean values to know whether you have a value or not, all while opening yourself to exceptions and/or null values. If you use multiple classes, you&#39;re forced to return dynamic and you lose all the compile-time safety.</li></ul><p>As for things that are in Dart that aren’t in Kotlin — you get mixins, but I’m yet to make use of them (I’m sure there is potential here, though). dynamic keyword and not having to specify type is sometimes useful (like mapping jsons if you know the structure), but overall I guess I&#39;m a fan of knowing my types everywhere anyway ¯\<em>(ツ)</em>/¯. Async-await is great and a big win over Java, and I&#39;d say the single thing that really keeps me sane with Dart (other than Flutter obviously). Kotlin does have coroutines, but I find the learning curve much steeper for them than for Dart&#39;s async-await.</p><p>Overall I feel I’m half as productive in Dart vs Kotlin, especially when working with the domain layer, where the language matters the most. I’m sure one can get used to it, but it ain’t a pleasant journey.</p><p>Now I know not every language can be Kotlin, and I don’t expect Dart to be exactly like Kotlin in every area. Will Dart get some of those useful features, though? It’s difficult to say — let’s take a look at issues for some features I’d love to see in Dart:</p><ul><li><a href="https://github.com/dart-lang/sdk/issues/30347">Remove or make semicolons optional</a>. While it’s not closed, the discussion doesn’t give much hope, as we hear arguments that e.g. <em>Kotlin, as previously mentioned, makes choices that make its semicolon-insertion confusing and bug-prone.</em>. Who writes in Kotlin and feels lack of semicolons is bug-prone raise your hand up. Oh and this issue <a href="https://github.com/dart-lang/sdk/issues/34">has already been closed as not planned</a> — 34th issue, didn’t take long, did it?</li><li><a href="https://github.com/dart-lang/sdk/issues/9991">Scoped object extensions (aka extension functions)</a>. Also not closed, but discussed for 5 years now.</li><li><a href="https://github.com/dart-lang/sdk/issues/49">Method overloading based on parameter types</a> — closed because, obviously, <em>Even in language with mandatory types, type-based overloading is a bad idea, creating brittleness and ambiguity.</em> However seems like it’s reopen as <a href="https://github.com/dart-lang/sdk/issues/26488">a new issue</a>.</li><li><a href="https://github.com/dart-lang/sdk/issues/55">Everything won’t be an expression</a>, because <em>it doesn’t seem like a great fit for a conservative language like Dart. People wil use it in ways that might confuse the average programmer.</em>.</li><li><a href="https://github.com/dart-lang/sdk/issues/798">No infix function syntax</a> — closed, as the feature <em>violates the simple conservative design principle for Dart</em>.</li></ul><p>But there there still is hope, since we get some issues that are still open and apparently considered after Dart 2.0 (which should be coming soon).</p><ul><li><a href="https://github.com/dart-lang/dart_enhancement_proposals/issues/30">non-nullable types</a></li><li><a href="https://github.com/dart-lang/sdk/issues/4938">union types</a></li><li><a href="https://github.com/dart-lang/sdk/issues/31253">data classes</a></li></ul><p>All in all I hope that Dart’s development will accelerate with respect to new, fancy features. Until that happens I’ll keep wishing I could write Flutter apps in Kotlin.</p><h3>The libraries</h3><p>Dart’s standard library is okay-ish, about on par with Java 6 that we get on Adroid. It’s not as rich as it could be, and <a href="https://github.com/google/quiver-dart">quiver library</a> seems to be an attempt at enriching it, but without extension functions <em>it’s just not the same</em>. Overall I don’t feel hindered by Dart’s standard library, and if I do, there’s a official (or a semi-official) package on pub for that (let’s just hope we don’t go as far as npm with left-pad here). Assertions library for tests feels a bit like simple JUnit, but that might be simply becuse I got used to AssertJ for Kotlin.</p><p>There are lots of third-party libraries, though, and it seems to be related to the fact that it’s much easier to distribute a library via pub than it is via maven — simply keep the proper layout of the files and run pub publish. I really appreciate the <a href="https://pub.dartlang.org/help#scoring">package score</a> as well, which shows you how popular the package is and how many issues are there in code.</p><p>Importing a library is easy enough, not that different from adding an artifact using Gradle. However sometimes the library resolution doesn’t work, since you (and all the libraries) can declare a dependency on a specific version range of a particular library. It’s easy to get a situation where e.g. two libraries depend on non-overlapping ranges of a single package. In that case it might be difficult to find just the right versions. I did have this problem, but this was also during the heated time of libraries migrating from Dart 1 to 2, and the fact that Dart 2 is a breaking release might have been a factor.</p><h3>The tooling</h3><p>Most people will say it’s fine, but I’ve found IDE support to be much more limited than for Kotlin. I’m using IntelliJ (you can also use Visual Studio Code — maybe it’s more feature-rich), and I find writing in Dart much less convenient than in Kotlin. I consider myself IntelliJ power-user, and I tend to use every single functionality that helps me write faster. For Dart some features are not available (like postfix completion) or are not as extensive as for Kotlin. Refactoring is a pain — e.g. when changing name of the class, I still have to rename the file (for which the naming convention is snake_case.dart) manually. For constructors that use the field assignment syntax (this.alignment) you don&#39;t see types in parameter info popup. The available intent actions are far and apart, static analysis integration is sometimes flaky, code completion often simply doesn&#39;t work for types that haven&#39;t yet been imported in a file etc. It&#39;s not that you&#39;re back to writing in Notepad, but there&#39;s much place for improvement. Dart is getting more traction thanks to Flutter now, so I suppose we can expect improvement in this area.</p><p>Dartfmt (a tool to format the code automatically) is cool and lets you focus less on the code style, and more on coding (although it still doesn’t prevent discussions in code review like ‘plx put a comma after last parameter’). Dartalyzer, which is a tool for static analysis of Dart code, is pretty nice, and just works. Overall I very much appreciate both tools being bundled with the language and available out of the box for every project without much hassle.</p><p>One thing I miss and I put it here because I believe it’s tooling issue — I haven’t found an easy way of having multiple Dart/flutter modules in a single project. For Android projects I usually end up with several separate modules which, and most of them are separate (e.g. database module won’t know about the REST-api module, and domain module won’t know about anything basically). That forces the developers to not mix up the layers and responsibilities of the components. I don’t seem to be able to do that in Dart other than by having multiple projects, which can’t be edited in one IntelliJ instance.</p><p>Of course hot reload is also enabled by tooling (as well as language design) and it’s a godsend, not much to say here.</p><p>To sum it up, I think it’s safe to say Dart — the language, tooling and libraries — is fine, and it really shouldn’t come as surprise. Is it as good as Java? Maybe. Is it as good as Kotlin? Most definitely not. Should it prevent you from trying out Flutter? That’s for you to decide, but now you maybe know a bit more about what to expect. The best description for Dart I’ve read is that it’s a <em>generic language</em>. You’re not blown away by it, but you are able to steadily write good code, and that’s enough.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1f60c4ad21ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Where do Gradle properties come from?]]></title>
            <link>https://medium.com/tooploox/where-do-gradle-properties-come-from-bf77e43ee572?source=rss-c635d6fd0ed4------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf77e43ee572</guid>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Łukasz Wasylkowski]]></dc:creator>
            <pubDate>Fri, 31 Mar 2017 13:59:32 GMT</pubDate>
            <atom:updated>2017-04-11T11:34:55.045Z</atom:updated>
            <content:encoded><![CDATA[<h3>Where do Gradle properties in Android come from?</h3><p>Understanding the insides of Gradle property resolution for (not only) Android developers</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*slxsRE6yAJeBQUJg4y6CXQ.png" /></figure><p>Following up on <a href="https://medium.com/@wasyl/understanding-android-gradle-build-files-e4b45b73cc4c">my previous post about Gradle</a>, I’ll now dive into Gradle properties — we’ll discuss what kinds of properties there are, how we can access them and what they’re used for. If you haven’t read the previous post, I encourage you to do so, as this post relies on some bits that I explain there.</p><h3>What are properties?</h3><p><a href="http://groovy-lang.org/objectorientation.html#_properties">Groovy documentation</a> defines a <em>property</em> as a combination of a private field and a getter/setter. Usually in Java we would declare a private field, a getter and a setter explicitly. In some programming languages though, this practice is wrapped in a concept of <em>properties</em>. In short, when declaring a <em>property</em>, a private field, a getter, and a setter (if the property is not final) are generated. By default, getters and setters just read/write to the backing field, but they can also be customised, so things like validation can be added.</p><h4>Syntax</h4><p>Here’s a comparison of Java code (without properties), and equivalent Kotlin and Groovy declarations using property syntax:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b043d4e075fcdc74dfa8a7a8398e28f7/href">https://medium.com/media/b043d4e075fcdc74dfa8a7a8398e28f7/href</a></iframe><p>The same written in Kotlin:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/453d01d1979d9b53b3b45c73fa159232/href">https://medium.com/media/453d01d1979d9b53b3b45c73fa159232/href</a></iframe><p>And the same in Groovy:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dfe92cbbb391c7f9bbe4126cdd15b1b4/href">https://medium.com/media/dfe92cbbb391c7f9bbe4126cdd15b1b4/href</a></iframe><p>This lengthy introduction to properties is only here so we know that:</p><ul><li>Property access syntax is similar to field access syntax in Java, but they shouldn’t be confused</li><li>Accessing a property is actually translated to a method call, and this method can <em>do things</em></li></ul><h4>Dynamic properties</h4><p>One more important thing in Groovy that affects Gradle heavily is that <em>Groovy is a dynamic language.</em> In my previous post I briefly mentioned the dynamic method access, but Groovy actually offers the <a href="http://groovy-lang.org/metaprogramming.html#_propertymissing">dynamic property access</a> as well. This means that sometimes even though a property with certain name wasn’t declared in the object, such property can still be accessed — provided the said object offers dynamic property access. This is important, since quite a lot of properties that we access in Gradle are added dynamically. Just for fun, let’s have a look at how we’d use dynamic properties in Groovy (this is what Gradle uses under the hood):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c22f7157bd9fa26806a8ac22855c3024/href">https://medium.com/media/c22f7157bd9fa26806a8ac22855c3024/href</a></iframe><p>As we can see, even though meaningOfLife doesn’t exist, the code above not only compiles, but also doesn’t crash at runtime. This mechanism is used <em>a lot</em> in Gradle scripts.</p><h3>Properties in Gradle</h3><p>Now that we know what the properties are, what their syntax is, and that some of them may be added dynamically, let’s have a look at different properties that we can encounter.</p><h4>Class properties</h4><p>This one is pretty obvious — if a class, say Project , declares a property, then it can be accessed.</p><h4>Extension properties</h4><p>Extension properties are ad-hoc properties that can be added to any object that implements <a href="https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtensionAware.html">ExtensionAware</a>. In other words, every ExtensionAware object can be extended with an arbitrary <em>extension</em> object with the given name. This is done via extensions property of type <a href="https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/ExtensionContainer.html">ExtensionContainer</a> present in every ExtensionAware object. Added extension objects can be then accessed using property syntax. Here’s an example of how that would work with Project class, which implements ExtensionAware :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f09b8bb69aae7f5ddff7fcccdcbae243/href">https://medium.com/media/f09b8bb69aae7f5ddff7fcccdcbae243/href</a></iframe><p>First thing to note is that theProject class does <em>not</em> normally have aperson property. It’s added dynamically when calling extensions.create method. Second thing to note is that for this newly created property we can use both normal property syntax (assigning the name) as well as namespace method, which allows us to configure extension object using a closure. This is what we usually see when configuring android extension. Both Project and Task are ExtensionAware.</p><blockquote><em>Talking about methods in a post about properties may be confusing, but the point is that </em><em>person is an </em><strong><em>extension</em></strong><em>, and extensions can both be accessed as properties, or configured via namespace method.</em></blockquote><p>We know extensions very well — for example Android Gradle plugin adds android extension of AppExtension type. We usually use method syntax to configure it, but we can access it using property syntax as well (e.g. android.defaultConfig.compileSdkVersion = 24 ).</p><h4>Extra properties</h4><p><em>Extra properties</em> is a special extension of type <a href="https://docs.gradle.org/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html">ExtraPropertiesExtension</a> added with name ext to all ExtensionAware objects. We can think of it as a container of arbitrary key-value pairs, just with special syntax:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57ef6e6f4e74e01f03a8a196f7155676/href">https://medium.com/media/57ef6e6f4e74e01f03a8a196f7155676/href</a></iframe><p>As we see, we can create and access keys in extra properties using both property syntax (prefer that) and accessor syntax. Here’s the thing, though: we can skip the ext part, and access a property directly from ExtensionAware object:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b265e087a61c6b7d12c86156f95d3923/href">https://medium.com/media/b265e087a61c6b7d12c86156f95d3923/href</a></iframe><blockquote><em>The above statements aren’t always equivalent — we’ll see why later.</em></blockquote><p>Another thing to remember is that <em>properties passed with </em><em>-P command line options or added to </em><em>gradle.properties file are added to the extra properties extension</em>. This is why (and how) we can access them — they’re not in a separate scope, or considered separately. They’re simply added to one of existing scopes. As we’ll see later, this is why project.name property can’t be set by adding it in gradle.properties, but for example project.personName can.</p><h4>Convention properties</h4><p><em>Convention properties</em> is an older version of extension properties, now mostly replaced by it. The difference is that convention properties are <em>immutable</em> (albeit assignable), and are <em>lazily evaluated</em>. We’ll not dig into details as extensions are much more prevalent in Gradle now.</p><h4>Tasks of the project</h4><p>Simply put, when we’re in Project class, its tasks are accessible as properties. For example project.assembleDebug is equivalent to project.tasks.getByName(&#39;assembleDebug&#39;) .</p><h3>Resolving properties in build.gradle files</h3><p>We now see that properties can come from many places — so now the questions is: what happens if there’s a collision? Supposed we created an extra property name. Which name will we get when we call project.name - name defined in Project class, or the one in extra property? Fortunately, there’s a strict order of scopes in which properties are searched. Remembering that build.gradle files are executed against a Project instances, the scopes which are searched for properties are:</p><ul><li><strong>Project object</strong> — if there’s a property with a given name declared in Project class, it’s used. Whether it’s readable or writable is determined by the presence of corresponding getter or setter method.</li><li><strong>Extra properties</strong> — from ext extension; these are both readable and writable</li><li><strong>Extensions</strong> — read-only, usually added by plugins</li><li><strong>Convention properties</strong></li><li><strong>Extra properties, extensions and convention properties of the parent project</strong> (recursively, up to the root project), but in the read-only scope</li></ul><h3>Examples</h3><p>Let’s have a look at some code that we could write in build.gradle files. Again, build.gradle means that the code is executed on Project instance, and Project is ExtensionAware.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/684bd3cba361695b5a2cc3f0227f5304/href">https://medium.com/media/684bd3cba361695b5a2cc3f0227f5304/href</a></iframe><p>[1] Here we write extra property called personName. This is the first time we do it, so this actually creates a mapping for this property’s name in ext object.</p><p>[2] Here we access extra property personName explicitly from ext namespace</p><p>[3] And here we access it second time, but this time without the namespace. This works because there’s no personName in Project class, and extra properties are next in order to look for a missing property. Notice that we wouldn’t be able to do that before [1], as the property wouldn’t be defined yet.</p><h4>Extensions example</h4><p>Let’s have a look at extensions now:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d8104e8abd13e185fc92fdf3f72c6d6/href">https://medium.com/media/6d8104e8abd13e185fc92fdf3f72c6d6/href</a></iframe><p>person in [1] is our PersonExtension, so we can access its name property. [2] defines extra property person , so now [3] will print “Tim” , because extra properties have higher priority than extensions. Notice that after we write [2], we can no longer write person.name , because person will now always refer to <em>extra property</em>, and not an <em>extension</em> (and this property is a String, not a PersonExtension)! If we wanted to change thename in person extension, we now have to refer to extension explicitly directly: this.extensions.person.name = &quot;Molly&quot; .</p><h4>Reading properties in child project</h4><p>If we have two files: root project’s build.gradle, and child project’s build.gradle like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45faa916fc35aa19f6749f1d5d7e58c2/href">https://medium.com/media/45faa916fc35aa19f6749f1d5d7e58c2/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd10bd17681d312fd14f312acf855de2/href">https://medium.com/media/dd10bd17681d312fd14f312acf855de2/href</a></iframe><p>[1] will print out “Tim” — because person is not found in the current project (it’s not in Project class, there’s no extra person in the child project, no extension like a convention or a task), the parent project is searched - and person is finally found in its extra properties.</p><p>[2] will fail, because ext refers to child project’s extra properties, and this one doesn’t have person defined. If we comment this line, then:</p><p>[3] will define person in child project’s extras</p><p>[4] and [5] Will now print out “John”, because child project’s extras now contain person property, and they’re searched before parent’s extras</p><p>[6] will print “Tim”, because now we explicitly ask for parent’s extra properties</p><h4>Writing properties in a child project</h4><p>Let’s take the same root project’s build.gradle :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45faa916fc35aa19f6749f1d5d7e58c2/href">https://medium.com/media/45faa916fc35aa19f6749f1d5d7e58c2/href</a></iframe><p>and the following child project’s build.gradle file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/357709ba0d1d3013b51a6718f564eb7c/href">https://medium.com/media/357709ba0d1d3013b51a6718f564eb7c/href</a></iframe><p>[1] will fail — this is because theperson property comes from parent project, so it’s read-only.</p><p>[2] and [3] Will work, and will assign “John” to theperson property in root project’s extras. This is because now we’re directly operating on parent project&#39;s, where its extra properties are writable (or on extra extension, in which everything is readable and writable).</p><blockquote><em>This may be confusing since previously it was stated that parent properties are accessed in read-only scope. This is true — when accessing property from parent using property access syntax </em><strong><em>on child project</em></strong><em>, then property is read-only. In [2] and [3] property from parent is accessed using property access </em><strong><em>on parent project</em></strong><em>, so it’s writable.</em></blockquote><p>[4] will define person in child project’s extras.</p><p>[5] and [6] Will assign “Tim” to child project’s person extra property. This differs from [1] in than this person is child project’s one, not parent’s.</p><p>[7] and [8] Will print “Tim” — both lines refer to child project’s extra person property</p><p>[9] will print “John” — this time we explicitly ask for parent.</p><h3>Properties vs variables</h3><p>It’s also important to remember the distinction between properties (which can be dynamically resolved by Gradle) and variables (which are part of Groovy language). If we want to declare a propery within a Project (for example to be used by child project), then we <em>have to</em> use ext property. But if we want to declare a variable to be used in the script, then we should use standard Groovy syntax - using def keyword, or by specifying type explicitly. But then what we declared is only a variable in the script, and it <strong>precedes all the scopes that we’ve talked about</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3e94ffc842bb9a005bab2b235c4249e/href">https://medium.com/media/f3e94ffc842bb9a005bab2b235c4249e/href</a></iframe><p>Here [1] will print “Molly”, because a script variable has precedence over properties. When in [2] we modify personName, we refer to the variable, so [3] will print “Tim”. [4] addresses extra property explcitly, so it will print “John”.</p><h3>Summary</h3><p>That’s about it when it comes to Gradle properties. Hopefully, this brings you a step closer to fully understanding Gradle scripts and the ability to confidently write one by yourself!</p><p>P.S. Again many thanks to Gradle team and Android team at Tooploox for proofreading the post, and to Ania Langiewicz for the wonderful feature graphic!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf77e43ee572" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tooploox/where-do-gradle-properties-come-from-bf77e43ee572">Where do Gradle properties come from?</a> was originally published in <a href="https://medium.com/tooploox">Tooploox</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Android Gradle build files]]></title>
            <link>https://medium.com/@wasyl/understanding-android-gradle-build-files-e4b45b73cc4c?source=rss-c635d6fd0ed4------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4b45b73cc4c</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[groovy]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[gradle]]></category>
            <dc:creator><![CDATA[Łukasz Wasylkowski]]></dc:creator>
            <pubDate>Wed, 01 Feb 2017 09:48:33 GMT</pubDate>
            <atom:updated>2017-02-01T09:48:33.129Z</atom:updated>
            <content:encoded><![CDATA[<h4>Taking the magic away</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PGagM2HrNGhYibE-tz7lrA.png" /></figure><p>For most purposes we don’t need to modify our *.gradle files much — we add dependencies, modify target and minimum API levels, maybe set up signing configs and build types. Anything more complicated than that, and we end up copy-pasting mysterious snippets from Stack Overflow, without understanding how they work. In this post we’ll write, step by step, Gradle configuration files for a single Android project in order to take some of the magic away.</p><h3>Groovy</h3><h4>Syntax</h4><p>Gradle files are basically <em>Groovy</em> scripts. <a href="http://groovy-lang.org/syntax.html">Syntax</a> is easy to grasp if you know Java, and for us it’s important that:</p><ul><li>there’s no need for parentheses when calling methods with at least one parameter (if it’s unambiguous):</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aec1f1e89dcd8be5c9ca4903e83cd9a/href">https://medium.com/media/2aec1f1e89dcd8be5c9ca4903e83cd9a/href</a></iframe><ul><li>if the last parameter is a <em>closure</em> (for now think <em>lambda</em>), it can be written outside the parentheses:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a410c3457899d2399c8d274f47b5fc3f/href">https://medium.com/media/a410c3457899d2399c8d274f47b5fc3f/href</a></iframe><ul><li>If you invoke Groovy method with named parameters, they are converted into a map and passed as first argument of the method. Other (non-named arguments) are then appended to parameters list:</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4aed06cd4e8f33d5ebf6627e5744961a/href">https://medium.com/media/4aed06cd4e8f33d5ebf6627e5744961a/href</a></iframe><p>This will print &quot;John is 24 years old&quot; followed by John works as Android developer at Tooploox. Mind that in both cases the result will be the same regardless of parameters order! Also notice omitted parentheses in both calls.</p><h4>Closures</h4><p>One important feature that needs some explaining are <a href="http://groovy-lang.org/closures.html"><em>closures</em></a>. If you’re familiar with Kotlin, you might find below explanation somewhat similar to <a href="https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver"><em>function literals with receiver</em></a><em>.</em></p><p>Closures in Groovy can be thought of as lambdas on steroids. They’re blocks of code that can be executed, can have parameters, and return values. What’s different is that we can change the <em>delegate</em> of a closure. Let’s consider following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86333a1a23eacb5a9754f65e3b983858/href">https://medium.com/media/86333a1a23eacb5a9754f65e3b983858/href</a></iframe><p>We can see that printClosure calls printText method on the delegate it is provided (the same goes for properties). We will see later why this is crucial in Gradle.</p><blockquote>There’s actually a bit more to delegates and how closure’s statements are executed. You can read more about delegation and delegation strategies <a href="http://groovy-lang.org/closures.html#_delegation_strategy">in Groovy documentation</a>.</blockquote><h3>Gradle</h3><h4>Script files</h4><p>There are three main script files that Gradle uses. Each one is a block of code (<em>closure</em>, anyone?) executed against various objects:</p><ul><li><em>build</em> scripts in build.gradle files. These are executed against <a href="https://docs.gradle.org/current/dsl/org.gradle.api.Project.html">Project</a> objects;</li><li><em>settings</em> scripts in settings.gradle files, executed against <a href="https://docs.gradle.org/current/dsl/org.gradle.api.initialization.Settings.html">Settings</a> object;</li><li><em>init</em> scripts used for global configuration (executed against <a href="https://docs.gradle.org/current/dsl/org.gradle.api.invocation.Gradle.html">Gradle</a> instance).</li></ul><h4>Projects</h4><p>Gradle build consists of one or more <em>projects</em>, and projects consist of <em>tasks</em>. There is always at least the <em>root project</em>, which may contain <em>subprojects</em>, which in turn can have nested <em>subprojects</em> as well. Common convention is that the root project’s role is only to orchestrate group projects, provide common configuration, plugins classpaths etc.</p><blockquote>From now on project will refer to whatever subproject we’re currently interested in, and root project will be used when referring to root project specifically.</blockquote><h3>Creating Gradle-based Android project</h3><p>In typical Android project we have the following folder structure:</p><pre>├── settings.gradle # [1]<br>├── build.gradle # [2]<br>├── gradle<br>│   └── wrapper<br>└── app<br>    ├── gradle.properties # [3]<br>    ├── build.gradle # [4]<br>    └── src</pre><ol><li>This is <em>root project</em>’s settings file, executed against its Settings instance</li><li><em>Root project</em>’s build configuration</li><li><em>App </em>project’s properties file, injected into <em>app</em>’s Settings</li><li><em>App</em> project’s build configuration</li></ol><p>Let’s go step by step, then.</p><h4>Creating a Gradle project</h4><p>Let’s create new folder, say example. If we cd into it and execute gradle projects, we can see that it’s already a Gradle project!</p><pre>$ gradle projects<br>:projects</pre><pre>------------------------------------------------------------<br>Root project<br>------------------------------------------------------------</pre><pre>Root project &#39;example&#39;<br>No sub-projects</pre><pre>To see a list of the tasks of a project, run gradle &lt;project-path&gt;:tasks<br>For example, try running gradle :tasks</pre><pre>BUILD SUCCESSFUL</pre><pre>Total time: 0.741 secs</pre><blockquote>If you don’t have Gradle installed locally, you can install it using macports or homebrew, or download an installer from <a href="https://gradle.org/gradle-download/">official webpage</a>. You can also create a new project in Android Studio and remove everything except for Gradle wrapper.</blockquote><h4>Setting up projects hierarchy</h4><p>If we want similar structure to a default Android project (empty root project and an app project with our application), we need a settings.gradle file. From <a href="https://docs.gradle.org/current/dsl/org.gradle.api.initialization.Settings.html">the documentation</a> we know that settings.gradle script:</p><blockquote>declares the configuration required to instantiate and configure the hierarchy of <a href="https://docs.gradle.org/current/dsl/org.gradle.api.Project.html">Project</a> instances which are to participate in a build.</blockquote><p>Further we read that we can add projects to the build using void include(<a href="http://download.oracle.com/javase/7/docs/api/java/lang/String.html">String</a>[] projectPaths) method. Let’s add a app subproject then:</p><pre>$ echo &quot;include &#39;:app&#39;&quot; &gt; settings.gradle<br>$ gradle projects</pre><pre># ...</pre><pre>------------------------------------------------------------<br>Root project<br>------------------------------------------------------------<br><br>Root project &#39;example&#39;<br>\--- Project &#39;:app&#39;<br><br>...<br><br>BUILD SUCCESSFUL<br><br>Total time: 0.666 secs</pre><blockquote>Colon (:) is used in Gradle to separate paths to subprojects, what we can see <a href="https://docs.gradle.org/current/javadoc/constant-values.html#org.gradle.api.Project.PATH_SEPARATOR">here</a>. That’s why we write <em>:app</em> and not <em>app</em> (although in this case <em>app</em> would work as well)</blockquote><blockquote>It’s also good practice to include rootProject.name = &lt;&lt;name&gt;&gt; in settings.gradle file. Without it, root project’s name defaults to the name of the folder in which the project is, which may be different for example on a CI server.</blockquote><h4>Setting up Android subproject</h4><p>Now we’d normally set up root project’s build.gradle file, but what do we need to put there? Let’s find out by trying to set up an Android project instead.</p><p>From the <a href="http://tools.android.com/tech-docs/new-build-system/user-guide">user guide</a> we know that we need to apply com.android.application plugin to our app project. Let’s have a look at apply <a href="https://docs.gradle.org/current/dsl/org.gradle.api.plugins.PluginAware.html#org.gradle.api.plugins.PluginAware:apply(groovy.lang.Closure)">method signatures</a>:</p><pre>void apply(Closure closure)<br>void apply(Map&lt;String, ?&gt; options)<br>void apply(Action&lt;? super ObjectConfigurationAction&gt; action)</pre><p>While the third one is the one that’s important — it uses statically typed API — we usually only use the second one, as it takes advantage of feature that we’ve mentioned before — named parameters are passed to the method as a map. To know what keys (parameters names) we can use, we peek into the documentation:</p><blockquote>void apply(<a href="http://download.oracle.com/javase/7/docs/api/java/util/Map.html">Map</a>&lt;<a href="http://download.oracle.com/javase/7/docs/api/java/lang/String.html">String</a>, ?&gt; options)</blockquote><blockquote>The following options are available: <br>from: A script to apply. (…)<br>plugin: The id or implementation class of the plugin to apply.<br>to: The target delegate object or objects. (…)</blockquote><p>We now know we need to pass our plugin id as plugin parameter. We <em>could</em> write apply(plugin: &#39;com.android.application&#39;), but we also know we can omit parentheses if the invocation is non-ambiguous, which it is. Let’s add apply plugin: ‘com.android.application’ to app’s build.gradle file, then:</p><pre>$ echo &quot;apply plugin: &#39;com.android.application&#39;&quot; &gt; app/build.gradle</pre><p>What now?</p><pre>$ gradle app:tasks</pre><pre>FAILURE: Build failed with an exception.<br><br>* Where:<br>Build file &#39;(...)/example/app/build.gradle&#39; line: 1<br><br>* What went wrong:<br>A problem occurred evaluating project &#39;:app&#39;.<br>&gt; Plugin with id &#39;com.android.application&#39; not found.<br><br>BUILD FAILED<br><br>Total time: 0.69 secs</pre><p>Okay then, there’s no com.android.application plugin defined. Well, we’re not surprised — how would Gradle find Android plugin’s jar file? We can see in the <a href="http://tools.android.com/tech-docs/new-build-system/user-guide">user guide</a> we need to add plugin’s classpath, and the repository in which it can be found.</p><p>Currently we can configure this classpath either in app’s or in root project’s build.gradle file, because buildscript closure is executed against ScriptHandler, which subprojects also use. This is not recommended though — all plugin dependencies should be declared at root project’s build.gradle instead. Let’s put buildscript block there, then, and discuss what it does:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/312b0fd324dbbfa9844599d75405ba47/href">https://medium.com/media/312b0fd324dbbfa9844599d75405ba47/href</a></iframe><p>If we add parentheses in our heads, we see all of these are simple method calls, of which some pass Closure as a parameter. If we then dig into <a href="https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:buildscript(groovy.lang.Closure)">the documentation</a>, we read what objects these closures are executed against. In summary:</p><ul><li>buildscript(Closure) is called on Project instance, and passed closure is executed against ScriptHandler object</li><li>repositories(Closure) is called on ScriptHandler instance, while passed closure is executed against RepositoryHandler</li><li>dependencies(Closure) is also called on ScriptHandler, but its argument is executed against DependencyHandler</li></ul><p>Which means that:</p><ul><li>jcenter() is called within RepositoryHandler</li><li>classpath(String) is called on DependencyHandler (*)</li></ul><p>We only need to know that the first call — buildscript — is executed against Project instance. For the rest, the documentation specifies the delegates explicitly.</p><blockquote>(*) If you inspect DependencyHandler code, you’ll notice there’s no classpath method. This is a special type of call, which we’ll discuss later on with dependencies.</blockquote><h4>Configuring Android subproject</h4><p>If we now try to execute some Gradle task, we’ll be greeted with an error:</p><pre>$ gradle projects</pre><pre>FAILURE: Build failed with an exception.<br><br>* What went wrong:<br>A problem occurred configuring project &#39;:app&#39;.<br>&gt; buildToolsVersion is not specified.</pre><p>Obviously, we haven’t put any Android-related configuration yet, but we can see that Android plugin is now applied correctly! Let’s add some configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e7c8e8cd0c1afdc69511d51e0bdf3dc/href">https://medium.com/media/3e7c8e8cd0c1afdc69511d51e0bdf3dc/href</a></iframe><p>We already see what’s happening — somehow there’s android method added to theProject instance, that delegates whatever closure it’s passed to some object (AppExtension in this case), which has buildToolsVersion and compileSdkVersion methods defined. This way Android plugin receives all configurations passed, including default configuration, flavors etc.</p><blockquote>In order to run any tasks now we still need two things — AndroidManifest.xml file, and a local.properties file with sdk.dir property (or ANDROID_HOME environment variable) pointing to Android SDK location on our machine.</blockquote><h4>Extensions</h4><p>But how did android method suddenly appear in the Project instance, against which our build.gradle is executed? In short, Android plugin registered AppExtension class as an extension with android name. This goes out of scope of this post, but what’s important for us is that Gradle adds configuration closure block for each extension object registered by the plugins.</p><h4>Dependencies</h4><p>There’s one last block that’s always there, that haven’t been discussed yet - dependencies. Here’s an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fa06d0dd20a219c08405cd91989e474/href">https://medium.com/media/6fa06d0dd20a219c08405cd91989e474/href</a></iframe><p>Why is this block special? Well, if you look into <a href="https://docs.gradle.org/current/dsl/org.gradle.api.artifacts.dsl.DependencyHandler.html#N162FA">DependencyHandler</a>, to which dependencies method delegates the passed closure, you’ll see there’s no compile method on it, nor testCompile or any of those we usually use. Which makes sense — if we add free flavor, we can write freeCompile &#39;somelib&#39; — DependencyHandler can’t define methods for all possible flavors now, can it? Instead, it uses another feature of Groovy language — <a href="http://www.groovy-lang.org/metaprogramming.html#_methodmissing"><em>methodMissing</em></a>, which allows for catching calls to undefined methods in runtime (*).</p><blockquote>(*) Actually Gradle uses abstraction over methodMissing declared in <a href="https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/java/org/gradle/internal/metaobject/MethodMixIn.java">MethodMixIn</a>, but the effect is mostly the same. Similar mechanism can also be applied to undefined properties.</blockquote><p>The relevant fragment of the default dependency handler implementation can be found <a href="https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/java/org/gradle/api/internal/artifacts/dsl/dependencies/DefaultDependencyHandler.java#L200-L227">here</a>, and it does the following:</p><ul><li>If <em>any undefined</em> method is called with more than 0 arguments, and</li><li>if there exists configuration(*) with the name of that method, then</li><li>depending on number of parameters and their type, call doAdd method with relevant parameters.</li></ul><blockquote>(*) Each plugin can add configurations to dependencies handler. For example java plugin defines compile, compileClasspath, testCompile and some other configurations, specified <a href="https://docs.gradle.org/current/userguide/java_plugin.html#sec:java_plugin_and_dependency_management">here</a>. Android plugin on the other hand adds annotationProcessor configuration, as well as &lt;variant&gt;Compile, &lt;variant&gt;TestCompile etc., based on defined build types and product flavors.</blockquote><p>While doAdd method is private, it’s being called by add method which is public. Thus, we could(*) rewrite above dependencies block as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8ae07a9ec7722f070f73d5819436686/href">https://medium.com/media/a8ae07a9ec7722f070f73d5819436686/href</a></iframe><blockquote>(*) But please, don’t do that.</blockquote><h4>Flavors, build types, signing configs</h4><p>Let’s consider this piece of code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2078f217c78640ef04ff4f4b5f86e9a/href">https://medium.com/media/d2078f217c78640ef04ff4f4b5f86e9a/href</a></iframe><p>What does productFlavors method delegate to? If we look into source code, productFlavors is declared like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b4bf77adae722b9b67c194324edeaac/href">https://medium.com/media/4b4bf77adae722b9b67c194324edeaac/href</a></iframe><blockquote>Action&lt;T&gt; in Gradle world is a closure executed against T</blockquote><p>So, here we have some NamedDomainObjectContainer which creates and configures objects of type ProductFlavorDsl and stores them alongside their names.</p><p>This container also uses dynamic method dispatch to create an object of a given type (here ProductFlavorDsl) and put it into the container along with its (<em>method</em>) name. So if we call method prod with parameter {}, it’s executed against productFlavors instance, which is NamedDomainObjectContainer. Here’s what happens:</p><ul><li>NamedDomainObjectContainer captures called method’s name,</li><li>creates ProductFlavorDsl object,</li><li>configures it against given closure,</li><li>stores mapping from method name to newly configured ProductFlavorDsl object</li></ul><p>We (and Android plugin) can then retrieve ProductFlavorDsl objects from productFlavors. What’s important, we can access them as properties, so in our case we can write productFlavors.dev, and we’ll retrieve ProductFlavorDsl that we’ve put with dev name. This is why we can write signingConfig signingConfigs.debug for example.</p><h3>Summary</h3><p>Gradle files are ubiquitous for Android developers, yet they’re often treated as a necessary evil, or at least as a magic black box that does <em>things</em>. But while there’s lots of conventions when it comes to writing Gradle scripts, and Gradle itself adds <em>some</em> complexity over Groovy language, when we get to know both, Gradle files aren’t that magical. I hope after reading this post and applying some curiosity, even that obscure code pasted from Stack Overflow will start to make sense now!</p><p>P.S. Many thanks to <a href="https://medium.com/u/bb1374cfe8c1">Stefan Oehme</a> and Android team at <a href="http://tooploox.com/">Tooploox</a> for valuable input and proof-reading this article!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4b45b73cc4c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Make your Gradle builds fast again!]]></title>
            <link>https://medium.com/@wasyl/make-your-gradle-builds-fast-again-ea323ce6a435?source=rss-c635d6fd0ed4------2</link>
            <guid isPermaLink="false">https://medium.com/p/ea323ce6a435</guid>
            <category><![CDATA[android-studio]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[gradle]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Łukasz Wasylkowski]]></dc:creator>
            <pubDate>Wed, 01 Feb 2017 07:36:35 GMT</pubDate>
            <atom:updated>2023-05-16T07:11:27.237Z</atom:updated>
            <content:encoded><![CDATA[<h4>A guide on how to not wait</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tr22Qgp6T_5lg1PbwI4omA.png" /></figure><blockquote>Note that this article is couple of years old at this point. While some of the advice outlined here still applies, it might not reflect current best practices.</blockquote><p>Nobody likes to wait endlessly for their projects to build. Here are some methods that we at Tooploox employ to spend more time <em>creating</em> great apps, and less time <em>building</em> them.</p><h3>Use the newest Gradle distribution</h3><p>Gradle keeps getting faster and faster with each release, and it rarely makes sense to use older version. Android Studio keeps generating gradle wrapper that is ages behind — Android Studio 2.3 Canary 2 still uses version 2.14.1 — already few months old as of writing this article. Unless it breaks your build — and it shouldn’t — update.</p><h3>Project-wide</h3><p>To update the gradle wrapper for your project, simply run wrapper task:</p><p>./gradlew wrapper --gradle-version &lt;&lt;version&gt;&gt; --distribution-type [all|bin]</p><p>For example:<br>./gradlew wrapper --gradle-version 3.3 --distribution-type all</p><p>This will download specified Gradle distribution into wrapper files. Also, consider periodically removing old versions leftovers from .gradle folder inside your project.</p><blockquote><em>You can update gradle wrapper from Android Studio as well — open </em><em>ProjectStructure dialog and update </em><em>Gradle version field in </em><em>Project tab. </em><em>wrapper task is preferred, though, since it also updates the wrapper </em><em>jar file as well as </em><em>gradlew scripts.</em></blockquote><h3>System-wide</h3><p>Despite gradle wrapper being the preferred method of running gradle builds, for non-production builds consider using locally installed gradle distribution. This way you can benefit from the latest performance improvements while not having to modify (possibly someone else’s) project’s gradle wrapper. To install gradle locally, refer to <a href="https://gradle.org/gradle-download/">official website</a>, or use packet manager like <a href="https://www.macports.org/">Macports</a> or <a href="http://brew.sh/">Homebrew</a>. And update it periodically!</p><h3>Configure Gradle properly</h3><p>Gradle is extremely complex build system and as such it can be configured extensively. We’ll cover only some options — the ones that are the most important from the performance standpoint.</p><h3>Project-wide</h3><p>Project-wide settings for your gradle build are kept in gradle.properties file in your project&#39;s root folder.</p><h3>System-wide</h3><p>System wide settings can be found in .gradle folder in your HOME directory - ~/.gradle/gradle.properties. On Windows look in C:\Users\username\.gradle\gradle.properties).</p><blockquote><em>First Gradle reads all project-wide options, followed by system-wide options. If an option is specified twice, the last one wins. This essentially means you always override project’s properties by your system-wide options.</em></blockquote><h3>Options</h3><ul><li>org.gradle.daemon - should be set to true to take advantage of <a href="https://docs.gradle.org/current/userguide/gradle_daemon.html">Gradle daemon</a>. Long story short, daemon means reusing things in subsequent builds</li><li>org.gradle.jvmargs - specifies jvmargs used for daemon process. <a href="https://medium.com/@skaliakoudas/decreasing-build-times-by-decreasing-gradle-memory-requirements-7fcafc6d98ea#.otnm0ofb6">You want to set high enough memory limit to fix <em>dex</em> in Gradle process</a>, so minimum configuration here is -Xmx2g, while 3-4 gigabytes won&#39;t hurt either</li><li>org.gradle.configureondemand - slight optimisation that, if set to true, configures only needed modules of the project. Useful in large, multi-module projects</li><li>org.gradle.parallel - allows Gradle to build modules in parallel. Only useful, if your modules don&#39;t depend on each other</li></ul><p>Sample, efficient gradle.properties file for a 16GB machine looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/435d92441c8cb1ae546cc50cd3f51203/href">https://medium.com/media/435d92441c8cb1ae546cc50cd3f51203/href</a></iframe><blockquote><em>Remember that </em><em>-Xmx argument should be set to at least your </em><em>dexOptions.javaMaxHeapSize option plus some memory for Gradle build itself. If you don&#39;t specify dex heap size manually, 1GB is used by default.</em></blockquote><h3>Convert your pngs to webps</h3><p>Android has partial (no transparency and no lossless encoding) support for webp format since Android 4.0 (API 14), and full support came with API 18. If you&#39;re lucky enough to be able to use it, Android Studio 2.3 offers quick fix that converts your existing pngs into webps - simply right-click image (or a folder, even res) and choose Convert to WebP.... This can shave off several megabytes, from your APK, which may also speed up sending your application to the device. One thing to keep in mind is that webp images aren&#39;t supported as launcher icons!</p><blockquote><em>Edited: processing or merging resources is not, in fact, related to dexing. Previously stated significant speedup from using </em><em>webp instead of </em><em>png was the result of </em><em>aapt not crunching images anymore. Thanks to Jake Wharton for pointing this out!</em></blockquote><h3>Do not crunch your pngs</h3><p>If you’re not lucky enough to use webps, you can still speed up your build by disabling png <em>crunching</em>. From <a href="https://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables">Android documentation on drawables</a>:</p><blockquote><em>Image resources placed in res/drawable/ may be automatically optimized with lossless image compression by the aapt tool during the build process. For example, a true-color PNG that does not require more than 256 colors may be converted to an 8-bit PNG with a color palette.</em></blockquote><p>This, obviously, takes time. You can skip this optimisation by providing proper option to aapt:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/557e6051791097413dbde01172f815e3/href">https://medium.com/media/557e6051791097413dbde01172f815e3/href</a></iframe><p>In one of ours png-heavy projects, this alone reduced build times from 4 minutes to 40 seconds. You may want to reenable this for production build (or disable only for development) though, as crunching <em>is</em>, after all, an optimisation.</p><h3>Use dex in process</h3><p>Fortunately this is now enabled by default, but to reiterate — make sure your gradle daemon has enough memory allocated (at least 1GB)!</p><p>If at some point your build suddenly slows down, it might be the sign that your heap size is not big enough to fit dex in process anymore. You can then increase dex memory size in your module’s build.gradle file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec7cc58e583bb2f0d35913fcafebdc10/href">https://medium.com/media/ec7cc58e583bb2f0d35913fcafebdc10/href</a></iframe><p>Remember that afterwards you need to increase heap size for Gradle as well!</p><blockquote><em>Increasing java max heap size for dex process should be used sparingly, as it’s rarely needed.</em></blockquote><h3>Pre-dex libraries</h3><p>Simply setting preDexLibraries to true may speed up incremental builds.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb023e9e827623f45c7104b6a2162619/href">https://medium.com/media/eb023e9e827623f45c7104b6a2162619/href</a></iframe><p>It can slow down clean builds though, so it’s worth disabling this option for builds in your CI environment.</p><h3>Set minSdkVersion to 21+ when using Multidex</h3><p>If you use multidex, you can greatly improve your build times if your minSdkVersion is set to 21+. You can either create a special dev flavor for you app that overrides minSdkVersion, or have the version computed dynamically based on a gradle property:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0af2414f6b985c4caf8702ade5605c9d/href">https://medium.com/media/0af2414f6b985c4caf8702ade5605c9d/href</a></iframe><p>And then compile with, for example, ./gradlew installDebug -PminSdk=23. You can set -PminSdk=23 option in Build-&gt;Compiler-&gt;Command line options in your Android Studio as well, to use this option for builds triggered from your IDE.</p><blockquote><em>Remember that Android Studio uses specified command line options also for generating signed APK. If you’re preparing a release APK, you might want to use command line, to be sure you haven’t built yours with too high minimum SDK version.</em></blockquote><h3>Avoid computations in Gradle configuration phase</h3><p>When you trigger a build, at first your gradle projects are configured. This (more or less) runs the parts of your gradle build scripts that aren’t tasks. There’s a popular trick of having git sha in your BuildConfig class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a5885bef273eb9a4f5096050c9eb631/href">https://medium.com/media/7a5885bef273eb9a4f5096050c9eb631/href</a></iframe><p>The problem here is that every time you run a task, getGitHash will be run. While this particular action may not be very inefficient in itself, lots of such small actions pile up. Consider only running these when building certain flavours, like staging builds, or only on CI environment.</p><p>This is also important when applying external plugins. You most likely use Crashlytics in your app — for development purposes it is a good idea to disable not only crashes reporting (directly in your application), but also Fabric plugin itself:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38394ba4ae3c5497146769f577767a75/href">https://medium.com/media/38394ba4ae3c5497146769f577767a75/href</a></iframe><h3>Limit packaged resources configurations</h3><p>In projects with multiple configurations (like translations) you can save some time by only packaging resources for single language or screen size:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3381650383087cfe6e66ae388b6277f/href">https://medium.com/media/b3381650383087cfe6e66ae388b6277f/href</a></iframe><p>You can mix configurations, so resConfigs &quot;en&quot;, &quot;xhdpi&quot; would be also valid, as well as for example resConfigs &quot;en&quot;, &quot;fr&quot;, &quot;xxhdpi&quot;.</p><h3>Use specific dependencies versions</h3><p>Aside from being bad practice, using + in dependencies versions forces gradle to check for newest dependency version each time you build your project. Always use specific library version, like compile &#39;com.squareup.retrofit2:retrofit:1.9.0&#39; instead of compile &#39;com.squareup.retrofit2:retrofit:1.9.+&#39;</p><h3>Use instant run (sometimes)</h3><p>Instant run speeds up builds immensely — if it works. Keep an eye on weird bugs or inexplicable errors. In bigger projects (or for some other reason less-instant-run-friendly projects) you might be better off spending more time building, and less time debugging non-existent errors.</p><h3>Update Java</h3><p>Simply put, Gradle is written in Java, so it’ll benefit from performance improvements in newer JVM implementations. While Android Studio now bundles its own JDK, if you still use older Java on your machine, updating it will also speed up Gradle outside Android Studio.</p><h3>Profile</h3><p>Above all, you should always profile and see where there’s place for improvement. Doing this for Gradle build is as simple as providing --profile option in the command line:</p><p>gradle app:assembleDebug --profile</p><p>In your build/reports/profile directory, you&#39;ll find an HTML report with summary of times and details for both configuration phase and task execution. This will give you some insight into which parts of your build deserve attention.</p><p>That’s it. Let me know what helped you most, and what other tricks you use to speed up <em>your</em> builds!</p><blockquote>This article originally appeared on <a href="http://blog.tooploox.com/make-gradle-builds-fast-again-2/">Tooploox company blog</a>.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ea323ce6a435" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>