title: How to import Guava into Android applications
body: <p>What is the proper way to import Guava into an Android project?  Every time I try to use it I get a <code>NoClassDefFoundError</code>.</p>
  
  <p>This is what I'm doing to generate the crash.  I'm using Android Studio 3.0 Canary 7.</p>
  
  <ol>
  <li>Create an new project <code>File > New > New Project</code>, target <code>API 26.0</code>, using the <code>Empty Activity</code> template.</li>
  <li><p>Add to <code>app/build.gradle</code> in the <code>dependencies</code> section</p>
  
  <pre><code>implementation "com.google.guava:guava:20.0"
  </code></pre></li>
  <li><p>Add this to the onCreate method in <code>MainActivity.java</code></p>
  
  <pre><code>ImmutableList&lt;String&gt; foo = ImmutableList.of("A", "B", "C");
  Log.d("MainActivity", foo.get(0));
  </code></pre></li>
  <li><p>Run the App and open up Logcat to see this exception:</p>
  
  <pre><code>FATAL EXCEPTION: main
  Process: com.letsdoit.guavaissue, PID: 14366
  java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/common/collect/ImmutableList;
      at com.letsdoit.guavaissue.MainActivity.onCreate(MainActivity.java:20)
      at android.app.Activity.performCreate(Activity.java:6679)
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
      at android.app.ActivityThread.-wrap12(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6119)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
   Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.common.collect.ImmutableList" on path: DexPathList[[zip file "/data/app/com.letsdoit.guavaissue-1/base.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.letsdoit.guavaissue-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.letsdoit.guavaissue-1/lib/x86, /system/lib, /vendor/lib]]
      at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
      at com.letsdoit.guavaissue.MainActivity.onCreate(MainActivity.java:20) 
      at android.app.Activity.performCreate(Activity.java:6679) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6119) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  </code></pre></li>
  </ol>
  
  <p>I'm almost certain it has to do with Gauva being large and not playing well with multidex, but am not sure what to do about it.  These are some of the note worthy things I've tried to no avail:</p>
  
  <ol>
  <li><p>Enabling multidex and specifying ImmutableList in the <a href="https://developer.android.com/studio/build/multidex.html" rel="nofollow noreferrer">multiDexKeepFile</a>.</p></li>
  <li><p>Disabling instant run.</p></li>
  <li><p>Pulled the APKs from the device and verified the Guava classes are in the APKs.</p></li>
  <li><p>Following the recommendations in this stack overflow <a href="https://stackoverflow.com/questions/14978699/is-it-a-good-idea-to-use-google-guava-library-for-android-development">question</a>.</p></li>
  </ol>