eclipse 在 Android 上使用 ProGuard

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11246842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:25:21  来源:igfitidea点击:

Using ProGuard with Android

javaandroideclipseobfuscationproguard

提问by flawyte

I have problems dealing with ProGuard and Android.

我在处理 ProGuard 和 Android 时遇到问题。

I searched on the web for hours and found multiple ways to obsfuscate an Android application. For now I'm trying one that looks to be the easiest, so :

我在网上搜索了几个小时,发现了多种混淆 Android 应用程序的方法。现在我正在尝试一种看起来最简单的方法,所以:

  • I created a config.cfgfile in the root directory of my project
  • I added proguard.config=config.cfgin my project.properties
  • The I used the Eclipse export wizard to export & sign the .apk file
  • 我在config.cfg我的项目的根目录中创建了一个文件
  • proguard.config=config.cfg在我的project.properties
  • 我使用 Eclipse 导出向导来导出和签署 .apk 文件

I got a message saying Proguard returned with error code 1. See consoleand in the console:

Proguard returned with error code 1. See console在控制台中收到一条消息:

Proguard returned with error code 1. See console
Note: there were 3847 duplicate class definitions.
Warning: com.fasterxml.Hymanson.databind.ext.DOMSerializer: can't find referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
Warning: com.fasterxml.Hymanson.databind.ext.DOMSerializer: can't find referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
Warning: com.fasterxml.Hymanson.databind.ext.DOMSerializer: can't find referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
Warning: com.fasterxml.Hymanson.databind.ext.DOMSerializer: can't find referenced class org.w3c.dom.bootstrap.DOMImplementationRegistry
      You should check if you need to specify additional program jars.
Warning: there were 4 unresolved references to classes or interfaces.
         You may need to specify additional library jars (using '-libraryjars').
Error: Please correct the above warnings first.

Even if I use the config provided by the official Proguard website, or the default one I have errors.
Here is my custom config (myProject/config.cfg):

即使我使用 Proguard 官方网站提供的配置,或者默认配置我也有错误。
这是我的自定义配置 (myProject/config.cfg):

-injars      bin/classes
-injars      libs
-libraryjars "C:\Program Files\Android\android-sdk\platforms\android-13\android.jar"

-dontskipnonpubliclibraryclasses
-optimizationpasses 5
-printmapping map.txt
-flattenpackagehierarchy
-dontpreverify
-repackageclasses ''
-allowaccessmodification
-optimizations !code/simplification/arithmetic
-keepattributes *Annotation*

-keep public class * extends android.app.Application
-keep public class * extends android.app.Activity
-keep public class * extends android.app.PreferenceActivity
-keep public class * extends android.view.View
-keep public class * extends android.widget.BaseAdapter
-keep public class * implements android.view.View.OnTouchListener

-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 * extends android.content.Context {
   public void *(android.view.View);
   public void *(android.view.MenuItem);
}

-keepclassmembers class * implements android.os.Parcelable {
    static android.os.Parcelable$Creator CREATOR;
}

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

I can't get it to work... any idea would be greatly appreciated !

NOTE :I use the Hymanson JSON librairies that are stored in the libsfolder of my project

我无法让它工作......任何想法将不胜感激!

注意:我使用存储在libs我的项目文件夹中的 Hymanson JSON 库

回答by Eric Lafortune

1) ProGuard manual > Troubleshooting > Note: duplicate definition of program/library class

1) ProGuard 手册 > 故障排除 > 注意:程序/库类的重复定义

The Android Ant/Eclipse builds already specify -injars/-libraryjars for you. If you specify them again in your configuration, ProGuard notes that they are duplicated. So don't specify -injars/-libraryjars.

Android Ant/Eclipse 构建已经为您指定了 -injars/-libraryjars。如果您在配置中再次指定它们,ProGuard 会注意到它们是重复的。所以不要指定-injars/-libraryjars。

2) ProGuard manual > Troubleshooting > Warning: can't find referenced class

2) ProGuard 手册 > 故障排除 > 警告:找不到引用的类

org.w3c.dom.bootstrap.DOMImplementationRegistry is not present in the input code, yet com.fasterxml.Hymanson.databind.ext.DOMSerializer is using it. If your application works anyway, you can let ProGuard accept it with:

org.w3c.dom.bootstrap.DOMImplementationRegistry 不存在于输入代码中,但 com.fasterxml.Hymanson.databind.ext.DOMSerializer 正在使用它。如果您的应用程序仍然有效,您可以让 ProGuard 接受它:

-dontwarn org.w3c.dom.bootstrap.DOMImplementationRegistry

回答by Androiderson

The ProGuard configuration file provided here:

此处提供的 ProGuard 配置文件:

https://groups.google.com/forum/#!msg/robospice/xGLRbGkLwQU/cAkaUQF34RsJ

https://groups.google.com/forum/#!msg/robospice/xGLRbGkLwQU/cAkaUQF34RsJ

Solve my problem!

解决我的问题!

回答by Julien R.

I just got stuck with the same problem, and I solved it by referencing the JRE rt.jar file:

我刚遇到同样的问题,我通过引用 JRE rt.jar 文件解决了它:

-libraryjars /lib/rt.jar

-libraryjars /lib/rt.jar

I hope it helps

我希望它有帮助