部分设备出现java.time.Month类缺失异常,疑与CosmoCalendar和ProGuard有关?
Absolutely, this is almost certainly a ProGuard issue with the CosmoCalendar library—here’s why and how to fix it step by step:
CosmoCalendar relies heavily on Java 8+ java.time classes like Month and LocalDate, which are natively supported only on Android API 26 and above. For older devices, we use Core Library Desugaring to make these classes work—but if ProGuard isn’t configured to preserve these classes (or the library code that uses them), it will strip them out during the build process, leading to the NoClassDefFoundError you’re seeing.
1. Double-Check Core Library Desugaring Setup
Even though you resolved a similar LocalDate error before, confirm your module-level build.gradle has the correct desugaring config (this is non-negotiable for pre-API 26 devices):
android { compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4' // Use the latest version available }
2. Add CosmoCalendar ProGuard Rules
Since you haven’t added rules for this library yet, this is likely the root fix. Add these lines to your proguard-rules.pro file:
# Preserve all CosmoCalendar classes and their members to avoid obfuscation/stripping -keep class com.applikeysolutions.cosmocalendar.** { *; } # Preserve java.time classes that the library depends on -keep class java.time.** { *; } -keep interface java.time.** { *; }
This tells ProGuard not to remove or rename any code in the CosmoCalendar library, nor the java.time classes it uses.
3. Verify MultiDex Configuration
While you mentioned enabling MultiDex in the Manifest, some older devices require explicit code setup:
- If your custom
Applicationclass extendsMultiDexApplication, you’re good to go. - If not, add this to your
Applicationclass:
@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }
4. Test on Older Devices
To confirm the fix, generate a signed release APK and test it on a device running Android API 25 or lower—this is where the java.time class resolution issues are most likely to occur.
Third-party libraries that use Java 8+ features almost always require explicit ProGuard rules to avoid runtime crashes. CosmoCalendar’s docs might not call this out explicitly, but preserving its code and dependent JDK classes is critical for compatibility across all devices.
内容的提问来源于stack exchange,提问作者Monica Aspiras Labbao

