Search code examples
androideclipsebuild-process

Android Eclipse: Customizing the build process


I have many graphic resources in my application and I would like to support different screen sizes with differently sized graphic resources. However, the app is very large if I include 3 different versions of every graphic resource in the final apk (i.e. res/drawable-hdpi, res/drawable-ldpi, res/drawable-mdpi). So I would like to be able to generate 3 different apks for testing--and eventually deployment--purposes (see using mulitple apks), each with only the relevant graphical resources included.

Is there a way to customize the android build process (I'm using eclipse) to make this possible? If it is not possible in eclipse is it possible from the command line?

My searching indicates that many people are using the build tools Ant and Maven for this type of problem, and that the android-sdk has a tool to support Ant out of the box.

Notes on answer: After performing the tutorial below follow these steps (they will be clear after the tutorial and reading the ant manual ant the main_rules.xml which comes with the android-sdk):

  1. copy the contents of main_rules into your build.xml (as is specified in the build.xml comments)
  2. Make three new tasks as follows:
  <property name="my.custom_res_folder" value="${basedir}/custom_res"/>
  <property name="my.drawable_folder" value="${my.custom_res_folder}/drawable"/>
  <property name="my.drawable_hdpi" value="${resource.absolute.dir}/drawable-hdpi"/>
  <property name="my.drawable_mdpi" value="${resource.absolute.dir}/drawable-mdpi"/>
  <property name="my.drawable_ldpi" value="${resource.absolute.dir}/drawable-ldpi"/>
  <property name="my.drawable_default" value="${resource.absolute.dir}/drawable"/>
  <target name="my-make-custom-resource-directory">
    <echo message="my-make-custom-resource-directory"/>
    <mkdir dir="${my.custom_res_folder}"/>
    <mkdir dir="${my.drawable_folder}"/>

    <copy todir="${my.custom_res_folder}/layout">
      <fileset dir="${resource.absolute.dir}/layout"/>
    </copy>         

    <copy todir="${my.custom_res_folder}/menu">
      <fileset dir="${resource.absolute.dir}/menu"/>
    </copy>         

    <copy todir="${my.custom_res_folder}/values">
      <fileset dir="${resource.absolute.dir}/values"/>
    </copy>         

  </target>


  <target name="my-make-res-small" depends="my-make-custom-resource-directory">
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_ldpi}"/>
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_mdpi}"/>
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_hdpi}"/>          
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_default}"/>           
    </copy> 

  </target>

  <target name="my-make-res-medium" depends="my-make-custom-resource-directory">
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_mdpi}"/>
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_hdpi}"/>          
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_ldpi}"/>
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_default}"/>           
    </copy> 

  </target>

  <target name="my-make-res-large" depends="my-make-custom-resource-directory">
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_hdpi}"/>
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_mdpi}"/>          
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_ldpi}"/>
    </copy> 
    <copy todir="${my.drawable_folder}" overwrite="false">
      <fileset dir="${my.drawable_default}"/>           
    </copy> 

  </target>
  1. after running one of the my-make-res-X targets a new resource folder should be created
  2. Modify the debug and packaging and compiling targets and remove all properties which point to the old resources folder with a property pointing to a new resource folder (you may want to replicate the tasks and name them something else
  3. Then run your modified debug task.

Solution

  • I would go with Ant. Here's a good tutorial for you: http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html