【问题标题】:How to configure Proguard in Android Studio - got errors running in device如何在 Android Studio 中配置 Proguard - 在设备中运行时出错
【发布时间】:2018-02-07 16:12:54
【问题描述】:

我编写了一个 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>(...);
}

【问题讨论】:

  • 您是否已经尝试清理项目?
  • 您也可以尝试参考这个错误,这可能不是proguard的原因:stackoverflow.com/questions/42378473/…
  • 嗨,是的,我已经清理过了,但为了安全起见,我再次尝试。关于第二条评论,肯定是混淆了,因为如果我不使用 Proguard (minify disabled),它可以正常运行。
  • 您在使用任何第三方库吗?他们经常列出正常工作所需的其他 proguard 规则
  • 是的,我有几个第三方库。例如,如果我有库 com.a_library.xxx,我如何才能完全排除对该库的混淆?

标签: android proguard android-proguard


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2015-01-02
    相关资源
    最近更新 更多