<?xml version="1.0" encoding="utf-8"?>  
<project name="RuneStoneKeeper" default="debug">  
    <property environment="env" />  

	<property name="appName" value="RuneStoneKeeper" />
    <property name="application-package" value="com.cimu.RuneStoneKeeper" />  
    <property name="version" value="1.0" />  

    <!-- SDK Locations -->  
    <property name="sdk-folder" value="${env.ANDROID_SDK_ROOT}" />  
    <property name="sdk-platform-folder" value="${sdk-folder}\platforms\android-17" />  
    <property name="sdk-tools" value="${sdk-folder}\tools" />  
	<property name="sdk-platform-tools" value="${sdk-folder}/platform-tools" />  
    <property name="build-tools" value="${sdk-folder}\build-tools\19.0.3" />  
  
    <!-- step 1.generate R.java by aapt  2.compile java to class by javac。exe 3.generate classes.dex by dx.bat  -->  
    <!-- 4.package resources by aapt 5. package resource and classes.dex by apkbuilder.bat 6.sign apk by jarsinger -->  
    <!-- Tools -->  
    <property name="aapt" value="${build-tools}\aapt.exe" />  
    <property name="dx" value="${build-tools}\dx.bat" />  
    <property name="apkbuilder" value="${sdk-tools}\lib\sdklib.jar" />  
    <property name="aidl" value="${build-tools}/aidl.exe" />  
    <property name="adb" value="${sdk-platform-tools}\adb.exe" />  
    <property name="android-jar" value="${sdk-platform-folder}\android.jar" />  
    <property name="jarsigner" value="${env.JAVA_HOME}\bin\jarsigner.exe" />  
  

		<!-- 生成R文件的相对目录 -->
        <property name="outdir-gen" value="gen" />
        <!-- 编译后的文件放置目录 -->
        <property name="outdir-bin" value="bin" />

        <!-- 清单文件 -->
        <property name="manifest-xml" value="AndroidManifest.xml" />
        <!-- 源文件目录 -->
        <property name="resource-dir" value="res" />
        <property name="asset-dir" value="assets" />
        <!-- java源文件目录 -->
        <property name="srcdir" value="src" />
        <property name="srcdir-ospath" value="${basedir}\${srcdir}" />
        <!-- 外部类库所在目录 -->
        <property name="external-libs" value="${basedir}\libs" />

        <!-- 生成class目录 -->
        <property name="outdir-classes" value="${outdir-bin}" />
        <property name="outdir-classes-ospath" value="${basedir}/${outdir-classes}" />

        <!-- classes.dex相关变量 -->
        <property name="dex-file" value="classes.dex" />
        <property name="dex-path" value="${outdir-bin}\${dex-file}" />
        <property name="dex-ospath" value="${basedir}\${dex-path}" />

        <!-- 经过aapt生成的资源包文件 -->
        <property name="resources-package" value="${outdir-bin}/resources.ap_" />
        <property name="resources-package-ospath" value="${basedir}\${resources-package}" />

        <!-- 未认证apk包 -->
        <property name="out-unsigned-package" value="${outdir-bin}\${appName}-unsigned.apk" />
        <property name="out-unsigned-package-ospath" value="${basedir}\${out-unsigned-package}" />

        <!-- 证书文件 -->
        <property name="keystore-file" value="${basedir}\rsk.keystore" />

        <!-- 已认证apk包 -->
        <property name="out-signed-package" value="${outdir-bin}\${appName}.apk" />
        <property name="out-signed-package-ospath" value="${basedir}\${out-signed-package}" />


        <!-- 初始化工作 -->
        <target name="init">
                <echo>Initializing all output directories...</echo>
                <delete dir="${outdir-bin}" />
				<delete dir="${outdir-gen}" />
                <mkdir dir="${outdir-bin}" />
                <mkdir dir="${outdir-classes}" />
				<mkdir dir="${outdir-gen}" />
        </target>

        <!-- 根据工程中的资源文件生成R.java文件 -->
        <target name="gen-R" depends="init">
                <echo>Generating R.java from the resources...</echo>
                <exec executable="${aapt}" failonerror="true">
                        <arg value="package" />
                        <arg value="-f" />
                        <arg value="-m" />
                        <arg value="-J" />
                        <arg value="${outdir-gen}" />
                        <arg value="-S" />
                        <arg value="${resource-dir}" />
                        <arg value="-M" />
                        <arg value="${manifest-xml}" />
                        <arg value="-I" />
                        <arg value="${android-jar}" />
                </exec>


                <!--<exec executable="${aapt}" failonerror="true">
					<arg value="package" />  
					<arg value="-f" />  
					<arg value="-M" />  
					<arg value="AndroidManifest.xml" />  
					<arg value="-S" />  
					<arg value="${resource-dir}" />  
					<arg value="-A" />  
					<arg value="${asset-dir}" />  
					<arg value="-I" />  
					<arg value="${android-jar}" />  
					<arg value="-F" />  
					<arg value="${resources-package}" />  
                </exec>-->
        </target>


        <!-- 编译aidl文件 -->
        <target name="aidl" depends="gen-R">
                <echo>Compiling .aidl into java files...</echo>
                <apply executable="${aidl}" failonerror="true">
                        <!-- 指定预处理文件 -->
                        <!--<arg value="-p${framework-aidl}" />-->
                        <!-- aidl声明的目录 -->
                        <arg value="-I${srcdir}" />
                        <!-- 目标文件目录 -->
                        <arg value="-o${outdir-gen}" />
                        <!-- 指定哪些文件需要编译 -->
                        <fileset dir="${srcdir}">
                                <include name="**/*.aidl" />
                        </fileset>
                </apply>
        </target>

        <!-- 将工程中的java源文件编译成class文件 -->
        <target name="compile" depends="aidl">
                <echo>Compiling java source code...</echo>
                <javac encoding="utf-8" target="1.6" destdir="${outdir-classes}" bootclasspath="${android-jar}">
                        <src path="src" />
                        <src path="gen" />
                        <!--<src path="${library-dir}/src" />-->
                        <classpath>
                                <fileset dir="${external-libs}" includes="*.jar" />
                                <!--<fileset dir="${library-dir}/libs" includes="*.jar" />
                                <filelist>
                                        <file name="${android-maps-jar}" />
                                </filelist>
								-->
                        </classpath>
                </javac>
        </target>

        <!-- 将.class文件转化成.dex文件 -->
        <target name="dex" depends="compile">
                <echo>Converting compiled files and external libraries into a .dex
                        file...
                </echo>
                <exec executable="${dx}" failonerror="true">
                        <arg value="--dex" />
                        <!-- 输出文件 -->
                        <arg value="--output=${dex-ospath}" />
                        <!-- 要生成.dex文件的源classes和libraries -->
                        <arg value="${outdir-classes-ospath}" />
                        <arg value="${external-libs}" />
<!--                         <arg value="${library-dir}/libs" /> -->
                </exec>
        </target>


        <!-- 将资源文件放进输出目录 -->
        <!--在这截断-->
        <target name="package-res-and-assets" depends="dex">
                <echo>Packaging resources and assets...</echo>
                <exec executable="${aapt}" failonerror="true">
                        <arg value="package" />
                        <arg value="-f" />
                        <arg value="-M" />
                        <arg value="${manifest-xml}" />
                        <arg value="-S" />
                        <arg value="${resource-dir}" />

                        <arg value="-A" />
                        <arg value="${asset-dir}" />
<!--
                        <arg value="-S" />
                        <arg value="${library-dir}/${resource-dir}" />

                        <arg value="-A" />
                        <arg value="${library-dir}/${asset-dir}" />
-->
                        <arg value="-I" />
                        <arg value="${android-jar}" />
                        <arg value="-F" />
                        <arg value="${resources-package}" />

                        <arg value="--auto-add-overlay" />
                </exec>
        </target>


        <!-- 打包成未签证的apk -->
        <target name="package" depends="dex, package-res-and-assets">
                <echo>Packaging unsigned apk...</echo>
				<!--
                <exec executable="${apkbuilder}" failonerror="true">
                        <arg value="${out-unsigned-package-ospath}" />
                        <arg value="-u" />
                        
                        <arg value="-z" />
                        <arg value="${resources-package-ospath}" />
                        <arg value="-f" />
                        <arg value="${dex-ospath}" />
                        <arg value="-rf" />
                        <arg value="${srcdir-ospath}" />
                        <arg value="-nf"/>
                        <arg value="${library-dir}/libs"/> 
                </exec>-->

				<java classpath="${sdk-tools}/lib/sdklib.jar" classname="com.android.sdklib.build.ApkBuilderMain">  
                        <arg value="${out-unsigned-package-ospath}" />
                        <arg value="-u" />
                        
                        <arg value="-z" />
                        <arg value="${resources-package-ospath}" />
                        <arg value="-f" />
                        <arg value="${dex-ospath}" />
						<arg value="-rf" />
                        <arg value="${srcdir-ospath}" />
                        <arg value="-nf"/>
                        <arg value="${external-libs}"/> 
				</java>  

                <echo>It will need to be signed with jarsigner before being published.</echo>
        </target>

        <!-- 对apk进行签证 -->
        <target name="jarsigner" depends="package">
                <echo>apk is signed...</echo>
                <exec executable="${jarsigner}" failonerror="true">
                        <arg value="-keystore" />
                        <arg value="${keystore-file}" />
                        <arg value="-storepass" />
                        <arg value="cimu2014" />
                        <arg value="-keypass" />
                        <arg value="cimu2014" />
                        <arg value="-signedjar" />
                        <arg value="${out-signed-package-ospath}" />
                        <arg value="${out-unsigned-package-ospath}" />
                        <!-- 不要忘了证书的别名 -->
                        <arg value="rsk" />
                </exec>
        </target>

        <!-- 发布 -->
        <target name="release" depends="jarsigner">
                <!-- 删除未签证apk -->
                <delete file="${out-unsigned-package-ospath}" />
                <echo>APK is generate for release.  path:${out-signed-package-ospath}</echo>
        </target>

		<target name="debug" depends="jarsigner">
                <!-- 删除未签证apk -->
				<delete file="${out-unsigned-package-ospath}" />
                <echo>APK is generate for debug.  path:${out-signed-package-ospath}</echo>
        </target>
</project>  