Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing external jar file in Android Studio---NoClassDefFoundError

I created a new project using Android Studio and copied a jar file into my application's libs. Then added it as a library, which made the IDE recognize it. Later, I added it to the build.gradle and it now compiles just fine. However, when I attempt to run the application on a device it crashes with NoClassDefFoundError.

Attached and in order:

  1. The project tree on Android Studio. Notice the test-jar-to-import.jar inside HelloSheepProject/HelloSheep/libs/.
  2. The contents of the MainActivity.java. It is attempting to create a new MyFile. Nothing else.
  3. The build.gradle inside HelloSheepProject/HelloSheep/. I added the line compile files('libs/test-jar-to-import.jar').
  4. The contents of MyFile.java. It was created in Eclipse and exported as a jar file.
  5. The error when I trying to run it on a device.

Any ideas on what I am missing? I have tried a ./gradlew clean in HelloSheepProject but it 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
        at top.furrylamb.example.hellosheep.MainActivity.onCreate(MainActivity.java:15)
        at android.app.Activity.performCreate(Activity.java:5206)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
        at android.app.ActivityThread.access$600(ActivityThread.java:140)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4898)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
        at dalvik.system.NativeStart.main(Native Method)

like image 371
Daniel Avatar asked Dec 25 '22 23:12

Daniel


1 Answers

I found the solution to my problem. Since I've been lost on this for hours I'll keep the thread instead of deleting it.

The jar I was importing had been compiled/exported with Java 7 in mind. Once I enabled compliance with Java 6 it worked fine.

To sum it up, when adding an external jar file in Android Studio:

  1. Copy jar to root/project/libs folder;
  2. Right-click and add as library;
  3. Add the jar to root/project/build.gradle (something like compile files('libs/test-jar-to-import.jar'));
  4. Make sure the imported jar complies with Java 6 (7 will not do, for now).
like image 99
Daniel Avatar answered Dec 30 '22 17:12

Daniel