Elements Blog

Jetpack Compose, future of Android’s UI?

History

Android’s layout system is now as old (10+ years) as the platform itself and has accrued technical debt during its many development iterations. Some of the problems with view widgets on the platform levels have been mitigated by Jetpack libraries (previously known as support libraries) which provide backwards compatibility across multiple Android versions. However, the layout system is still heavily influenced by its initial inheritance API approach which can be seen throughout all the classes in the Android’s Framework. e.g. EditText extends TextView which extends View. Where the View class is also reaching almost 30k lines of code.

React includes a declarative way to compose UI layouts and Leland Richardson attempted to create a cross language library to do the same across platforms. I attended his talk in 2018 and was fascinated by how he set-up Recoil. All his efforts led to what we now know as Compose.

Introduction

Compose is a modern UI toolkit entirely written in Kotlin and provides a simplified and more efficient way of creating user interfaces. Before Compose you had to write layouts in XML which had its pros and cons. However, in reality XML proves to have more cons in terms of readability, flexibility and efficiency. Compose is designed to tackle many of the existing problems by:

  • Separating libraries: Since Jetpack libraries are shipped with your app it is also way easier to update them without relying on the framework’s implementation.
  • Using the Kotlin Language: by creating layouts in Kotlin you can leverage the power of the Kotlin language.
  • Reactive approach: Compose includes both state and event management. Whenever the state changes the UI can react and update accordingly. This is similar to how React(Native) and Flutter have been built.
  • Views begone: in Compose components are no longer views and are only functions which draw onto the Canvas. This means all existing widgets would have to be recreated but would not be influenced by the legacy views.

Learning material

Since there are already many blog posts about Compose this blog will not provide any examples. Google provides further information and a very nice tutorial at https://developer.android.com/jetpack/compose.

Going forward: research & adoption

As an Android Developer & Technical Guide I am constantly investigating the new kid on the block, which could either be a new library, way of working or something different. With Compose it is the same and requires some time to be investigated and weighed.

There are multiple ways to decide whether a new tool is a good fit for you or your company:

  • Read blog posts: there are many experts that have committed their time and dedication to try new things and write down their findings. These provide a valuable insight into what is offered and which pros and cons there are.
  • Attend conferences or watch presentations on YouTube (from conferences): (recorded) presentations give a quick overview of tools and their features.
  • Try the tool yourself: trying things yourself gives a firsthand experience and better insight into what you can and cannot do with a certain tool. However, this one also requires more dedication. During work hours you might already be swamped and after working hours you might be too tired. That is why it is important to have dedicated hours for research and improvements during working hours.
  • Teach others: you will also learn a lot yourself by explaining concepts to others. This way you can also discuss and decide together whether a new tool should be adopted by the team.

Conclusion

I believe Compose is here to stay and, once it gets more traction and more widgets, will become the preferred way of creating layouts in Android. However, since it is in its early development there are still challenges to overcome. Some of the challenges I see I have written down as questions below:

Questions

  • How will Compose work with ConstraintLayout / MotionLayout? Will it be directly supported or would you still have to create an XML layout file and inflate it through Compose?
  • How will Compose work with RecyclerView and how would its performance be? Will it be similar to libraries such as Epoxy/Groupie whereas you only add the models to the list and Compose will take care of the diffing and all the notify* calls?
  • Another nice feature would be to have Kotlin Multiplatform support. That way you could almost create a whole application in one shared module.

No items found.
No items found.

This blog was written by

Jeffrey de Looff

on

Nov 15, 2019

Android
Kotlin
Compose
Technical Guide
Android App Development