简体   繁体   English

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

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

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? 没有沿着蚂蚁路走下去,任何人都有运气吗?

I also wanted to do this without using Ant or the command line approach. 我也想在不使用Ant或命令行方法的情况下这样做。 Here is what worked (on Eclipse + Windows only): 这是有效的(仅限Eclipse + Windows):

  • (You need to download Proguard. The script expects to find it here: C:\\android-sdk_r04-windows\\proguard\\lib\\proguard.jar). (你需要下载Proguard。脚本希望在这里找到它:C:\\ android-sdk_r04-windows \\ proguard \\ lib \\ proguard.jar)。

  • Create a Windows batch file, "C:\\android-sdk_r04-windows\\obfusc.bat": 创建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." 在Eclipse中,打开Android项目的属性页面,选择“Builders”窗格并添加“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}. 在“工作目录”字段中输入变量$ {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. 确保此构建工具紧随最后一个,就在Android包构建器之前。

  • Create a proguard config file in the root folder of the Android project. 在Android项目的根文件夹中创建proguard配置文件。 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: 我使用的文件在脚本中命名为“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. 您将希望在大多数情况下关闭此Builder。 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. 当需要测试或导出混淆的APK时,将其打开,然后从Eclipse执行“Project> Clean”,包括项目及其依赖的任何库项目。

I think that's about it. 我认为就是这样。

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. 它使用Ant,但所有的硬件都是为你完成的,只要你遵循指令,它就可以了。 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. 注意:不要跳过更新,这只适用于SDK r7 +,对于AVD更新来说,运行eclipse的更新并不是一个坏主意。

And, for anyone adding external jars, setup ProGuards procfg.txt and add: 并且,对于任何添加外部jar的人,请设置ProGuards procfg.txt并添加:

-libraryjars {path}{file_name}.jar -libraryjars {path} {file_name} .jar

If you are using eclipse just uncomment the project.properties line 如果您正在使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM