导出已签名的混淆 Android 应用程序时,如何在 Eclipse 中设置 ProGuard?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3762678/
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 15:07:51  来源:igfitidea点击:

How to set up ProGuard in Eclipse when exporting a signed, obfuscated Android application?

androideclipseobfuscationproguard

提问by rndStr

I'm publishing an Android application developed in Eclipse and, as stated in the title, I would like to integrate Proguard obfuscation into the build, specifically for exporting a signed app.

我正在发布一个在 Eclipse 中开发的 Android 应用程序,正如标题中所述,我想将 Proguard 混淆集成到构建中,专门用于导出已签名的应用程序。

Anyone had any luck without going down the ant path?

没有走蚂蚁之路的人有运气吗?

回答by x-code

I also wanted to do this without using Ant or the command line approach. Here is what worked (on Eclipse + Windows only):

我也想在不使用 Ant 或命令行方法的情况下做到这一点。这是有效的(仅在 Eclipse + Windows 上):

  • (You need to download Proguard. The script expects to find it here: C:\android-sdk_r04-windows\proguard\lib\proguard.jar).

  • Create a Windows batch file, "C:\android-sdk_r04-windows\obfusc.bat":

  • (您需要下载 Proguard。脚本希望在此处找到它:C:\android-sdk_r04-windows\proguard\lib\proguard.jar)。

  • 创建一个 Windows 批处理文件,“C:\android-sdk_r04-windows\obfusc.bat”:


    DEL /S /Q obfuscated
    MKDIR obfuscated

    java -jar C:\android-sdk_r04-windows\proguard\lib\proguard.jar @android.pro

    DEL /S /Q bin\com\
    DEL /S /Q bin\org\

    ROBOCOPY obfuscated\com bin\com /S
  • In Eclipse, bring up the properties page of your Android project, select the "Builders" pane and add a new builder of type "Program." In the "Location" field of the main tab put the absolute path of the script in the previous step. In the "Working directory" field put the variable ${build_project}. In the "Build options" tab select "After a clean" under "Run builder."

  • Make sure this build tool comes next to last, just before the Android package builder.

  • Create a proguard config file in the root folder of the Android project. I customize these slightly and include them in revision control, but that's up to you. The file I use is called "android.pro," as named in the script, and is similar to the config in the dev blog but includes a header with injar, outjar, and libraryjar statements, for example:

  • 在 Eclipse 中,打开您的 Android 项目的属性页面,选择“Builders”窗格并添加一个类型为“Program”的新构建器。在主选项卡的“位置”字段中放置上一步中脚本的绝对路径。在“工作目录”字段中放置变量 ${build_project}。在“构建选项”选项卡中,选择“运行构建器”下的“清理后”。

  • 确保此构建工具紧随其后,就在 Android 包构建器之前。

  • 在 Android 项目的根文件夹中创建一个 proguard 配置文件。我稍微定制了这些并将它们包含在修订控制中,但这取决于您。我使用的文件名为“android.pro”,如脚本中所命名,与开发博客中的配置类似,但包含一个包含 injar、outjar 和 libraryjar 语句的标头,例如:


    -injars      bin(!.svn/**)
    -outjars     obfuscated
    -libraryjars C:\android-sdk_r04-windows\android-sdk-windows\platforms\android-1.6\android.jar
    -libraryjars C:\GoogleAnalyticsAndroid_0.7\libGoogleAnalytics.jar

    -optimizationpasses 5
    -dontusemixedcaseclassnames
    -dontskipnonpubliclibraryclasses
    -dontpreverify
    -verbose
    -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

    -printmapping proguard.map
    -keepattributes SourceFile,LineNumberTable

    -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

    -keepclasseswithmembernames class * {
        native ;
    }

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

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

    -keepclassmembers enum * {
        public static **[] values();
        public static ** valueOf(java.lang.String);
    }
  • You will want to keep this Builder turned off most of the time. When it is time to test or export an obfuscated APK, turn it on and then do a "Project > Clean" from Eclipse, including the project and any library projects it depends on.
  • 大多数情况下,您会希望此 Builder 处于关闭状态。当需要测试或导出混淆的 APK 时,将其打开,然后从 Eclipse 执行“项目 > 清理”,包括项目及其依赖的任何库项目。

I think that's about it.

我想就是这样。

回答by rndStr

Ok, this is the most relevant and recent post on the topic:

好的,这是关于该主题的最相关和最近的帖子:

http://android-developers.blogspot.com/2010/09/proguard-android-and-licensing-server.html

http://android-developers.blogspot.com/2010/09/proguard-android-and-licensing-server.html

It dose use Ant, but all the hard parts are done for you, and as long as you follow the instruction, it works. A note: don't skip the update this only work with SDK r7+, and it's not a bad idea to run eclipse's update, for the AVD update too.

它使用Ant,但所有困难的部分都为您完成,只要您按照说明进行操作,它就可以工作。注意:不要跳过仅适用于 SDK r7+ 的更新,并且运行 eclipse 的更新也不错,对于 AVD 更新也是如此。

And, for anyone adding external jars, setup ProGuards procfg.txt and add:

而且,对于任何添加外部 jars 的人,设置 ProGuards procfg.txt 并添加:

-libraryjars {path}{file_name}.jar

-libraryjars {path}{file_name}.jar

回答by musterjunk

If you are using eclipse just uncomment the project.propertiesline

如果您使用的是 eclipse,只需取消注释该project.properties

# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt