Kotlin — Installing & Setting up Android Studio
In this article, I will be covering how you can install and set up an android studio such that you can get started with coding in Kotlin and start building awesome apps.
If you haven’t installed and set up Kotlin have a read at this article:-
In this article I will be covering about:-
- Installing android studio
- Installing the Android SDK
- Creating and Setting up AVD
- Setting up Kotlin
Installing Android Studio
So for setting up an android studio you need to install an android studio.
First, be sure you download the latest version of Android Studio.
Windows
To install Android Studio on Windows, proceed as follows:
- If you downloaded an
.exe
file (recommended), double-click to launch it. - If you downloaded a
.zip
file, unpack the ZIP, copy the android-studio folder into your Program Files folder, and then open the android-studio > bin folder and launchstudio64.exe
(for 64-bit machines) orstudio.exe
(for 32-bit machines). - Follow the setup wizard in Android Studio and install any SDK packages that it recommends. For the initial setup setting, keep everything to default.
Mac
To install Android Studio on your Mac, proceed as follows:
- Launch the Android Studio DMG file.
- Drag and drop Android Studio into the Applications folder, then launch Android Studio.
- Select whether you want to import previous Android Studio settings, then click OK.
- The Android Studio Setup Wizard guides you through the rest of the setup, which includes downloading Android SDK components that are required for the development.
Linux
If you are using a 64-bit Linux system then you need to have certain packages/dependencies installed before installing android studio. These are ubuntu.
sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386
To install Android Studio on Linux, proceed as follows:
- Unpack the
.zip
file you downloaded to an appropriate location for your applications, such as within/usr/local/
for your user profile, or/opt/
for shared users. - If you’re using a 64-bit version of Linux, make sure you first install the required libraries for 64-bit machines.
- To launch Android Studio, open a terminal, navigate to the
android-studio/bin/
directory, and executestudio.sh
. - Select whether you want to import previous Android Studio settings or not, then click OK.
- The Android Studio Setup Wizard guides you through the rest of the setup, which includes downloading Android SDK components that are required for the development.
Installing the Android SDK
The SDK is now included with Android Studio. Android development for beginners is getting easier and easier and this relatively recent change means you now only need to go through a single installation to get your development environment up and running. There’s even an open Java Development Kit (JDK) included, so you no longer need to separately install the latest version separately.
Creating and Setting up AVD
An Android Virtual Device (AVD) is a configuration that defines the characteristics of an Android phone, tablet, Wear OS, Android TV, or Automotive OS device that you want to simulate in the Android Emulator.
To create a new AVD:
- Open the AVD Manager by clicking Tools > AVD Manager.
- Click Create Virtual Device, at the bottom of the AVD Manager dialog.
- The Select Hardware page appears.
- Select a hardware profile, and then click Next. If you don’t see the hardware profile you want, you can create or import a hardware profile. The System Image page appears.
- Select the system image for a particular API level, and then click Next. Keep the recommended setting intact. If you see Download next to the system image, you need to click it to download the system image. You must be connected to the internet to download it. After this the verify configuration page appears.
- Change AVD properties as needed, and then click Finish.
- The AVD which you have created will not be visible in the android studio and the apps which you are developing can be run in this.
Setting up Kotlin
In order to ensure Android Studio supports Kotlin, the first thing is to install the Kotlin Plugin for your Android Studio.
Android Studio → Preferences… →Plugins → Browse Repository → type “Kotlin” in search box → install
If you are creating a new project after Kotlin plugin is installed you don’t need to set up the classpath given below.
Adding Kotlin classpath to project build.gradle:- For Gradle to have Kotlin support, add the two classpaths below, i.e. Kotlin-Gradle-Plugin and Kotlin-Android-Extensions. Also in this file I setup the variable to define Kotlin version, which could be shared by all.
buildscript {
ext.kotlin_version = "1.1.1"
ext.supportLibVersion = "25.3.0"
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Adding Kotlin library and apply the Kotlin plugin in our module build. Gradle file:- In the module that will use Kotlin, you will add the Kotlin library into its Build.gradle. Also, remember to apply both the Kotlin Android and its extension plugin to your project (I often forgot this, after adding the library).
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
// ... various gradle setup
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$supportLibVersion"
compile "com.android.support:recyclerview-v7:$supportLibVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
Now you have set up Kotlin in android studio and you can make projects in kotlin or can convert java files to kotlin using Shift-Alt-Cmd-K or Shift-Shift + search Convert Java File to Kotlin File.
If you have any doubts in any of the steps mentioned above, comment them down below. I would be happy to help.
Connect with me on Linkedin:- Harsh Kumar Khatri | LinkedIn
References:-