如何在Android Studio中配置Proguard - 在设备

时间:2018-01-29 07:27:29

标签: android proguard android-proguard

我编写了一个Android应用程序,我想用Proguard生成一个Apk,然后我想混淆代码。我使用Android Studio 3。

在我的build.gradle中,我有:

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

我知道Proguard配置在'proguard-android.txt'和'proguard-rules.pro'里面。 一开始我在构建过程中遇到错误,我用'proguard-rules.pro'解决了这些错误,添加了一些行:

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

之后建筑物没问题,但是当我在设备中运行时,由于混淆而出现错误;我得到的前两个是:

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

然后,在一些日志之后,我得到了

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)

然后应用程序终止。

要解决我尝试的第一个错误:

-keep class android.** { *; }

但它不起作用......

有人可以帮忙吗?

在'proguard-android.txt'中(由Android STudio构建......)我有:

-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>(...);
}

1 个答案:

答案 0 :(得分:0)

我在'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