DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android Clean Code :  Part 5  -  Configurator and Scaffolding

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.

Mohanraj Karatadipalayam user avatar by
Mohanraj Karatadipalayam
CORE ·
Aug. 07, 17 · Tutorial
Like (2)
Save
Tweet
Share
4.34K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • clean-swift

  • Make your own File Templates in Android Studio

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

Android (robot) Scaffolding (bioinformatics)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Distributed Stateful Edge Platforms
  • Artificial Intelligence in Drug Discovery
  • Best Practices to Succeed at Continuous AWS Security Monitoring
  • Three SQL Keywords in QuestDB for Finding Missing Data

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: