为Eclipse ADT创建的android项目通过ant添加proguard混淆支持

假设已有ADT创建的android项目HelloAndroid并且已经有可用的proguard混淆器(我用的proguard4.4)。
第一步要做的是为项目添加ant build支持。由于ADT插件并不直接支持proguard等混淆器,所以不能像j2me那样在有功能比较完善的插件(如EclipseME等)的基础上直接一键混淆一键打包,不过好在android sdk本身提供了比较完善的ant编译支持,可以通过类似命令行编译的方式,对android项目的整个编译、生成过程进行自定义,从而能够实现对编译中途生成的java bytecode进行插入proguard混淆操作然后再转android dex码生成apk。
进入命令行模式,并切换到项目目录,执行如下命令为ADT创建的项目添加ant build支持:

d:/android-sdk-windows/tools/android.bat update project -p . -t 3
结果如下:
Updated default.properties
Updated local.properties
Added file D:/Projects/HelloAndroid/build.xml

刷新eclipse项目文件夹,侦测出sdk工具添加的build.xml和local.properties文件。然后,修改项目属性,在Builders栏中新建一个Ant Builder(Ant在较新版本的Eclipse中已经内置,旧版本的话可能需要自己安装相关插件),对其中Buildfile选择生成的build.xml,Base Directory选择项目目录即可。如果希望在编译项目时自动执行ant build就在刚创建builder的地方给ant builder打上勾,不勾的话就得手动执行build.xml进行编译。
现在尝试编译项目的话,会在控制台内显示类似如下的信息:
Buildfile: D:/Projects/HelloAndroid/build.xml
[setup] Project Target: Android 2.1
[setup] API level: 7
help:
[echo] Android Ant Build. Available targets:
[echo] help: Displays this help.
[echo] clean: Removes output files created by other targets.
[echo] compile: Compiles project’s .java files into .class files.
[echo] debug: Builds the application and signs it with a debug key.
[echo] release: Builds the application. The generated apk file must be
[echo] signed before it is published.
[echo] install: Installs/reinstalls the debug package onto a running
[echo] emulator or device.
[echo] If the application was previously installed, the
[echo] signatures must match.
[echo] uninstall: Uninstalls the application from a running emulator or
[echo] device.
BUILD SUCCESSFUL
Total time: 375 milliseconds
这样就基本说明使用ant build的android项目已经ok了,build脚本默认target是help,所以会显示如上信息,修改target为debug或release就可以像无ant时一样编译、生成以及调试了。
接下来看这一部分:

<!-- Execute the Android Setup task that will setup some properties specific to the target, and import the build rules files. The rules file is imported from <SDK>/platforms/<target_platform>/templates/android_rules.xml To customize some build steps for your project: - copy the content of the main node <project> from android_rules.xml - paste it in this build.xml below the <setup /> task. - disable the import by changing the setup task below to <setup import="false" /> This will ensure that the properties are setup correctly but that your customized build steps are used. -->

上面提示的就是我们需要做的,自定义build过程,根据提示copy android_rules.xml里
节点的内容到下,然后修改setup属性,加入import=”false”。
完事后再次执行build.xml的话就已经用自己的build.xml覆盖了默认的整个build过程了。
如果在以上编译过程中控制台提示了编码问题,如中文注释等,可以对target compile中的

<javac encoding="utf-8" target="1.6" debug="true" extdirs="" destdir="${out.classes.absolute.dir}" bootclasspathref="android.target.classpath" verbose="${verbose}" classpath="${extensible.classpath}"> <src path="${source.absolute.dir}" /> <src path="${gen.absolute.dir}" /> <classpath> <fileset dir="${external.libs.absolute.dir}" includes="*.jar" /> </classpath> </javac>

中的encoding值进行修改,如改为GBK。

接下来,添加调用proguard混淆器的build targe:”optimize”

<target name="optimize" depends="compile"> <jar basedir="${out.classes.absolute.dir}" destfile="temp.jar" /> <java jar="E:/android/proguard4.5.1/lib/proguard.jar" fork="true" failonerror="true"> <jvmarg value="-Dmaximum.inlined.code.length=32" /> <arg value="-injars temp.jar" /> <arg value="-outjars optimized.jar" /> <arg value="-libraryjars '${android.jar}'" /> <arg value="-dontpreverify" /> <arg value="-dontoptimize" /> <arg value="-dontusemixedcaseclassnames" /> <arg value="-repackageclasses" /> <arg value="-allowaccessmodification" /> <arg value="-keep public class * extends android.app.Activity" /> <arg value="-keep public class * extends android.app.Service" /> <arg value="-keep public class * extends android.content.BroadcastReceiver" /> <arg value="-keep public class * extends android.content.ContentProvider" /> <arg value="-keep public class * extends android.view.View" /> <arg value="-optimizationpasses 1" /> <arg value="-verbose" /> <arg value="-dontskipnonpubliclibraryclasses" /> <arg value="-dontskipnonpubliclibraryclassmembers" /> </java> <delete file="temp.jar" /> <delete dir="${out.classes.absolute.dir}" /> <mkdir dir="${out.classes.absolute.dir}" /> <unzip src="optimized.jar" mce_src="optimized.jar" dest="${out.classes.absolute.dir}" /> <delete file="optimized.jar" /> </target>

其中需要修改java jar=”D:/Program Files/proguard4.4/lib/proguard.jar”中的proguard路径为对应路径,

<arg value=”-keep public class ............... /> 这部分中的class需要按照manifest中指定的类进行修改,同时,引用的外部lib也需要在这里指定,具体过程可以参考最后的几篇文章。

最后,修改-dex target的depends项,depends=”compile”加入optimize,为:depends=”compile,optimize”
再次执行编译,可看到optimize target的输出如下:
optimize:
[jar] Building jar: D:/Projects/HelloAndroid/temp.jar
[java] ProGuard, version 4.4
[java] Reading input…
[java] Reading program jar [D:/Projects/HelloAndroid/temp.jar]
[java] Reading library jar [D:/android-sdk-windows/platforms/android-2.1/android.jar]
[java] Initializing…
[java] Ignoring unused library classes…
[java] Original number of library classes: 2514
[java] Final number of library classes: 196
[java] Shrinking…
[java] Removing unused program classes and class elements…
[java] Original number of program classes: 13
[java] Final number of program classes: 5
[java] Obfuscating…
[java] Writing output…
[java] Preparing output jar [D:/Projects/HelloAndroid/optimized.jar]
[java] Copying resources from program jar [D:/Projects/HelloAndroid/temp.jar]
[delete] Deleting: D:/Projects/HelloAndroid/temp.jar
[delete] Deleting directory D:/Projects/HelloAndroid/bin/classes
[mkdir] Created dir: D:/Projects/HelloAndroid/bin/classes
[unzip] Expanding: D:/Projects/HelloAndroid/optimized.jar into D:/Projects/HelloAndroid/bin/classes
[delete] Deleting: D:/Projects/HelloAndroid/optimized.jar
表示proguard混淆已成功运作。
PS:如果在以上过程中提示javac不能找到这样类似的问题的话,可能的原因是创建的ant builder中的JRE项没有指定对,可以尝试修改为使用Separate JRE换换ant的JRE试试。

主要参考文章:
为android项目增加支持ant构建 http://marshal.easymorse.com/archives/1665
請為你的 Android 程式加上 obfuscation 吧! http://ysl-paradise.blogspot.com/2008/10/android-obfuscation.html
android混淆(Obfuscate) http://fonter.javaeye.com/blog/489728

 

 

转载:http://blog.k-res.net/?p=711

你可能感兴趣的:(为Eclipse ADT创建的android项目通过ant添加proguard混淆支持)