NoClassDefFoundError when using proguard
Asked Answered
T

1

8

My Project contains some librarys (ViewPagerIndicator, ActionbarSherlock, SlidingMenu...). After using proguard, my application crash every time when starts. Below is error log

java.lang.NoClassDefFoundError:android.support.v4.appp.FragmentActivity$2 at android.support.v4.app.FragmentActivity.<init> (Unknown Source)....

And here is my proguard script:

    -injars      bin/classes
-injars      libs
-outjars     bin/classes-processed.jar

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*
-verbose

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.support.v4.app.FragmentActivity
-keep public class * extends android.support.v4.app.Fragment

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}



-dontwarn android.support.**
-dontwarn com.google.android.maps.**
-dontwarn com.slidingmenu.lib.app.SlidingMapActivity
-keep class android.support.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
-keep class com.slidingmenu.** { *; }
-keep interface com.slidingmenu.** { *; }
-keep public class com.viewpagerindicator.** { *; }
-keep public interface com.viewpagerindicator.** { *; }

Does anyone know what wrong with my proguard script? Please let me know, thank you in advance.

Telpherage answered 29/11, 2012 at 11:13 Comment(0)
M
10

Rather than being as specific with the package names

-keep public class * extends android.support.v4.app.FragmentActivity
-keep public class * extends android.support.v4.app.Fragment

Change it to this:

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }

In the error it is looking for (note the extra p):

android.support.v4.appp.FragmentActivity$2 

rather than just

android.support.v4.app.FragmentActivity.<init>

Edit, not sure what it could be, here's a full proguard for one of my apps that uses the support library and ActionBarSherlock.

##---------------Begin: proguard configuration common for all Android apps ----------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt 
-printseeds seeds.txt 
-printusage unused.txt 
-printmapping mapping.txt 
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService

# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# Preserve all native method names and the names of their classes.
-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* {
  public static <fields>;
}

# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep public class * {
    public protected *;
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
##---------------End: proguard configuration common for all Android apps ----------

# Remove Logging statements
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** e(...);
    public static *** i(...);
}

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.antew.redditinpictures.library.imgur.** { *; }
-keep class com.antew.redditinpictures.library.reddit.** { *; }

##---------------End: proguard configuration for Gson  ----------



##---------------Begin: proguard configuration for ActionBarSherlock  ----------
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }

##---------------End:   proguard configuration for ActionBarSherlock  ----------
Mosora answered 29/11, 2012 at 18:4 Comment(6)
Hmm, I'm not sure what to try next. I've posted a full proguard file from one of my apps that uses ActionBarSherlock and the Support Library, I see a few differences between the configurations. Hopefully it helps.Mosora
Your proguard config works for me after some edits (replace keepclasseswithmembernames with keepclasseswithmembers (lint error), add -dontwarn com.google.android.maps.** (warning)) but when decompile I see that all of my package and class name not changed although -repackageclasses '' already specified. Do you know why?Telpherage
Oh I see, because of this -keep public class * { public protected *; }. Why yout want this? :DTelpherage
I wasn't worried about the obfuscation with my code, I really wanted it for removing log statements, and compacting the APK. I got the base for it from the proguard example included with gson.Mosora
OK, thanks you for your help, base on your code, I have my own working proguard file.Telpherage
-keep class android.support.v4.app.** { *; } already covers the -keep interface ... case, so -keep interface is superfluous.Spoofery

© 2022 - 2024 — McMap. All rights reserved.