How to create a Kotlin project using Gradle and run it with Android Studio
Tags: gradle, androidstudio, kotlin
Published: 2019-07-08 01:52:45

---
title: How to create a Kotlin project using Gradle and run it with Android Studio
published: true
description: Create the Gradle project using the cli then open with Android Studio and enjoy
tags: Gradle, Android Studio, Kotlin
cover_image: https://thepracticaldev.s3.amazonaws.com/i/7jmw0x20oo3f7y5tgac0.png
---


Working on Android has plenty of facets, one of them is `gradle`, configuring it can be hard sometimes but it is very rewarding, in most serious companies there are people specialized in the gradle build. So I want to learn more about gradle and challenge myself to create a simple Kotlin project using and then running in Android Studio to leverage the power of the IDE.


## 1. Install Kotlin

Use the installer of your choice, [see the manual](https://kotlinlang.org/docs/tutorials/command-line.html) (brew, sdk, ports)

Using brew it would be

```
brew install kotlin
```

If the only things you want to do is to create a hello world, then you can just do this and run it using the terminal following the above documentation.


## 2. Install Gradle
This should not mess with your Android Studio projects because Android projects use a dynamic gradle online, that is why you can configure offline on Android Studio. If offline Android Studio peak your interest [this article](https://hackernoon.com/speed-up-gradle-build-in-android-studio-80a5f74ac9ed) should help you (I have mixed experiences using offline, not a recommendation neither discourage, find your flavor).

Again use the installer of your choice for installing gradle, [see the manual](https://gradle.org/install/)

With brew it will be

```
brew install gradle
```

**You need to have Java before this**, if you are coming from Android Studio this shouldn't be a problem.


### 3. Create a Gradle project

We are now gonna create a gradle project using the cli, there is some info on the Gradle and official Kotlin but I was confused, so I rather give you my own recipe based on those.

 1. Create the project

```
mkdir devDotTo
cd devDotTo
gradle init
```

That will trigger the interactive CLI, is literally reading what it says and following it, but since I'm clueless I got tons of mistake, so let me spare you the trial and error.

 2. Select Library (number 3 and enter)
 3. Select Kotlin (number 4 and enter)
 4. This last selection is the language used for the gradle files, so choose the one you are more comfortable. I'm used to Android, so I like groovy, and I won't change to the Kotlin extension until I deeply understand what am doing.
 5. The next 2 options are naming so you can type whatever suits you and your standards or just press enter twice.

This creates a template project with one default class and a default test, we will have to modify that a little on Android Studio.

### 4. Run it using Android Studio

 1. Open Android Studio and open the project, click on the directory and click on open

![open on android studio](https://thepracticaldev.s3.amazonaws.com/i/18umeey0f07b8b4q1bzs.png)

 2. Let Android Studio do the thingy... blow some air on your computer if you need to cool it, xD Seriously, if you are used to Android projects, you can select the "Android" vision on the pane A.K.A. the left tab with the files

 3. Open the file `Library.kt` is gonna be the only file there. We are going to add the **main method** so we can configure our IDE to run it. There is a default method, if you remove it, make sure to remove it on test folders as well. Your file should look like this:


```
class Library {

    companion object {

        @JvmStatic
        fun main(args: Array<String>) {
            println("DEV DOT TO")
        }
    }

    fun someLibraryMethod(): Boolean {
        return true
    }
}
```

As you can see we have to add it inside the `companion object`.

 4. Now we are going to configure Android Studio to run it. Click on "Add Configuration" on the top bar

![add configuration button](https://thepracticaldev.s3.amazonaws.com/i/9456kg7in7hgzybzaocv.png)

In the dialog that shows up, don't click the templates, click the plus button (you don't want to edit a template, you want to use a template to create a configuration). Select the Kotlin template:

![add configuration kotlin](https://thepracticaldev.s3.amazonaws.com/i/7jmw0x20oo3f7y5tgac0.png)

Now you have to select the Main class, if you press on the 3 dots, you will see it right away because it has the `main method`

![3 dots](https://thepracticaldev.s3.amazonaws.com/i/gps2rv8w1ooz8ejvug2n.png)

![main class](https://thepracticaldev.s3.amazonaws.com/i/9fwtwpkeskiqdylrg5mx.png)


Now the only thing you have to do is **save the configuration**. The default name is "Unnamed" I like naming it "Run".

You can click on the run button or `ctrl + r` on Mac and in Windows `shift+F10`

![run](https://thepracticaldev.s3.amazonaws.com/i/o7rjz7iqbwovqmnn7pbf.png)

Hooray! We have our logs in the console

![DEV DOT TO](https://thepracticaldev.s3.amazonaws.com/i/zkmzwrvyp4rqhxgavhel.png)