small medium large xlarge

Generic-user-small
13 Jun 2016, 14:52
H. Paul Robertson (9 posts)

I’m running into an error in the Android chapter, Day 1, the section “Making a Request with OkHttp” that I can’t figure out how to solve.

(And no, it’s not the error that the book says I should run into =)

I added OkHttp to the build.gradle file in the dependencies section.

It’s working fine in Android Studio (it added the import statement and it’s not showing an error in the editor.

However, when I run the app I get an error that looks like the OkHttp library isn’t found on the device (maybe it’s not being copied into the app bundle?)

This is the error message:

06-13 09:29:34.488 17826-17826/com.probertson.sevenmobileapps.currencyconverter E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.probertson.sevenmobileapps.currencyconverter, PID: 17826
  java.lang.NoClassDefFoundError: Failed resolution of: Lcom/squareup/okhttp/OkHttpClient;
  at com.probertson.sevenmobileapps.currencyconverter.ConversionActivity.onCreate(ConversionActivity.java:39)
  at android.app.Activity.performCreate(Activity.java:6237)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
  at android.app.ActivityThread.-wrap11(ActivityThread.java)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5417)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: java.lang.ClassNotFoundException: Didn't find class "com.squareup.okhttp.OkHttpClient" on path: DexPathList[[dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-support-annotations-23.4.0_6afbafee57ff25f6ae65a75701217da50e70f13e-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-internal_impl-23.4.0_342ec0ecc4e73e435f05823d406bb9c603a0c363-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.4.0_2dde827177ea289beb204fc63192619722cca3d7-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-support-v4-23.4.0_6c3c507cec71ef43041a30a9a6a49e93772bc8ea-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-recyclerview-v7-23.4.0_8f9755b88ff0893bde22a735ef3ce1694eb0f8a4-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-design-23.4.0_f6396606dfc67e70e90a7684e4e0f44ad49afad5-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.4.0_687e49494c4be3a44229d0490cf1911a8d1d455b-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.4.0_799e71ebda4cdceac8ddad9fa1305acfe6bdc15f-classes.dex"],nativeLibraryDirectories=[/data/app/com.probertson.sevenmobileapps.currencyconverter-1/lib/x86, /vendor/lib, /system/lib]]
                                                                                                  	at 
06-13 09:29:34.488 17826-17826/com.probertson.sevenmobileapps.currencyconverter D/Error: ERR: exClass=java.lang.ClassNotFoundException
06-13 09:29:34.489 17826-17826/com.probertson.sevenmobileapps.currencyconverter D/Error: ERR: exMsg=Didn't find class "com.squareup.okhttp.OkHttpClient" on path: DexPathList[[dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-support-annotations-23.4.0_6afbafee57ff25f6ae65a75701217da50e70f13e-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-internal_impl-23.4.0_342ec0ecc4e73e435f05823d406bb9c603a0c363-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.4.0_2dde827177ea289beb204fc63192619722cca3d7-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-support-v4-23.4.0_6c3c507cec71ef43041a30a9a6a49e93772bc8ea-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-recyclerview-v7-23.4.0_8f9755b88ff0893bde22a735ef3ce1694eb0f8a4-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-design-23.4.0_f6396606dfc67e70e90a7684e4e0f44ad49afad5-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.4.0_687e49494c4be3a44229d0490cf1911a8d1d455b-classes.dex", dex file "/data/data/com.probertson.sevenmobileapps.currencyconverter/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.4.0_799e71ebda4cdceac8ddad9fa1305acfe6bdc15f-classes.dex"],nativeLibraryDirectories=[/data/app/com.probertson.sevenmobileapps.currencyconverter-1/lib/x86, /vendor/lib, /system/lib]]
Generic-user-small
13 Jun 2016, 15:58
H. Paul Robertson (9 posts)

I was able to resolve this by using (some of) the instructions here: http://stackoverflow.com/questions/35578135/noclassdeffounderror-for-okhttpclient

Specifically I did the following:

  1. Add multiDexEnabled true to the defaultConfig section
  2. Added compile 'com.android.support:multidex:1.0.0' to the dependencies section
  3. Modified the compile statement for OkHttp to 'compile 'com.squareup.okhttp:okhttp:2.5.0'
  4. Clean and Re-Build and Sync (and Clean and Re-Build and Sync again a couple more times!)

I have no idea what the implications of enabling multidex are, so hopefully this won’t impact the app as I move forward.

Mypicture_pragsmall
14 Jun 2016, 03:49
Tony Hillerson (39 posts)

That’s strange. I wonder if this is a recent problem caused by newer versions of Android Studio running older generated code. What version of Android Studio are you using?

I need to test this a bit before I make a change to the code.

Generic-user-small
14 Jun 2016, 16:48
H. Paul Robertson (9 posts)

I just installed Android Studio the same day I posted my original comment, so it was the newest build at that point. It looks like I have v2.1.2

You must be logged in to comment