Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Android Clean Code :  Part 5  -  Configurator and Scaffolding

DZone's Guide to

Android Clean Code :  Part 5  -  Configurator and Scaffolding

Learn more about the Clean Code Android mobile design pattern and unit testing your Android app piece by piece for code quality.

· Mobile Zone ·
Free Resource

This article is part of a series that begins here. The previous installment can be found here.

Having understood Android Clean Code, you must have worried about writing the classes and wiring the dependencies yourself.

Wiring the dependencies must be done carefully, otherwise you end up creating circular references — in other words, memory leaks.

Who does the wiring of the Interactor, Presenter, Router and the Activity classes?

Configurator.

Let’s look at the configurator code:

import java.lang.ref.WeakReference;

public enum HomeConfigurator {
    INSTANCE;
    public void configure(HomeActivity activity){

        HomeRouter router = new HomeRouter();
        router.activity = new WeakReference<>(activity);

        HomePresenter presenter = new HomePresenter();
        presenter.output = new WeakReference<HomeActivityInput>(activity);

        HomeInteractor interactor = new HomeInteractor();
        interactor.output = presenter;

        if (activity.output == null){
            activity.output = interactor;
        }
        if (activity.router == null){
            activity.router = router;
        }
    }
}

The code is straightforward. Why WeakReferences?

We should not have any strong references of Activity classes from Router and Presenter, otherwise Activity will never get released from the memory.

Due to single responsibility principle, we have many classes and effectively, yes, too much boilerplate code.

Don’t worry — Android Clean Code Generator solves all of above-mentioned issues.

We have created a tool that will auto generate the code and wire the dependencies. All you need is to clone the project and import the project into Android Studio.

Where to Import

  • Windows: Navigate to the location of the templates folder: cd {ANDROID_STUDIO_LOCATION}/plugins/android/lib/templates/other/
  • MacOS: Navigate to the location of the templates folder: cd /Applications/Android\ Studio.app/Contents/plugins/android/lib/templates/other/

Finally, restart Android Studio — it should work without any issues.

How to Use It

File → New → Other → Android Clean Code.

Below is the visual demo of the usage:

Android Clean Code Generator

These code generation project ideas aren’t new in any way. Credit has to be given to the following projects, listed below:

What’s next? We’ll talk about “Android Clean Code Implementation and Afterthoughts” in the next post.

Topics:
clean code ,mobile testing ,unit testing ,mobile ,mobile app development ,android

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}