NoClassDefFoundError with libraries in Android Stu

2019-01-22 07:25发布

问题:

This question already has an answer here:

  • Android Studio: Add jar as library? 34 answers

I've spent hours trying to get a library to work with my project in Android Studio, and I just can't figure it out.

Here's what my dependencies look like for my module

And my libraries:

I've even added

compile files('libs/signpost-commonshttp4-1.2.1.2.jar')
compile files('libs/signpost-core-1.2.1.2.jar')

to my build.gradle. Without this, the app would not even compile.

At this stage, when I try to reference one of the libraries in code, the app crashes with a NoClassDefFoundError exception.

Something tells me this shouldn't be this hard - what am I overlooking?

LogCat:

05-17 21:51:15.676: I/ActivityManager(714): START u0 {flg=0x10104000 cmp=com.crazydog.splitwiser/.InitialSplash bnds=[328,402][656,692]} from pid 1513
05-17 21:51:15.896: I/ActivityManager(714): Start proc com.crazydog.splitwiser for activity com.crazydog.splitwiser/.InitialSplash: pid=18174 uid=10288 gids={50288, 3003, 1028}
05-17 21:51:16.326: I/ActivityManager(714): Displayed com.crazydog.splitwiser/.InitialSplash: +437ms (total +1m9s209ms)
05-17 21:51:17.017: I/ActivityManager(714): START u0 {cmp=com.crazydog.splitwiser/.LogIn} from pid 18174
05-17 21:51:17.097: E/dalvikvm(18174): Could not find class 'oauth.signpost.commonshttp.CommonsHttpOAuthConsumer', referenced from method com.crazydog.splitwiser.Toolkit.oAuthTransaction
05-17 21:51:17.097: W/dalvikvm(18174): VFY: unable to resolve new-instance 662 (Loauth/signpost/commonshttp/CommonsHttpOAuthConsumer;) in Lcom/crazydog/splitwiser/Toolkit;
05-17 21:51:17.097: D/dalvikvm(18174): DexOpt: unable to opt direct call 0x112f at 0x08 in Lcom/crazydog/splitwiser/Toolkit;.oAuthTransaction
05-17 21:51:17.107: E/AndroidRuntime(18174):    at com.crazydog.splitwiser.Toolkit.oAuthTransaction(Toolkit.java:65)
05-17 21:51:17.107: E/AndroidRuntime(18174):    at com.crazydog.splitwiser.LogIn.onResume(LogIn.java:116)
05-17 21:51:17.117: W/ActivityManager(714):   Force finishing activity com.crazydog.splitwiser/.LogIn
05-17 21:51:17.117: W/ActivityManager(714):   Force finishing activity com.crazydog.splitwiser/.InitialSplash
05-17 21:51:17.618: W/ActivityManager(714): Activity pause timeout for ActivityRecord{40d040d0 u0 com.crazydog.splitwiser/.LogIn}

回答1:

I had the exact same problem, And like you i thought it's enough to add the

compile files('libs/signpost-commonshttp4-1.2.1.2.jar')
compile files('libs/signpost-core-1.2.1.2.jar')

It all worked after i did a ./gradlew clean (I'm on a mac, so in your case, the equivalent.) You should do this command from the Terminal/Command prompt when you're located on the root of your project.