Anko Commons Tutorial

See how to improve the readability and conciseness of your Android Kotlin code for Intents, Dialogs and more using the Anko library from JetBrains. By Arturo Mejia.

Leave a rating/review
Download materials
Save for later
Share

Anko is a library for Android devs that want to achieve more while writing less. It simplifies common tasks that are tedious and generate a lot of boilerplate, making your code enjoyable to read, concise and clean. Neat and Tidy, just what the doctor ordered for my Java headaches. :]

The folks at JetBrains, makers of titles like Kotlin (A New Hope) and the IntelliJ Platform (the foundation of Android Studio), have created and maintain Anko.

Anko consists of a number of different components:

  • Anko Commons: helpers for intents, dialogs, logging;
  • Anko Layouts: lets you quickly and programattically create type-safe Android layouts;
  • Anko SQLite: helpers for working with Android SQLite;
  • Anko Coroutines: utilities for using Kotlin coroutins.

In this tutorial, we are going to be focused on Anko Commons. Future tutorials will cover other parts of Anko.

You’ll become an Anko Commons master by updating an existing app that doesn’t use Anko, and then comparing how to do the same things with and without Anko. This will help you be able to make a conscious decision about whether or not to use the library.

Note: This tutorial assumes you have previous experience with developing for Android in Kotlin. If you are unfamiliar with the language, have a look at this tutorial. If you’re just beginning with Android, check out some of our Getting Started and other Android tutorials.

Note: This tutorial assumes you have previous experience with developing for Android in Kotlin. If you are unfamiliar with the language, have a look at this tutorial. If you’re just beginning with Android, check out some of our Getting Started and other Android tutorials.

Getting Started

Kanime is the app that we are going to use to showcase Anko’s power. It shows a list of my top 10 animes that you should watch. It’s built without using Anko, but not for long, along the way you’re going to update it.

Meet Kanime. :]

Download the Kanime source code using the download button at the top or bottom of the tutorial. Open the starter project up by starting Android Studio 3.1.2 or later and selecting Open an existing Android Studio project:

Open an existing Android Studio project

Before going on with the tutorial, take a look at the existing starter code.

Kanime Structure

The Kotlin source code packages in the starter project appear as follow:

  • Activities
    • MainActivity.kt
      Here is where you show the top 10 best anime list.
    • AnimeDetailActivity.kt
      When you tap an Anime, this activity opens up. It shows all the info about the selected anime.
    • AboutActivity.kt
      A screen to show information about the app.
  • Adapters
    • AnimeAdapter.kt
      Contains all the code to show each anime item on the list.
  • data
    • AnimeDataSource.kt
      Provides all the anime data.
  • model
    • Anime.kt
      Anime model data class.

Now you have a clear picture of how Kanime is structured. Let’s Rock!

Anko

Anko is a set of helpers functions (Kotlin extension functions) that help you to get something done with the least amount of boilerplate code. It’s subdivided into modules that help you to deal with Layouts, SQLite, and Coroutines. This modularization allows you to pick and choose only what you need.

Note: If you don’t have experience using Kotlin extension functions, then please take a look at the optional Extension Function Fundamentals section. It will help you to understand how Anko works under the hood. If you already have experience with extension functions, then feel free the skip this section.

Extension Function Fundamentals (Optional)

To understand how Anko works, you need to understand Kotlin Extension Functions. They allow you to add a function to an existing class without modifying the class.

For example, say you had a Dog class:

Dog.kt

class Dog(val name: String, val breed: String)

In another Kotlin file you could add a function to Dog without modifying the original file:

Extensions.kt

package com.raywenderlich.doggy

fun Dog.bark(): Unit{
  println("woof woof")
}

To create the extension function, after fun type the class, then a dot, then the name of the extension function.

You could test your extension function in another file as follows”

Main.kt


//Importing bark extension function :]
import com.raywenderlich.doggy.bark

fun main(args: Array<String>) {
  var myPuppy = Dog("Max", "Pug")
  myPuppy.bark()
}

To use the extension function, you only import bark, and then every Dog object will be able to use the bark() function.

Let’s see what happen if you don’t import the bark:

Main.kt


//import com.raywenderlich.doggy.bark
fun main(args: Array<String>) {
  var myPuppy = Dog("Max", "Pug")
  myPuppy.bark() // Compile error ¯\_(ツ)_/¯
}

Note: If you don’t import the extension function, you won’t be able to use it, because it won’t be visible in your code. For this reason, the dog isn’t able to bark. ¯\_(ツ)_/¯

After this small introduction to Kotlin extension functions, now you are ready to become an Anko rock star!

Setting Up Anko Commons

Let’s spice up Kanime by adding Anko Commons!

To set up Anko in your project you only need to include the Gradle dependency. The dependency comes in two flavors:

1.The full Anko artifacts

implementation "org.jetbrains.anko:anko:$anko_version"

This dependency contains all Anko components. If you are only going to use some of them then this is not the way to go, because it will add unnecesary size to your APK for code you are not going to use.

2. Specific Artifacts

Anko allows you to pick and choose only the dependencies that you need:

implementation "org.jetbrains.anko:anko-commons:$anko_version"

This only brings the specific classes for Anko Commons. We are going go with this one for Kanime because it is more lightweight.

Open the app module build.gradle file and add this line to your dependencies :

implementation "org.jetbrains.anko:anko-commons:0.10.5"
Note: As of this writing, the latest version of Anko is 0.10.5. You can find the last version of Anko here. Each new release comes with release notes for what has changed since the last version.

Click “Sync Now” to sync your Gradle files and wait a bit until it finishes.

Hooray!! Now you can use Anko Commons in your project.