Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Zohaib Ahmad
2,843 PointsError java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient at capptify.com.stormy.MainActivity
Im getting this error, and no idea how to fix it.
Any help would be appreciated.
Here is my Logcat:
09-14 13:32:31.690 1695-1695/capptify.com.stormy E/dalvikvm﹕ Could not find class 'com.squareup.okhttp.OkHttpClient', referenced from method capptify.com.stormy.MainActivity.onCreate
09-14 13:32:31.690 1695-1695/capptify.com.stormy W/dalvikvm﹕ VFY: unable to resolve new-instance 37 (Lcom/squareup/okhttp/OkHttpClient;) in Lcapptify/com/stormy/MainActivity;
09-14 13:32:31.700 1695-1695/capptify.com.stormy D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x0050
09-14 13:32:31.700 1695-1695/capptify.com.stormy D/dalvikvm﹕ DexOpt: unable to opt direct call 0x002b at 0x58 in Lcapptify/com/stormy/MainActivity;.onCreate
09-14 13:32:31.700 1695-1695/capptify.com.stormy D/dalvikvm﹕ DexOpt: unable to opt direct call 0x002d at 0x64 in Lcapptify/com/stormy/MainActivity;.onCreate
09-14 13:32:31.700 1695-1695/capptify.com.stormy I/dalvikvm﹕ Failed resolving Lcapptify/com/stormy/MainActivity$1; interface 36 'Lcom/squareup/okhttp/Callback;'
09-14 13:32:31.700 1695-1695/capptify.com.stormy W/dalvikvm﹕ Link of class 'Lcapptify/com/stormy/MainActivity$1;' failed
09-14 13:32:31.700 1695-1695/capptify.com.stormy D/dalvikvm﹕ DexOpt: unable to opt direct call 0x0014 at 0x82 in Lcapptify/com/stormy/MainActivity;.onCreate
09-14 13:32:31.890 1695-1695/capptify.com.stormy D/AndroidRuntime﹕ Shutting down VM
09-14 13:32:31.890 1695-1695/capptify.com.stormy W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1ababa8)
09-14 13:32:31.920 1695-1695/capptify.com.stormy E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: capptify.com.stormy, PID: 1695
java.lang.NoClassDefFoundError: com.squareup.okhttp.OkHttpClient
at capptify.com.stormy.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
09-14 13:37:32.450 1695-1695/capptify.com.stormy I/Process﹕ Sending signal. PID: 1695 SIG: 9
Zohaib Ahmad
2,843 PointsHarry James Thanks so much!
Here is my app file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "capptify.com.stormy"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.squareup.okhttp:okhttp:2.5.0'
}
Harry James
14,780 PointsHey again Zohaib!
Looks like you do in fact have the OkHttp dependency - that rules that out.
I think I now know what is causing this but just so that I can check, can I ask that you paste your MainActivity.java contents here, including the full list of imports.
I'll be in touch tomorrow :)
3 Answers
Zohaib Ahmad
2,843 PointsHi Harry James !
Sorry for the late response, I've been so discouraged that this hasn't been working :( I think that the problem has to do with the "OkHttpClient client = new OkHttpClient();" line, when I check the log, it says class not found. Also, I have a new version of Android Studip (6.0) hopefully that's not what is causing the problem. Lastly, now I just lost my R class... It says cannot resolve symbol and the R goes in red.
Im in the middle of a video, but here is my MainActivity.Java:
package capptify.com.stormy;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import java.io.IOException;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String apiKey = "7ca1a9a89c6d46587bba680d61913531";
double latitude = 37.8267;
double longitude = -122.423;
String forecastURL = "https://api.forecast.io/forecast/" + apiKey +
"/" + latitude + "," + longitude;
if (isNetworkAvailable) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastURL)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
Log.v(TAG, response.body().string());
if (response.isSuccessful()) {
} else {
alertUserAboutError();
}
} catch (IOException e) {
Log.e(TAG, "Exception caught: ", e);
}
}
});
}
Log.d(TAG, "Main UI code is running!");
}
private void alertUserAboutError() {
AlertDialogueFragment dialog = new AlertDialogueFragment();
dialog.show(getFragmentManager(), "error_dialog");
}
}
Zohaib Ahmad
2,843 PointsHey Harry James Are you there? Can you please help with this?
Harry James
14,780 PointsHey Zohaib!
Sorry for my late response! I have been away over the weekend so weren't able to take a look at this. I'll make sure to get back to you tomorrow though :)
Harry James
14,780 PointsHey again Zohaib!
It looks like you've written your code correctly here as well so it leads me to believe that this error is being caused during the building process somehow (This is a compile-time error, not a runtime error).
First of all, we can fix the issue with the R class usually by just regenerating it. To do this, go to the Project view on the dropdown and navigate to Stormy >> app >> build >> generated >> source >> r >> debug >> capptify.com.stormy. Then, right-click on the R class and Delete it - it will regenerate when you next build your project.
As for your main issue, if it is still not fixed then I would like you to post your project files here so that I can take a look into this a bit more. I have a few things I would like to try out to see if they solve the issue so if you're still having issues, follow the below instructions to share your project:
## WARNING: Before you continue, you may wish to remove your API Key for Stormy. This is not a major deal as it's still possible for people to discover your API key when the app makes a direct connection to Forecast.io - if someone had access to the key it only means they can call for weather data from your key. ##
To share your project, right-click on your project in Android Studio and press Show in Explorer. Then, upload your project directory into a service like Google Drive, SkyDrive or another service of your choice.
Then, share the project via a URL and I'll download it and try and reproduce your problem, then find a solution.
Hopefully we can get the issue solved soon :)
albara khaled
5,018 PointsAny update about this issue, I'm getting the same :(
Harry James
14,780 PointsHey albara,
Sorry for the late response.
If you're still having issues, could you please provide your MainActivity.java and build.gradle for the app module here so that I could take a look into the issue further?


Harry James
14,780 PointsHarry James
14,780 PointsHey Zohaib!
Could you please provide your build.gradle file for the app module and I'll take a look for you :)