Should new Android developers learn Compose or XML?

Jetpack Compose or XML-based View?

ยท

4 min read

In the next three months, I'll be volunteering to teach undergrad students Android development. So, I'm also faced with this question, should I teach a new developer Compose or XML?

In my opinion, aspiring Android developers should just start learning Compose. Here are my four reasons why:

1. Production-ready

Compose is ready for production. Twitter, Play Store, and Airbnb are just a few companies that already using Compose. It has been more than one year since Compose reached version 1.0.

What do I mean by production ready?

  • No major bugs or unnecessary troubleshooting.
  • The APIs are stable, consistent, and intuitive.
  • There is no performance issue, your User Interface (UI) will render smoothly.

When I'm coming back to Android development after working as a backend engineer for a year, I'm curious whether I can build a complex UI with Compose. As a proof of concept, I've made a clone of Astro, a quick commerce app that I believe has typical requirements of the real app (layered UI, custom behavior, multiple UI sections, etc.).

So, you don't need to worry that you will encounter too many issues when learning Compose. You can publish a real app with Compose.

2. Simplicity

This is an example of how you write UI using XML.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hi, my name is Aldo" />

    <ImageView
        android:id="@+id/ivProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/tvRole"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I'm an Android developer ๐Ÿค–" 
        />

</LinearLayout>

And this is how you write UI using Compose.

Column {
    Text("Hi, my name is $name", style = TextStyle(
        fontWeight = FontWeight.Bold))
    AsyncImage(model = "https://example.com/img/user.png")
    Text("I'm an Android developer ๐Ÿค–")
}

Even from this simple use case, you can see that Compose uses fewer lines of code. The simpler the code, the easier it is to learn or to teach it.

You don't need to learn:

  • XML (just use Kotlin)
  • Binding XML to a Kotlin class
  • Improving performance by flattening your View hierarchies using ConstraintLayout

This means you've got more time to learn other Android topics, like ViewModel, testing, Coroutines, and Jetpack libraries.

3. Familiarity

If you've learned declarative UI from other platforms, you will feel right at home.

Take a look at these code snippets that build the same UI for iOS (SwiftUI) and cross-platform (Flutter).

VStack {
    Text("Hi, my name is \(name)!")
        .fontWeight(.bold)
    AsyncImage(url: URL(string: "https://example.com/img/user.png"))
    Text("I'm an iOS developer ๐Ÿ")
}
Column(
    children: const<Widget>[
        Text('Hi, my name is $name', style: TextStyle(
            fontSize: FontWeight.bold)),
        Image.network('https://example.com/img/user.png')
        Text('I'm a Flutter developer โฌ…๏ธ')
    ]
)

It looks somewhat similar, isn't it?

Chances are, a new Android developer is not going to pursue a career as a full-time Android developer. They might be exploring Flutter, iOS, backend, or not even pursuing a career as a developer.

By using Compose, you've learned a concept that can be applied to other platforms.

4. Support

Learning anything new can be difficult at first, no matter how good a library is. When you're stuck, Compose has many resources to help you, from official Android documentation to a plethora of community articles. To mention some:

You don't need to worry about integration with popular libraries as well. Popular libraries like Jetpack Components (ViewModel, Navigation, Hilt, etc.), Glide, or Koin is compatible with your Compose code.

Compose is still actively supported by Google, so even if you find a bug or a feature request, you can expect it's addressed in the next version.

Summary

In conclusion, Compose is a safe choice for a developer who's just getting started with Android development. While XML is still dominant in the legacy project, Compose is the modern toolkit for building native Android UI. Go ahead and use Compose in your first app!

Notes

  • I'll be teaching Pemograman Mobile course at Universitas Musamus Merauke (UNMUS) as part of praktisimengajar.id government program. Thank you for giving me this opportunity!

  • To be fair, XML is declarative UI too, a verbose one compared to Kotlin. But in XML, you still need to write imperative code for many tasks.

ย