title: Instrumentation run failed due to 'java.lang.NoClassDefFoundError'
body: <p>I am trying to test out that an async task that performs an api request in the background and returns some data. Everything runs fine when run through the emulator, but I get a <code>NoClassDefFoundError</code> when I run the written test. The class that it seems to refer to is the builder class for the Google Cloud api class. If I comment out the api call code and return a hard coded Joke the test passes.</p>
  
  <p>Is there something else that has to be done to include the Google api libraries in the test?</p>
  
  <h3>Test</h3>
  
  <pre><code>package com.udacity.gradle.builditbigger;
  
  import android.test.AndroidTestCase;
  import android.util.Log;
  
  import org.chrisolsen.common.Joke;
  
  import java.util.concurrent.CountDownLatch;
  import java.util.concurrent.TimeUnit;
  
  public class JokeTaskTest extends AndroidTestCase {
  
      private static final String TAG = "JokeTaskTest";
      private CountDownLatch signal = null;
  
      @Override
      protected void setUp() throws Exception {
          Log.d(TAG, "setUp:");
          signal = new CountDownLatch(1);
      }
  
      @Override
      protected void tearDown() throws Exception {
          signal.countDown();
      }
  
      public void testTheAsyncTask() throws Throwable {
          final JokeTask task = new JokeTask(new JokeTask.JokeTaskListener() {
              @Override
              public void onJokeReceived(Joke joke) {
                  Log.d(TAG, "onJokeReceived: in the listeneer");
                  assertTrue(true);
                  signal.countDown();
              }
          });
  
          task.execute();
          signal.await(10, TimeUnit.SECONDS);
      }
  }
  
  package com.udacity.gradle.builditbigger;
  
  import android.os.AsyncTask;
  import android.util.Log;
  
  import com.google.api.client.extensions.android.http.AndroidHttp;
  import com.google.api.client.extensions.android.json.AndroidJsonFactory;
  import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
  import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
  
  import org.chrisolsen.backend.myApi.MyApi;
  import org.chrisolsen.common.Joke;
  
  import java.io.IOException;
  
  public class JokeTask extends AsyncTask&lt;Void, Void, Joke&gt; {
  
      public interface JokeTaskListener {
          void onJokeReceived(Joke joke);
      }
  
      private static final String TAG = "JokeTask";
      private JokeTaskListener mJokeListener;
      private MyApi mApiService;
  
      public JokeTask(JokeTaskListener listener) {
          mJokeListener = listener;
      }
  
      @Override
      protected Joke doInBackground(Void... voids) {
          if(mApiService == null) {  // Only do this once
              // THIS IS WHERE --+ IT SEEMS TO FAIL
              //                 V
              MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                      new AndroidJsonFactory(), null)
                      // options for running against local devappserver
                      // - 10.0.2.2 is localhost's IP address in Android emulator
                      // - turn off compression when running against local devappserver
                      .setRootUrl("http://10.0.2.2:8080/_ah/api/")
                      .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                          @Override
                          public void initialize(AbstractGoogleClientRequest&lt;?&gt; abstractGoogleClientRequest) throws IOException {
                              abstractGoogleClientRequest.setDisableGZipContent(true);
                          }
                      });
              mApiService = builder.build();
          }
  
          try {
              String text = mApiService.getJoke().execute().getData();
              Log.d(TAG, "doInBackground: " + text);
              return new Joke(text);
          } catch (IOException e) {
              Log.d(TAG, "fetch: " + e.getLocalizedMessage());
              return null;
          }
      }
  
      @Override
      protected void onPostExecute(Joke joke) {
          Log.d(TAG, "onPostExecute: JOKE: " + joke);
          mJokeListener.onJokeReceived(joke);
      }
  }
  </code></pre>
  
  <h3>Error details</h3>
  
  <pre><code>FATAL EXCEPTION: AsyncTask #1
          java.lang.RuntimeException: An error occured while executing doInBackground()
          at android.os.AsyncTask$3.done(AsyncTask.java:278)
          at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
          at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
          at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
          at java.util.concurrent.FutureTask.run(FutureTask.java:137)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
          at java.lang.Thread.run(Thread.java:856)
          Caused by: java.lang.NoClassDefFoundError: com.udacity.gradle.builditbigger.JokeTask$1
          at com.udacity.gradle.builditbigger.JokeTask.doInBackground(JokeTask.java:33)
          at com.udacity.gradle.builditbigger.JokeTask.doInBackground(JokeTask.java:16)
          at android.os.AsyncTask$2.call(AsyncTask.java:264)
  </code></pre>
  
  <h3>Manifest</h3>
  
  <p>
      </p>
  
  <pre><code>    &lt;!-- Include required permissions for Google Mobile Ads to run --&gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  
      &lt;application
          android:name="android.support.multidex.MultiDexApplication"
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
  
          &lt;!-- This meta-data tag is required to use Google Play Services. --&gt;
          &lt;meta-data
              android:name="com.google.android.gms.version"
              android:value="@integer/google_play_services_version" /&gt;
  
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;!-- Include the AdActivity configChanges and theme. --&gt;
          &lt;activity
              android:name="com.google.android.gms.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
              android:theme="@android:style/Theme.Translucent" /&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <h3>Gradle</h3>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 23
      buildToolsVersion "22.0.1"
  
      defaultConfig {
          applicationId "com.udacity.gradle.builditbigger"
          minSdkVersion 15
          targetSdkVersion 23
          versionCode 1
          versionName "1.0"
  
          multiDexEnabled true
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  
      dexOptions {
          javaMaxHeapSize "4g"
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      // Added for AdMob
      compile project(':common')
      compile project(':android')
  
      compile 'com.android.support:appcompat-v7:23.1.0'
      compile 'com.google.android.gms:play-services:8.3.0'
      compile 'com.android.support:multidex:1.0.0'
  }
  </code></pre>