title: Invoke "Maven Project Builder" in Eclipse on Run As -> Android Application (or manually)
body: <h1>Setup Android develop environment using Eclipse and Maven</h1>
  
  <p>I'm working on an Android project with Maven as build tool, and been playing around with the configuration of Eclipse, Android ADT, m2eclipse, Maven Android Plugin, Maven Integration for Android Development Tools, and Subclipse for a while now, but can't get it to work acceptable. That is, either the build time is unnecessarily long or the project is not rebuilt and deployed on the emulator as it should be.</p>
  
  <h2>Current configuration state</h2>
  
  <p>At the moment, I have done approximately the following with the result described below:</p>
  
  <ul>
  <li>Installed Eclipse Galileo (3.5.2) from the default repositories of Ubuntu 11.04.</li>
  <li>Installed the Eclipse plugins mentioned above and the Android SDK.</li>
  <li>Imported the project through <em>Check out Maven Projects from SCM</em>.</li>
  <li>Disabled all Builders for the project, except <em>Maven Project Builder</em>.</li>
  <li>Made sure <em>Build Automatically</em> is checked.</li>
  </ul>
  
  <h3>On file save</h3>
  
  <p><em>Maven Project Builder</em> is invoked and runs:</p>
  
  <ul>
  <li><pre>aapt [package, -m, -J, .../application/target/generated-sources/r, -M, .../application/AndroidManifest.xml, -S, .../application/target/generated-sources/combined-resources/res, -A, .../application/assets, -I, .../android-7/android.jar]</pre></li>
  <li><pre>dx [--dex, --output=.../application/target/classes.dex, .../application/target/android-classes]</pre></li>
  <li><pre>aapt [package, -f, -M, .../application/AndroidManifest.xml, -S, .../application/target/generated-sources/combined-resources/res, -A, .../application/target/generated-sources/combined-assets/assets, -I, .../android-7/android.jar, -F, .../target/xxx-android-project-1.0-SNAPSHOT.ap_]</pre></li>
  </ul>
  
  <p>Time taken: ~15 seconds, during which:</p>
  
  <ul>
  <li>Eclipse is blocked for subsequent user operations, such as Save.</li>
  <li>One CPU core is busy and the system gets sluggish.</li>
  </ul>
  
  <h3>On Run As -> Android Application</h3>
  
  <p>The application is installed and launched correctly within ~10 seconds.</p>
  
  <p>Output in Console (Android):</p>
  
  <pre>Android Launch!
  adb is running normally.
  Performing com.xxx.android.activity.LoginActivity activity launch
  Automatic Target Mode: Preferred AVD 'xxx_test_device' is available on emulator 'emulator-5554'
  Uploading xxx-android-project.apk onto device 'emulator-5554'
  Installing xxx-android-project.apk...
  Success!
  Starting activity com.xxx.android.activity.LoginActivity on device emulator-5554
  ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.xxx.android/.activity.LoginActivity }
  </pre>
  
  <h2>What I would like to happen</h2>
  
  <h3>On file save</h3>
  
  <p>Only the file being saved should be compiled and placed into the configured output folder, instantly quick.</p>
  
  <h3>On Run As -> Android Application</h3>
  
  <ul>
  <li><em>Maven Project Builder</em> is invoked, which performs the lengthy <strong>aapt</strong> and <strong>dx</strong> process, which is currently performed on every file save.</li>
  <li>The application is installed and launched on the emulator.</li>
  </ul>
  
  <h2>Questions</h2>
  
  <ul>
  <li>How do I achieve what is described above?</li>
  <li>Would it be possible to make Maven Project Builder run just before Run As -> Android Application but not on every file save? How?</li>
  </ul>
  
  <h2>Other approaches</h2>
  
  <p>I have tried to disable <em>Build Automatically</em>, enable all Builders and then run <em>Project</em> -> <em>Build Project</em> on the project in question, but then nothing happens and when I do <em>Run As</em> -> <em>Android Application</em> the code changes are not present.</p>
  
  <p>I have tried to disable the <em>Maven Project Builder</em> and enable all other Builders, enable <em>Build Automatically</em>, but after saving the file I see this in the Console (Android):</p>
  
  <pre><code>Refreshing resource folders.
  Starting incremental Pre Compiler: Checking resource changes.
  Nothing to pre compile!
  Starting incremental Package build: Checking resource changes.
  Starting full Post Compiler.
  Refreshing resource folders.
  Starting incremental Pre Compiler: Checking resource changes.
  Nothing to pre compile!
  </code></pre>
  
  <p>And on <em>Run As</em> -> <em>Android Application</em> the application crashes with this error:</p>
  
  <pre><code>java.lang.RuntimeException: Unable to instantiate application com.xxx.android.XXXApplication: java.lang.ClassNotFoundException: com.xxx.android.XXXApplication in loader dalvik.system.PathClassLoader@44bfe130
  </code></pre>
  
  <h2>Related questions</h2>
  
  <p><a href="https://stackoverflow.com/questions/2883635/android-compilation-is-slow-using-eclipse">Android compilation is slow &#40;using Eclipse&#41;.</a></p>
  
  <p>Explains why the <strong>dx</strong> process is so lengthy and suggests using Ant from the command line to build manually and only when necessary. I actually tried this, but couldn't get it to work with the Maven dependencies, even when copying all dependency JARs to <code>libs/</code> using a Maven plugin.</p>
  
  <p><a href="https://stackoverflow.com/questions/4658465/for-a-maven-project-in-eclipse-can-i-configure-menu-option-project-clean-to-invok">For a maven project in eclipse can I configure menu option Project/Clean to invoke mvn clean?</a></p>
  
  <p>Explains how to run specific goals after cleaning the project through Eclipse, but I would not like to clean the project before every installation on the emulator.</p>
  
  <h2>TL;DR</h2>
  
  <p>I would like Eclipse to run the <strong>aapt</strong> and <strong>dx</strong> processes through <em>Maven Project Builder</em> only when I do <em>Run As</em> -> <em>Android Application</em>, and not when saving a file. The application should then be launched in the emulator.</p>