Epoxy without Annotation Processing

Annotation Processing / KAPT is a helpful technique for reducing code boilerplate, however it affects the Gradle build performance.

Epoxy without Annotation Processing

Annotation Processing / KAPT is a helpful technique for reducing code boilerplate, however it affects the Gradle build performance.

Introduction

Epoxy is one of my most favorite libraries for building complex and declarative UIs for quite sometime now, it has really helped me a lot in my daily work as an Android developer. It has taken over the default RecyclerView adapter because of its flexibility and reusability.

Epoxy
Epoxyβ€”Build Declarative & Reusable UI Components
Working with RecyclerView made simple!

Epoxy was originally built for Java back when Kotlin had not been an official programming language for Android, so some of its APIs has been inspired by Java code styles and patterns which at this point seems to be not intuitive in the Kotlin world. Epoxy uses Annotation Processing to generate Model builder classes in order to easily be used inside EpoxyController with less code, but it decreases the build speed. We’ll use Kotlin Extensions & ViewBinding to do so.

Getting rid of the annotation processing is to improve the build speed of our application. Epoxy can work perfectly fine with or without the Annotation Processing, however, we will lose some of its advanced/automated features.

Custom Epoxy Holder

Start by creating a new Epoxy Holder with ViewBinding.

Custom Epoxy Model

Create a new Epoxy Model with our custom Epoxy Holder above.

Extension Functions

Yes, we have to use Reflection for instantiating our Epoxy Models. However, it is not expensive to do so in my opinion. I saw FragmentFactory used reflection to instantiate fragment as well, so it should be safe. 🀞

Work with Custom Epoxy Model

Now let’s create a new Epoxy Model combined with what we have above.

The model class should be non-abstract class and we do not need to use @EpoxyModelClass and @EpoxyAtrribute annotation anymore.

ModelMenuTopicBindingViewBindingR.layout.model_menu_topic

Usage in Epoxy Controller

We have to use different approach to declare our models inside Epoxy Controller.

That’s basically it! We just need to call append<T> {} to create and add a model to the controller.

Create Models without Adding to the Controller

In some cases, we may need to create a list of models but not append them to the controller right away. For example, creating a carousel will require passing a list of models.

Thanks to the power of Kotlin Extensions & ViewBinding that enables us to shrink significant code boilerplate that we used to write back in the Java world.

Final Thoughts

Epoxy is already a well design library for us but we just need to customize it to fit our use-cases. For Java codebase, I think you should stick with the annotation processing approach or incrementally move to Kotlin.

If you have any questions or found any mistakes, please let me know in the comments.