Modernize Your Android Legacy App
A step by step guide on how to modernize your legacy app
One of the biggest dilemmas when deciding to modernize a legacy app is wondering exactly why you have to do it. I won’t lie. It requires a significant amount of refactoring and hard work.
So what exactly is a modern app? a modern app is one that uses the newest best coding practices along with the recommended architecture.
In this article, I’ll share with you the why and how. Stay tuned:)
Why modernize your legacy App?
- Gain more Performance for your application
- Speed up your application Delivery
- Leverage your application Security
- Improve your application Compatibility
- Improve your application Stability
Let’s get started
Some Tips
- Do it gradually!
- Make sure your new code does not break your existing tests.
- Measure how your changes affects the application.
Step #1 - Migrate to Kotlin
Why migrate to Kotlin from Java?
- Google’s recommendation
- Null Safety leaves the 1 billion dollar mistake behind you.
- Using Kotlin you can write approx 40% less lines of code compared to Java and as you know: less code = less bugs = less coding time.
- Write async code in a sequential manner using Coroutines.
To learn more about it, you can read the following article:
In the following example, you can see how to simplify code that executes asynchronously:
It’s simple, using Retrofit library — a type-safe HTTP client & Coroutines we are sending a request to server to get our Inbox response which is an async call and then return a response or an exception in case of failure.
Step #2 - Start using Android Jetpack
Jetpack is a suite of libraries, tools, and guidance to help developers write high-quality apps easier. These components help you follow best practices, free you from writing boilerplate code, and simplify complex tasks, so you can focus on the code you care about.
To enjoy the jetpack goodies, you first have to migrate your application to AndroidX.
AndroidX
- Jetpack comprises the androidx.* package libraries
- Support library version 28.0.0 is the last release of the support library
- AndroidX artifacts with version 1.0.0 are binary equivalent to the support library 28.0.0 artifacts.
Migrate to AndroidX
With Android Studio 3.2 and higher, you can migrate an existing project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu bar.
To learn more about it you can read here.
Architecture Components
Android architecture components are a collection of libraries that help you design robust, testable, and maintainable apps.
ViewModel
- Helper class for the UI controller responsible for preparing data for the UI
- Automatically retained during configuration changes
- Scoped to a lifecycle
LiveData
LiveData is an observable data holder class.
LiveData is lifecycle-aware and it has the following advantages:
- Auto handling of lifecycle-related stuff
- Avoids memory leaks
- Avoids stupid crashes
- Always up to date data
In the following example, you can see how to implement MVVM architecture using LiveData & ViewModel:
Loaders have been deprecated as of Android P (API 28). The recommended option for dealing with loading data while handling the Activity and Fragment lifecycles is to use a combination of
ViewModels
andLiveData
.
Room
Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.
There are 3 major components in Room:
- Database: database holder
- Entity: a class represents a table within the database
- Dao: contains the methods used for accessing the database
Why migrate to Room?
- Integration with LiveData to always be updated
- Better performance and internal caching
- Easy to write instrumentation tests using Room.inMemoryDatabaseBuilder()
In the following example, you can see how to create the Room components:
To learn more about Android Jetpack, you can read here.
Step #3 - Migrate to Junit5
What’s New?
Extension Model
- Extends the behavior of test classes or methods (lifecycle)
- Replaces JUnit runner and Junit Rules
- Supports multiple extensions
New @TestInstance mode: Lifecycle.PER_CLASS
Creates only one instance of the test class and reuse it between tests.
In the following example, you can see how to write Unit Test for our InboxViewModel:
Now, have we finished our work? Hmmm… the answer is No! We live in a dynamic environment where everything can change and evolve, which is a good thing. We will always need to be updated with the newest best practices in the Android world and keep modifying our apps with new changes.
That’s all! I hope you liked this article.