NoSuchMethodError - a headache in a multi-module project.

NoSuchMethodError - a headache in a multi-module project.
Photo by Erik Mclean / Unsplash

Lately, I’ve been working with a legacy multi-module project in Android. I aimed to complete all the application initialization in my newly created module.

However, the legacy module had an Application class of it’s own with quite a bit of logic.

I faced a problem - how do I initialize the legacy application class, since you can only have one in Android?

Well, I decided to simply make the legacy Application an ordinary class and pass Context from my Application into the former application.

This worked beautifully on debug. However, when the time came to make a release build, the app started crashing with NoSuchMethodError. It couldn’t find the method I created to initialize the legacy Application class. I knew the problem was due to optimizations / obfuscation being done, but I couldn’t understand why. My Application was clearly calling this method during initialization. I added the proguard (optimization) rules to keep the method, turned off minifyEnabled, etc. Still - nothing helped.

Well, after hours of debugging and scratching my head - I finally got it. I correctly specified the proguard rules and minification… but in the wrong module.

Sure, my newly created module was clearly referencing the legacy code. However, the legacy module knew nothing about it. Since modules are treated as separate gradle projects - all code that’s not used within the module itself will be deleted during optimization.

Solution?

Specify the method/classes you want to keep in your proguard-rules.pro file INSIDE the module where this method/class exists. That way, this code won’t get removed during optimization.

Subscribe to Multiplatform Mindset