What is Kotlin?
Kotlin is a statically-typed programming language that runs on the Java Virtual Machine and also can be compiled with JavaScript source code or uses the LLVM compiler infrastructure. It is primarily developed by a team of JetBrains programmers based in Saint Petersburg.
Why Kotlin?
- Kotlin is aimed at eliminating the danger of null references from code.
- Kotlin compiles to JVM bytecode or JavaScript.
- It imposes no runtime overhead.
- The standard library is small and tight, it consists mostly of focused extensions to the Java standard library.
One of Kotlin’s greatest strengths as a potential alternative to Java is the sheer level of interoperability between Java and Kotlin—you can even have Java and Kotlin code existing side by side in the same project, and everything will still compile perfectly. Below, you can see an example of a project that consists of a Java Activity and a Kotlin Activity.
How to configure Kotline in Android Studio
Step 1: Setup the Kotlin Plugin in Android Studio
In order to ensure Android Studio support Kotlin, the first thing is to install the Kotlin Plugin for your Android Studio.
Android Studio → Configuration → Plugins →Install JetBrains Plugins → type “Kotlin” in search box →install
That’s all the generically applied to your Android Studio. Only needed to do once per installation of Android Studio.
Now restart the android Studio.Aftet create a new android project and fill all the required Detail. File -> New -> android project.
Step 2: Add Kotlin classpath to project Build.Gradle
To configure the plugin, we first need to add the plugin to the root, project’s build.gradle, 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"
}
}
Step 3: Add Kotlin library and apply Kotlin Plugins in your module Build.gradle.
You will add the Kotlin Library into its Build.gradle.Also, remember to apply both the Kotlin Android and its extension plugin to your project.
Step 4: Ready to start with Kotlin…
You can also convert the java file to Kotlin go to the code menu and choose the convert java file to Kotlin file (ctrl + alrt+shift +k ).