DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. How to Set Up Android Studio for Kotlin Development

How to Set Up Android Studio for Kotlin Development

Kotlin has many benefits, including an IntelliJ/Android Studio Plugin. Adding the plugin to a new Android project and converting your code from Java to Kotlin is easy.

Segun Famisa user avatar by
Segun Famisa
·
Feb. 02, 17 · Tutorial
Like (3)
Save
Tweet
Share
13.55K Views

Join the DZone community and get the full member experience.

Join For Free

If you have been following the latest trends in the AndroidDev world, you are likely to have heard about Kotlin.

Kotlin is a (not so) new programming language developed by JetBrains, the makers of IntelliJ IDE that the Android Studio is based on. Kotlin is statically typed, it runs on the JVM, and it offers a lot of benefits. There are tons of articles that talk about the advantages of Kotlin in comparison to Java, so I won’t be covering that in this post.

In this post, however, we will learn how to set up Android Studio for Kotlin development and will write our “Hello World” app in Kotlin.

Let’s get started!

Install Kotlin Plugin for Android Studio

The good people at JetBrains have created an IntelliJ/Android Studio Plugin for Kotlin. First, we’re going to go ahead and install the plugin.

To do this, navigate to Preferences > Plugins > Browse Repositories, search for Kotlin, and click Install, as shown in the figure below.

When the install is complete, you will need to restart Android Studio to apply the new plugin.

Just in case you’re using IntelliJ IDEA 15 or above, you’re in luck. Koltin plugin ships with the IDE.

Create a New Android Project

Now that we have the plugin installed, let’s go ahead and create a new Android project the usual way we would. Navigate to File > New > New Project and follow through the project creation wizard. Select the Create Empty Activity option at the end.

For more information on creating an Android project, check out this guide.

Apply Kotlin Plugin to the Project

The next step is to apply the Kotlin plugin to the project. There is an automated tool to do this but sometimes, the tool messes things up, so let’s just walk through the manual process of applying the plugin in our build.gradle files (both at the project level and the app module level).

Add Kotlin Gradle Plugin

To configure the plugin, we first need to add the plugin to the root project’s build.gradle, the same way it’s done for the Gradle plugin (automatically). Add the plugin to the dependencies closure in the project build.gradle file. The project build.gradle file looks like this:

buildscript {
    ext.kotlin_version = "1.0.6" // replace with the latest (stable) version: https://github.com/JetBrains/kotlin/releases/latest

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

A few notes about the block of code above:

  • We created a variable ext.kotlin_version. The reason we have this is we may have other places in the project where we need to reference the Kotlin version, so it makes sense to “externalize” that value. You can check out this post about externalizing your dependencies versions using gradle extra properties for more info.
  • We placed this variable in the buildscript closure. This is because the buildscript is the entry point into this file when the project is being built. If we place the variable outside this closure, this variable won’t be available before the project is built, and the build will fail.

Apply the Kotlin Android Plugin

After adding the Kotlin Gradle plugin, the next step is to apply the plugin. To do this, we need to add apply plugin: kotlin-android to the app-module’s build.gradle file:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' // apply kotlin android plugin

Convert Activity Code from Java to Kotlin

We’ve applied the plugin and setup all we need to, but our “Empty Activity” generated code is still in Java.

Luckily for us, the Kotlin plugin can help us convert our code from Java to Kotlin.

Currently, our Java Activity looks like this:

To do this, select the file and navigate to Code > Convert Java File to Kotlin File, or use the shortcut Command + Alt + Shift + K (I believe you can replace Command with Control if you’re on a PC).

After converting our code to Kotlin, our “Empty Activity” looks like this:

Bonus: Staying Up-to-Date With Kotlin Versions

You’ve made it this far — let’s add a bonus tip!

Kotlin is still heavily being worked on, and there are (frequent) fixes and updates. It’s generally a good idea to stay up to date with the new features and changes in the language. The Kotlin Android Studio plugin helps us manage the versions of Kotlin. If you navigate to Tools > Kotlin > Configure Kotlin Plugin Updates as shown in the screenshots below, you will be able to select the update channel — Stable, Early Access Preview - 1.0.x, or  Early Access Preview - 1.1 — which is currently the bleeding edge version.

Further Reading

There are a number of great resources scattered around the internet for learning Kotlin. Some of them are:

  • Official Kotlin website.
  • Kotlin links.
  • Kotlin Weekly.

In future posts, I’ll be writing about some specific features of the language.

Thanks for reading this post. I am always open to questions and comments. If you have any, kindly drop them in the comments section below or send me a tweet.

If you found this post useful, or know someone that may find it useful, please feel free to share. Thank you!

Kotlin (programming language) Android Studio Android (robot)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How and Why You Should Start Automating DevOps
  • Hidden Classes in Java 15
  • The 12 Biggest Android App Development Trends in 2023
  • An Introduction to Data Mesh

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: