Hitting 64K method limit in .dex file for Android API 17

I’m having trouble with my Android app that uses SugarORM. When I try to build for API 17, I get an error. But it works fine for Android 5.0 and up. If I remove the SugarORM dependency, it builds okay for both 4.2.2 and 5.0 devices.

The error message says something about too many method references in the .dex file. It looks like this:

Error: The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at [link removed]
Error: Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

Any ideas on how to fix this for older Android versions? I’m not sure why it only happens with SugarORM.

The 64K method limit is a common hurdle when developing for older Android versions. I encountered this issue in a project last year. Multidex is indeed a viable solution, but it can slow down app startup on older devices. Another approach I found effective was to use ProGuard aggressively. By fine-tuning ProGuard rules, I managed to strip out a significant number of unused methods from third-party libraries, including ORM solutions. Additionally, consider manually removing unused features from SugarORM if possible. If these optimizations don’t suffice, you might want to explore more lightweight ORM alternatives or even consider a custom, simplified data persistence solution tailored to your specific needs. This approach helped me reduce method count substantially while maintaining essential functionality.

I’ve run into this 64K method limit issue before, and it can be a real headache, especially with older Android versions. In my experience, SugarORM tends to bloat the method count quite a bit due to its reflection-heavy nature.

One approach that worked for me was enabling multidex support. You can do this by adding multiDexEnabled true to your app’s build.gradle file and including the multidex library in your dependencies.

If that doesn’t solve it, you might want to consider alternatives to SugarORM. I switched to Room in one of my projects and found it much more efficient in terms of method count. It’s part of the Android Jetpack libraries and works well even on older Android versions with the right setup.

Lastly, you could try using ProGuard to strip out unused methods, which might get you under the limit. It requires some configuration, but it can significantly reduce your method count.

Remember, the 64K limit is per DEX file, so multidex is often the most straightforward solution for complex apps.

hey man, i feel ur pain. sugaorm can be a real memory hog. have u tried using multidex? it’s a lifesaver for older android versions. just add ‘multiDexEnabled true’ to ur gradle file and throw in the multidex library. if that doesnt work, maybe ditch sugarorm for somethin lighter?