Tag: kotlin

[HOW TO] Set up Kotlin Source Code & Tests in your Android Apps

[HOW TO] Set up Kotlin Source Code & Tests in your Android Apps

After reading a lot of different posts raving about Kotlin, I couldn’t help myself. I had some serious FOMO. In this blog post, I will be looking at getting Kotlin set up in existing Android Apps.

What is Kotlin?

Kotlin is a statically typed programming language that can be used to write Android Apps. It is made by JetBrains, the same guys who make IntelliJ, Resharper and other awesome tools.

What does Kotlin code look like?

Below is how you would declare a function in Kotlin:

As you can see there are a few notable differences from Java:

  1. The lack of semicolons (Yay right?).
  2. The declaration of the return type is at the end of the function definition.
  3. The type of the parameters are defined after the parameter name.

For more basic syntax differences, you can read more here.

Why is it better than Java?

A quick summary of Kotlin’s Advantages over Java:

This video by Jake Wharton highlights the differences between Java and Kotlin quite well. There are more links at the end of this blog post.

So now that we know what Kotlin is and why we might want to use it for our Android Apps. Lets take a look at getting set up in our existing Android Project.

Setting up your existing Android Project to use Kotlin

  1. Install the Kotlin plugin for Android Studio :
    • Go to “Preferences”→ “Plugins”→ “Install JetBrains Plugin”→ Type “Kotlin” in the search box→ Select “Kotlin” and install it. You will probably need to restart Android Studio for the settings to take effect.Kotlin Plugin in Settings Menu - Android Studio
  2. Go to your project level build.gradle file and add the following line into the classpath section
  3. In your app level build.gradle  add the following:
  4. Create a folder in your app/src/main/  folder called kotlin . You can then create a folder with your package name, so for my example it would be  org.bookdash.android.(You can also add kotlin files into the same directory as the Java files, I just prefer to separate them out by language.)
    Folder Structure when using kotlin programming language in android studio
  5. To add a new Kotlin class, right click on the package name and click “New -> Kotlin File or class”. Give it a name, select the type and you can start writing Kotlin code!How to Add new Kotlin class file in Android Studio

Convert existing Java files to Kotlin

In Android Studio if you want to convert a Java class to Kotlin, do the following:

  1. Open the file you wish to convert
  2. Run the following command. (Navigate to Action):
    • MacOs: CMD + Shift + A
    • Linux & Windows: CNTRL + Shift + A
  3. Then type “kotlin”, you should see the option to convert existing Java file into Kotlin.  Convert existing Java code to Kotlin
  4. This will take the Java file that you are in and convert it into a Kotlin class. I would make sure that the code that is generated is okay. I am always a bit worried about generated code not working. 😁
  5. I converted the AboutActivity  from my Book Dash App to use Kotlin, this is the result:

    If you want to see an example of an app using Kotlin and Java, you can check out my github project, the Feature-Kotlin-AboutActivity branch.

Writing Tests in Kotlin

After getting some code written in Kotlin, I thought it would be great to see if I could set up some Android tests in Kotlin. Here is how to do it:

  1. In your androidTest folder create a kotlin folder
  2. Create your package name that matches the package you are testing.Getting Android Tests set up in Kotlin
  3. In your app build.gradle file, add the following:

    Adding that line tells Gradle that it should also look in the kotlin folder for source code and it should include it when building.
  4. Then you can start adding code to that folder. Below is an example of my AboutActivityTest  which does some basic espresso testing in Kotlin.

    Why I like Kotlin

  • The code looks neater and concise (no more semi-colons)
  • It is not just for Android apps.  It runs on the JVM you can use it pretty much anywhere.
  • You can write in both Java and Kotlin in the same project, they work together.
  • Of course the Null Safety Lambdas , Extension functions are great too 😁

What next?

I suggest reading through the docs about the advantages of Kotlin and try out some Kotlin code on the website or in a sample app. Then go forth and #MakeKotlinApps!

I would love to know what your are experiences with it so far? Do you have any other tips or tricks to getting started with Kotlin?

Links:

Book Dash Android App Github

Why Kotlin is my next Programming Language

Kotlin – The swift of Android

Using Project Kotlin for Android

RedChain –  Github Project for Android App – Daily goal tracker written in Kotlin