The New ResourceProvider

Mark Dappollone
The Startup
Published in
3 min readMay 1, 2020

If you’ve been following my work at all, you know that I’m obsessed with Passive View MVP software architectures, and that I’m constantly looking for new and better ways to implement them. Enter ResourceProvider: The Annotation Processor I wrote to generate agnostic resource management APIs for Android app development.

Thanks, really.

Lots of you might argue that setting resources can just be a responsibility that you delegate to the View class in your MVP system, but logic around which strings and images go where needs to go into the Presenter, and that can lead to writing lots of View-class APIs with names like setErrorTextForInvalidAmount(), which can be tedious and time consuming.

Instead, ResourceProvider looks at your R.java file, and generates a few helpful classes that contain all the APIs you could ever want, based on the options you set. That way, your presenter can affectively handle resource-related logic, without being polluted with an Android Context instance. The processor will generate APIs just for strings, or strings and drawables, or any combination of resources that you want. Or… well… it used to do that.

Perfect, every time.

Ok, so I didn’t break it, so much as not update it, and then everything else broke it. Well, not everything, so much as specifically the Android Gradle Plugin. As it turns out, in version 3.6.0 of the AGP, they stopped generating the R.java file, and started generating the R class as bytecode directly, and making a JAR of it. From the release notes:

Faster R class generation for library projects: Previously, the Android Gradle plugin would generate an R.java file for each of your project's dependencies and then compile those R classes alongside your app's other classes. The plugin now generates a JAR containing your app's compiled R class directly, without first building intermediate R.java classes. This optimization may significantly improve build performance for projects that include many library subprojects and dependencies, and improve the indexing speed in Android Studio.

You can still force R.java generation, but that’ll be going away soon, so instead I decided to:

Rewrite the Whole Thing As a Gradle Plugin

Yup, I started over from scratch, and I’m happy to say that, as of today, you can start using the new Resource Provider plugin in your Android MVP projects with a few quick additions to your build script. Hopefully soon I’ll be able to publish the source, but until then you can start playing with it by adding these lines to the top of your build.gradle file:

After that, you just need to tell ResourceProvider the Android application id. This is the package name specified either in your AndroidManifest.xml or your build.gradle. I like to do this by setting up an ext variable for it, and then using it everywhere:

All of the generateXProvider variables shown above are optional and default to true. But to cut down on the method count, if there are any you don’t need, you can just turn them off.

The new ResourceProvider plugin will generate the exact same code as the old annotation provider, so if you’re using it currently, you can just drop in the replacement and you’ll be functioning again with AGP version 3.6.0 and above.

For now leave any questions or issues in the comments, and I’ll try to get a new repo published soon.

Update

The RP2 github repo is now available, as is version 1.1.0, which includes some bug fixes and support for Kotlin projects

https://github.com/Comcast/resourceprovider2

--

--