Android Apps in Kotlin: Getting Started
What is kotlin?
Kotlin is known as an open source programming language designed by Jet Brians, is becoming increasingly popular among Java developers. It is often touted as Java’s successor. Comparing to Java, it offers a richer development experience, because it is more modern, expressive, and concise.
When you run a Java application, the app is compiled into a set of instructions called Bytecode and runs in a virtual machine. Over the past several years, a number of new programming languages have been introduced to also run on the JVM. The resulting app looks the same for the virtual machine, the idea is the language features can help developers write simpler code and fix some of Java’s issues.
It is a statically typed programming language for the JVM, Android and the browser which is now officially supported by Google For Android.
It is cent% interchangeable with Java, to the point where you can continue to use existing Java libraries and frameworks, and can even use a mixture of Java and Kotlin code in your Android projects.
Its syntax is designed to feel very similar to Java, meaning it should be fairly easy for Java developers to get to grips with the fundamentals of Kotlin.
Why use Kotlin?
As we are still using Java for Android development, now we can use kotlin for Android Development there are some reason to use it. Code optimazationis main reason and it is cent%interchangeable with java, Syntax easy for java developers and It’s well-supported by Android Studio.
Installing the Kotlin plugin:
1. Open Android Studio and click Android Studio==>Preferences
2. Select Plugins here.
3. Now select Install JetBrains plugin
4. In search box you will have to typeand select Kotlin and on the right side you get the option to Install Kotlin Plugin.
5.Configuring Your Project to Use Kotlin: Now you are able to run kotlin code, but you still need to configure kotlin, Soselect Tools from Android studio toolbar, select Kotlinand Configure Kotlin in Project.
6. AfterConfiguring,then go to android studio project and select build.gradle(Project)and check ext.kotlin_version = ‘1.0.0’added in buildscript andclasspath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”line added in dependencies.
buildscript { ext.kotlin_version = '1.0.0' 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}
7. Selectbuild.gradle (Module:app) plugin addedapply plugin: ‘kotlin-android’ andcompile “org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version”this line add independencies.
apply plugin: 'com.android.application'apply plugin: 'kotlin-android'android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.gkmit.ajay.ajay" minSdkVersion 16 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' } }}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.3.1' testCompile 'junit:junit:4.12' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"}repositories { mavenCentral()}
Convert Any Java File to Kotlin:
Aftercompleting all the steps,Android Studio is ready for Development. and now i will make you understandhow we canconvert already existing java file into kotlin. Go to Android Studio Toolbar and select Code and Convert Java File into Kotlin Filein few seconds java code will be converted into kotlin.