title: ExceptionWithContext gets thrown when trying to build an Android app with Ant
body: <p>I've tried searching around both on Google and on stackoverflow for an answer to this, but I've been unable to find anyone with the exact issue I'm having. I'm attempting to set up a continuous integration server (Bamboo, specifically) to update, build, and export an APK every time someone makes a change in source control. I'm running into the same error both on my local machine when I do every step by hand and on the server when I use the job I've set up. The error happens when I reach the dex step of the build. I've gotten the same output so far with <code>ant debug</code>, <code>ant release</code>, <code>ant clean debug</code>, and <code>ant clean release</code>. The output of the entire dex step, complete with error, is as follows:</p>
  
  <pre><code>-dex:
        [dex] input: C:\Users\...\Android\bin\classes
        [dex] input: C:\Users\...\google-play-services_lib\bin\classes.jar
        [dex] input: C:\Program Files (x86)\Android\android-sdk\tools\support\annotations.jar
        [dex] input: C:\Users\...\Android\libs\FlurryAgent.jar
        [dex] input: C:\Users\...\Android\libs\gcm.jar
        [dex] input: C:\Users\...\Android\libs\android-support-v4.jar
        [dex] input: C:\Users\...\google-play-services_lib\libs\google-play-services.jar
        [dex] Pre-Dexing C:\Users\...\google-play-services_lib\bin\classes.jar -&gt; classes-64c0adfe92ddc950c7ab8c5002ceabf2.jar
        [dex] Pre-Dexing C:\Program Files (x86)\Android\android-sdk\tools\support\annotations.jar -&gt; annotations-62bab95d6948a2db17bbc7976160b014.jar
        [dex] Pre-Dexing C:\Users\...\Android\libs\FlurryAgent.jar -&gt; FlurryAgent-499d43756a3ce626a64773e6dfd5eaec.jar
        [dex] Pre-Dexing C:\Users\...\Android\libs\gcm.jar -&gt; gcm-ae2640f44640eb4fd7b13964b65d2d70.jar
        [dex] Pre-Dexing C:\Users\...\Android\libs\android-support-v4.jar -&gt; android-support-v4-fa30b373a3e3ba9f2cf94900a9eb42fe.jar
        [dex] Pre-Dexing C:\Users\...\google-play-services_lib\libs\google-play-services.jar -&gt; google-play-services-9efad6e9178399c185fae6c0b6bdc4c6.jar
        [dex] Converting compiled files and external libraries into C:\Users\...\Android\bin\classes.dex...
         [dx]
         [dx] UNEXPECTED TOP-LEVEL EXCEPTION:
         [dx] com.android.dx.util.ExceptionWithContext
         [dx]     at com.android.dx.util.ExceptionWithContext.withContext(ExceptionWithContext.java:46)
         [dx]     at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:344)
         [dx]     at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:134)
         [dx]     at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:87)
         [dx]     at com.android.dx.command.dexer.Main.processClass(Main.java:487)
         [dx]     at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
         [dx]     at com.android.dx.command.dexer.Main.access$400(Main.java:67)
         [dx]     at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:135)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
         [dx]     at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
         [dx]     at com.android.dx.command.dexer.Main.processOne(Main.java:422)
         [dx]     at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
         [dx]     at com.android.dx.command.dexer.Main.run(Main.java:209)
         [dx]     at com.android.dx.command.dexer.Main.main(Main.java:174)
         [dx]     at com.android.dx.command.Main.main(Main.java:91)
         [dx] Caused by: java.lang.NullPointerException
         [dx]     at com.android.dx.cf.code.ConcreteMethod.&lt;init&gt;(ConcreteMethod.java:87)
         [dx]     at com.android.dx.cf.code.ConcreteMethod.&lt;init&gt;(ConcreteMethod.java:75)
         [dx]     at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:247)
         [dx]     ... 23 more
         [dx] ...while processing &lt;init&gt; (Lcom/.../android/LocationService;)V
         [dx] ...while processing com/.../android/LocationService$1.class
         [dx]
         [dx] 1 error; aborting
  </code></pre>
  
  <p>For context, I'm using Ant v1.9.2 and Android build-tools v18.0.1 on a Windows machine and I haven't edited the build scripts in any way. I generated one for the app and one for the library using <code>android update project --path .</code> in their two directories. I also haven't tried setting it up to automatically use the proper keystore for signing yet, although to my (limited) understanding that shouldn't be necessary, at least not for a debug build with Ant.</p>
  
  <p>Has anyone seen this particular issue before? Is it a problem with the generated .class file? The build files? This is my first real foray into building with Ant (I generally just let Eclipse do all the hard work for me), so I have very little to go on. Any help would be much appreciated.</p>
  
  <p><del><strong>Update:</strong> In case anyone was paying attention to this question, my issue seems to have resolved itself. How and why, I don't know. I tried updating the source this morning (we had a few changes in), reran <code>android update project -p .</code>, tried an <code>ant clean debug</code>, and lo and behold, it worked. As did <code>ant release</code>, which even signed it properly with the key I gave it. My best guess is that there was something weird in that LocationService class file, although what it was is beyond me.</del></p>
  
  <p><strong>Update 2:</strong> Anything I said in my first update is now invalid. I've isolated the issue, but am no closer to understanding it. This block of code is the culprit:</p>
  
  <pre><code>if (Settings.DEBUG) {
      Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
          @Override
          public void uncaughtException(Thread thread, Throwable ex) {
              storeDebugNotification(AndroidUncaughtExceptionHandler.getStackTraceString(ex));
          }
      });
  }
  </code></pre>
  
  <p>Here's where things get weird. When the <code>Settings.DEBUG</code> flag is <code>true</code>, this builds fine with ant. When it's <code>false</code>, it fails, giving me the error shown above. When I comment the whole thing out, it works fine with either <code>DEBUG</code> setting. The same goes for having the <code>if (Settings.DEBUG)</code> line and its curly braces commented but the body left intact, as well as commenting the body and leaving the if portion alone. So... I'm at a loss here. Something about the interaction between the if statement and the body, in this particular file, when <code>DEBUG</code> is false is causing problems. And the other weird part is that we have that <em>exact same if block</em> in another file in the app (an activity, whereas this one is a service).</p>