当前位置: 代码迷 >> 综合 >> ant script to obfuscate android code
  详细解决方案

ant script to obfuscate android code

热度:35   发布时间:2024-01-15 18:16:10.0

 

最近正在做CHINA区的build machine. J2ME已经开始大规模使用,公司最近要支持OPHONE手机,于是也把ANDROID支持到buildmachine里去。

 

 

android有自己的anttask.jar,没有SDK源码,于是想反编译anttasks.jar来获得,结果反编译后有些问题。所以只得自己硬着头皮写一个了。

 

网上有一哥们写了一个,不过拿下来有几个target没有,比如package-resource, 所以自己写了个完整的。 代码混淆也加上了。

 

<!--

Author: haibo.liu@glu.com
refer to http://just2us.com/2009/07/tutorial-obfuscate-an-android-application/

-->
<project name="myproject" default="debug-obf" basedir=".">

<property name="version" value="1_0_5" />

<property name="JDK_HOME" value="C:/Program Files/Java/jdk1.6.0_01" />
<property name="rootdir" value="./" />
<property name="outdir" value="./bin" />
<property name="outdir-classes" value="${outdir}/classes" />

<property name="outdir-compile-src" value="${outdir}/srctemp" />

<property name="sdk.dir" value="C:/tools/android-sdk21-windows" />
<property name="sdk.platform" value="${sdk.dir}/platforms/android-5" />
<property name="tool-aapt" value="${sdk.platform}/tools/aapt.exe" />
<property name="tool-dx" value="${sdk.dir}/tools/dx.bat" />
<property name="tool-apkbuilder" value="${sdk.dir}/tools/apkbuilder.bat" />
<property name="tool-jarsigner" value="${JDK_HOME}/bin/jarsigner.exe" />



<property name="pkres" value="${outdir}/${ant.project.name}.ap_" />
<property name="out-apk" value="C:/development/games/myproject/bin/${ant.project.name}_unalign.apk" />
<property name="outtemp-apk" value="C:/development/games/myproject/bin/${ant.project.name}_temp.apk" />

<property name="signed-apk" value="C:/development/games/myproject/bin/${ant.project.name}_${version}.apk" />


<property name="proguardlib" value="./tools/proguard-4.1.jar" />

<property name="library-jar" value="./libs" />
<property name="src.dir" value="./src" />
<property name="resource.dir" value="./res" />
<property name="externallibs" value="./libs" />
<property name="intermediate-dex-location" value="C:/development/games/myproject/bin/classes.dex" />
<property name="android-jar" value="${sdk.dir}/platforms/android-5/android.jar" />


<property file="local.properties" />
<property file="build.properties" />
<property file="default.properties" />
<path id="android.antlibs">
<pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
<pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
<pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
<pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
<pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
</path>
<taskdef name="setup" classname="com.android.ant.SetupTask" classpathref="android.antlibs" />
<setup />

<target name="package-resource" depends="clean">

<mkdir dir="${outdir}/classes"/>
<mkdir dir="${rootdir}/gen"/>
<exec executable="${tool-aapt}">
<arg line="package" />
<arg line="-f" />
<arg line="-m" />
<arg line="-J ${rootdir}/gen " />
<arg line="-M AndroidManifest.xml" />
<arg line="-S res" />
<arg line="-I ${android-jar}" />
<arg line="-F ${pkres}" />
</exec>
</target>


<target name="compile" depends="package-resource">

<mkdir dir="${outdir-compile-src}"/>

<copy todir="${outdir-compile-src}" overwrite="true">
<fileset dir="${src.dir}">
<exclude name="*svn" />
<include name="**/*.java" />
</fileset>
</copy>

<copy todir="${outdir-compile-src}" overwrite="true">
<fileset dir="${rootdir}/gen">
<exclude name="*svn" />
<include name="**/*.java" />
</fileset>
</copy>


<javac srcdir="${outdir-compile-src}" destdir="${outdir}/classes" debug="off" debuglevel="none" target="1.5" source="1.5">
<bootclasspath path="${android-jar}" />

</javac>
</target>


<target name="debug-obf" depends="dex-obf">

<exec executable="${tool-apkbuilder}">
<arg line="${out-apk}" />
<arg line="-u" />
<arg line="-z C:/development/games/myproject/${pkres}" />
<arg line="-f ${intermediate-dex-location}" />
</exec>
</target>

<target name="release-obf" depends="dex-obf">

<exec executable="${tool-apkbuilder}">
<arg line="${out-apk}" />
<arg line="-u" />
<arg line="-z C:/development/games/myproject/${pkres}" />
<arg line="-f ${intermediate-dex-location}" />
</exec>

</target>


<target name="optimize" depends="compile">
<jar basedir="${outdir-classes}" destfile="temp.jar" />

<java jar="${proguardlib}" fork="true" failοnerrοr="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="-printmapping map.txt" />

<arg value="-keep public class com.seafit.android.**" />


<arg value="-optimizationpasses 1" />
<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>
<!-- Convert into .dex -->

<target name="dex-obf" depends="optimize">


<apply executable="${dx}" failοnerrοr="true" parallel="true">
<arg value="--dex" />
<arg value="--output=${intermediate-dex-location}" />
<arg path="${outdir-classes}" />
<fileset dir="${externallibs}" includes="*.jar" />
</apply>
</target>


<target name="signapk" depends="release-obf">

<copy file="${out-apk}" tofile="${outtemp-apk}" />

<exec executable="${tool-jarsigner}">
<arg line="-verbose" />
<arg line="-keystore ./key/.keystore" />
<arg line="-storepass spss" />
<arg line="-keypass android" />
<arg line="${outtemp-apk} android" />
</exec>

<exec executable="${tool-jarsigner}">
<arg line="-verify -certs ${outtemp-apk}" />


</exec>

<copy file="${outtemp-apk}" tofile="${signed-apk}" />
<delete file="${outtemp-apk}" />
</target>

<target name="clean">
<delete dir="${outdir}" />
</target>
</project>

  相关解决方案