Android KTX or Anko
Asked Answered
M

2

13

I'm a little confused about the advantages and disadvantages of using Android KTX vs Anko. Because both libraries are trying to achieve the same end goal, and the line between them it's getting a little bit blurry to the point, in some cases, the same functionality is available in both libraries.

I will be very thankful if you help me to clarify the mission of each one, and their use cases.

Thanks in advance!

Moneymaker answered 16/5, 2018 at 1:57 Comment(2)
KTX is a newer project maintained by the actual Android team. I wouldn't be surprised to find that features are being copied over / rewritten from AnkoCressi
I have the same question for coroutines. Those are now available in the std-lib so maybe anko could be deprecated?Raychel
T
24

Anko is a project JetBrains started while developing Kotlin and making it relevant for Android. I'd qualify it as an early production-ready showcase of Kotlin possibilities.

Anko is well known for Anko layouts, but also has some extensions for Android development in Anko Commons, and also a few other modules like SQLite.

However, Anko libraries have accumulated a significant number of methods updates after updates, which today make the library relatively heavyweight when you are limited to 65K methods per dex file (I prefer staying with a single one, for apk size and performance reasons).

Anko commons provides extensions for Android, so it's similar to Android KTX in a way, but it's not as well integrated and organized (packages and artifacts wise) as Android KTX which is now part of AndroidX, which is part of Android Jetpack. Making the libraries smaller, and more loosely coupled is part of the Android Jetpack / Android X philosophy BTW.

So Android KTX, is clearly the future, and I see no reason to not jump in the ship, or rather, not letting it jump in your Android projects.

Also, as you yourself pointed out, future Anko versions will deprecate (and probably finally remove) overlap with Android KTX, to provide only features that are not in AndroidX.

Includes a shameless plug: Since you're talking about Anko, if Anko layouts got your attention, but you experienced struggles with them (especially when interacting with custom or third-party views), I'd recommend you to check out Views DSL in Splitties, which is more flexible than Anko layouts, and very lightweight. The other modules may also interest you. I started this project to leverage Kotlin features in my Android projects at work and at home.

You've not asked about it, but here's my take on Anko coroutines: I find the bg { … } extension confusing. I prefer to use kotlinx.coroutines with the Android artifact, using launch, withContext, etc, along with Dispatchers.IO.

However, the Anko coroutines listeners (e.g. onClick that can suspend) are not a bad idea IMO.

Teraterai answered 17/5, 2018 at 21:48 Comment(3)
Thanks for your detailed answer, it really helped up!Moneymaker
@Louis CAD, Is there any migration for Anko Layouts to Android KTX and/or to XML? Since i am working on a project where the designs are made up with Anko Layouts, and the anko support library preview is not working for recent IDE updates and the Anko team are not publishing any updates regarding this. Soit would be helpful to have conversion method.Vowelize
@Krantiz There's no such thing. However, you can integrate an Anko layout in a custom view (e.g. FrameLayout subclass) that you can then preview in xml. That's what I'm doing in Splitties Views DSL. If you want deeper advice, you can ask me on Kotlin's Slack or by Twitter DM.Teraterai
C
0

Anko is a Kotlin library from JetBrains. It shares the goals with Android KTX of making your life as an Android developer easier and making your code more readable and pleasant to use. Android KTX and are Anko are similar but not quite the same.

Differences Between Anko and Android KTX

They have common goals but with different scopes in mind. The mission of KTX is to bring idiomatic APIs to Kotlin users. KTX does not intend to add new functionality to the existing Android APIs. Anko has a wider scope, and this gives it the flexibility to innovate and create new features

Deciding When to Use Anko or Android KTX

Generally speaking, Anko and Android KTX cover different parts of the Android API, and there’s no reason you can’t use both in your project. Investigate each one individually and use either as much as you want in your projects!

Carnay answered 10/4, 2020 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.