Kotlin for Android (I) Why I love Kotlin

27 Jun

If there is only one sentence to describe Kotlin, it would be: “Kotlin is great”!

You will not appreciate the beauty of it if you do not suffer from the repetitive, boilerplate coding in java. Let’s see an example first.

Consider you have two classes, as shown below, they do the same thing, interchangeable, exactly the same in terms of functionality, which one do you choose? Left or right one?

  image  image

The left one is java implementation, and the right, Kotlin.

This is a real example that I encountered in developing an Android App. The App tries to get the new questions on StackOverflow site: Open the link below in your browser to see how you can get the json from StackExchange API:

You will be presented a similar json as below:


In order to display a list of such questions in Android views, I need to deserialize this json string to a POJO (Plain Old Java Object) representation. To avoid hands dirty,  we can go to and paste the json string to get the POJO class:


Three POJO classes will be generated for you:


Copy these classes to the AndroidStudio project, then you will have a 49-line, a 122-line and a 68-line java class. So 49+ 122 + 68 = 239 lines, keep this number in your heart!

All right, so far so good., let’s present these stack overflow questions into a TextView (Yes, you might challenge, why not RecyclerView or ListView? You are correct, these two views are better, but these are not the focus here)

First add a text view to the XML of the activity:

<ScrollView android:id="@+id/textAreaScroller"

Then in the activity’s onCreate():

public class StackOverflowActivity extends AppCompatActivity {

    private final String mAppKey = "JE1srJAZi8)AoOEHcHUWdg((";
    private TextView resultView;
    private OkHttpClient client = new OkHttpClient();

    protected void onCreate(Bundle savedInstanceState) {
        resultView = (TextView) findViewById(;

        String url = String.format("https://api.stackexchange...=%s", mAppKey);
        new GetJsonTask().execute(url);

    class GetJsonTask extends AsyncTask<String, Void, String> {
        private Exception exception;

        protected String doInBackground(String... urls) {
            try {
                Request request = new Request.Builder()

                Response response = client.newCall(request).execute();
                return response.body().string();
            } catch (Exception e) {
                this.exception = e;
                return null;

        protected void onPostExecute(String result) {
            RecentQuestions questions = new Gson().fromJson(result, RecentQuestions.class);
            String info = "";
            for (Items q : questions.getItems()) {
                info += q.getTitle() + "\n";

Now if we run this App, we can get the latest StackOverflow posts/questions:


Everything works! But hold on: think how much boilerplate code you have written: so many getters and setters, these are not pertinent to your logic, right? Though you might think these POJO classes are lengthy, but they are auto-generated for you, and then no bother, right?


But let’s do a simple quick improvement:

Select the folder “pojos” above, and click the menu item “Code>Convert Java File to Kotlin Files”


Then you will see all the three java files are converted to Kotlin classes:


The old 49-line java class now reduced to a 15 line Kotlin class! And now you have 15+19+30 = 64 lines, instead of 239 lines to make it work the same! Rebuild the app, and now you should see the results as shown in the snapshot.


Now you used 26% code and you accomplished the same task. All the beauty comes from Kotlin’s conciseness.  Note that the more you write, the more you maintain!

But why don’t I have the menu  “Code>Convert Java File to Kotlin Files” in my AndroidStudio? No worry, you need to head for the next post Install Kotlin in AndroidStudio!

Leave a comment

Posted by on June 27, 2016 in General


Tags: , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: