teaching machines

Build Script for a Unity Android Project

October 8, 2012 by . Filed under public, reader animated.

Having learned how to integrate my Android code and resources with Unity, I went looking for a way to simplify the migration of my files to my Unity project. (I found repeatedly exporting a JAR and copying over the manifest and resource directories painful.) The result was this Ant script, which drops the class files, manifest, and resources directly into my Unity project.

<project default="build">
  <!-- Set this to your top-level Unity project directory! -->
  <property
    name="project.dir"
    value="D:/rezcycle" />

  <!-- All Android files will be placed inside Assets/Plugins/Android. No 
    need to touch this. -->
  <property
    name="assets.dir"
    value="${project.dir}/Assets/Plugins/Android" />

  <target name="build">
    <!-- Make the assets directory in our project. -->
    <mkdir dir="${assets.dir}" />

    <!-- Copy over the manifest. -->
    <copy
      file="AndroidManifest.xml"
      todir="${assets.dir}" />

    <!-- Copy over the raw files in res/ and assets/. -->
    <copy todir="${assets.dir}">
      <fileset
        dir="."
        includes="res/**" />
      <fileset
        dir="."
        includes="assets/**" />
    </copy>

    <!-- Produce a JAR file of all your pre-compiled classes. Don't bring 
      R stuff. -->
    <jar destfile="${assets.dir}/custom_android.jar">
      <fileset
        dir="bin/classes"
        excludes="**/R.class,**/R$*.class" />
    </jar>
  </target>
</project>

I drop this in to my top-level Android project directory in Eclipse, naming it build.xml. Eclipse knows how to run Ant files, so I simply run as “Ant Build”.