Installing the Kotlin Plugin in
Android Studio
The very first thing you need to do is add Kotlin support to
your Android Studio installation.
Click on Configure icon, and then select Plugins from dropdown:
Click the Install JetBrains plugins button:
In the opened dialog, search for Kotlin, select the corresponding
plugin and press Install:
When you’re finished with downloading and installing, the
next step is following the prompts to restart the IDE
Configuring Your Project to Use Kotlin
Now the IDE knows what to do with Kotlin,
but you’ll still need to configure Kotlin every time you
want to use it in a new project.
Let’s create a new project and configure that project to use
Kotlin now.
so your next move is to modify the project’s build
configuration.
Go to Tools\Kotlin\Configure Kotlin in Project.
Select Android with Gradle from the Choose Configurator
popup that appears.
On the Configure Kotlin in Project popup, select the plugin
version you want to use and click ok.
This action will make a number of changes to your
build.gradle files.
// 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.3'
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
}
buildscript {
ext.kotlin_version = '1.1.3-2'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
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 take a look at your module-level build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.xsoftech.kotlintest"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
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:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
mavenCentral()
}
apply plugin: 'kotlin-android'
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.xsoftech.kotlintest"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
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:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
mavenCentral()
}
Finally, sync your changes either by clicking Sync Now from
the popup and done.
NOTE: Using Kotlin
with Android Studio >= 3.0
Starting from Android Studio 3.0 the Kotlin plugin is
already installed. No additional setup is required.
Convert or Create code for Kotlin
Convert Any Java File to Kotlin:
One feature of the Kotlin plugin that’s particularly useful
for Kotlin newcomers is its ability to convert any Java source file to Kotlin,
while maintaining full runtime compatibility.
Let’s convert our project’s TestKotlinActivity file into a
Kotlin source file. There are two ways of invoking the Kotlin plugin’s Convert
Java File to Kotlin File action, so either:
Select your TestKotlinActivity file, and then select Code
from Android Studio’s menu bar, followed by Convert Java File to Kotlin File.
After Converted in Koltin Lanugange of java code, look like:
package com.xsoftech.kotlintest
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
/**
* Created by kunwar on 03-08-2017.
*/
class TestKotlinActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
/**
* Created by kunwar on 03-08-2017.
*/
class TestKotlinActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
}
Just be aware that, depending on the complexity of your
code, the conversion may not always be 100% accurate, so you should always
check your converted code for errors.
This may be a simple Activity, but these few lines
illustrate some key characteristics of the Kotlin syntax. Since this is our
first look at some actual Kotlin code.
Creating Kotlin Activity:
If you continue working with Kotlin in your project, then
sooner or later you’re going to need to start creating new Kotlin files rather
than simply converting existing Java ones.
To create a Kotlin file, Control-click your
app/src/main/kotlin directory and select New > Kotlin Activity.
Your new class should look like this:
package com.xsoftech.kotlintest
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
}
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class Main2Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
}
Building and Run the Kotlin application for Android
You are now ready to build the application and run it on an emulator or device. This works in exactly the same way as in Java.
Comments
Post a Comment