Getting Started With Android Studio and Kotlin
Technology is the core of everything. Nowadays, we can’t live without it and mobile technology is one of those. So many people use technology but not everyone can create technology. Let’s start with something simple, a simple technology for mobile and it’s mobile technology. As we know there are Android and iOS that mostly used by people around the world. So for starter, in this post I will talk about the how to create simple Android app from the beginning and how to set up Android Studio using Kotlin for Android.
This is my first tutorial post and we’re gonna learn about developing Android application using the newest Android Programming language, Kotlin. Previously developers use Java to develop Android apps and many of them still use Java, but we’re not gonna talk about Java anyway. I’m gonna talk about this awesome new Kotlin language which has more advantages from Java.
Kotlin is the alternative programming language for Android Java programming. Even though Java is mainly used by so many Android developers out there, it has some downsides and Kotlin here is aiming to fill the missing gap of Java and believe me, you will not regret it when learning Kotlin. The main things Kotlin offer are:
- Simpler code and reduce the amount of boilerplate when you write code.
- Make your code to be more readable and understandable.
- Fully interoperable of Java code so you can write Java and Kotlin code in the same project since Kotlin also run on JVM same like Java.
- Safely avoid classes of error within the code such as Null Pointer exception etc.
It is very recommended for new developers and even experienced Android developers to use Kotlin when creating Android apps, believe me you’ll like it!
So first thing first before we begin, you must have the latest Android Studio installed on your machine. You can get it from this link. After you download and install it, we can now begin.
Note: In this post I used Android Studio 3.2.1 so the appearance might or might not be similar like yours and it’s okay though, but make sure you use the latest Android Studio, or if you already install it, be sure to check the latest update. You can download Android Studio from here.
Setting up SDK Manager
Open the Android Studio application from your machine and it will appear like this.
The first thing is we have to set up the our Android SDK, so click the Configure button and select the from the dropdown menu.
The SDK Manager dialog will be like this:
There are so many options to choose in SDK Platforms tab. Let’s click the Show Package Details checkbox first and it will display the details of each Android version. At the first, I suggest you to follow these simple steps and you can learn other options in intermediate level.
This option here shows individual packages for each SDK platform and not all of them are mandatory, only select what you need but make sure you select the latest Android SDK Platform and in my case here I select:
- Android SDK Platform 28
- Sources for Android 28
- Google Play Intel x86 Atom System Image
The SDK platform below the latest version is optional though. After you select those options, head over to SDK Tools tab.
In this tab I choose the options like the picture above, you may follow my config like that as long as the mandatory like:
- Android SDK Build-Tools
- Android Emulator
- Android SDK Platform-Tools
- Android SDK Tools
- HAXM Installer
After that you can click Apply or OK button to start downloading. The installation may take a while depend on your internet connection speed.
Creating your first project
The very first step before writing code, open your Android Studio and you will see dialog like this and choose Start a new Android Studio Project.
The next screen will display dialog like this:
You can fill Application Name with the name of app that you will create, I use MyFirstApp as the Application name. The Company Domain is the unique identifier of your app, if you have website, you can place your website name in it or if you don’t have one you can use like yourname.com as the company domain. In this post I use thesimplycoder.com as my company domain. Project location is the directory where your first project will be stored on your computer. The last is make sure that you check the Include Kotlin support checkbox and then click Next.
Target Android Devices
In this screen you can select the minimum Android version that can run your app. For now check the Phone and Tablet and select API 19: Android 4.4 (Kitkat) should be good to go. Click Next.
Add the First Activity
In this screen, you can select any Activity Template that already provided by Android Studio. Activity is the screen in Android app development if simply say it. It’s also responsible for handling user’s operations and interactions. If you want to know detail about what Activity is and how it works in Android, you can read it in my post here. For now select Empty Activity and click Next.
In this step you will have to give a name of your first Activity and its layout. By default this screen will be pre-populated by Android Studio, so just leave it be. Use MainActivity as Activity Name, and activity_main as Layout Name, and check Generate Layout File and Backwards Compatibility (AppCompat) checkboxes, then click Next.
Your first Android App
Android studio will be preparing your first application before we can begin and after that the screen will be look like this.
If Android Studio prompt you to install latest version of Kotlin, just accept and install it or you can do it manually by click the Android Studio > Preferences…
It will display the Preferences dialog like this:
Select Plugins and scroll down to Kotlin and select it. It will display the Kotlin version, if it is still not the latest version, you can update it here.
Back to Our First App
This is still just the beginning actually and before we begin, I will talk with you slightly about what you have to know when developing Android Application.
Android Project Components:
The Android Manifest or AndroidManifest.xml is located inside manifest directory within the project files which provides the important configuration and information that is required for Android system to run your application.
Resource / res directory
All resources and assets will be stored in this directory in order to be used inside our application. Application resources such as XML layouts, images, colors, strings, and styles will be defined here.
- res/layout directory is for defining XML layout for app. We use XML layout for creating and designing Android screen user interface.
- res/drawable directory to store and define the images that we will use for our application.
- res/values directory is where you define the resources like colors (res/values/colors.xml), string or text (res/values/strings.xml), dimensions (res/values/dimens.xml), etc.
- res/menu directory is where you define application menu that used in application.
Gradle is a package builder and handy build tool for Android. It contains so many configurations and settings about your application like Kotlin version that you will use, Build Tool version, the libraries you use, etc. For now, we’ll skip about the Gradle thing first as you will understand further about it as you learn to develop Android applications.
Okay we’re back on our topic now, I don’t want to confuse you with so many things in hand before we begin. Let’s take a look at our project file here and when you open the MainActivity.kt you will see the code like this:
This is the very basic of our Activity code using Kotlin. The function of
onCreate() will be called when we run the application. Inside
onCreate() function there
setContentView(R.layout.activity_main) code to instruct this MainActivity to use screen user interface from activity_main.xml inside res/layout folder. Enough of that, we can run this app first to see the result. On the top toolbar click the green Run button to run this application.
You will be prompted to choose available device but the message says that No USB devices or running emulators detected. Hmm.. Why does this happen?
AVD (Android Virtual Device)
Android Studio comes with a software-based Android device which we know as Android Emulator, so basically AVD is Android Emulator that will help us to run and test our code, hm.. very convenient. So if you haven’t set up this simulator or you don’t know how to set up it, so follow along with me in the next step.
Setting Up the Emulator
Click the Create New Virtual Device button to open AVD configuration dialog.
Click the Phone tab on the left to start create phone emulator then select the available devices from the list, in this case I choose Nexus 5 with Play Store enabled for it and then click Next.
On the Select System Image dialog on the Recommended tab will be displayed a list of recommended devices to choose. If it is not available you can download it first. If you want to choose another system images, you can choose the x86 Images like this:
In this tutorial I go with Pie with Google Play and then click Next. After that the AVD Config dialog will show up.
You can name your new AVD Name with whatever you want and you can modify the Advanced Setting by clicking the Show Advanced Settings button. For now I go with the default setting and click Finish button.
Running the App
Great! Now the AVD has been set up, now we run the app.
Our new AVD that we have created is already listed in the Deployment Target dialog. You can now select it and click OK button.
It may take some time to boot up, so give it time. After that your app will be run like this:
Great! Now our first Kotlin Android app is successfully run on the emulator. Yeah I know that it is still displaying just a text saying “Hello World!” and that’s okay. So we managed to set up our Android Studio program and also the Android development environment using Kotlin.
Where to go next?
The next step we will get to know about Android Activity Lifecycle in my next post. We will also get to know about simple functionality of our app to display images, text, and button in my another post. Hope you like my post and share it!