Jan 11, 2024

Google Flutter vs Android Jetpack Compose : A detailed comparison

Flutter code looks remarkably similar to Android Jetpack Compose. However which one of them is better ? We find out in this article.

Google Flutter vs Android Jetpack Compose : A detailed comparison

Flutter is a framework from Google based on a programming language that Google created called Dart. Flutter is known for its performant UI and hot-reload feature, making it a popular choice among developers. Flutter's key strength was that it was cross platform. It meant it could run on both Android as well as iOS.

Android compose on other hand is just a new library for traditional Android development which allows you to declare your UI using declarative syntax which will certainly remind you for Flutter UI.

Problems solved by Flutter

Flutter was create for solving the problem of building cross-platform applications with a performant UI. Its hot-reload feature allows developers to quickly iterate and see changes in real-time. Additionally, Flutter's use of the Dart programming language provides a familiar and efficient development experience. Dart is a modern programming language which overcomes many problems faced by Java and Javascript programmers.

Check this example flutter code.

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Column Demo'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: const [
              Text(
                'Hello, Flutter!',
                style: TextStyle(fontSize: 24),
              ),
              SizedBox(height: 20), // Add spacing between elements
              ElevatedButton(
                onPressed: null, // Replace with button functionality
                child: Text('Click me'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Problems solved by Compose

Compose is not a language. It is just a library. In traditional Android development, it was easiest to write UI using XML. Then your code would "inflate" this XML into UI. The problem with this approach was that reading code did not make sense. When you looked at a particular XML file, it did not give you the big picture view of the UI. The java code on other hand did not give you slightest idea of the UI as it was decoupled from the XML.

Compose makes this simpler. You can actually look at the code and understand what it is actually doing and how it is supposed to look. Take an example:

import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview


@Composable

fun SimpleComposable() {
    Column {  // Arrange elements vertically
        Text(text = "Hello, Compose!")  // Display a text
        Button(onClick = { /* Handle button click */ }) {
            Text(text = "Click me")  // Button text
        }
    }
}

@Preview(showBackground = true) // Render a preview in Android Studio
@Composable
fun SimpleComposablePreview() {
    SimpleComposable()
}

Same code in traditional Android development would look like below in XML layout.

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

    <TextView
        android:id="@+id/text_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Hello, Android!"
        android:textSize="24sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click me" />

</LinearLayout>

Note that the XML file is far too bloated to make sense of and we have not even see the Java code that powers it yet.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Looking at this Kotlin code you do not understand what exactly it is displaying. To understand what is happening you need to look at two files and realize how they are linked. In real world applications there would be dozens of XML layout files that get used inside the activity to give it a particular appearance.

Flutter development environment

Flutter development environment is completely different from your standard java based development. You need the Android SDK and all that but you have the freedom to use wide variety of tools including VS Code. You need Dart and Flutter installed on your machine.

One advantage of Flutter is hot reload. Whether you are using simulator or real device any code changes immediately reflect in the app, almost like browser refresh when you are developing a webpage. This speeds up your development time.

Android compose development environment

With compose you should ideally use Android studio. Also, hot reload while Google claims is possible, remain extremely buggy feature. Google has tried a lot to make Android studio better experience but remains far from what developers would love using. Certainly not as good as iOS development in Xcode.

Speed of development

I have written dozens of apps in both and I would say Flutter is certainly great when it comes to writing cross platform apps. It is faster to develop than the traditional android development provided you have learned Dart pretty well.

Android Compose has been pretty good too and more importantly your app will have smaller footprint than usual.

App performance

Between Flutter and java based android apps, java based Android apps win by a good margin. Flutter apps take longer to install, longer to startup and are more prone to crashes and ANRs. But this performance penalty is often due to the fact that Flutter engine has to bootstrap. However, once it is fully bootstrapped and running, the complexity of your app does not really decrease the performance.

Compose based apps on other hand are very snappy when it comes to install and startup. However the performance of the app will degrade if you write without attention to the performance and best practices.

Flutter has a concept of "tree shaking" which means it can detect which parts of the code are not getting used and get rid of them. Android has similar concept in the form of proguard but I have seen that Flutter is lot better at this. However, because each flutter app must include the flutter engine with it, the total size of the app will always be much greater for Flutter apps.

Conclusion

There is no real good reason to write your apps in Flutter unless you want to build cross platform apps. In that case Flutter has clear advantage. However, Android Compose is an excellent library that brings the Flutter like paradigm to build UI and makes writing UI as good as Flutter.

Continue Reading
Learn about other front end technologies

Learn about other front end technologies

We are partnering with Frontendeng.dev to cross promote the content around front end engineering.

Published Dec 12, 2023

Android paired with Windows Laptops

Android paired with Windows Laptops

The new trend is to build Android and Windows Laptop into one hardware.

Published Jan 11, 2024

What are co-routines in Kotlin ? Kotlin for Android Part -2

What are co-routines in Kotlin ? Kotlin for Android Part -2

In this part we will learn about Kotlin's co-routines. It is an important concurrency design pattern that is extremely useful when designing asynchronous programs.

Published Dec 31, 2023

Backends for your Android App

Backends for your Android App

In this article, we will explore the various options available for app developers who are primarily focused on building an app backend.

Published Jan 1, 2024

Minimal starting template for Android Compose

Minimal starting template for Android Compose

Find a simple minimal code to open an app with top and bottom bar. Find full code on https://github.com/Wiseland-Inc/dev.androidauthority.app

Published Dec 11, 2023

Spring Boot for App Backends

Spring Boot for App Backends

Simple spring boot template to deploy on Google Cloud and to be used as your app backend.

Published Jan 6, 2024

Learning Kotlin for Android development - Part 1

Learning Kotlin for Android development - Part 1

A guide focused on teaching you basics of Kotlin just for Android development.

Published Dec 7, 2023

Making android development less painful

Making android development less painful

Android development has always been challenging and frustrating right from start. However things are looking better now.

Published Dec 4, 2023

Understanding Layouts in Android Compose

Understanding Layouts in Android Compose

A very quick tutorial on compose and layouts.

Published Dec 10, 2023

What are adaptive android apps ?

What are adaptive android apps ?

Adaptive apps are app equivalent of responsive web apps. Apps that resize to give good user experience across several devices.

Published Dec 9, 2023