ant script to obfuscate android code


Last Updated:

  1. seafit

    seafit New Member This Topic's Starter

    Joined:
    Jun 9, 2010
    Messages:
    4
    Likes Received:
    0
    <!--

    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">

    <apkbuilder outfolder="${outdir}" basename="${ant.project.name}" signed="true" verbose="false">
    <file path="${intermediate-dex-location}" />
    <sourcefolder path="${pkres}" />
    <jarfolder path="${externallibs}" />
    </apkbuilder>
    </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" 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="-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}" failonerror="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>
     

    Advertisement
  2. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    nice -- is this needed for copy protected apps? I'm not sure what the "copy protection" entails. Is it encryption, or just a flag that makes it so you can't copy it off of a non-rooted phone?
     
  3. seafit

    seafit New Member This Topic's Starter

    Joined:
    Jun 9, 2010
    Messages:
    4
    Likes Received:
    0
    I'm not sure which copy do you mean. if you mean the copy apk it's just to keep the no signed apk. you can remove it if you want.

    In just2us's page the package-resource target is missing. so I wrote it by myself. hope it can help anyone who is doing the same job
     
  4. aryaxt

    aryaxt Member

    Joined:
    Jun 1, 2010
    Messages:
    21
    Likes Received:
    0
    i don't have any experience with Obfuscation.
    Can you please provide the steps.

    Thanks
     
  5. aryaxt

    aryaxt Member

    Joined:
    Jun 1, 2010
    Messages:
    21
    Likes Received:
    0
    BUILD FAILED
    /Users/aryan/Desktop/t/build.xml:48: taskdef class com.android.ant.SetupTask cannot be found

    I've been stuck on this for 2 weeks :(
     
  6. abedross

    abedross New Member

    Joined:
    Oct 7, 2010
    Messages:
    1
    Likes Received:
    0
    Hey aryaxt, check the path to the android-sdk folder that is installed on your machine to fix this problem.
     
  7. eugene889

    eugene889 New Member

    Joined:
    Jan 12, 2012
    Messages:
    3
    Likes Received:
    0
    Thanks for the discussion! found some useful things
     
  8. eugene889

    eugene889 New Member

    Joined:
    Jan 12, 2012
    Messages:
    3
    Likes Received:
    0

Share This Page

Loading...