Fausto70
Fausto70

Reputation: 551

How to configure Proguard in Android Studio - got errors running in device

I have coded an Android application and I want to generate an Apk with Proguard, then I want to obfuscate the code. I use Android Studio 3.

In my build.gradle I have:

buildTypes {
        release {
            shrinkResources true  
            minifyEnabled true  
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
}

I understood that the Proguard configuration is inside 'proguard-android.txt' and 'proguard-rules.pro'. At the beginning I had errors during building, and I solved them writing in 'proguard-rules.pro', adding some rows:

-dontwarn org.**
-dontwarn com.**
-dontwarn java.**
-dontwarn javax.**
-dontwarn sun.**

After that the building is OK, but when I run in the device I got errors due to obfuscation; the first two ones I get are:

E/dalvikvm: Could not find class 'android.hardware.display.DisplayManager', referenced from method l.a.i.aX
E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

then, after some logs, I get

E/AndroidRuntime: FATAL EXCEPTION: main
                  java.lang.NoSuchFieldError: UNKNOWN
                      at java.lang.reflect.Method.getDefaultValue(Native Method)
                      at java.lang.reflect.Method.getDefaultValue(Method.java:362)
                      at org.apache.harmony.lang.annotation.AnnotationFactory.getElementsDescription(AnnotationFactory.java:75)
                      at org.apache.harmony.lang.annotation.AnnotationFactory.<init>(AnnotationFactory.java:115)
                      at org.apache.harmony.lang.annotation.AnnotationFactory.createAnnotation(AnnotationFactory.java:97)
                      at java.lang.reflect.Field.getAnnotation(Native Method)
                      at java.lang.reflect.Field.getAnnotation(Field.java:212)
                      at com.a.a.d.f.a(Unknown Source)
                      at com.a.a.a.e.a(Unknown Source)
                      at com.a.a.a.e.a(Unknown Source)

And then the app is terminated.

To solve the first error I tried:

-keep class android.** { *; }

but it doesn't work...

Can anybody help?

Inside the 'proguard-android.txt' (built by Android STudio...) I have:

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

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

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

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

Upvotes: 2

Views: 4113

Answers (1)

Fausto70
Fausto70

Reputation: 551

I solved using the following text inside 'proguard-rules.pro':

##  ________________________________________________________________________________________________________________

-dontwarn org.**
-dontwarn com.**
-dontwarn java.**
-dontwarn javax.**
-dontwarn sun.**



##  ________________________________________________________________________________________________________________

-keep class android.** { *; }
-keep class org.** { *; }
-keep class java.** { *; }
-keep class javax.** { *; }
-keep class sun.** { *; }
-keep class de.mindpipe.** { *; }
-keep class com.j256.** { *; }


##  ________________________________________________________________________________________________________________
## Preserve line numbers in the obfuscated stack traces.
-keepattributes LineNumberTable
-keepattributes SourceFile

Upvotes: 2

Related Questions