首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android: Proguard的推荐配置是什么?

Android: Proguard的推荐配置是什么?
EN

Stack Overflow用户
提问于 2011-02-22 00:09:05
回答 3查看 58.9K关注 0票数 65

我正在为Android开发应用程序,并使用Proguard来混淆代码。

目前我使用的是ProGuard配置:

代码语言:javascript
复制
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-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 com.android.vending.licensing.ILicensingService

要维护布局XML中使用的自定义组件名称:

代码语言:javascript
复制
-keep public class custom.components.**

要删除调试日志:

代码语言:javascript
复制
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

为了避免更改在布局的onClick上调用的方法的名称:

代码语言:javascript
复制
-keepclassmembers class * {
 public void onClickButton1(android.view.View);
 public void onClickButton2(android.view.View);
 public void onClickButton3(android.view.View);
}

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

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

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

问题是:

还有没有其他推荐的标签?为什么?为什么?

可以对proguard.cfg文件进行注释吗?我希望有一些行正在做的评论,这样其他开发人员就不会对我添加的原因产生怀疑。

在proguard中,是否可以维护文件的注释头(带有版权)?如果不是,或者这不是一个好的政策,我应该在哪里添加版权?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-27 19:57:31

Android SDK (r20或更高版本)

请检查project.properties中引用的预定义proguard.config

代码语言:javascript
复制
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

更多信息:http://proguard.sourceforge.net/manual/examples.html#androidapplication

在这里,您可以查看我不断更新的proguard“默认”文件https://medium.com/code-procedure-and-rants/android-my-standard-proguard-ffeceaf65521

Android SDK (r19或更低版本)

根据我的回答Enabling ProGuard in Eclipse for Android,我最终得到了这个通用文件。我添加了注释,以记住每一行的用途。它可能会对外面的人有所帮助,所以这里就是:

代码语言:javascript
复制
-optimizationpasses 5

#When not preverifing in a case-insensitive filing system, such as Windows. Because this tool unpacks your processed jars, you should then use:
-dontusemixedcaseclassnames

#Specifies not to ignore non-public library classes. As of version 4.5, this is the default setting
-dontskipnonpubliclibraryclasses

#Preverification is irrelevant for the dex compiler and the Dalvik VM, so we can switch it off with the -dontpreverify option.
-dontpreverify

#Specifies to write out some more information during processing. If the program terminates with an exception, this option will print out the entire stack trace, instead of just the exception message.
-verbose

#The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle. Note that the Dalvik VM also can't handle aggressive overloading (of static fields).
#To understand or change this check http://proguard.sourceforge.net/index.html#/manual/optimizations.html
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

#To repackage classes on a single package
#-repackageclasses ''

#Uncomment if using annotations to keep them.
#-keepattributes *Annotation*

#Keep classes that are referenced on the AndroidManifest
-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 com.android.vending.licensing.ILicensingService


#To remove debug logs:
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
}

#To avoid changing names of methods invoked on layout's onClick.
# Uncomment and add specific method names if using onClick on layouts
#-keepclassmembers class * {
# public void onClickButton(android.view.View);
#}

#Maintain java native methods 
-keepclasseswithmembernames class * {
    native <methods>;
}

#To maintain custom components names that are used on layouts XML.
#Uncomment if having any problem with the approach below
#-keep public class custom.components.package.and.name.**

#To maintain custom components names that are used on layouts XML:
-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);
}

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

#To keep parcelable classes (to serialize - deserialize objects to sent through Intents)
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

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

###### ADDITIONAL OPTIONS NOT USED NORMALLY

#To keep callback calls. Uncomment if using any
#http://proguard.sourceforge.net/index.html#/manual/examples.html#callback
#-keep class mypackage.MyCallbackClass {
#   void myCallbackMethod(java.lang.String);
#}

#Uncomment if using Serializable 
#-keepclassmembers class * implements java.io.Serializable {
#    private static final java.io.ObjectStreamField[] serialPersistentFields;
#    private void writeObject(java.io.ObjectOutputStream);
#    private void readObject(java.io.ObjectInputStream);
#    java.lang.Object writeReplace();
#    java.lang.Object readResolve();
#}
票数 83
EN

Stack Overflow用户

发布于 2011-02-22 07:07:28

对于使用Ant或Eclipse构建的标准,Android SDK (r20或更高版本)已经提供了合适的配置,由文件project.properties中的属性proguard.config引用(正如Michal在他的回答中指出的那样)。在较旧的版本中,您总是必须指定自己的完整配置,因此较旧的建议(如第一个答案)可能会导致一些混乱。

对于自定义构建,您可以在ProGuard manual > Examples > A complete Android application中找到最新的推荐配置。

值得注意的是,您可能希望保留视图扩展中的注释和setter。

您可以在散列字符'#‘后添加注释,就像示例文件中所做的那样。

票数 10
EN

Stack Overflow用户

发布于 2013-03-21 06:11:16

在名为project.properties,的项目文件中设置以下值:

代码语言:javascript
复制
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

这将从Android SDK获取最新版本的Proguard设置。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5068251

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档