Mobile Development 25 min read

Master Kotlin for Android: A Quick Start Guide for Mobile Developers

This guide walks Android engineers through adding Kotlin support to new or existing projects, explains Kotlin's variable declarations, null‑safety features, type inference, val/var usage, visibility modifiers, class inheritance, open/final semantics, and safe casting, providing concise code examples and practical tips.

Jike Tech Team
Jike Tech Team
Jike Tech Team
Master Kotlin for Android: A Quick Start Guide for Mobile Developers

Adding Kotlin Support to a Project

To start using Kotlin, create a new Android project and select Kotlin as the language, or add the required Kotlin dependencies to an existing project's

build.gradle

files.

<code>buildscript {
    ext.kotlin_version = '1.3.41'
    repositories { ... }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0-beta05'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
</code>

Variables

Kotlin requires variables to be initialized or marked as

lateinit

. By default variables are non‑null; to allow null you add a

?

after the type, creating a nullable type.

<code>var view: View // must be initialized
lateinit var view: View // will be initialized later
var name: String? = null // nullable variable
</code>

Accessing a nullable variable requires safe calls (

?.

) or non‑null assertions (

!!

).

<code>view?.setBackgroundColor(Color.RED)
view!!.setBackgroundColor(Color.RED)
</code>

Functions

Functions are declared with

fun

. The return type follows the parameter list; if omitted, the function returns

Unit

(similar to

void

).

<code>fun cook(name: String): Food { ... }
fun main() { /* Unit is implicit */ }
</code>

Parameters can be nullable, and you must handle nullability when calling the function.

Classes and Objects

Classes are declared with

class

. By default they are

final

; use

open

to allow inheritance. The

override

keyword replaces Java’s

@Override

annotation.

<code>open class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) { ... }
}
</code>

Instantiate objects without the

new

keyword:

<code>val activity: Activity = NewActivity()
</code>

Getters, Setters, and Backing Fields

Kotlin properties automatically generate getters and setters. You can customize them using

get()

and

set(value)

, accessing the underlying field with

field

.

<code>var name: String = "Mike"
    get() = field + " nb"
    set(value) { field = "Cute " + value }
</code>

Visibility Modifiers

Properties and classes are

public

by default. Use

private

,

protected

, or

internal

as needed.

Type Checking and Casting

Use

is

for type checks; the compiler smart‑casts within the block. For explicit casts, use

as

(unsafe) or

as?

(safe, returns null on failure).

<code>if (activity is NewActivity) {
    activity.action()
}

(activity as? NewActivity)?.action()
</code>

Exercises

Create a new Kotlin Android project with an

Empty Activity

and add a non‑null

View

property initialized in

onCreate

.

Declare a function that takes a

View?

parameter, passes the previously created

View

to it, and prints the view’s

id

.

Mobile DevelopmentAndroidKotlinClassesfunctionsNull Safetyvariables
Jike Tech Team
Written by

Jike Tech Team

Article sharing by the Jike Tech Team

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.