【问题标题】:Enabling ProGuard in Eclipse for Android在 Eclipse for Android 中启用 ProGuard
【发布时间】:2011-06-11 14:02:33
【问题描述】:

ProGuard for Android 上的新文档说要在项目主目录的 default.properties 文件中添加一行。但是,在打开此文件时,我在顶部阅读:

# This file is automatically generated by Android Tools. 
# Do not modify this file -- YOUR CHANGES WILL BE ERASED! 

我错过了什么吗?

另外,有没有办法只为来自 Eclipse 的生产构建启用 ProGuard(即,在导出成品时)?

【问题讨论】:

  • 我同意你的观点,每次都会重新生成 default.properties。因此,这是一个有趣的问题
  • 你应该接受 ligi 的回答,NeTeInStEiN 不再成立,让新用户感到困惑。
  • 我已将答案更改为最新的。
  • 对于较新的安装,ligi 的答案仍然比 neteinstein 的更清晰。最重要的是,如果您需要针对特定​​项目的自定义设置,它会显示 proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

标签: android eclipse proguard obfuscation


【解决方案1】:

Android SDK(r20 或更高版本)

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

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

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

在 Gradle 上:

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

            ...
  }
 }

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


Android SDK(r19 或更低版本)

您可以将其添加到 default.properties。到目前为止,我一直在手动添加,没有遇到任何问题。

如果添加行:

proguard.config=proguard.cfg

如前所述,它只会在导出签名应用程序时使用 ProGuard(Android 工具 => 导出签名应用程序)

如果您在 Android 2.3 之前使用 SDK 启动项目,则不会创建 proguard.cfg 文件(在 default.properties 旁边,如 2.3>)。

要启用自动创建,只需更新到 Android 2.3 的 SDK 并使用现有源(即您当前拥有的项目的源)创建一个新项目。

将自动创建proguard.cfg 填充。

如果仍然存在,您想手动创建它,这是它应该包含的内容:

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontwarn android.support.**
-verbose

-dontoptimize
-dontpreverify


-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 com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment

-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 public class * extends android.view.View {
  void set*(***);
  *** get*();
 }

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

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

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

我想我已经回答了以上所有问题。

更新

逐行解释:

#Use 5 step of optimization 
#-optimizationpasses 5

#When not preverifing in a case-insensitive filing system, such as Windows. This tool will unpack your processed jars,(if using windows you should then use):
-dontusemixedcaseclassnames

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

# 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

-dontwarn android.support.**

#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/*

# 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.

#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 * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
#Compatibility library 
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment

#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.**

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

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

#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:
-keep public class * extends android.view.View {
    public <init>(android.content.Context);
}
-keep public class * extends android.view.View {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keep public class * extends android.view.View {
    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();
#}

更新 2:

在最近的 ADT/Proguard 中使用 -keepclasseswithmembers 而不是 -keepclasseswithmembernames

【讨论】:

  • @NeTeInStEiN 我更新到 SDK 16 (Android 4.x),添加了行 proguard.config=proguard.cfg 但 proguard.cfg 文件无处可见......尽管多次导出,但重新启动Eclipse 等。知道为什么吗?以及如何解决这个问题?谢谢。
  • @NeTeInStEiN 没关系。事实证明,我应该自己创建一个。
  • @NeTeInStEiN 在我创建项目时,虽然它的构建目标是 Android1.1 ,但我发现 proguard.cfg 文件是自动创建的。
  • @NeTeInStEiN 了不起的人...非常感谢您的时间和努力,干杯!!
  • @user31231234124 添加了您要求的信息。
【解决方案2】:

只是一个后续,因为我正在寻找同样的东西 - 这里的答案已经过时 - 最近基本的 proguard 配置在 sdk 目录中 - 所以你只需将它放入你的 project.properties:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

如果您想进行特定于项目的修改,请创建一个 proguard-project.txt 并将该行更改为:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 

【讨论】:

  • 查看我的项目(在 r20 中,但在以前的版本中生成)它似乎使用了上述方法的混合:
  • 还是挺混乱的,因为project.properties还说#这个文件是Android Tools自动生成的。 # 不要修改此文件——您的更改将被删除!
  • "你只需要把它放到你的 project.properties 中"。此行将在 project.properties 中,但默认情况下已注释。只需取消注释即可。
【解决方案3】:

至少从 ADT 16 开始,您确实可以在 project.properties 中添加该行,它将被保留。您可以尝试更改目标 SDK 版本,并看到 project.properties 已相应更新,但添加的行仍然存在。所以,我认为警告只是措辞不当;这意味着文件中的设置(例如target)将被项目设置覆盖,而不是相反。

【讨论】:

    【解决方案4】:

    ProGuard 配置的更改来自 ADT 版本 17。ProGuard 从 4.4 更新到 4.7,并引入了配置文件参考中的差异。请注意,现有项目将保持不变,使它们没有包含在此和较新 ADT 版本中的较新规则集。 上面ligi 已经指出的较新配置安排的相关文档可在以下位置获得:-

    http://tools.android.com/recent/proguardimprovements "其次,我们改变了配置文件的处理方式。"

    【讨论】:

      【解决方案5】:

      您可以将该行添加到build.properties,如default.properties 中所述。

      【讨论】:

      • build.properties 在哪里?还是我需要创建它?
      • 它在项目目录中,在 default.properties 旁边(至少对于 Android SDK r8)。
      • 这是在哪里寻找它,但在我的任何项目中都没有这样的文件。我正在使用最新的插件,刚刚创建了一个 8 级项目来检查这一点。
      • 事实证明,使用 build.properties 仅适用于 Ant 构建,不适用于 Eclipse 构建。
      • 2016 android studio buld 中的 build.properties 在哪里?
      猜你喜欢
      • 2011-05-24
      • 2023-04-04
      • 1970-01-01
      • 2011-03-17
      • 2016-12-30
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多