Provider and Lazy Injections with Dagger2

Sometimes, when providing dependencies with Dagger, you may want to change how the instances of your dependencies are instantiated. Dagger gives us some options with two generic interfaces we can use at an injection site: Lazy and Provider. If you are new to Dagger, I would suggest reading the introductory article I wrote. Lazy Lazy allows us to instantiate an instance lazily. This means that the dependency will only be created once a method is called on it and then subsequently cached. Let’s look at an example. The first logging statement will output 0, since SomeClass has not yet been instantiated. However, the second log will output 1. This is because the instance will only be created once Lazy’s get() method is called. Further calls to get() will not recreate the instance; it is cached when get() is called the first time. Note that Lazy does not work like a scoped binding, if we were to Lazy inject multiple instances of the same class via our constructor, they would all be unique instances – not the same. Provider Provider creates a new instance each time get() is called, this is essentially the opposite to how Lazy initialization works. In this example, we’re calling get() three times. As you can probably guess, instantionCount will be 0 initially and then rise to 3 at the last log statement. Direct Injection But how does our class work without using Lazy or Provider? What about plain old direct injection? Can you guess what the logs will say? Both logging statements will print 1. Why? This is because with direct injection, the instance is resolved and instantiated immediately. This is different to both Lazy and Provider where instantiation is deferred to their get() calls. Wrap up Now that you know how Provider, Lazy and direct injection works – you …

Multibindings in Dagger2

One of the advanced features Dagger2 gives us is multibindings. Multibindings allow us to provide multiple dependencies in a single collection, even when these dependencies are in different modules. Multibindings have a variety of different use cases which should become apparent after we take a look at some code. If you have no idea what Dagger2 is, I would recommend reading my introduction to the library here. Multibinding with @IntoSet Let’s say we wish to have a Set of dependencies (a collection containing no duplicates) be injectable, we can achieve this quite easily using the @IntoSet annotation. Here we have a standard module which is providing a String, “A”. The only difference is that we are also annotating the providing method with @IntoSet. This annotation will tell Dagger to add this String to a generically typed Set, Set<String>. How does Dagger know to add it to a Set<String>? Because we are returning a String. We can add more String dependencies into our Set by following the exact steps taken above. So now that we have two modules which are providing two Strings annotated with @IntoSet, this means that our component will now construct a Set containing these two Strings. Taking advantage of constructor injection, we can inject the Set like this: And the necessary component: We will now have access to all the String dependencies we marked (in their providing methods) with @IntoSet. Of course, this works with any type – the usage of String here is just for example purposes. Multiple Elements It’s also possible to add multiple elements to our Set at once, instead of adding one at a time. We can do this by using the @ElementsIntoSet annotation. The process is quite similar to before. Instead of returning a single dependency, we are now returning a Set …

10 Reasons Why You Should Drop Java and Switch to Kotlin

In the following article, I’m going to be listing 10 advantages Kotlin has over Java. Some common complaints I usually hear against learning Kotlin is that “Java already works fine” or “I already know Java and don’t want to learn another language”. The first reason is true to an extent, however the second is sheer laziness. Believe it or not, your language of choice will probably not be popular or around forever and even if it is, better ones may come along. As was the case with Kotlin being announced as an official language for Android development at Google IO 2017. On top of this, if you are already an intermediate or above in Java – learning Kotlin should not take you much time at all. If you are new to Android development I suggest reading another one of my articles, “How to Become an Android Developer” or this one if you want to see what Android development with Kotlin looks like. Note: these are in no particular order 10. Model Objects Unlike in Java, where, when creating classes only meant to hold data (models or POJOs to be specific), we are required to write all of the fields, setters, getters as well as a constructor. In Kotlin, we can define all of these things on a single line. Java

Kotlin

Both of these classes are identical in functionality. As you can see the Java class is your standard POJO (Plain Old Java Object) with a constructor and setter/getter methods. The Kotlin code looks a little bit different (and much shorter), but it is the same class. In Kotlin, we include the constructor and class members when declaring the class itself. We also do not need to write explicit setter and getter methods, which is huge. Var is used to …

Beginner’s Guide to Dagger in Android, with Kotlin

What is Dagger? Newer developers tend to think it’s a library that simply enables the use of dependency injection – this is false. Dagger2 is just a dependency injection framework that makes DI much easier to implement. In the following article I’m going to walk you through the entire process of setting up Dagger with code examples. I’ll also explain the basic concepts, such as components, modules and subcomponents in order to help you make sense of everything. If you are new to Android development, I’d recommend reading another article I wrote on how to become an Android developer. NOTE: Be sure to follow along with the sample project I wrote located here: https://github.com/jtrollkarl/DaggerExample This sample project makes use of the Mosby library, which allows for quick setup of the MVP pattern. The most important thing to note is that the presenter(s) is retained across device reconfig. If you do not understand what MVP is, check out this article explaining it. Dagger2 – Something Confusing? Similar to RxJava, Dagger is often said to come off as being difficult to understand and use. I can admit that when I first tried to learn Dagger, I felt completely lost. I read a lot of tutorials and watched every YouTube video I could find. Slowly but surely, one day it finally clicked. Though hopefully the learning process won’t be as arduous for you with the following article. Just remember to breathe and take it slow. The Advantages Dagger really shines in projects with a lot of dependencies. It can be a pain to manually inject every single one of our dependencies into a class when creating it – Dagger can do this for us quite easily. Let’s take a look at the following example of a function which builds a class through constructor injection. Without Dagger As you …

Refactoring Your Java Android App to Kotlin

With the release of Android back in 2008, the go-to language for app development has always been Java – however, that’s beginning to change. At Google IO 2017, it was announced that Android would be receiving “first class support” for Kotlin. Since then, the language has been slowly ramping up to becoming the preferred language for development on the Android platform. In the following article I’m going to give you some ways you can refactor/rewrite your existing Java app code to Kotlin, while also pointing out the advantages Kotlin gives us. But what about Java? I still feel like Java is worth knowing, since a lot of the Android APIs, as well as their documentation, are written in it. If you’re just starting Android development, I would reccommend developing at least an intermediate level of Java knowledge before moving on to Kotlin. If you’d like an in-depth guide to getting started with Android development, you can read an article I wrote detailing how to become an Android developer in under a year. I should also mention that Kotlin is interoperable with Java. For this reason, you aren’t forced to choose one or the other. You’re able to call Java code with Kotlin and vise versa. So, don’t feel like you’re wasting time just because you’re learning Java. Refactoring Java to Kotlin I’m going to present some Java classes from my own personal projects, with the Kotlin counterpart following. The advantages Kotlin presents should be evident from simply looking at the code, but I’ll be sure to go through each. SharedPreferences Here’s an implementation of an interface used for managing a few SharedPreferences  values. The class is quite straight-forward, as far as preference managers go. Let’s take a look at the same class in Kotlin. Immediately, you can already see the amount …