Proguard功能和配置

时间:2018-10-30 01:59:31

标签: android android-proguard

我对Proguard配置有一些疑问:

  1. 为什么Proguard不会用这些代码混淆我的班级?

Java

public static String encrypt(String keyString, String text) {
    try {
        Cipher cipher = Cipher.getInstance(...);

        ...

        byte[] key = new byte[...];
        ...
        SecretKeySpec keySpec = new SecretKeySpec(key, ...);

        byte[] encrypted = cipher.doFinal(text.getBytes(...));

        return ...

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

build.gradle

buildTypes {
    release {
        minifyEnabled true
        proguardFiles 'proguard-rules.pro'
    }
    debug {
        signingConfig signingConfigs.config
    }
}

Proguard

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-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
-keepclassmembers class **.R$* {public static <fields>;}
-keep class **.R$*
-keepattributes JavaScriptInterface

# If you want to enable optimization, you should include the
# following:
# -optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
# -optimizationpasses 5
# -allowaccessmodification
#
# Note that you cannot just include these flags in your own
# configuration file; if you are including this file, optimization
# will be turned off. You'll need to either edit this file, or
# duplicate the contents of this file and remove the include of this
# file from your project's proguard.config path property.

-keepattributes *Annotation*

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

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

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

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembernames class * implements android.os.Parcelable {
  public static final android.os.Parcelable$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.**

阻止我的代码模糊不清是怎么回事?

  1. 我学到的是,不要使用Proguard混淆活动片段,对吗?

  2. Proguard无法混淆strings.xml,而是将字符串替换为Java代码吗?

1 个答案:

答案 0 :(得分:0)

您应该已经添加了Android SDK提供的默认proguard配置文件proguard-android.txt

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

  

我学到的是,不要使用Proguard混淆“活动”和“片段”,对吗?

是的,应保留这些活动和片段类(使用-keep)作为Proguard的入口点。


  

Proguard不能混淆strings.xml,而是将字符串替换为Java代码吗?

是的,根据https://developer.android.com/studio/build/shrink-code

,Proguard仅会收缩资源,而不会进行资源混淆
  

Gradle的Android插件提供了资源缩减功能,   这会从打包的应用程序中删除未使用的资源,包括   代码库中未使用的资源。它与代码结合使用   缩小以致一旦删除了未使用的代码,任何资源   不再引用也可以安全删除。

从android SDK目录中查看更多详细信息:<your-path>/sdk/tools/proguard