Kotlin : Retrofit + RxAndroid + Realm
In this article I thought I should cover some of the libraries (that I use regularly), like Retrofit and Realm in the context of Kotlin and Rx.
So let’s begin : Kotlin + Rx + Retrofit + Realm
If you’re anything like me, then you absolutely hate writing unnecessary code. *cough Java 6 cough*. And in my Android development experience, I’ve come across many situations where I was like
In the context of Languages, I think Kotlin is definitely a life-saver. And libraries like Retrofit, Realm and RxAndroid also significantly reduce the amount of unnecessary code.
Horror story: I once came across a project, which had around 12 model classes aka POJOs. Then there were 12 Database (ORM) Table classes and there were also 12 Model-to-Database-Mapping classes. If you’re good at counting, you’ll notice… Those are 36 classes right there! And not only that, a change to a single class meant, changing all 3 classes.
I suppose, even if we don’t use any ORMs, we still would be writing code for mapping the Models to the Database! And that’s no good!
So, what was the solution… Well… For me it was Realm + Retrofit… I added in RxAndroid to the mix later on, because Rx makes almost everything better and more delicious! And of course Kotlin, because…
Kotlin is love… Kotlin is life…
With the combination of these, those 36 classes would be represented in just 12 (as they should be). And not just that, code will be more concise and expressive because of Kotlin!
Simple Example: Getting data from Github API
Imagine an app which gets data from an API, stores/persists that data to the database and also displays it. A pretty common scenario, right?
This is what I’d be making… An extremely simple example… Which should hopefully cover the basics!
Creating the Project and Enabling Kotlin
The first thing we do is enable Kotlin in the project — Just make sure the Kotlin Plugin is installed in your Android Studio.
I created an Empty project with a single MainActivity — Went straight into the build.gradle file and… Triggered the action…
Cool! Now we can write code in Kotlin!
We can (magically) convert the existing java code to Kotlin… By doing the following…
Bamm! The MainActivity becomes…
Adding Rx — Retrofit — Realm Dependencies
Time to add some dependencies!
Now you might be thinking what the hell is kapt?
Well it’s an annotation processor built for Kotlin.
In our project we’ll also have to enable generateStubs as well, in order for kapt to generate code.
So… Now we have everything we need! Time to start coding!
The endpoint we’re dealing with…
And the response…
"location": "Rawalpindi, Pakistan",
Ok for this example, I just want to extract id, name, avatar_url and public_repos from the response. So my model class (which is also a realm class btw), would look something like
Note: If you want to extract everything from the JSON response, then I suggest visiting to this awesome website, and generate a POJO. Then convert the Java code to Kotlin as described earlier.
Ok, lots of things to cover here… Starting off from the RealmClass annotation… For Realm, this annotation is necessary for Realm’s code generation in Kotlin… PrimaryKey annotation is also a Realm annotation, representing the Primary Key field (duh!)… Rest of the annotations are for Gson…
Now the open keyword! In kotlin it’s the opposite of final in Java. By default, Kotlin classes are final — that means if you want a class to be inherrited — we explicitly have to declare it as open. Same is the case with properties. Like in our model, name is a property, with an actual getter and a setter (thanks to Kotlin). And in order for the getter/setter to be overridable (which Realm requires them to be), we put the keyword open along with the declaration of the property. Which makes sense (to me at least).
Again the endpoint url is
So the interface would look something like :-
Notice how I’m returning Observable of a Github. This is possible because Retrofit allows Rx integration. And that’s super-super-cool!
Because of Realm we use a special Gson instance, which basically adds an exclusion strategy for skipping Realm generated fields. Otherwise Gson doesn’t work with the model.
Next we create an instance of Retrofit with RxJavaCallAdapter factory which allows Rx integration and we also add the Gson converter using the Gson instance we created before.
Now all I do is get the observable, subscribe to it and get our Github object. Once we have the object, it’s easy to persist the user to Realm database.
Caching is also (sort of) possible, because we can first fetch data from Realm Database (if it has already been saved)… Like this…
And… That’s it! Now if you run the app…
You can find the full code example here. Hope the article was somewhat useful!