《Obfuscate an Android application》 这篇文章的作者介绍了如何在Android平台下混淆程序,并且给出ant的build.xml文件,经过自己的测试,发现里面少了一些target项,在Eclips会出错,后来结合zxing开源项目的build.xml文件,终于混淆成功,项目包减少了不少。
(http://fonter.javaeye.com/blog/489728)
这里主要提一些注意事项,不提供源代码
1、创建build.xml文件
直接在项目的目录下新建一个名为build.xml的文件
2、创建Ant打包方式
项目 –> properties –> builders –> new –> ant builder
接着Buildfile选择项目build.xml文件,Base Dir选择项目目录就行了,记得勾上你的builders
3、build.xml文件修改
zxing这个文件没有加入混淆包选项,所以得做一些修改,如下示例
- <property name="proguard-home" value="D://Program Files//proguard4.2//lib//"/>
- <target name="optimize" depends="compile">
- <jar basedir="${outdir-classes}" destfile="temp.jar"/>
- <java jar="${proguard-home}/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="-libraryjars ${library-jar}/some_lib_used.jar"/> -->
- <arg value="-dontpreverify"/>
- <arg value="-dontoptimize"/>
- <arg value="-dontusemixedcaseclassnames"/>
- <arg value="-repackageclasses ''"/>
- <arg value="-allowaccessmodification"/>
- <arg value="-keep public class com.iwidsets.WidsetsActivity"/>
- <!-- <arg value="-keep public class com.just2me.obfapp.receiver.*"/> -->
- <arg value="-optimizationpasses 7"/>
- <arg value="-verbose"/>
- <arg value="-dontskipnonpubliclibraryclasses"/>
- <arg value="-dontskipnonpubliclibraryclassmembers"/>
- </java>
- <delete file="temp.jar"/>
- <delete dir="${outdir-classes}"/>
- <mkdir dir="${outdir-classes}"/>
- <unzip src="optimized.jar" dest="${outdir-classes}"/>
- <delete file="optimized.jar"/>
- </target>
注意:keep public class com.iwidsets.WidsetsActivity需改成你的主Activity
并修改target dex
- <!-- Convert this project's .class files into .dex files. -->
- <target name="dex" depends="compile,optimize">
- <echo>Converting compiled files and external libraries into ${out-folder}/${dex-file}...</echo>
- <apply executable="${dx}" failonerror="true" parallel="true">
- <arg value="--dex" />
- <arg value="--output=${intermediate-dex-location}" />
- <arg path="${outdir-classes-location}" />
- <fileset dir="${external-libs-folder}" includes="*.jar"/>
- </apply>
- </target>
There is no easy way to obfuscate Java classes of an Android app. This is no Eclipse plugin, nor even a working ant script.
Maybe there are working ant scripts. But for Android SDK 1.5, it seems like no one has supplied a proper one, yet. And hence, I spent half a day hacking out a script that works. There are more effort and hurdles than what are presented here.. but for a simple working script, here it is how I did it:
1 Create a Project
From the Terminal, in /android-sdk/tools/, run:
./android create project –target 2 –path /PATH/TO/ObfuscatedApp –activity MyActivity –package com.just2me.obfapp
2 Edit build.xml
Replace the build.xml with my modified build.xml, which is the most important item in this tutorial. The obfuscation work is in the optimize target as seen below.
Some points to note in the build.xml ant script:
- Edit the properties for the path to your Android SDK and Proguard
- If you use any libraries, copy them to /lib and add -libraryjars ${library-jar}/some_lib_used.jar accordingly
- -dontoptimize is needed for Android
- For all classes that are declared in AndroidManifest.xml Application nodes (Activities, Receviers, etc), add an arg to tell proguard not to obfuscate. Exampe: "-keep public class com.just2me.obfapp.activity.*"
3 Run ant
The last step is to simply run the ant script!
To package a debug version that is signed with a debug key, run "ant debug-obf".
To package a release version that is unsigned, run "ant release-obf".
If all is well, then you will find the products in /bin. ObfuscatedApp-debug.apk would be the debug package that could be installed on simulator/devices.