How can I get around the 64K reference limit in debug builds?

103 Views Asked by At

I develop an app that was designed from inception with minSdkVersion 23. I recently realized that with minimal work (basically just gating off the few method calls that actually require SDK > 19), I could get the app to run, with most of its functionality available, on SDK >= 19, so I lowered minSdkVersion to 19 - and soon ran into the 64K reference limit.

Release builds are fine, since my Gradle configuration does R8 minimization of release builds, and even day-to-day debug builds are fine, since Android Studio is smart enough to automatically enable multidex when building and deploying a debug build to a connected device running Android 5.0 or higher. The problem occurs only with standalone debug builds (e.g., from my GitHub Workflow CI runner) or when I actually want to deploy a debug build to a pre-5.0 device.

What's the recommended solution for this situation? The documentation suggests removing unnecessary dependencies, but I'm not using very many dependencies to begin with. I could enable minification for debug builds, but Android Studio as well as the online documentation recommend against doing this, for performance reasons. I could manually configure multidex, but the changes to the app necessary to do so feel too intrusive and drastic for the limited benefit of getting standalone and pre-5.0 debug builds without minification working.

I've resorted to enabling minification for debug builds in the published Gradle configuration, but manually disabling it during actual development. This works, but feels kludgy. Is there a better solution?

1

There are 1 best solutions below

3
Gabe Sechan On

Don't lower it. Do you know how few devices in the world use SDK 19? Less than 1.8% of devices worldwide that have downloaded from the Play Store in the last few months are using a version under 23. You'd be adding SIGNIFICANT work to support them. Also consider your audience- that's the worldwide number, if your target audience is US or EU based it will be under .1% (those numbers aren't published, but the oldest devices skew to poorer countries in Asia and Africa. Unless that's your target market, it's even less reason to worry about them).

If you're not going to do that- there are older forms of multidex before it became standardized that should still work. You'd need to dig up posts on how to do it, because it was clunky.

If you really want to go to no multidex at all- you're realistically going to have to rewrite your entire app to a style that minimizes function calls. Don't use any libraries, because they won't be written in that style and modern Android libraries and styles tend to use a TON of function calls and lambdas. It could be done, but I can't think of a good reason to do it.

Not quite what you asked for but I'd also comment this- 23 is lower than you should be aiming for. Even giant apps that try to target huge audiences don't set their min version that low anymore. You can target 94% of devices by using a minVersion of 26. You can do 86% with 28. Either of those are more reasonable minimums. And the same argument about market and version distribution apply. If it already works I wouldn't change it (yet), but in the future I would consider how much additional work you're adding by not having access to newer features and additional time spent to code work arounds, and consider if it's actually worth the time and money of supporting those older versions.

(Also note that all those numbers I threw around are lagging indicators. Those numbers are going down constantly. So if you're considering an app you're just about to build, factor in 3-6 months of additional decline before deciding what version to use as your minimum.