【问题标题】:Android proguard doesn't obfuscate classesAndroid proguard 不会混淆类
【发布时间】:2016-12-09 11:08:59
【问题描述】:

我已经使用 proguard 构建了一个发布的 Android 应用程序,但是在反编译 APK 后,我看到必须混淆的类具有正常的来源。我的 gradle build 有一部分:

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

这是我的 proguard-rules.pro 文件:

-dontpreverify
-repackageclasses ''
-allowaccessmodification
-keepattributes *Annotation*

-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.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 * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

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

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

-keepnames class * implements java.io.Serializable

-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>;
    !private <fields>;
    !private <methods>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

-assumenosideeffects class android.util.Log {
    public static *** e(...);
    public static *** w(...);
    public static *** wtf(...);
    public static *** d(...);
    public static *** v(...);
}

-keepclasseswithmembernames class * {
    native <methods>;
}
-keepclassmembers class * {
    public void *ButtonClicked(android.view.View);
}

-dontwarn okio**
-dontwarn java.lang.invoke**
-dontwarn org.apache.commons.io**
-dontwarn org.codehaus**
-keep public class java.nio**
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
-keep class net.sqlcipher.** { *; }

-keep class net.sqlcipher.database.** { *; }

-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keep class com.google.gson.** { *; }
-keep class com.google.inject.* { *; }
-keep class org.apache.http.** { *; }
-keep class org.apache.james.mime4j.** { *; }
-keep class javax.inject.** { *; }
-keepclassmembernames interface * {
    @retrofit.http.* <methods>;
}

-keep class sun.misc.Unsafe { *; }
-keep public class !com.vidal.cardio.datas.MySQLCipherOpenHelper { *; }
-keep public class !com.vidal.cardio.datas.SCLcipherOpenHelper { *; }

嗯,我预计 MySQLCipherOpenHelperSCLcipherOpenHelper 会被混淆,但实际上他们没有。也许 proguard-rules.pro 有错误?

【问题讨论】:

标签: android proguard android-proguard


【解决方案1】:

Keep规则会相互独立地进行分析和处理,所以如果你这样写规则

-keep public class !com.vidal.cardio.datas.MySQLCipherOpenHelper { *; }
-keep public class !com.vidal.cardio.datas.SCLcipherOpenHelper { *; }

ProGuard 将执行以下操作:

  • 处理第一条规则以保留除 MySQLCipherOpenHelper 之外的所有内容
  • 处理第二条规则以保留除 SCLcipherOpenHelper 之外的所有内容

如您所见,使用第一条规则,您还隐含地保留了第二类,而使用第二条规则,您也保留了第一类。

为了不同时保留它们,您必须像这样合并规则:

-keep public class !com.vidal.cardio.datas.MySQLCipherOpenHelper, 
                   !com.vidal.cardio.datas.SCLcipherOpenHelper { *; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-28
    • 2013-02-09
    • 2016-07-04
    • 2016-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多