2. Hello Kotlin

Birat Rai
3 min readDec 19, 2017

--

This is the second milestone towards our Kotlin series. Go to the first milestone if you haven’t done so.

What does Kotlin in Android feel like?

Kotlin support in Android

What does our MainActivity.kt class look like?

Steps to create a Hello Kotlin app

  1. In the main Welcome to Android Studio window, click “Start a new Android Studio project”.
  2. In the New Project window, give your application an Application Name, such as “Hello World”.
  3. Check the box to include Kotlin Support and follow the initial setup wizard process.
// import kotlin extensions to remove findViewById boiler plate
import kotlinx.android.synthetic.main.activity_main.*
// Class declarations and extending AppCompatActivity
class MainActivity : AppCompatActivity() {
// Overriding class methods
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//local variable declaration (can be either val or var)
val helloKotlin = "Hello Kotlin!"
displayHelloKotlin(helloKotlin)
}
// Private function declaration
private fun displayHelloKotlin(helloKotlin: String) {
// We can directly call the id of the textView
hello_kotlin.text = helloKotlin
}
}
  • .kt extension → Kotlin file.
  • MainActivity : AppCompatActivity()Extending AppCompatActivity.
  • override → Overriding a method. No need of @ annotation.
  • savedInstanceState: Bundle?savedInstanceState can be null or Bundle type.
  • val hello_kotlin → Variable declaration and initialization. No type casting required.
  • fun displayHelloKotlin() → Function in Kotlin.
  • hello_kotlin →
  • (hello_kotlin: String) → Params in function.
  • PS: No need of semi-colon at the end of statement.
No more trauma of missing semicolon

Where does all of this come from?

  • A look at build.gradle (app and project) will show kotlin plugin:
dependencies {         
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
--> Apply classpath at project build.gradle
apply plugin: 'kotlin-android' --> To target the JVM, the Kotlin plugin needs to be applied at the app build.gradle
  • Another is kotlin-extensions. This helps in Importing synthetic properties. The plugin will generate some extra code that will allow you to access views in the layout XML, just as if they were properties. IDE is able to auto-import it.
  • Eg; hello_kotlin.text = helloKotlin
apply plugin: 'kotlin-android-extensions' --> Applied at app build.gradle

The source code: Hello Kotlin github

What are the tools useful for kotlin?

The Kotlin team offers a set of tools for Android development that goes beyond the standard language features:

  • Kotlin Android Extensions is a compiler extension that allows you to get rid of findViewById() calls in your code and to replace them with synthetic compiler-generated properties.
  • Anko is a library providing a set of Kotlin-friendly wrappers around the Android APIs, as well as a DSL that lets your replace your layout .xml files with Kotlin code.

What are the Android-Studio shortcuts for Kotlin?

  • The most notable is converting the existing Java code to Kotlin.
cmd + optn + shift + k // To convert to Kotlin in mac
  • Configuring Kotlin in Java Project. If the IDE automatically doesn’t show you a prompt to configure Kotlin you can,
invoke the configuration by selecting Tools | Kotlin | Configure Kotlin in Project from the main menu.

The third milestone in this series. 3. Getting Started and build your first App

References:

  1. Using Kotlin for Android development ~ Kotlin.org
  2. Build your first Android App in Kotlin ~ codelabs.developers
  3. Resources to Learn Kotlin ~ developer.android
  4. Why switch to Kotlin? ~ Medium blog
  5. Kotlin libraries list~ Kotlin.org
  6. Getting Started with Kotlin ~ Kotlin.org

--

--

Birat Rai
Birat Rai

No responses yet