<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>a little madness</title>
	<atom:link href="http://www.alittlemadness.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.alittlemadness.com</link>
	<description>A man needs a little madness, or else he never dares cut the rope and be free. -Nikos Kazantzakis</description>
	<lastBuildDate>Wed, 11 Aug 2010 04:58:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pulse Continuous Integration Server 2.2!</title>
		<link>http://www.alittlemadness.com/2010/08/11/pulse-continuous-integration-server-2-2/</link>
		<comments>http://www.alittlemadness.com/2010/08/11/pulse-continuous-integration-server-2-2/#comments</comments>
		<pubDate>Wed, 11 Aug 2010 04:58:55 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Zutubi]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=722</guid>
		<description><![CDATA[Big news today: Pulse 2.2 has graduated to stable!  This release includes a stack of new features and improvements, including:

Build UI overhaul: all tabs improved and restyled.
New logs tab: making it easier to access stage logs.
Featured artifacts: choose which artifacts should appear prominently.
Build navigator: easily move forward and backward through history.
Working copy browser: view [...]]]></description>
			<content:encoded><![CDATA[<p>Big news today: <a href="http://zutubi.com/products/pulse/">Pulse 2.2</a> has graduated to stable!  This release includes a stack of new features and improvements, including:</p>
<ul>
<li>Build UI overhaul: all tabs improved and restyled.</li>
<li>New logs tab: making it easier to access stage logs.</li>
<li>Featured artifacts: choose which artifacts should appear prominently.</li>
<li>Build navigator: easily move forward and backward through history.</li>
<li>Working copy browser: view working copies for in progress builds.</li>
<li>Move refactoring: move projects and agents in the template hierarchy.</li>
<li>Template navigation: navigate directly up and down a hierarchy.</li>
<li>Subscription by label: subscribe to notifications by project groups.</li>
<li>Agent executing stages: see what all agents are building at a glance.</li>
<li>Subversion exports: for faster and smaller builds.</li>
<li>Performance improvements: key for larger installations.</li>
</ul>
<p>See the <a href="http://zutubi.com/products/pulse/new/">new in 2.2 page</a> for full details.</p>
<p>In conjunction with this release, we&#8217;ve also given our <a href="http://zutubi.com/">website</a> a complete overhaul.  The new site has a fresher look, and communicates the key features of Pulse more directly.  The updates also include some new features: </p>
<ul>
<li>RSS feeds for news items.</li>
<li>Links to our latest blog posts on the front page.</li>
<li>An improved buying process, allowing multiple licenses to be purchased in one transaction.</li>
<li>Self-service renewal payments &#8211; just enter your current license key and go!</li>
<li>A more user-friendly downloads page.</li>
</ul>
<p>We hope you enjoy the new release, and the new site.  And please, <a href="mailto:support@zutubi.com">let us know</a> what you think!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/08/11/pulse-continuous-integration-server-2-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simpler Ant Builds With the Ant Script Library</title>
		<link>http://www.alittlemadness.com/2010/07/29/simpler-ant-builds-with-the-ant-script-library/</link>
		<comments>http://www.alittlemadness.com/2010/07/29/simpler-ant-builds-with-the-ant-script-library/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 09:39:21 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Project Automation]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=694</guid>
		<description><![CDATA[Introduction
Ant may be unfashionable these days, but it still has its advantages.  Key among these are familiarity and simplicity: most Java developers have worked with Ant, and with an Ant build what you get is what you see.  A major disadvantage, though, is that Ant provides very little out-of-the-box.  When you start [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p><a href="http://ant.apache.org/">Ant</a> may be unfashionable these days, but it still has its advantages.  Key among these are familiarity and simplicity: most Java developers have worked with Ant, and with an Ant build what you get is what you see.  A major disadvantage, though, is that Ant provides very little out-of-the-box.  When you start a new project, you&#8217;ve got a lot of grunt work to endure just to get your code compiled, packaged, and tested.  An all-too-common solution, in the grand tradition of <a href="http://www.gnu.org/software/make/">make</a>, is to copy a build file from an existing project as an easy starting point.</p>
<p>Over the years, though, Ant has gradually expanded support for creating reusable build file snippets.  On top of this a few projects have emerged which aim to simplify and standardise your Ant builds, including:</p>
<ul>
<li><a href="http://www.easyant.org/">EasyAnt</a></li>
<li><a href="http://code.google.com/p/ant-master-build-scripts/">Ant Master Build Scripts</a></li>
<li><a href="http://www.exubero.com/asl/">Ant Script Library</a></li>
</ul>
<p>Today I&#8217;ve taken my first proper look at the latter, and so far I like what I see.</p>
<h3>The Ant Script Library</h3>
<p>In the author Joe Schmetzer&#8217;s own words:</p>
<blockquote><p>The Ant Script Library (ASL) is a collection of re-usable Ant scripts that can be imported into your own projects. The ASL provides a number of pre-defined targets that simplify setting up build scripts for a new project, bringing re-use and consistency to your own Ant scripts.</p></blockquote>
<p>ASL consists of several Ant XML files, each of which provides a group of related functionality via predefined targets.  For example, the <b>asl-java-build.xml</b> file defines targets for compiling and packaging Java code.  The <b>asl-java-test.xml</b> file extends this with the ability to run JUnit tests, and so on.  Essentially, ASL packages up all the grunt work, allowing you to concentrate on the small tweaks and extra targets unique to your project.  The modular structure of ASL, combined with the fact that it is just Ant properties and targets, makes it easy to take what you like and leave the rest.</p>
<h3>An Example</h3>
<p>Allow me to illustrate with a simple project I have been playing with.  This project has a straightforward directory structure:</p>
<ul>
<li>&lt;project root&gt;
<ul style="font-size: 1em;">
<li>asl/ &#8211; the Ant Script Library</li>
<li>build.xml &#8211; Ant build file</li>
<li>lib/ &#8211; Jar file depedencies</li>
<li>src/ &#8211; Java source files</li>
<li>test/ &#8211; JUnit-based test source files</li>
</ul>
</li>
</ul>
<p>To add ASL to my project, I simply downloaded it from the project <a href="http://www.exubero.com/asl/download.html">download page</a> and unpacked it in the <b>asl/</b> subdirectory of my project<sup>1</sup>.  Then I can start with a very simple build file that supports building my code and running the tests:</p>
<pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;project name=&quot;zutubi-android-ant&quot; default=&quot;dist&quot;&gt;
    &lt;property name=&quot;java-build.src-dir&quot; location=&quot;src&quot;/&gt;
    &lt;property name=&quot;java-test.src-dir&quot; location=&quot;test&quot;/&gt;
    &lt;property name=&quot;java-build.lib-dir&quot; location=&quot;libs&quot;/&gt;

    &lt;property name=&quot;asl.dir&quot; value=&quot;asl&quot;/&gt;

    &lt;import file=&quot;${asl.dir}/asl-java-build.xml&quot;/&gt;
    &lt;import file=&quot;${asl.dir}/asl-java-test.xml&quot;/&gt;
&lt;/project&gt;</pre>
<p>Notice that I am using non-standard source locations, but that is easily tweaked using properties which are <a href="http://www.exubero.com/asl/reference.html">fully documented</a>.  With this tiny build file, let&#8217;s see what targets ASL provides for me:</p>
<pre class="brush: plain;">$ ant -p
Buildfile: build.xml

Main targets:

 clean                 Deletes files generated by the build
 compile               Compiles the java source
 copy-resources        Copies resources in preparation to be packaged in jar
 dist                  Create a distributable for this java project
 generate              Generates source code
 jar                   Create a jar for this java project
 test-all              Runs all tests
 test-integration      Runs integration tests
 test-run-integration  Runs the integration tests
 test-run-unit         Runs the unit tests
 test-unit             Runs unit tests
Default target: dist</pre>
<p>It&#8217;s delightfully simple!</p>
<h3>Adding Reports</h3>
<p>It gets better: ASL also provides reporting with tools like <a href="http://cobertura.sourceforge.net/">Cobertura</a> for coverage, <a href="http://findbugs.sourceforge.net/">FindBugs</a> for static analysis and so on via its <b>asl-java-report.xml</b> module.  The full range of supported reports can be seen in the <b>report-all</b> target:</p>
<pre class="brush: xml;">&lt;target name=&quot;report-all&quot;
        depends=&quot;report-javadoc, report-tests, report-cobertura, report-jdepend, report-pmd, report-cpd, report-checkstyle, report-findbugs&quot;
        description=&quot;Runs all reports&quot;/&gt;</pre>
<p>Having support for several tools out-of-the-box is great.  For my project, however, I&#8217;d like to keep my dependencies down and I don&#8217;t feel that I need all of the reporting.  Although the choice of reports is not something that is parameterised by a property, it is still trivial to override by providing your own report-all target.  This shows the advantage of everything being plain Ant targets:</p>
<pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;project name=&quot;zutubi-android-ant&quot; default=&quot;dist&quot;&gt;
    &lt;property name=&quot;java-build.src-dir&quot; location=&quot;src&quot;/&gt;
    &lt;property name=&quot;java-test.src-dir&quot; location=&quot;test&quot;/&gt;
    &lt;property name=&quot;java-build.lib-dir&quot; location=&quot;libs&quot;/&gt;

    &lt;property name=&quot;asl.dir&quot; value=&quot;asl&quot;/&gt;

    &lt;import file=&quot;${asl.dir}/asl-java-build.xml&quot;/&gt;
    &lt;import file=&quot;${asl.dir}/asl-java-test.xml&quot;/&gt;
    &lt;import file=&quot;${asl.dir}/asl-java-report.xml&quot;/&gt;

    &lt;target name=&quot;report-all&quot;
            depends=&quot;report-javadoc, report-tests, report-cobertura, report-pmd, report-checkstyle&quot;
            description=&quot;Runs all reports&quot;/&gt;
&lt;/project&gt;</pre>
<p>Here I&#8217;ve included the <b>java-report</b> module, but defined my own <b>report-all</b> target that depends on just the reports I want.  This keeps things simple, and allows me to trim out a bunch of ASL dependencies I don&#8217;t need.</p>
<h3>Conclusion</h3>
<p>I&#8217;ve known of ASL and such projects for a while, but this is the first time I&#8217;ve actually given one a go.  Getting started was pleasantly simple, as was applying the small tweaks I needed.  So next time you&#8217;re tempted to copy an Ant build file, give ASL a shot: you won&#8217;t regret it!</p>
<p><br/></p>
<p>&#8211;<br />
<sup>1</sup> In this case I downloaded the full tarball including dependencies, which seemed on the large side (21MB!) but in fact can be easily trimmed by removing the pieces you don&#8217;t need.  Alternatively, you can start with the basic ASL install (sans dependencies) and it can pull down libraries for you.  Sweet <img src='http://www.alittlemadness.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/07/29/simpler-ant-builds-with-the-ant-script-library/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Android Testing: XML Reports for Continuous Integration</title>
		<link>http://www.alittlemadness.com/2010/07/14/android-testing-xml-reports-for-continuous-integration/</link>
		<comments>http://www.alittlemadness.com/2010/07/14/android-testing-xml-reports-for-continuous-integration/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 07:57:28 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=680</guid>
		<description><![CDATA[Summary
This post introduces the Android JUnit Report Test Runner, a custom instrumentation test runner for Android that produces XML test reports.  Using this runner you can integrate your Android test results with tools that understand the Ant JUnit task XML format, e.g. the Pulse Continuous Integration Server.
The motivation and details of the runner are [...]]]></description>
			<content:encoded><![CDATA[<h3>Summary</h3>
<p>This post introduces the <a href="http://github.com/jsankey/android-junit-report">Android JUnit Report Test Runner</a>, a custom instrumentation test runner for Android that produces XML test reports.  Using this runner you can integrate your Android test results with tools that understand the Ant JUnit task XML format, e.g. the <a href="http://zutubi.com/products/pulse/">Pulse</a> Continuous Integration Server.</p>
<p>The motivation and details of the runner are discussed below.  For the impatient: simply head on over to the project <a href="http://github.com/jsankey/android-junit-report">home page</a> on GitHub and check out the README.</p>
<h3>Introduction</h3>
<p>If you&#8217;ve been following my <a href="/2010/06/29/android-functional-testing-vs-dependency-injection/">recent</a> <a href="/2010/07/06/android-testing-using-pure-unit-tests/">posts</a> you&#8217;ll know that I&#8217;ve been figuring out the practical aspects of testing Android applications.  And if you&#8217;ve been following for longer, you might know that my day job is development of the <a href="http://zutubi.com/products/pulse/">Pulse</a> Continuous Integration Server.  So it should come as no surprise that in my latest foray into the world of Android testing I sought to bring the two together <img src='http://www.alittlemadness.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h3>Status Quo</h3>
<p>Out of the box, the Android SDK supports running functional tests on a device or emulator via <a href="http://developer.android.com/guide/topics/testing/testing_android.html">instrumentation</a>.  Running within Eclipse, you get nice integrated feedback.  Unfortunately, though, there are no real options for integrating with other tools such as continuous integration servers.  Test output from the standard Ant builds is designed for human consumption, and lacks the level of detail I&#8217;d like to see in my build reports.</p>
<h3>The Solution</h3>
<p>On the upside, having access to the Android source makes it possible to examine how the current instrumentation works, and therefore how it can be customised.  I found that the default <a href="http://developer.android.com/reference/android/test/InstrumentationTestRunner.html">InstrumentationTestRunner</a> may be fairly easily extended to hook in extra test listeners.  So I&#8217;ve implemented a custom JUnitReportTestRunner that does just that, with a listener that generates a test report in XML format.  The format is designed to be largely compatible with the output of the Ant <a href="http://ant.apache.org/manual/Tasks/junit.html">JUnit task&#8217;s</a> XML formatter &#8212; the most widely supported format in the Java world.  Tools like Pulse can read in this format to give rich test reporting.</p>
<h3>How It Works</h3>
<p>As mentioned, the JUnitReportTestRunner extends the default InstrumentationTestRunner, so it can act as a drop-in replacement.  The custom runner acts identically to the default, with the added side-effect of producing an XML report.</p>
<p>For consistency with the SDK&#8217;s support for generating coverage reports, the XML report is generated in the file storage area of the <em>target application</em>.  The default report location is something like:</p>
<pre class="brush: plain;">/data/data/&lt;tested application package&gt;/files/junit-report.xml</pre>
<p>on the device.  To retrieve the report, you can use <b>adb pull</b>, typically as part of your scripted build.</p>
<h3>Using the Runner</h3>
<p>Full details on using the runner are provided in the README on the project <a href="http://github.com/jsankey/android-junit-report">home page</a>.  Briefly:</p>
<ul>
<li>Add the android-junit-report-&lt;version&gt;.jar to the libraries for your test application.</li>
<li>Replace all occurrences of <b>android.test.InstrumentationTestRunner</b> with <b>com.zutubi.android.junitreport.JUnitReportTestRunner</b>:
<ul style="font-size: 1em;">
<li>In the <b>android:name</b> attribute of the <b>instrumentation</b> tag in you test application&#8217;s <b>AndroidManifest.xml</b>.</li>
<li>In the <b>test.runner</b> property in the Ant build for your test application (<em>before</em> calling the Android <b>setup</b> task).</li>
<li>In the <b>Instrumentation runner</b> field of all Android JUnit Run Configurations in your Eclipse project.</li>
</ul>
</li>
<li>Add logic to your Ant build to run <b>adb pull</b> to retrieve the report after the tests are run.</li>
</ul>
<p>As an example for retrieving the report in your Ant build:</p>
<pre class="brush: xml;">&lt;target name=&quot;fetch-test-report&quot;&gt;
    &lt;echo&gt;Downloading XML test report...&lt;/echo&gt;
    &lt;mkdir dir=&quot;${reports.dir}&quot;/&gt;
    &lt;exec executable=&quot;${adb}&quot; failonerror=&quot;true&quot;&gt;
        &lt;arg line=&quot;${adb.device.arg}&quot;/&gt;
        &lt;arg value=&quot;pull&quot; /&gt;
        &lt;arg value=&quot;/data/data/${tested.manifest.package}/files/junit-report.xml&quot; /&gt;
        &lt;arg value=&quot;${reports.dir}/junit-report.xml&quot; /&gt;
    &lt;/exec&gt;
&lt;/target&gt;</pre>
<h3>In the Wild</h3>
<p>You can see a complete example of this in action in my simple <a href="http://github.com/jsankey/droidscope">DroidScope</a> Android application.  The custom runner is applied in the droidscope-test application in the test/ subdirectory.  You can even see the <a href="http://pulse.zutubi.com/browse/projects/droidscope/builds/latest/tests/default/">test results</a> being picked up by Pulse on our demo server.  Note that some of the tests are <a href="/2010/07/06/android-testing-using-pure-unit-tests/">pure unit tests</a>, which are run on a regular JVM, whereas others are run with the custom runner on an emulator.  It&#8217;s nice for all the results to be collected together!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/07/14/android-testing-xml-reports-for-continuous-integration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Android Testing: Using Pure Unit Tests</title>
		<link>http://www.alittlemadness.com/2010/07/06/android-testing-using-pure-unit-tests/</link>
		<comments>http://www.alittlemadness.com/2010/07/06/android-testing-using-pure-unit-tests/#comments</comments>
		<pubDate>Tue, 06 Jul 2010 08:06:01 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=667</guid>
		<description><![CDATA[Introduction
The Android SDK comes with support for testing, allowing tests to be run on an Android device (or emulator) via instrumentation.  This is useful for functional tests that require a realistic environment, but for the majority of tests it is overkill.  The instrumentation and emulation layers add complexity to the process, making tests [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>The Android SDK comes with support for <a href="http://developer.android.com/guide/topics/testing/testing_android.html">testing</a>, allowing tests to be run on an Android device (or emulator) via instrumentation.  This is useful for functional tests that require a realistic environment, but for the majority of tests it is overkill.  The instrumentation and emulation layers add complexity to the process, making tests much slower to run and harder to debug.</p>
<p>The good news is that there is no need to run most of your tests via instrumentation.  Because Android applications consist of regular Java code, it is possible to isolate much of the implementation from the Android environment.  In fact, if you&#8217;ve separated concerns in your application already, it&#8217;s likely that large parts of it are already independent of the Android APIs.  Those sections of your code can be tested on a regular JVM, using the rich ecosystem of tools available for unit testing.</p>
<h3>Unit Testing Requirements</h3>
<p>To put this idea into practice, I set out the following requirements for unit testing my Android application:</p>
<ol>
<li>The unit tests should run on a regular JVM, with no dependency on the Android APIs or tools.</li>
<li>It should be possible to run the tests within Eclipse.</li>
<li>It should be possible to run tests using Ant.</li>
<li>Running tests via Ant should produce reports suitable for use with a Continuous Integration server.</li>
</ol>
<p>These requirements allow the tests to be run quickly within the development environment, and on every commit on a build server.</p>
<h3>Adding a Unit Testing Project</h3>
<p>In keeping with my existing <a href="/setting-up-an-android-project-build/">Android project setup</a>, I decided to use an additional  project specifically for unit testing.  To recap, in the original setup I had two projects:</p>
<ol>
<li>The main project: containing the application itself.</li>
<li>The test project: containing an Android test project for instrumentation testing, in a test/ subdirectory of the root.</li>
</ol>
<p>Both projects had Ant build files and Eclipse projects.  Similar to the use of a test/ subdirectory for instrumentation tests, I added my new unit test project in a unit/ subdirectory of the root.  As with the other projects, the source code for the unit tests lives in a src/ subdirectory, giving the following overall layout:</p>
<pre class="brush: plain;">my-app/
    src/        - main application source
    test/
        src/    - functional tests
    unit/
        src/    - unit tests</pre>
<p>Creating the Eclipse project for unit testing was trivial: I just added a new Java Project named <b>my-app-unit</b>.  I then edited the build path of this project to depend on my main <b>my-app</b> project, so that I could build against the code under test.</p>
<h3>Testing Libraries</h3>
<p>The main tool required for this setup is a unit testing framework.  I decided to go with <a href="http://junit.org/">JUnit 4</a> as it is well supported in Eclipse, Ant and CI servers.  (JUnit is also used by the instrumentation testing support in the Android SDK.)  In addition, for mocking I am a fan of <a href="http://mockito.org/">Mockito</a>.  Note, though, that the beauty of using pure Java tests is you can use any of the myriad of mocking (and other) libraries out there.</p>
<p>For consistency with the existing projects, I added the JUnit and Mockito jars to a libs/ subdirectory of the unit project.  I then added those jars to the build path of my Eclipse project, and I was ready to implement some tests!</p>
<h3>A Trivial Test</h3>
<p>To make sure the setup works, you can try adding a trivial JUnit 4 test case:</p>
<pre class="brush: java;">package com.zutubi.android.myapp;

import static org.junit.Assert.*;

import org.junit.Test;

public class MyAppTest
{
    @Test
    public void testWorld()
    {
        assertEquals(2, 1 + 1);
    }
}</pre>
<p>If all is well you should be able to run this in Eclipse as a JUnit test case.  Once you have this sanity test passing, you can proceed to some Real Tests.</p>
<h3>Adding an Ant Build</h3>
<p>Setting up an Ant build took a little more effort than for the original projects, as their build files import Android rules from the SDK.  For the unit tests, I wrote a simple build file from scratch, trying to keep within the conventions established by the Android rules:</p>
<pre class="brush: xml;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project name=&quot;my-app-unit&quot; default=&quot;test&quot;&gt;
    &lt;property name=&quot;source.dir&quot; value=&quot;src&quot;/&gt;
    &lt;property name=&quot;libs.dir&quot; value=&quot;libs&quot;/&gt;

    &lt;property name=&quot;out.dir&quot; value=&quot;build&quot;/&gt;
    &lt;property name=&quot;classes.dir&quot; value=&quot;${out.dir}/classes&quot;/&gt;
    &lt;property name=&quot;reports.dir&quot; value=&quot;${out.dir}/reports&quot;/&gt;
    &lt;property name=&quot;tested.dir&quot; value=&quot;..&quot;/&gt;
    &lt;property name=&quot;tested.classes.dir&quot; value=&quot;${tested.dir}/build/classes&quot;/&gt;
    &lt;property name=&quot;tested.libs.dir&quot; value=&quot;${tested.dir}/libs&quot;/&gt;

    &lt;path id=&quot;compile.classpath&quot;&gt;
        &lt;fileset dir=&quot;${libs.dir}&quot; includes=&quot;*.jar&quot;/&gt;
        &lt;fileset dir=&quot;${tested.libs.dir}&quot; includes=&quot;*.jar&quot;/&gt;
        &lt;pathelement location=&quot;${tested.classes.dir}&quot;/&gt;
    &lt;/path&gt;

    &lt;path id=&quot;run.classpath&quot;&gt;
        &lt;path refid=&quot;compile.classpath&quot;/&gt;
        &lt;pathelement location=&quot;${classes.dir}&quot;/&gt;
    &lt;/path&gt;

    &lt;target name=&quot;clean&quot;&gt;
        &lt;delete dir=&quot;${out.dir}&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;-init&quot;&gt;
    	&lt;mkdir dir=&quot;${out.dir}&quot;/&gt;
    	&lt;mkdir dir=&quot;${classes.dir}&quot;/&gt;
    	&lt;mkdir dir=&quot;${reports.dir}&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;-compile-tested&quot;&gt;
        &lt;subant target=&quot;compile&quot; buildpath=&quot;${tested.dir}&quot;/&gt;
    &lt;/target&gt;

    &lt;target name=&quot;compile&quot; depends=&quot;-init,-compile-tested&quot;&gt;
        &lt;javac target=&quot;1.5&quot; debug=&quot;true&quot; destdir=&quot;${classes.dir}&quot;&gt;
            &lt;src path=&quot;${source.dir}&quot;/&gt;
            &lt;classpath refid=&quot;compile.classpath&quot;/&gt;
        &lt;/javac&gt;
    &lt;/target&gt;

    &lt;target name=&quot;run-tests&quot; depends=&quot;compile&quot;&gt;
        &lt;junit printsummary=&quot;yes&quot; failureproperty=&quot;test.failure&quot;&gt;
            &lt;classpath refid=&quot;run.classpath&quot;/&gt;

            &lt;formatter type=&quot;xml&quot;/&gt;

            &lt;batchtest todir=&quot;${reports.dir}&quot;&gt;
                &lt;fileset dir=&quot;${source.dir}&quot; includes=&quot;**/*Test.java&quot;/&gt;
            &lt;/batchtest&gt;
        &lt;/junit&gt;

        &lt;fail message=&quot;One or more test cases failed&quot; if=&quot;test.failure&quot;/&gt;
    &lt;/target&gt;
&lt;/project&gt;</pre>
<p>The <b>run-tests</b> target in this build file compiles all of the unit test code against the libraries in the unit test project, plus the classes and libraries from the project under test.  It then runs all JUnit tests in classes that have names ending with <b>Test</b>, printing summarised results and producing full XML reports in build/reports/.  These XML reports are ideal for integrating your results with a CI server (<a href="http://zutubi.com/products/pulse/">Pulse</a> in my case, of course!).</p>
<h3>Wrap Up</h3>
<p>The Android SDK support for testing is useful for functional tests, but too slow and cumbersome for rapid-feedback unit testing.  However, there is nothing to stop you from isolating the pure Java parts of your application and testing them separately.  In fact this is one of those rare win-wins: by clean design of your code you also get access to all the speed and tool support of testing on a regular JVM!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/07/06/android-testing-using-pure-unit-tests/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Android Functional Testing vs Dependency Injection</title>
		<link>http://www.alittlemadness.com/2010/06/29/android-functional-testing-vs-dependency-injection/</link>
		<comments>http://www.alittlemadness.com/2010/06/29/android-functional-testing-vs-dependency-injection/#comments</comments>
		<pubDate>Tue, 29 Jun 2010 08:04:28 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=654</guid>
		<description><![CDATA[I commonly use Dependency Injection (DI) to create testable Java code.  Dependency injection is simple: instead of having your objects find their own dependencies, you pass them in via the constructor or a setter.  One key advantage of this is the ability to easily substitute in stub or mock dependencies during testing.
Naturally, as [...]]]></description>
			<content:encoded><![CDATA[<p>I commonly use <a href="http://jamesshore.com/Blog/Dependency-Injection-Demystified.html">Dependency Injection</a> (DI) to create testable Java code.  Dependency injection is simple: instead of having your objects find their own dependencies, you pass them in via the constructor or a setter.  One key advantage of this is the ability to easily substitute in stub or mock dependencies during testing.</p>
<p>Naturally, as I started working on an Android application, I tried to apply the same technique.  Problems arose when I tried to combine DI with the Android SDK&#8217;s <a href="http://developer.android.com/guide/topics/testing/testing_android.html">Testing and Instrumentation</a> support.  In particular, I am yet to find a suitable way to combine DI with functional testing of Android activities via <a href="http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html">ActivityInstrumentationTestCase2</a>.  When testing an activity using the instrumentation support, injection of dependencies is foiled by a couple of factors:</p>
<ol>
<li>Constructor injection is impossible, as activities are constructed by the framework.  I experimented with various ways of creating the Activity myself, but was unable to maintain a connection with the Android system for true functional testing.</li>
<li>Setter injection is fragile, as activities are started by the framework as soon as they are created.  There is no time to set stub dependencies between the instantiation of the Activity and its activation.</li>
</ol>
<p>Not ready to give DI away, I scoured the web for existing solutions to this problem.  Although I did find some DI libraries with Android support (notably <a href="http://code.google.com/p/google-guice/wiki/OptionalAOP">Guice no AOP</a> and <a href="http://code.google.com/p/roboguice/">roboguice</a> which builds upon it), the only testing support I found was restricted to unit tests.  Although roboguice has support for Activities, it relies on being able to obtain a Guice Injector from somewhere &#8212; which just shifts the problem by one level of indirection.</p>
<p>Given how complex any DI solution was going to become (if indeed it is possible at all) I decided to step back and consider alternatives.  A classic alternative to DI is the Service Locator pattern: where objects ask a central registry for their dependencies.  Martin Fowler&#8217;s article <a href="http://martinfowler.com/articles/injection.html">Inversion of Control Containers and the Dependency Injection pattern</a> compares and contrasts the two patterns in some detail.  Most importantly: a Service Locator still allows you to substitute in different implementations of dependencies at test time.  The main downside is each class is dependent on the central registry &#8212; which can make them harder to reuse.  As I&#8217;m working with Activities that are unlikely to ever be reused outside of their current application, this is no big deal.</p>
<p>Implementation-wise, I went with the simplest registry that works for me.  I found it convenient to use my project&#8217;s <a href="http://developer.android.com/reference/android/app/Application.html">Application</a> implementation as the registry.  In production, the Application onCreate callback is used to create all of the standard dependency implementations.  These dependencies are accessed via simple static getters.  Static setters are exposed to allow tests to drop in whatever alternative dependencies they desire.  A contrived example:</p>
<pre class="brush: java;">public class MyApplication extends Application
{
    private static IService service;
    private static ISettings settings;

    @Override
    public void onCreate()
    {
        super.onCreate();
        if (service == null)
        {
            service = new ServiceImpl();
        }

        if (settings == null)
        {
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            settings = new PreferencesSettings(preferences);
        }
    }

    public static IService getService()
    {
        return service;
    }

    public static void setService(IService s)
    {
        service = s;
    }

    public static ISettings getSettings()
    {
        return settings;
    }

    public static void setSettings(ISettings s)
    {
        settings = s;
    }
}</pre>
<p>I access the dependencies via the registry in my Activity&#8217;s onCreate callback:</p>
<pre class="brush: java;">
public class MyActivity extends Activity
{
    private IService service;
    private ISettings settings;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        service = MyApplication.getService();
        settings = MyApplication.getSettings();

        setContentView(R.layout.main);
        // ...
    }

    // ...
}
</pre>
<p>And I wire in my fake implementations in my functional test setUp:</p>
<pre class="brush: java;">
public class MyActivityTest extends ActivityInstrumentationTestCase2&lt;MyActivity&gt;
{
    private MyActivity activity;

    public MyActivityTest()
    {
        super(&quot;com.zutubi.android.example&quot;, MyActivity.class);
    }

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        MyApplication.setService(new FakeService());
        MyApplication.setSettings(new FakeSettings());
        activity = getActivity();
    }

    public void testSomething() throws Throwable
    {
        // ...
    }
</pre>
<p>After all of the angst over DI, this solution is delightful in its simplicity.  It also illustrates that static is not always a dirty word when it comes to testing!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/06/29/android-functional-testing-vs-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Android Ant Builds: Targeting a Specific Device</title>
		<link>http://www.alittlemadness.com/2010/06/15/android-ant-builds-targeting-a-specific-device/</link>
		<comments>http://www.alittlemadness.com/2010/06/15/android-ant-builds-targeting-a-specific-device/#comments</comments>
		<pubDate>Tue, 15 Jun 2010 06:09:57 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Build]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=647</guid>
		<description><![CDATA[While developing for Android, I often have more than one device available.  For example, I might have an actual G1 hooked up via USB, and an emulator instance running.  In this case, when installing and running development versions of my Android applications, I need to specify which device to target.  If I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>While developing for Android, I often have more than one device available.  For example, I might have an actual G1 hooked up via USB, and an emulator instance running.  In this case, when installing and running development versions of my Android applications, I need to specify which device to target.  If I&#8217;m working directly with <a href="http://developer.android.com/guide/developing/tools/adb.html">adb</a>, there are flags for just this purpose:</p>
<pre class="brush: plain;">jsankey@caligula:~$ adb -?
Android Debug Bridge version 1.0.26

 -d                            - directs command to the only connected USB device
                                 returns an error if more than one USB device is present.
 -e                            - directs command to the only running emulator.
                                 returns an error if more than one emulator is running.
 -s &lt;serial number&gt;            - directs command to the USB device or emulator with
                                 the given serial number. Overrides ANDROID_SERIAL
                                 environment variable.
...
</pre>
<p>I prefer to use the general -s flag, as this works no matter how many devices I have connected.  This does require a serial number, but they are easily listed:</p>
<pre class="brush: plain;">jsankey@caligula:~$ adb devices
List of devices attached
HT845KV55555	device
emulator-5554	device
</pre>
<p>Normally, however, I&#8217;m not running adb directly, but indirectly via an Ant build.  If I try, for example, to run the install target with multiple devices attached, adb is not happy:</p>
<pre class="brush: plain;">jsankey@caligula:~/work/my-app$ ant install
Buildfile: build.xml
    [setup] Android SDK Tools Revision 6
    [setup] Project Target: Android 2.2

...

install:
     [echo] Installing /home/jsankey/work/my-app/build/MyAppActivity-debug.apk onto default emulator or device...
     [exec] error: more than one device and emulator

BUILD FAILED
/usr/local/java/android/platforms/android-8/ant/ant_rules_r2.xml:362: The following error occurred while executing this line:
/usr/local/java/android/platforms/android-8/ant/ant_rules_r2.xml:191: exec returned: 1

Total time: 1 second</pre>
<p>The Ant build cannot guess which device to use by default, so it errors out.  By digging into the Android Ant rules file, I found that the Ant builds support a property named <b>adb.device.arg</b>, which allows you to pass flags through to adb.  For example, I can instruct the Ant build to install the application on the emulator as follows:</p>
<pre class="brush: plain;">jsankey@caligula:~/work/my-app$ ant -Dadb.device.arg=&quot;-s emulator-5554&quot; install
Buildfile: build.xml
    [setup] Android SDK Tools Revision 6
    [setup] Project Target: Android 2.2

...

install:
     [echo] Installing /home/jsankey/work/my-app/build/MyAppActivity-debug.apk onto default emulator or device...
     [exec] 266 KB/s (14446 bytes in 0.052s)
     [exec] 	pkg: /data/local/tmp/MyAppActivity-debug.apk
     [exec] Success

BUILD SUCCESSFUL
Total time: 6 seconds</pre>
<p>There is, however, one sticking point.  To run a test application via Ant, you can use the run-tests target.  As of the latest SDK release, however, the run-tests target does <b>not</b> respect the adb.device.arg property.  When I discovered this a couple of weeks ago, I <a href="http://code.google.com/p/android/issues/detail?id=8693">raised an issue</a> about it, and it was promptly fixed.  Until the fix is released, though, a workaround is needed.</p>
<p>Luckily, adb supports another way to specify the device: via the <b>ANDROID_SERIAL</b> environment variable.  This variable takes the same serial values as the -s flag, and being part of the environment is independent of how you end up running adb.  To repeat my previous example, I just need to set the variable to the serial for my emulator:</p>
<pre class="brush: plain;">jsankey@caligula:~/work/my-app$ export ANDROID_SERIAL=emulator-5554
jsankey@caligula:~/work/my-app$ ant install
Buildfile: build.xml
    [setup] Android SDK Tools Revision 6
    [setup] Project Target: Android 2.2

...

install:
     [echo] Installing /home/jsankey/work/my-app/build/MyAppActivity-debug.apk onto default emulator or device...
     [exec] 277 KB/s (14446 bytes in 0.050s)
     [exec] 	pkg: /data/local/tmp/MyAppActivity-debug.apk
     [exec] Success

BUILD SUCCESSFUL
Total time: 4 seconds</pre>
<p>Presuming you have control of the environment in which adb is run, the ANDROID_SERIAL variable is probably your best bet for targeting a specific device.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/06/15/android-ant-builds-targeting-a-specific-device/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Understanding the Android Build Process</title>
		<link>http://www.alittlemadness.com/2010/06/07/understanding-the-android-build-process/</link>
		<comments>http://www.alittlemadness.com/2010/06/07/understanding-the-android-build-process/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 04:37:54 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=618</guid>
		<description><![CDATA[Introduction
In my previous post, I ran through how I set up a build for an Android project.  The build was based around the Ant and Eclipse support provided in the Android SDK. This time around, I&#8217;ll dig into what actually happens under the hood when you run an Android build.  This helped me [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>In my <a href="/2010/05/31/setting-up-an-android-project-build/">previous post</a>, I ran through how I set up a build for an Android project.  The build was based around the Ant and Eclipse support provided in the <a href="http://developer.android.com/sdk/index.html">Android SDK</a>. This time around, I&#8217;ll dig into what actually happens under the hood when you run an Android build.  This helped me to understand how everything fits together, which is key for diagnosing problems or making future changes.</p>
<h3>Overview of the Build Process</h3>
<p>The easiest way to get a handle on the build process as a whole is to trace the inputs and outputs at each stage, which I have drawn up in the graph below:</p>
<p><a href="http://www.alittlemadness.com/wp-content/uploads/android-build-2.png"><img src="http://www.alittlemadness.com/wp-content/uploads/android-build-2.png" alt="" title="android-build-2" width="459" height="895" class="aligncenter size-full wp-image-624" style="border: solid 1px #aaa; padding: 6px"/></a></p>
<p>Briefly, your source and resources are compiled, converted to run on the Android VM, and then packaged up in an <b>apk</b> file (a zip-compatible format).  In the following sections I&#8217;ll explain each step in a little more detail.  Note that throughout the explanations I will refer to the default input and output locations (e.g. <b>src/</b> for Java source, and <b>bin/</b> for binary output)  &#8212; if you customise these paths then adjust as necessary.</p>
<h3>Resource Pre-compilation</h3>
<p>The first step in the build process involves generation of Java source files from your Android resources.  The resources, stored in the <b>res</b> subdirectory, include such things as icons, layouts and strings.  These are compiled using the <b>aapt</b> tool into a file named <b>R.java</b>, stored in the <b>gen/</b> subdirectory.  If you take a look at the generated file, you will see that it defines a bunch of constants:</p>
<pre class="brush: java;">/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.zutubi.android.myapp;

public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040001;
        public static final int hello=0x7f040000;
    }
}</pre>
<p>The constants are used to refer to your resources, which are stored in the package file separately in a later step.</p>
<h3>Service Interface Pre-compilation</h3>
<p>The second build step also involves generation of Java source.  If your project uses any <a href="http://developer.android.com/guide/developing/tools/aidl.html">service interfaces</a>, you need to include the service interface definition files (which have an <b>.aidl</b> extension) in your project.  These files superficially resemble normal Java interfaces:</p>
<pre class="brush: plain;">package com.zutubi.android.myapp;

interface ISimpleService
{
    String echo(in String s);
}</pre>
<p>The <b>aidl</b> tool is used to generate actual Java interfaces for these services.  The Java source files will have the same name as the input files (with the <b>.aidl</b> extension replaced by <b>.java</b>) and are created in the <b>gen/</b> subdirectory.  These generated sources serve as a basis for you to implement or call the service interfaces in your own code.</p>
<h3>Java Compilation</h3>
<p>After the two pre-compilation steps, your project&#8217;s Java code is complete and ready to be compiled itself.  This step is a standard Java compilation from <b>.java</b> source files (both hand-crafted and generated) to <b>.class</b> bytecode files.  The binary bytecode files are stored in the <b>bin/classes</b> subdirectory.</p>
<p>One thing to be aware of is the <em>classpath</em> used to compile your source.  This includes:</p>
<ul>
<li>The <b>android.jar</b> file for your target Android platform.  This jar includes class and method stubs for all of the Android APIs.</li>
<li>External library jars you have added to your project (all <b>.jar</b> files in the <b>libs/</b> subdirectory).</li>
<li>For test projects only: the class files and external libraries for the <em>tested</em> project.</li>
</ul>
<h3>Bytecode Translation</h3>
<p>After compilation, you have standard Java bytecode, which would run on a standard Java VM.  However, Android uses its own <a href="http://en.wikipedia.org/wiki/Dalvik_(software)">Dalvik VM</a>, which requires a different bytecode format.  Thus, after compilation, the <b>dx</b> tool is used to translate your class files into a Dalvik executable or <b>.dex</b> file.  This includes the class files stored in any external library jars you have added to your project.  All classes are package up in a single output file, named <b>classes.dex</b>, which is produced in the <b>bin/</b> subdirectory.</p>
<h3>Resource Packaging</h3>
<p>Next, the resources are compiled into a partial Android package file.  This is done by the same <b>aapt</b> tool that generates Java source corresponding to the resources.  The resource package is created, named after your application with an <b>ap_</b> suffix in the <b>bin</b> directory.  You can use unzip to take a peek inside the package:</p>
<pre class="brush: plain;"> jsankey@caligula:~/work/my-app/build$ unzip -t MyApp.ap_
Archive:  MyApp.ap_
    testing: res/layout/main.xml      OK
    testing: AndroidManifest.xml      OK
    testing: resources.arsc           OK
    testing: res/drawable-hdpi/icon.png   OK
    testing: res/drawable-ldpi/icon.png   OK
    testing: res/drawable-mdpi/icon.png   OK
No errors detected in compressed data of MyApp.ap_.</pre>
<p>Note that although icon and layout files are included at their original location, they have been processed during packaging (presumably for more efficient storage and/or processing).  The icons appear to be optimised but still valid images, whereas layout XML files are converted to a binary format. Strings are compiled into the binary <b>resources.arsc</b> file.</p>
<h3>Debug Packaging and Signing</h3>
<p>Now all of the components required for the final Android package are ready to be bundled up into an <b>apk</b> file named after your application.  In the default debug mode, this build step also includes signing of the package with a debug key.  Note that for release, signing is a separate step that requires access to your own key (and may prompt for a password).  Android packages are assembled with the <b>apkbuidler</b> tool, which takes input from several sources:</p>
<ul>
<li>The Dalvik executable file <b>bin/classes.dex</b>.</li>
<li>All non-Java resources from your source directory (<b>src/</b>).</li>
<li>All non-Java resources from your external libraries (found by searching all <b>.jar</b> files in the <b>libs/</b> subdirectory).</li>
<li>Any native code shared-libraries included by your project.</li>
<li>The resource package built in the previous step.</li>
</ul>
<p>The produced package will be placed in the <b>bin/</b> subdirectory, named something like <b>MyApp-debug-unaligned.apk</b>.</p>
<h3>Alignment</h3>
<p>As a final optimisation step, the package file is aligned using the <b>zipalign</b> tool.  This step ensures that resources in the package file are aligned on 4-byte word boundaries.  This allows the Dalvik VM to memory-map those parts of the file for more efficient access.  You can read more about alignment on the <a href="http://android-developers.blogspot.com/2009/09/zipalign-easy-optimization.html">Android Developers Blog</a>.  This step takes the <b>-unaligned</b> package as input, and produces an output something like <b>bin/MyApp-debug.apk</b>.  This is the final, signed, aligned Android package &#8212; ready to be installed on an Android device!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/06/07/understanding-the-android-build-process/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript Compressor Comparison</title>
		<link>http://www.alittlemadness.com/2010/06/03/javascript-compressor-comparison/</link>
		<comments>http://www.alittlemadness.com/2010/06/03/javascript-compressor-comparison/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 07:13:29 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=583</guid>
		<description><![CDATA[So following on from last weeks post I have taken a closer look at the common tools used to compress JavaScript.  Below is a graph of the compression ratios that these tools achieved when applied to ext-all-debug.js, the uncompressed JavaScript from the popular ExtJS framework :

Some points to note are:

GZip is rather different from [...]]]></description>
			<content:encoded><![CDATA[<p>So following on from last weeks <a href="http://www.alittlemadness.com/2010/05/25/shrinksafe-via-ant/">post</a> I have taken a closer look at the common tools used to compress JavaScript.  Below is a graph of the compression ratios that these tools achieved when applied to ext-all-debug.js, the uncompressed JavaScript from the popular <a href="http://www.extjs.com/">ExtJS</a> framework :</p>
<p><a href="http://www.alittlemadness.com/wp-content/uploads/results-summary1.png"><img src="http://www.alittlemadness.com/wp-content/uploads/results-summary1.png" alt="" title="results-summary" width="447" height="319" class="alignleft size-full wp-image-606" /></a></p>
<p style="clear:both">Some points to note are:</p>
<ul>
<li>GZip is rather different from the other forms of compression in that it a compression of the content rather than the JavaScript, and therefore can be applied to compressed JavaScript.  I have included it in the graph to provide an indication of the level of compression it can provide. </li>
<li>The simple compilation option was used with Google Closure as this is typically the one that will be used.  For a discussion on why, check out the excellent post on <a href="http://www.jslog.com/google-closure-advanced-compilation">A Log of Javascript</a>.</li>
<li>Packer is similar to GZip in that it is more a compression of the content rather than JavaScript itself.  Unlike GZip however, it has a runtime cost associated with the unpacking of the JavaScript on each page load.</li>
</ul>
<p>Below is a graph of the processing time required for the above compressions:</p>
<p><a href="http://www.alittlemadness.com/wp-content/uploads/results-time-summary1.png"><img src="http://www.alittlemadness.com/wp-content/uploads/results-time-summary1.png" alt="" title="results-time-summary" width="438" height="316" class="alignleft size-full wp-image-608" /></a></p>
<p style="clear:both">Aside from Packer, there is not a lot of difference, and since all of the processing is done before deployment the compression cost does not impact the performance of the JavaScript.</p>
<p>The final graph below shows the compression ratios where each of the compressed JavaScript files are then GZipped, as is more typical of production environments.</p>
<p><a href="http://www.alittlemadness.com/wp-content/uploads/results-gzip-summary1.png"><img src="http://www.alittlemadness.com/wp-content/uploads/results-gzip-summary1.png" alt="" title="results-gzip-summary" width="437" height="316" class="alignleft size-full wp-image-609" /></a></p>
<p style="clear:both">In the final analysis it is clear that you should GZip your JavaScript, although be aware that not all <a href="http://schroepl.net/projekte/mod_gzip/browser.htm">browsers</a> correctly handle GZipped content.  As to which of the other compression tools you use, it comes down to your JavaScript.  My experience showed Packer to produce the best results for ext-all-debug.js whereas <a href="http://www.julienlecomte.net/blog/2007/08/13/">Julien found</a> that the YUI Compressor is a better choice for jQuery.  <a href="http://compressorrater.thruhere.net/">CompressorRater</a> can help with this task although it does not yet include Google Closure.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/06/03/javascript-compressor-comparison/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Setting Up An Android Project Build</title>
		<link>http://www.alittlemadness.com/2010/05/31/setting-up-an-android-project-build/</link>
		<comments>http://www.alittlemadness.com/2010/05/31/setting-up-an-android-project-build/#comments</comments>
		<pubDate>Mon, 31 May 2010 02:28:11 +0000</pubDate>
		<dc:creator>Jason</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Build]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=523</guid>
		<description><![CDATA[As the owner of a G1 I&#8217;ve played around at several times in the past with simple Android application development.  The SDK tools and introductory documentation provided by Google make it easy to get started.  Before embarking on a more serious project, however, I decided to figure out how to set up projects [...]]]></description>
			<content:encoded><![CDATA[<p>As the owner of a <a href="http://www.t-mobileg1.com/">G1</a> I&#8217;ve played around at several times in the past with simple Android application development.  The SDK tools and <a href="http://developer.android.com/index.html">introductory documentation</a> provided by Google make it easy to get started.  Before embarking on a more serious project, however, I decided to figure out how to set up projects in a systematic way that allows for both development and reliable scripted builds (e.g. for continuous integration).  Creating a project layout and build that works takes a few steps and tweaks, not all of which are covered in the documentation I found, so I thought I&#8217;d walk through my approach to it for those that are interested.</p>
<h3> Goals</h3>
<p>My goals for this project setup were as follows:</p>
<ul>
<li>Support for development in <a href="http://www.eclipse.org/">Eclipse</a> using the <a href="http://developer.android.com/guide/developing/eclipse-adt.html">ADT</a>.  Although Eclipse is not my first choice of IDE, it is decent enough and compelling when combined with the ADT tooling.</li>
<li>Support for building from the command-line, with no dependency on an IDE.</li>
<li>The ability to run tests on a device (or emulator) using the SDK <a href="http://developer.android.com/guide/topics/testing/testing_android.html">testing and instrumentation</a> support.</li>
<li>Containment of all components within a single directory which can be easily versioned.</li>
</ul>
<p>This all seems like it should be simple: the Android SDK has support for Eclipse, Ant builds, and testing.  However, putting it all together can take some work, because not all the pieces play together as nicely as you might hope.  First of all, although it is easy to create either an Eclipse project or an Ant one (and both cases are well documented), I would like both.  I experimented with two methods: creating an Eclipse project either before or after setting up the Ant build.  In my experience adding an Eclipse project to an existing Ant build proved more troublesome, and more difficult to debug if there was an issue.  So I recommend starting by setting up your projects (application and test) in Eclipse first.</p>
<h3>Setting Up the Eclipse Projects</h3>
<p>Select <strong>File &gt; New &gt; Project</strong>, and choose <strong>Android Project</strong> in the dialog:</p>
<p><a href="http://www.alittlemadness.com/wp-content/uploads/new-project.png"><img src="http://www.alittlemadness.com/wp-content/uploads/new-project.png" alt="" title="Choose Project Wizard" width="400" height="326" class="aligncenter size-full wp-image-553" /></a></p>
<p>Fill in your Project, Application, Package and Activity names:</p>
<p><a href="http://www.alittlemadness.com/wp-content/uploads/app-project.png"><img src="http://www.alittlemadness.com/wp-content/uploads/app-project.png" alt="" title="Application Project" width="400" height="517" class="aligncenter size-full wp-image-557" /></a></p>
<p>Click <strong>Next</strong>, then check the box to <strong>Create a Test Project</strong>.  So that both projects live under simple, single root directory, <em>uncheck</em> the <strong>Use default location</strong> box, and specify the location as the &#8220;test&#8221; subdirectory of your main application&#8217;s directory:</p>
<p><a href="http://www.alittlemadness.com/wp-content/uploads/test-project.png"><img src="http://www.alittlemadness.com/wp-content/uploads/test-project.png" alt="" title="test-project" width="400" height="517" class="aligncenter size-full wp-image-559" /></a></p>
<p>Click <strong>Finish</strong> and the easy part is done: you should have two new projects in your workspace, with the test project nested neatly within the main one<sup>1</sup>.  If you list the contents of your application&#8217;s directory, you should see something like:</p>
<pre class="brush: plain;">jsankey@caligula:~$ ls -a1 work/my-app/
.
..
AndroidManifest.xml
assets
bin
.classpath
default.properties
gen
.project
res
.settings
src
test</pre>
<p>Likewise, the test subdirectory should contain:</p>
<pre class="brush: plain;">jsankey@caligula:~$ ls -a1 work/my-app/test
.
..
AndroidManifest.xml
assets
bin
.classpath
default.properties
gen
.project
res
.settings
src</pre>
<h3>Adding a Simple Test Case</h3>
<p>At this point, it&#8217;s worth adding a simple test case to your test project as a starting point.  This will allow you to experiment with the Eclipse setup and (forthcoming) Ant builds.  To do this, navigate to your test project and select <strong>File &gt; New &gt; Class</strong>.  To actually utilise the instrumentation support in the test project, have your test class extend <strong>android.test.ActivityInstrumentationTestCase2</strong>:</p>
<p><a href="http://www.alittlemadness.com/wp-content/uploads/test-class.png"><img src="http://www.alittlemadness.com/wp-content/uploads/test-class.png" alt="" title="test-class" width="400" height="484" class="aligncenter size-full wp-image-567" /></a></p>
<p>You&#8217;ll need to fill in the generic type parameter for this class, add a constructor, and add a simple test case:</p>
<pre class="brush: java;">package com.zutubi.android.myapp.test;

import com.zutubi.android.myapp.MyAppActivity;

import android.test.ActivityInstrumentationTestCase2;

public class MyAppActivityTest extends ActivityInstrumentationTestCase2&lt;MyAppActivity&gt; {

	public MyAppActivityTest() {
		super(&quot;com.zutubi.android.myapp&quot;, MyAppActivity.class);
	}

	public void testSanity() {
		assertEquals(2, 1 + 1);
	}
}</pre>
<p>To try it out, select <strong>Run > Run As > Android JUnit Test</strong> and choose (or start) a device.  If the universe is in working order, the test should pass!</p>
<h3>Adding Ant Builds</h3>
<p>To add standard Android Ant builds to the existing projects, I used the android command line tool from the SDK.  Firstly, I added a build to the main application using <strong>android update project</strong>:</p>
<pre class="brush: plain;">jsankey@caligula:~$ cd /home/jsankey/work/my-app
jsankey@caligula:~/work/my-app$ android update project -p .
Updated local.properties
Added file ./build.xml
It seems that there are sub-projects. If you want to update them
please use the --subprojects parameter.</pre>
<p>As the output suggests, this command adds an Ant build.xml file to your project, and a local.properties file that stores the location of the Android SDK. Notice that the tool has picked up the fact that there is a test/ subdirectory with another project in it.  However, <strong>do not</strong> be tempted to take its advice to run with the &#8211;subprojects flag: this will treat your test project as a regular project.  Instead, change into the test/ subdirectory and run <strong>android update test-project</strong>:</p>
<pre class="brush: plain;">jsankey@caligula:~/work/my-app$ cd test
jsankey@caligula:~/work/my-app/test$ android update test-project -p . -m ..
Resolved location of main project to: /home/jsankey/work/my-app
Updated default.properties
Updated local.properties
Added file ./build.xml
Updated build.properties</pre>
<p>Again, the build.xml and local.properties files are added, although this time the build file will contain test rules.  Notice that the <em>relative</em> path to the main project is passed using the -m flag.  You can see the effect of this in the created build.properties file, which sets the value of tested.project.dir.</p>
<p>At this point you can build the project and run the tests from the command line.  First ensure that you have only one device available (e.g. one emulator running), then, in the test/ subdirectory where we left off, run <strong>ant run-tests</strong>:</p>
<pre class="brush: plain;">jsankey@caligula:~/work/my-app/test$ ant run-tests
... clipped several lines of output ...
run-tests:
     [echo] Running tests ...
     [exec]
     [exec] com.zutubi.android.myapp.test.MyAppActivityTest:.
     [exec] Test results for InstrumentationTestRunner=.
     [exec] Time: 0.158
     [exec]
     [exec] OK (1 test)
     [exec]
     [exec] 

BUILD SUCCESSFUL
Total time: 10 seconds</pre>
<p>Excellent: now we have the build and test working with both Eclipse and Ant (or so it seems&#8230;).</p>
<h3>Separating the Output Directories</h3>
<p>Although on the surface our two methods of building appear to work, once you start working in this environment you will notice problems.  Most likely, you will start to see errors showing up in your Eclipse build (or the Android Console) after builds from the command line.  By default both the Eclipse and Ant builds use the same folders for generated source (gen/) and output (bin/), which causes this conflict.  The Eclipse project does not react well to changes that occur underneath it.</p>
<p>To solve this problem, we can force the two different builds to use different output locations.  The Ant build makes it easy to override both the generated source and output locations, using gen.dir and out.dir properties.  The best place to set these properties is in the build.properties file for each of the projects:</p>
<pre class="brush: plain;">out.dir=build
gen.dir=build/gen</pre>
<p>Note that you will need to create build.properties for your main project (your test project should already have one).  I chose to put the gen/ directory used by Ant under the output directory just to tidy things up a little.</p>
<p>This should be all that is required, however, thanks to a <a href="http://code.google.com/p/android/issues/detail?id=8001">bug</a> in the default Ant rules, if you customise the output directory your test project will not find the output of your main project to build against.  To fix this, you need to edit test/build.xml, and add the line:</p>
<pre class="brush: xml;">&lt;property name=&quot;extensible.classpath&quot; value=&quot;${tested.project.absolute.dir}/${out.dir}/classes&quot;/&gt;</pre>
<p>just before the closing &lt;/project&gt; tag.  This assumes your test and main projects have the same out.dir, which although slightly lazy is a simpler than loading the main project&#8217;s properties to get its out.dir (and a sane assumption in my book).</p>
<p>You can make sure everything works by doing clean builds using Ant:</p>
<pre class="brush: plain;">jsankey@caligula:~/work/my-app/test$ cd ..
jsankey@caligula:~/work/my-app$ ant clean
Buildfile: build.xml
    [setup] Android SDK Tools Revision 6
    [setup] Project Target: Android 2.2
    [setup] API level: 8
    [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.
    [setup] Importing rules file: platforms/android-8/ant/ant_rules_r2.xml

clean:
   [delete] Deleting directory /home/jsankey/work/my-app/build
   [delete] Deleting directory /home/jsankey/work/my-app/build.gen

BUILD SUCCESSFUL
Total time: 0 seconds
jsankey@caligula:~/work/my-app$ cd test
jsankey@caligula:~/work/my-app/test$ ant clean run-tests
... clipped several lines of output ...
run-tests:
     [echo] Running tests ...
     [exec]
     [exec] com.zutubi.android.myapp.test.MyAppActivityTest:.
     [exec] Test results for InstrumentationTestRunner=.
     [exec] Time: 0.147
     [exec]
     [exec] OK (1 test)
     [exec]
     [exec] 

BUILD SUCCESSFUL
Total time: 9 seconds</pre>
<p>Then switch back to Eclipse, try editing and saving the activity and test cases and ensure everything is happy.</p>
<h3>Checking It In</h3>
<p>Finally, you can check the project into your version control system.  I won&#8217;t go into the tool-specific details, just make sure you check in the correct files.  You should <strong>exclude</strong>:</p>
<ul>
<li>The bin/, build/, and gen/ directories: as these all contain build output.</li>
<li>The files local.properties and test/local.properties: which are intended to contain properties specific to your machine (e.g. the location of the Android SDK).</li>
</ul>
<p>Make sure you don&#8217;t forget to check in the hidden Eclipse files .classpath, .project and .settings.  By structuring the projects so they live under a single top-level folder, it should be easy to add them to the version control server of your choice.</p>
<h3>Wrap Up</h3>
<p>That covers the basics of setting up an Android project, with tests, for both development and automated builds.  Although some tweaking is required, this setup is still built upon the tooling provided by the Android SDK.  I hope that this will mean it is easy to take advantage of new capabilities when new SDK versions are released (which happens pretty frequently at the moment!).</p>
<p>&#8211;<br />
<sup>1</sup> For reasons unknown to me, when my projects were first created they had build errors.  A clean rebuild magically fixed the problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/05/31/setting-up-an-android-project-build/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>ShrinkSafe via Ant</title>
		<link>http://www.alittlemadness.com/2010/05/25/shrinksafe-via-ant/</link>
		<comments>http://www.alittlemadness.com/2010/05/25/shrinksafe-via-ant/#comments</comments>
		<pubDate>Tue, 25 May 2010 04:50:05 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://www.alittlemadness.com/?p=526</guid>
		<description><![CDATA[The development of Pulse 2.2 has seen a steady increase in the amount of JavaScript we are using to drive our web front end.  To help maintain a snappy user experience, we have started to look at products / packages to reduce the size of our JavaScript.  The first such package that I [...]]]></description>
			<content:encoded><![CDATA[<p>The development of <a href="http://www.zutubi.com/products/pulse/">Pulse 2.2</a> has seen a steady increase in the amount of JavaScript we are using to drive our web front end.  To help maintain a snappy user experience, we have started to look at products / packages to reduce the size of our JavaScript.  The first such package that I encountered was <a href="http://shrinksafe.dojotoolkit.org/">ShrinkSafe</a>, a part of the <a href="http://dojotoolkit.org/">Dojo Toolkit</a>.</p>
<p>ShrinkSafe is packaged as a jar file, and with the js.jar (<a href="http://www.mozilla.org/rhino/">Rhino</a>) on the classpath, can be run as follows:</p>
<div class="codesnip-container" >java -jar shrinksafe.jar infile.js > outfile.js</div>
<p>To run this via <a href="http://ant.apache.org/">ant</a>, you could do something like:</p>
<pre class="brush: xml;">
&lt;java jar=&quot;shrinksafe.jar&quot; fork=&quot;true&quot; output=&quot;outfile.js&quot;&gt;
    &lt;arg value=&quot;infile.js&quot;/&gt;
    &lt;classpath&gt;
        &lt;pathelement location=&quot;js.jar&quot;/&gt;
    &lt;/classpath&gt;
&lt;/java&gt;
</pre>
<p>Whilst effective, this does become a little verbose when you need to apply it to multiple JavaScript files and does not support a dynamic set of files.  Since the Pulse build uses ant and has a growing set of JavaScript files, I created an ant task to drive ShrinkSafe, the source of which is available via <a href="http://github.com/danielostermeier/shrinksafe-ant-task">github</a>.  </p>
<p>So now we can run shrinksafe as follows:</p>
<pre class="brush: xml;">
&lt;taskdef name=&quot;shrinksafe&quot; classname=&quot;com.zutubi.ant.shrinksafe.ShrinksafeTask&quot;/&gt;
&lt;shrinksafe outputDir=&quot;${out.dir}&quot; sourceDir=&quot;${src.dir}&quot;&gt;
    &lt;include name=&quot;**/*.js&quot;/&gt;
&lt;/shrinksafe&gt;
</pre>
<p>We can also append a suffix to the processed files for when we version them.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.alittlemadness.com/2010/05/25/shrinksafe-via-ant/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
