Importing external jar file in Android Studio---NoClassDefFoundError -


i created new project using android studio , copied jar file application's libs. added library, made ide recognize it. later, added build.gradle , compiles fine. however, when attempt run application on device crashes noclassdeffounderror.

attached , in order:

  1. the project tree on android studio. notice test-jar-to-import.jar inside hellosheepproject/hellosheep/libs/.
  2. the contents of mainactivity.java. attempting create new myfile. nothing else.
  3. the build.gradle inside hellosheepproject/hellosheep/. added line compile files('libs/test-jar-to-import.jar').
  4. the contents of myfile.java. created in eclipse , exported jar file.
  5. the error when trying run on device.

any ideas on missing? have tried ./gradlew clean in hellosheepproject didn't help.


enter image description here


package top.furrylamb.example.hellosheep;  import android.os.bundle; import android.app.activity;  import top.furrylamb.example.myfile;  public class mainactivity extends activity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          myfile myfile = new myfile("hi", "there");     }  } 

buildscript {     repositories {         mavencentral()     }     dependencies {         classpath 'com.android.tools.build:gradle:0.5.+'     } } apply plugin: 'android'  repositories {     mavencentral() }  dependencies {     compile 'com.android.support:support-v4:13.0.+'     //compile filetree(dir: "libs", include: "*.jar")     compile files('libs/test-jar-to-import.jar') }  android {     compilesdkversion 17     buildtoolsversion "17.0.0"      defaultconfig {         minsdkversion 14         targetsdkversion 16     } } 

package top.furrylamb.example;  public class myfile {     public final string left;     public final string right;     public myfile(string left, string right) {         this.left = left;         this.right = right;     } } 

07-19 19:26:33.855  13656-13656/? e/androidruntime: fatal exception: main         java.lang.noclassdeffounderror: top.furrylamb.example.myfile         @ top.furrylamb.example.hellosheep.mainactivity.oncreate(mainactivity.java:15)         @ android.app.activity.performcreate(activity.java:5206)         @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1083)         @ android.app.activitythread.performlaunchactivity(activitythread.java:2064)         @ android.app.activitythread.handlelaunchactivity(activitythread.java:2125)         @ android.app.activitythread.access$600(activitythread.java:140)         @ android.app.activitythread$h.handlemessage(activitythread.java:1227)         @ android.os.handler.dispatchmessage(handler.java:99)         @ android.os.looper.loop(looper.java:137)         @ android.app.activitythread.main(activitythread.java:4898)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:511)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773)         @ dalvik.system.nativestart.main(native method) 

i found solution problem. since i've been lost on hours i'll keep thread instead of deleting it.

the jar importing had been compiled/exported java 7 in mind. once enabled compliance java 6 worked fine.

to sum up, when adding external jar file in android studio:

  1. copy jar root/project/libs folder;
  2. right-click , add library;
  3. add jar root/project/build.gradle (something compile files('libs/test-jar-to-import.jar'));
  4. make sure imported jar complies java 6 (7 not do, now).

Comments