Table of Contents
Overview
Kotlin is the main programming language for modern Android development. It is concise, expressive, and designed to help you avoid many common programming errors. In this chapter you will get an orientation to Kotlin in the specific context of Android, and you will see what Kotlin code looks like inside a typical Android project. Later chapters will explore individual topics such as variables, control flow, functions, null safety, and packages in more depth, so this chapter focuses on the overall picture and how Kotlin fits into Android apps.
Why Android Uses Kotlin
Google officially supports Kotlin as a first class language for Android development. When you create a new project in Android Studio, Kotlin is the default language choice for your Activity classes and most generated code. You can still encounter Java in older projects or third party libraries, but Kotlin is now preferred for new apps.
Kotlin runs on the Java Virtual Machine (JVM) and compiles to the same kind of bytecode that Java uses. This means Kotlin can interoperate with existing Java code and libraries, which is very important on Android where much of the platform and ecosystem is written in Java. You can call Java code from Kotlin and Kotlin code from Java within the same project.
Compared to Java, Kotlin usually requires less code for the same task, which often makes Android classes easier to read and maintain. It also includes language features that help reduce bugs, such as built in null safety, which you will learn about in its own chapter.
Kotlin Inside an Android Project
When you create a new Android project with Kotlin, Android Studio generates a basic app structure that already includes Kotlin source files. These files are stored under the app/src/main/java directory of your project, even though they contain Kotlin code. The convention is to keep both Java and Kotlin files in this same java folder.
A simple Kotlin source file for your main activity might look like this:
package com.example.myfirstapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}You will not fully dissect this code here, because later chapters in this course will cover classes, functions, and activity lifecycles in detail. For now, focus on recognizing the Kotlin elements that you will encounter frequently in Android apps.
At the top of the file, the package line declares the package name for this Kotlin file, and the import lines allow you to reference other classes without writing their full names. The class MainActivity line declares a Kotlin class. The override fun onCreate part defines a function that overrides a function from a parent class. You will revisit each of these components in later chapters when you study packages, object-oriented programming, and lifecycle callbacks.
Basic Look and Feel of Kotlin Code
The syntax of Kotlin will become familiar through repetition, but an early sense of its appearance helps you navigate Android code even before you know every detail.
Kotlin uses fun to declare a function. For example:
fun greetUser(name: String): String {
return "Hello, $name"
}
You will learn how parameters and return types work in the Functions chapter, but notice that types appear after the name with a colon, not before as in some other languages. Kotlin also supports string templates, as shown by $name in the returned text.
Variables in Kotlin are usually declared with val or var. You will study their differences in the Variables and Data Types chapter, but for now you should recognize them visually:
val appName = "My App"
var counter = 0
Kotlin uses curly braces to group blocks of code, such as the body of a class or function. It uses if, when, and loop constructs to control the flow of execution, and you will explore these in their own dedicated chapter. For Android development, control flow is used heavily to react to user input, handle different states, and update the user interface.
Kotlin and Android Framework Classes
On Android, your Kotlin code works together with classes provided by the Android framework and libraries. In the earlier MainActivity example, AppCompatActivity and Bundle are not defined by you, but by Android and its support libraries.
Kotlin does not replace these Android classes. Instead it provides a more modern language to work with them. When you extend AppCompatActivity in Kotlin, you still interact with the normal Android activity lifecycle. When you pass a Bundle to a function, you still rely on Android mechanisms for saving and restoring state.
Kotlin also cooperates with many Android specific libraries such as Jetpack components. For example, ViewModel and LiveData are usually used from Kotlin code, and many Android documentation examples now use Kotlin first. Kotlin features such as null safety and data classes work particularly well with these libraries, something you will see in later chapters when you work with architecture components.
Kotlin in Android Studio
Android Studio is optimized for Kotlin development. The editor offers code completion, quick fixes, refactorings, and automatic imports tailored for Kotlin. When you type class names that are not yet imported, Android Studio can insert the required import lines at the top of the file. It can also convert simple Java code snippets to Kotlin, which is useful when you copy older examples that were originally written in Java.
In the project settings, Gradle automatically configures the Kotlin compiler so you usually do not have to set it up manually. When you build or run your app, the Kotlin code is compiled behind the scenes, and the output becomes part of your Android application package.
If you encounter Kotlin related errors while you build, they will appear in the Gradle build output and in the editor. These might include type mismatches, missing imports, or incorrect null handling. You will slowly learn how to interpret these messages as you become more comfortable with the language.
How Kotlin Concepts Map to Upcoming Chapters
The rest of this section of the course breaks Kotlin into smaller topics that are easier to master step by step. It is useful to understand where each concept fits in the bigger picture of Android development.
Variables and data types describe how you store information in memory, such as text, numbers, and more complex objects. You will use these constantly in activities, fragments, view models, and almost every part of your app.
Control flow with if, when, and loops lets your app make decisions and repeat actions. In Android code this is used to manage user choices, network responses, and data processing.
Functions describe reusable blocks of behavior. Almost all Android APIs that you call are functions, and every activity or fragment override that you implement is also a function.
Null safety is extremely important for Android because many platform APIs can return null. Kotlin helps you express when something can be absent and forces you to handle that case, which reduces crashes.
Packages and imports organize your code into logical groups and let you access Android framework classes and third party libraries without long names. Understanding them keeps your project manageable as it grows.
Each of these topics will receive its own focused chapter, where you will explore syntax, examples, and usage in more depth, especially in typical Android situations such as handling user input or processing network data.
Practical Mindset for Learning Kotlin on Android
When learning Kotlin for Android, it helps to focus less on abstract language theory and more on practical usage. Almost every Kotlin concept you learn will soon appear in your app screens, network calls, or data handling.
As you go through the upcoming chapters, try to connect each new idea with a concrete Android use case. For example, when you learn about variables, imagine storing text from an EditText. When you study control flow, think about navigating to different screens depending on user choices. When you meet functions, picture code that responds to a button click.
Over time, Kotlin will feel less like a separate subject and more like the natural way you express your app’s behavior. The goal of this section of the course is not to make you a general Kotlin expert, but to give you enough understanding to read, write, and reason about Android code comfortably.
For this course, always assume that new Android code should be written in Kotlin unless there is a specific reason to use Java.
With this orientation, you are ready to dive into the individual Kotlin topics. You will start with variables and data types, since they are the foundation of almost everything else you will write in your Android applications.