An Introduction to Kotlin for Android Developers

How to Get Started

Android Studio 3.0

Dependencies

maven { url 'https://maven.google.com' }
buildscript { ext.kotlin_version = '1.1.2-4'//note use the latest version available .... }
dependencies { //note, be sure to reference what you defined as your version name classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
apply plugin: 'kotlin-android'
//we are referencing the same version name defined in the project build file compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

Creating Kotlin Classes and Activities

Kotlin Developer Basics

Null Safety

var testVar : String = "a test string" testVar = null //error, won't compile
var testVar : String? = "a test string" testVar = null //no issue.
val testVal : String? = "a test string" val testValCount : Int = testVal.length //wont compile as testVal may be null!
val testVal : String? = "a test string" val testValCount : Int? = testVal?.length //compiles, but testValCount may be null! val testCalc : Int = testValCount * 2 //wont compile....testValCount may be null
val testVal : String? = "a test string" val testValCount : Int = testVal?.length ?: 0//defaults to 0 in the event that testVal is null val testCalc : Int = testValCount * 2 //this works!
val testVal : String? = "a test string" val testValCount : Int = testVal!!.length//compiles but may thrown a NPE

The Static Modifier

class PracticeArea() { companion object { val staticFun = "STATIC FUN" } }
Log.i("TAG", PracticeArea.staticFun)

Access/Visibility Modifiers

Methods are Final

protected fun onViewReceived(){ //is set to final } protected open fun onViewReceived(){ //children can override }

What does Any mean?

Anonymous Inner Classes

//can be replaced with lambda, but shown for demo purposes view.setOnClickListener(object : View.OnClickListener { 
override fun onClick(v: View?) { //do your click stuff... }
})

Android Practices in Kotlin

Parcelables

data class PracticeArea(val name: String, val imageLink: String,
val subTitle: String, val body: String,
val serviceOfferings: ArrayList<ServiceOffering>) : Parcelable {

/**
* Companion object is used to defined static members to the class..
* in our case the creator.
*/

companion object {
/**
* Parcelable creator.
*
* @JvmField used make the creator implementation visible as a field to Java.
*
*/

@JvmField
val CREATOR = object : Parcelable.Creator<PracticeArea> {
override fun createFromParcel(source: Parcel): PracticeArea? = PracticeArea(source)
override fun newArray(size: Int): Array<out PracticeArea?> = arrayOfNulls(size)
}
}

/**
* Secondary constructor for the parcelable
*/


protected constructor(parcelIn: Parcel) : this(parcelIn.readString(), parcelIn.readString(),
parcelIn.readString(), parcelIn.readString(), parcelIn.createTypedArrayList(ServiceOffering.CREATOR))


override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.writeString(name)
dest?.writeString(imageLink)
dest?.writeString(subTitle)
dest?.writeString(body)
dest?.writeTypedList(serviceOfferings)
}

override fun describeContents() = 0
}

Starting an Activity

//Java Intent mIntent = new Intent(context, DestinationActivity.class); //Kotlin val intent = Intent(context, DestinationActivity::class.java);

findViewById No More

import kotlinx.android.synthetic.main.activity_list.*
import kotlinx.android.synthetic.main.list_view_holder.view.*
fun bindView(area: PracticeArea?, practiceListener: PracticeListener) {
itemView.listImage.setImageURI(area?.imageLink)
itemView.listTitle.text = area?.name
itemView.listBody.text = area?.body
itemView.listSubTitle.text = area?.subTitle

......
}
apply plugin: 'kotlin-android-extensions'

Adapters, RxJava, & More

Road Bumps

  1. If you currently use GSON, you may be used to leveraging the fromJson(string, class) method. However, I ran into issues with this and Lists/Arrays. Gson throws a casting exception at runtime when trying to pass the java class reference of your Kotlin Data Class for use. To fix this without having to implement another framework, I simply adjusted my code to use a TypeToken instead.
  2. At the time of writing this post, there is currently a bug in the Kotlin Gradle plugin 1.1.2–4 and Android Gradle 3.0.0-alpha-1 which causes the enabling of DataBinding to through a circular dependency error. This can be avoided by switching your kotlin plugin to 1.1.2-2 or by waiting until the bug is fixed.

Closing

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Clinton Teegarden

Clinton Teegarden

Mobile Lead & Architect @ CapTech. I specialize in delivering products for Fortune 500 clients in Mobile, Android Architecture, & IoT