<?xml version="1.0" encoding="UTF-8"?>
<!--
	Author: Junda Ong
	Website: Just2us.com
	Email: junda@just2me.com
	Date created: 20 July 09
	
	This build.xml is created for obfuscating Android apps. As there is currently
	no available easy way, such as integration of proguard in eclipse for Android,
	this ant build will help. To use:
	
	1) Change the property paths as needed
	2) In target optimize, add "-keep public class com.all.app.nodes.classes.declared.in.manifest"
	3) "ant debug-obf" or "ant release-obf"
-->
<project name="ObfuscatedApp" default="debug-obf">

	<!-- Echo the Java JDK version. 
		If needed, link /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK to 1.6 -->
	<echo message="ant is using Java sdk ${ant.java.version}"/>

	<!-- Some properties. Change paths as needed -->
  	<property name="sdk-folder" value="/Applications/android-sdk-mac_x86-1.5_r2"/>
  	<property name="proguard-home" value="/Applications/proguard4.3/lib"/>
  	<property name="outdir" value="bin"/>
  	<property name="outdir-classes" value="${outdir}/classes"/>
  	<property name="library-jar" value="libs"/>
  	<property name="android-jar" value="${sdk-folder}/platforms/android-1.5/tools/android.jar"/>


	<!-- Template from command 'android create project' 
		Uses: ${sdk-folder}/platforms/android-1.5/templates/android_rules.xml -->
    <property file="local.properties"/>
    <property file="build.properties"/>
    <property file="default.properties"/>

    <path id="android.antlibs">
        <pathelement path="${sdk-location}/tools/lib/anttasks.jar" />
        <pathelement path="${sdk-location}/tools/lib/sdklib.jar" />
        <pathelement path="${sdk-location}/tools/lib/androidprefs.jar" />
        <pathelement path="${sdk-location}/tools/lib/apkbuilder.jar" />
        <pathelement path="${sdk-location}/tools/lib/jarutils.jar" />
    </path>

    <taskdef name="setup"
        classname="com.android.ant.SetupTask"
        classpathref="android.antlibs"/>

    <setup />


    <!-- Package the application and sign it with a debug key, with obfuscation
         This is the default target when building. It is used for debug. -->
    <target name="debug-obf" depends="dex-obf, package-resources">
        <apkbuilder
                outfolder="${out-folder}"
                basename="${ant.project.name}"
                signed="true"
                verbose="false">
            <file path="${intermediate-dex}" />
            <sourcefolder path="${source-folder}" />
            <jarfolder path="${external-libs-folder}" />
            <nativefolder path="${native-libs-folder}" />
        </apkbuilder>
    </target>


    <!-- Package the application without signing it, with obfuscation
         This allows for the application to be signed later with an official publishing key. -->
    <target name="release-obf" depends="dex-obf, package-resources">
        <apkbuilder
                outfolder="${out-folder}"
                basename="${ant.project.name}"
                signed="false"
                verbose="false">
            <file path="${intermediate-dex}" />
            <sourcefolder path="${source-folder}" />
            <jarfolder path="${external-libs-folder}" />
            <nativefolder path="${native-libs-folder}" />
        </apkbuilder>
        <echo>All generated packages need to be signed with jarsigner before they are published.</echo>
    </target>


	<!-- Using proguard for the actual obfuscation. 
		Referenced from: http://code.google.com/p/zxing/source/browse/trunk/android-m3/build.xml?r=321 -->
  <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.just2me.obfapp.MyActivity"/>
                  <!-- <arg value="-keep public class com.just2me.obfapp.receiver.*"/> -->
                  <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="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="${out-classes-location}" />
          <fileset dir="${external-libs-folder}" includes="*.jar"/>
      </apply>
  </target>

	<!-- Cleaning up the house -->
  <target name="clean">
    <delete dir="${outdir}"/>
  </target>

</project>
