Kotlin Android Extensions

Kotlin Android Extensions

If you develop Android apps for a while, find your daily life to recover views and get tired of your everyday life. Or you have dropped and begun to use the famous star library. If it is your case, you will love the Kotlin android extensions. Kotlin has a built-in injection for Android that allows you to reduce the need for frameworks such as butter Knife.

Kotlin Android Extensions are another kotlin plug-in, which is included regularly, and actions will be allowed to recover views from the fragments and ideas.

The plug-in will create some additional code that allows you to access your views of your layout XML as the ID you used on the layout. It makes a local view cache. So the first time a property is used, it will do a regular findViewById. But following times, the view will be recovered from the cache so that the access will be faster.

Integrating Kotlin Android Extensions with code

The plug-in comes integrated into the regular one; if you want to use it, you have to add an extra application in the Android module.

When you start working with Kotlin, it is exciting to understand the bytecode when using one feature. This will help you to understand the hidden cost behind your decisions.

A decisive action below Tools –> Kotlin, called Show Kotlin Bytecode. If you click here, you'll see the bytecode generated when the class file you have opened is compiled.

Kotlin Android Extensions on fragments

This plug-in can also be used on fragments. The problem with fragments is that the view can be recreated, but the fragment instance will be kept alive. What happens then? This means that the statements inside the cache would no longer be valid.

Kotlin Android Extensions in 1.1.4

Since this new version of Kotlin, the Android Extensions have incorporated some new exciting features: caches in any class (which interestingly includes ViewHolder) and a new annotation called @Parcelize. There’s also a way to customize the generated cache.

Kotlin Android Extension to implement Parcelable

With the new @Parcelize annotation, you can make any class straightforwardly implement Parcelable.