Introduction

Hi all, the idea will be to always show something from scratch and then apply it to a real project. I created an old-fashion project where we will apply all the techniques we are gonna learn. That means that you will always have 2 parts, but they probably won’t be launched at the same moment.

Ok let’s go!

Note: This post assumes you have certain knowledge about Android, Java, Kotlin. The post is more about the way to architecture an app to add Kotlin if you don’t want to put it in the same package where the Java code is.

You want to start coding with Kotlin, but you don’t want to start mixing everything. So you think, “Ok I will create a new folder called ‘Kotlin’ and put all the code there…” Just no. Kotlin works perfectly with Java, so you do not do that.

A good way to face this is by using the new multi-module architecture. How? Well just a bit of patience. To show you, I will make a sample app using the Marvel API.

Remember if are starting from scratch, when you create the project, check the Kotlin support checkbox.

FYI, in the next part we will apply this modularization to a real app. Like I said, the idea is to know how something is implemented from scratch and then to apply it to your daily app.

App module

This is the main module. It is the module created when you create a project from scratch. I created a main activity MainMarvelsActivity.java which displays a list of Marvel Characters, it is a basic Java-Android code with a RecyclerView and an Adapter.

I get a response from the Marvel API to get some characters and save the response in a .json file, which I read and parse using GSON.

To begin, we need some POJOs to put the data from the .json file. Let’s call them MarvelCharacter and MarvelImage. Once we have that working, all’s cool for now.

So next we want to improve the app and add a new feature using Kotlin.

When you click on a item from the list, we want it to open a new activity showing more details of the Marvel Character clicked.

The first thing we have to do is, look for classes, objects, or anything that we will need in the new activity called DetailCharacterActivity. In this case we only need MarvelCharacter and MarvelImage.

We have to do something so we can use those classes. Probably you are thinking, we don’t have to do anything, just create the activity and use it, but no. Remember we want to create a modular architecture, so keep reading.

Java Module

Now that we have identified what we need, we have to create a module where we can put those classes so everyone can use them.

As we can see, we only have Plain Java Objects, so we can create a Java Module. Right click on the root folder in your hierarchy panel, new module, Java Library (at the bottom).

You can create an android module, but a Java one is a bit faster.

Now we can move the Java classes to that module, and that’s it…

Mmmmm, wait the App Module now can’t find the classes, so we have to add the new module and probably update the path of the imports.

Go to the App Module gradle and add this line in the dependencies section

implementation project(‘:marvelpojo’)

Now you can use those classes like any library, YAY!

Detail module setup

We have all the basic ready, so we can move on to the Detail Module where we will create the DetailCharacterActivity.

First we have to create an Android Module. Just like before, go to new module. This time you have to click on Android Library. You can leave the defaults and finish.

In the new module, we will need MarvelCharacter and MarvelImage classes, so like we did on the App Module, we go to the Detail Character Module gradle and add the same dependency.

Finally we’re here, the moment we wanted for so long, we can start writing our first Kotlin code.

Create a DetailCharacterActivity.kt, remember to select the Kotlin source language when prompted. Let’s add a bit of code to get the Marvel info and display it on screen. (You know startActivity with a new Intent)

That’s it! For sure there are many ways to do the same thing, to add a new feature in Kotlin and not mix it with the Java world. This is one way I use.

Summary

We started with the idea of adding Kotlin into our project with the goal of not mixing it with the Java world. One approach we can take is to use modules.

Using modules makes your app behave like Lego blocks; you have the main piece and you keep adding more onto it. By doing this you can improve your build time. This might give you the chance to add Instant App in the future, apply TDD because you are speeding-up things.

Like I said, in the next part we will do the same but with a bigger project.

Links:

Source code: https://github.com/gedu/teddydroid_modulation_kotlin

Marvel API: https://developer.marvel.com/

Modularization and time: https://medium.freecodecamp.org/how-modularisation-affects-build-time-of-an-android-application-43a984ce9968

Copy editing: Daniel Cohen

--

--