I’m having trouble with my Android app that uses the Google Drive API. Everything works fine without ProGuard, but when I enable it, I get a NullPointerException at runtime. The error occurs in the Google API client code, specifically in Types.java.
I’ve tried adding keep rules for Jackson and Google libraries in my ProGuard config:
But it didn’t help. The crash still happens when I try to execute a Files.List request:
FileList files = request.execute();
I also tried disabling obfuscation with -dontobfuscate, but that led to a ‘Conversion to Dalvik format failed’ error when building the APK.
I suspect the issue might be related to how ProGuard is handling generic types or interfaces. Has anyone encountered this problem before? Any suggestions on how to fix it while still using ProGuard for my app?
I’ve encountered this issue before when working with Google APIs and ProGuard. The problem often stems from ProGuard stripping away essential metadata or annotations. In addition to the rules you’ve already tried, consider adding these to your ProGuard configuration:
These rules should preserve important annotations and inner classes that the Google Drive API relies on. Also, make sure you’re using the latest version of the Google Drive API library, as older versions may have compatibility issues with ProGuard. If the problem persists, you might need to create a more granular ProGuard configuration for specific Google API classes.
I’ve dealt with similar ProGuard headaches when working with Google APIs. One thing that helped me was using the -keepclasseswithmembers directive. Try adding this to your ProGuard config:
-keepclasseswithmembers class * { @com.google.api.client.util.Key ;
}
This preserves classes with fields annotated by @Key, which is crucial for the Drive API.
Also, don’t forget to keep your model classes:
-keep class com.yourpackage.model.** { *; }
Replace ‘yourpackage’ with your actual package name.
If you’re still hitting issues, you might need to generate a ProGuard configuration file specific to your app. Run a debug build with ProGuard enabled and check the output folder for a file named ‘proguard-rules.pro’. This file often contains additional rules you might need.
Hope this helps. ProGuard can be a real pain, but once you get it right, it’s worth it for the APK size reduction.