Kotlin, Zero to Kotlin Hero

Zero to Kotlin Hero: More on Anko Commons

The previous article introduced the Anko library and covered a bit on Anko commons.

(If you are not familiar with Anko or how to add it to your Android project, please read that article before reading this).

In this article, we would cover the rest of the features that Anko Commons has for us. That includes Helpers for logging, resources, and dimensions.

 

AnkoLogger

AnkoLogger is part of anko-commons. If you have not added the anko-commons artifact to your project, I have written about how to add it here.

In my opinion, AnkoLogger does not necessarily affect the length of your code. However, it makes the code easy to read if you had an Android beginner trying to go through your codebase.

I mean, Log.d(TAG, "Message"); is cool. But to someone with a technical background in other domains but Android, what does Log.d even mean?

Well, AnkoLogger to the rescue!

The table below shows the translation of the android.util.Log class to it’s AnkoLogger equivalent.

 

 

 

 

 

 

 

 

There are two ways to add AnkoLogger to your project: Using the AnkoLogger trait-like interface or Using AnkoLogger as an object.

 

The trait-like interface

class MainActivity : Activity(), AnkoLogger {
    fun doSomething() {
        ...
        info("This is some information")
        warn("Be careful")
    }
}

AnkoLogger as an object

class MainActivity : Activity() {
    private val log = AnkoLogger(this.javaClass)

    private fun doSomething() {
        ...
        log.info("This is some information")
        log.warn("Be careful")
    
}

 

There are some helpers that are not grouped into any of the Anko subsystems. e.g. colors and dimensions

 

Conclusion

In this article, we covered the Anko helper for logging also known as the AnkoLogger.

This is the end of the Zero to Kotlin Hero series.

In the next article, we would cover the AndroidX migration. See you there!

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *