Beginning Android Development with Kotlin, Part Two: Using Android Studio

In this Android Studio tutorial, you’ll learn the fundamental concepts of developing with Android Studio using Kotlin by creating an app to read your fortune. By Kevin D Moore.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Overview of Gradle

It’s now time to shift gears to Gradle. In a nutshell, Gradle is a build system that Android Studio uses. It takes the Android project and builds or compiles it into an installable Android Package Kit, or APK, file, which you can then install on devices.

As shown below, you can find the build.gradle file under Gradle scripts in your project at two levels: Module level and project level. Most of the time, you’ll edit the module level file.

build.gradle

Open the build.gradle (Module:app) file. You’ll see the default gradle setup:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
  compileSdkVersion 28
  defaultConfig {
    applicationId "com.raywenderlich.android.fortuneball"
    minSdkVersion 21
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  implementation fileTree(dir: 'libs', include: ['*.jar'])
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
  implementation 'com.android.support:appcompat-v7:28.0.0'
  implementation 'com.android.support.constraint:constraint-layout:1.1.3'
  implementation 'com.android.support:design:28.0.0'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'com.android.support.test:runner:1.0.2'
  androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

Now, take a step-by-step look at the major components:

Another important component is minSDKVersion, which defines the minimum SDK version a device should have installed in order to run your app.

For example, if your device’s SDK version is 20, then this app won’t be able to run on your device, since the minimum supported version is 21.

  • Apply plugin: ‘com.android.application’: Applies the Android plugin at the parent level and makes available the top-level build tasks required to build an Android app.
  • Apply plugin: ‘kotlin-android’ and apply plugin: ‘kotlin-android-extensions’: Applies Kotlin-related plugins that allow your app to use the Kotlin compiler and Android-specific Kotlin extensions.
  • Android{…}: Gives you configuration options such as targetSdkVersion. You should keep the target SDK for your app at the latest API level, which was 28 at the time we published this tutorial.

    Another important component is minSDKVersion, which defines the minimum SDK version a device should have installed in order to run your app.

    For example, if your device’s SDK version is 20, then this app won’t be able to run on your device, since the minimum supported version is 21.

  • Dependencies{…}: Important dependencies like implementation 'com.android.support:appcompat-v7:VERSION' and implementation 'com.android.support:design:VERSION' provide support and compatibility of new features from the latest API to the older APIs.
  • Implementation”org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version”: Allows your code to use the Kotlin standard library.

Installing Third-Party Libraries

In addition to Android compatibility libraries, you can also add other third party libraries in the dependencies{...} component.

For this tutorial, you’ll add an animation library that will let you add cool effects to user interface elements in your app.

To put these into place, find dependencies and add the following two lines at the bottom:

  implementation 'com.daimajia.easing:library:2.0@aar'
  implementation 'com.daimajia.androidanimations:library:2.2@aar'

Here, you add two new third-party dependencies that will help you make Fortune Ball shine. Android Studio will automatically download and integrate these libraries.

In fact, once you add these dependencies, Android Studio will realize that it needs to download them and will tell you to do so. Look for a bar across the top of the build.gradle file as shown the next screenshot. Click Sync Now to integrate these dependencies in your app.

sync now

Syncing takes a few seconds. You can monitor the Gradle file update in the Messages tab in the bottom panel. Look for a success message in that panel, as shown in the screenshot below.

Messages

All right, that’s all the configuration you need to do to Gradle for now. You’re now set to add fancy animations to your app, which you’ll do a little later.

Importing Files

An important part of making an Android app is integrating resources such as images, custom fonts, sounds, videos and so on. You must import these resources into Android Studio and place them in appropriate folders. This allows the Android operating system to pick the correct resource for your app.

For Fortune Ball, you’ll import image assets and place them in drawable folders. Drawable folders can hold images or custom XML drawables. For example, you can draw shapes via XML code then use them in your app’s layouts.

Adding Image Files to Your App

To get started, download the image assets here, then unzip the contents and save them where you can access them easily.

There are two ways to import the files:

  • Copy and paste the files from your file system. You won’t use that method in this tutorial.
  • Use Android Studio to import the files.

Back in the project in Android Studio, switch the view from Android to Project. Next, open the res folder under app ▸ src ▸ main.

Now you’ll want to right-click on the res folder and select New ▸ Android resource directory.

New resource directory

You’ll get a window titled New Resource Directory. From the Resource type drop-down, select the drawable option. In the Available qualifiers list, select Density and click the button highlighted in the screenshot below:

Density qualifier

In the subsequent window, select XX-High Density from the Density drop-down, then click OK.

xx-hdpi

Repeat the same process and create drawable-xhdpi, drawable-hdpi and drawable-mdpi folders by going to the Density drop-down and selecting X-High Density, High Density and Medium Density, respectively.

Setting up Images for Different Screen Resolutions

Each drawable folder that has a density qualifier, such as xxhdpi, xhdpi and hdpi, houses images corresponding to that particular density or resolution. For example, the folder drawable-xhdpi contains images that are extra-high density, meaning an Android device with an extra-high resolution screen will pick images from this folder.

This allows your app to look great on all Android devices, regardless of the screen quality. To learn more about screen densities, check out the Android documentation.

After creating all the drawable folders, go back to the unzipped contents in your file manager, copy the image file img_crystral.png from each folder using Cmd + C on Mac or Ctrl+C on PC and paste it into the corresponding folder in Android Studio using Cmd + V, Ctrl+V on PC.

Image file copy

After you paste the files, you’ll see the Copy window. Select OK to continue.

Copy

You’ve just put the ball in Fortune Ball, and learned how to import files along the way. Looks like you just checked another feature off your to-learn list!