如何告诉 ProGuard 将所有内容保存在特定包中?

发布于 2024-12-05 08:39:58 字数 211 浏览 0 评论 0原文

我的应用程序有很多活动,也使用本机库。使用 Eclipse 生成的默认 ProGuard 配置,ProGuard 会删除很多东西 - OnClick 方法、静态成员、我的本机库使用的回调方法...是否有一种简单的方法来指示 ProGuard 不要从我的包中删除任何内容?删除一些东西只能节省大约 2.5% 的应用程序大小,但会完全破坏我的应用程序。在 ProGuard 配置中逐类配置、测试和维护它会很痛苦。

My application has many activities and uses native library too. With the default ProGuard configuration which Eclipse generates ProGuard removes many things - OnClick methods, static members, callback methods which my native library uses... Is it there a simple way to instruct ProGuard to NOT remove anything from my package? Removing things saves only about 2.5% of the application size, but breaks my application completely. Configuring, testing and maintaining it class by class in ProGuard configuration would be a pain.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺ぐ爱和你が 2024-12-12 08:39:58

编辑 这个答案已经有 10 年历史了 - 它可能不适用于较新的 proguard 版本。

我认为你至少需要添加这些标志(修改你个人的包名称):

-keep class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }

另外,添加这些标志:

-dontshrink
-dontoptimize
-dontpreverify

这是我的整个配置文件:我的 Proguard.cfg:

-dontshrink
-dontoptimize
-dontpreverify
-verbose

-dontwarn javax.management.**
-dontwarn java.lang.management.**
-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.slf4j.**
-dontwarn org.json.**


-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.android.vending.licensing.ILicensingService
-keep class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }

-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 *;
}

EDIT This answer is 10 years old - it may not apply to newer proguard versions.

I think you need to add these flags at the very least (modify for you individual package names):

-keep class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }

Also, add these flags:

-dontshrink
-dontoptimize
-dontpreverify

Here's my whole config file: of my Proguard.cfg:

-dontshrink
-dontoptimize
-dontpreverify
-verbose

-dontwarn javax.management.**
-dontwarn java.lang.management.**
-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.slf4j.**
-dontwarn org.json.**


-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.android.vending.licensing.ILicensingService
-keep class javax.** { *; }
-keep class org.** { *; }
-keep class twitter4j.** { *; }

-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 *;
}
稀香 2024-12-12 08:39:58

作为最终结果,我发现仅保留所有类成员对于我的应用程序的正确工作来说是不够的,也没有必要。我在设置文件中添加了以下内容:

-keepclasseswithmembers class * {
    void onClick*(...);
}
-keepclasseswithmembers class * {
    *** *Callback(...);
}

onClick* 的情况适用于我在 .xml 布局文件中的 android:onClick 属性中处理的所有方法(我以“onClick”开头所有此类方法的名称)。

*Callback 的情况适用于我从本机代码(通过 JNI)调用的所有回调方法。我在每个此类方法的名称中添加后缀“Callback”。

此外,我还为一些特殊情况添加了几行,这些变量是我从本机代码中使用的变量,例如:(

-keep class com.myapp.mypackage.SomeMyClass {
    *** position;
}

对于来自包 com.myapp.mypackage 中名称为“SomeMyClass”的类中名称为“position”的变量)

所有这些都是因为这些onClick、callback等不仅必须存在,而且要保留原来的名称。其他的东西 ProGuard 可以自由优化。

本机方法的情况也很重要,但在 Eclipse 文件生成的文件中有一个声明:

-keepclasseswithmembernames class * {
    native <methods>;
}

As final result I found that just keeping all class members is not enough for the correct work of my application, nor necessary. I addded to the settings file this:

-keepclasseswithmembers class * {
    void onClick*(...);
}
-keepclasseswithmembers class * {
    *** *Callback(...);
}

The case with onClick* is for all methods which I address in android:onClick atribute in .xml layout files (I begin the names of all such methods with 'onClick').

The case with *Callback is for all callback methods which I call from my native code (through JNI). I place a suffix 'Callback' to the name of every such method.

Also I added few rows for some special cases with variables which I use from native code, like:

-keep class com.myapp.mypackage.SomeMyClass {
    *** position;
}

(for a varible with name 'position' in a class with name 'SomeMyClass' from package com.myapp.mypackage)

All this is because these onClick, callback etc. must not only be present but also kept with their original names. The other things ProGuard can optimize freely.

The case with the native methods is important also, but for it there was a declaration in the generated from Eclipse file:

-keepclasseswithmembernames class * {
    native <methods>;
}
平定天下 2024-12-12 08:39:58

我知道这是一个老问题,但我希望以下信息可以帮助其他人。

您可以阻止 ProGuard 删除某个包中的任何内容,如下所示;

-keep,allowoptimization,allowobfuscation class com.example.mypackage.** { *; }

使用修饰符 allowoptimizationallowobfuscation 将确保 ProGuard 仍然对代码进行混淆和优化。收缩当然会按预期被禁用。

您可以轻松验证这些 -keep 规则如何影响代码,而无需使用 ProGuard 游乐场

I know this is an old question but I hope that the following information might help other people.

You can prevent ProGuard from removing anything in a certain package as follows;

-keep,allowoptimization,allowobfuscation class com.example.mypackage.** { *; }

Using the modifiers allowoptimization and allowobfuscation will make sure that ProGuard still obfuscates and optimizes the code. Shrinking will of course be disabled as intended.

You can easily verify how these -keep rules affect the code without the need to (re-)build using the ProGuard Playground.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文