Introduction Installation Process and Hello World Application in Kotlin

Posted By : Aman Goyal | 09-Aug-2017


This tutorial contains short intro to Kotlin ,installation process and Develop a Hello World Application in android using Kotlin.
Indroduction to Kotlin

Kotlin is statically typed programming language developed by JetBarins.It is used for JVM, Android and for the browser and now it is officialy supported by google for Android. Kotlin provide more expressive syntax and these syntax reduces the 40% lines of code. Using Kotlin you can develop cross-platform mobile application, cross-platform gaming applications.And it is 100% interoperable with JVM And android. And it directly complies to machine code without any virtual machine.

Kotlin syntax are very rich using these you don't need to write large number of lines of code.

For installing the kotlin plugin in android studio follow these steps.
1. File->Settings->Plugins->Browse Repositories.
2. Search Kotlin and then install it.
3. Restart the Android Studio.
4. Created the new project.
5. After crreating the project it generates the default java code with old syntax.
6. Go to Menu Code->Convert Java File to Kotlin File or Pres Ctrl+Alt+Shift+k.
7. After Doing this your code becomes:

 

package com.example.aman.kotlindemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}   
        

8. Configure Kotlin for your Android Project press shift twice then you see finder and type convert kotlin.
9. Then a configure kotlin project popUp would be opened then select All modules containing Kotlin files: and select Kotlin version. and press Ok and sync the project.
10. After sync the project you  project based build.gradle file becomes:

buildscript {
    ext.kotlin_version = '1.0.5-2'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}   
        

And module based build.gradle becomes:

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

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.poovarasan.kotlindemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
    mavenCentral()
}
   

Kotlin Android Extensions

The Kotlin android extensions provide an easy way to add your layout  all views into your activity class file.

      apply plugin: 'kotlin-android-extensions'   

Add this line in module based build.gradle file and code becomes:

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

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.poovarasan.kotlindemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
    mavenCentral()
}   
      

Add this classPath dependency in project based build.gradle file:

classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"   
  

 

After this dependency code becomes:
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.3-2'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}   
    

And in the activity_main.xml file add the ids for the views which you want to inject or use in activity class.

         


    
   
        

 

Now import this file into activity class

          import kotlinx.android.synthetic.main.activity_main.*   
      

After adding this file your code becomes:

         package com.example.aman.kotlindemo

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*    //this file direct inject the views from layout to activity class and activity_main is your xml file name. Those views you want to use in that activity class import that xml file like activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        helloText.text="Hello Oodles"                    //helloText is your TextView Id and like this you can modify all types of views.
    }
}   
        

About Author

Author Image
Aman Goyal

Aman is an Android developer. Quite good in building logics. Languages he is good in C, C++, java. He is keen on learning new things, flexible, and adaptive to the new environment. Also, have some experience in marketing.

Request for Proposal

Name is required

Comment is required

Sending message..