Brief about Kotlin
What I am impressed in Kotlin is Android Extensions. It enables you to remove boiler plate code of setting views. You have bind your view object by calling findViewbyId() but Kotlin supports it just importing kotlinx.android.synthetic.res/layout/login.xml
LoginActivity.kt
package com.ispark.kotlin.login
import kotlinx.android.synthetic.login.*
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.util.Log
import android.widget.TextView
import android.widget.Toast
import com.ispark.kotlin.R
import kotlin.String
public class LoginActivity : AppCompatActivity() {
private val LOG_TAG: kotlin.String = "LoginActivity"
override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login)
setTitle(R.string.pleaseSignIn)
loginButton.setOnClickListener {
if (required(loginEditText)){
Toast.makeText(this, "ID is required", Toast.LENGTH_SHORT).show()
}
if (required(passwordEditText)){
Toast.makeText(this, "Password is required", Toast.LENGTH_SHORT).show()
}
}
}
fun required(v : TextView) = TextUtils.isEmpty(v?.getText().toString().trim())
}
Where is loginButton member variable? The import kotlinx.android.synthetic.login.* makes it possible. You can use the ID in the layout XML as a member object. What an amazing! There are still other powerful things in Kotlin. I guess you could find them easily if you visit the website.
Setup Kotlin
My testing environment
- Mac OSX Yosemite(10.10.3)
- Java 1.7
- Android Studio 1.2.1.1
- Gradle 1.2.3
Plugin Installation
Got toAndroid Studio > Preferences > Plugins and install plugins below:- Kotlin
- Kotlin Extensions For Android
Gradle Builds
project build.gradle
allprojects {
repositories {
mavenCentral()
}
}
module build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.ispark.kotlin"
minSdkVersion 10
targetSdkVersion 22
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'
androidTest.java.srcDirs += 'src/androidTest/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
buildscript {
ext.kotlin_version = '0.11.91.4'
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
}
}
There are different languages you can use for Android development such as Groovy and Scala. You can also use Java8 Lambda expression by using Retrolambda but I think Kotlin looks like the way to take than any others because it is very similar to Java and you can easily pick up the language. It also works on JVM and provide powerful features other languages have and you can even add your own function to the build in classes like String. Of course, it provides much more powerful Lambda than Java8 and there are many more nice points worthy to check.
Comments
Post a Comment