android工作室:java.lang.NoClassDefFoundError

android studio: java.lang.NoClassDefFoundError

我在尝试使用 csvreader 时出现 java.lang.NoClassDefFoundError

错误

项目中,我把opencsv-3.2.jar放在libs文件夹下
在 gradle、

compile files ('libs/opencsv-3.2.jar')  

添加在依赖项中

对于我的 csv 文件,它被放在 src/main/assests/data.csv

在我的代码中:

 AssetManager assetManager = context.getAssets();
        try {
            InputStream csvStream = assetManager.open("data.csv");
            InputStreamReader csvStreamReader = new InputStreamReader(csvStream);
            CSVReader csvReader = new CSVReader(csvStreamReader);
            String [] nextLine;
            Log.d("test","reading csv");

           // error in following sentence
            while ((nextLine = csvReader.readNext()) != null) {
                // nextLine[] is an array of values from the line
                Log.d("test",nextLine.toString() + "etc...");
                break;
            }


        } catch (FileNotFoundException e) {
            Log.d("test","fail read csv");
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("test","io exception csv");
            e.printStackTrace();
        }`

异常:

    02-01 15:59:57.587    2271-2271/com.example.brian.myapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.brian.myapp, PID: 2271
        java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/lang3/StringUtils;
                at com.opencsv.CSVParser.isAllWhiteSpace(CSVParser.java:478)
                at com.opencsv.CSVParser.parseLine(CSVParser.java:338)
                at com.opencsv.CSVParser.parseLineMulti(CSVParser.java:270)
                at com.opencsv.CSVReader.readNext(CSVReader.java:226)
                at com.example.brian.myapplication.HandbookDatabase.readAndInsertData(HandbookDatabase.java:57)
                at com.example.brian.myapplication.HandbookDatabase.<init>(HandbookDatabase.java:40)
                at com.example.brian.myapplication.MainActivity.onCreate(MainActivity.java:50)
                at android.app.Activity.performCreate(Activity.java:5933)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                at android.app.ActivityThread.access0(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.lang3.StringUtils" on path: DexPathList[[zip file "/data/app/com.example.brian.myapp-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
                at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                at com.opencsv.CSVParser.isAllWhiteSpace(CSVParser.java:478)
                at com.opencsv.CSVParser.parseLine(CSVParser.java:338)
                at com.opencsv.CSVParser.parseLineMulti(CSVParser.java:270)
                at com.opencsv.CSVReader.readNext(CSVReader.java:226)
                at com.example.brian.myapplication.HandbookDatabase.readAndInsertData(HandbookDatabase.java:57)
                at com.example.brian.myapplication.HandbookDatabase.<init>(HandbookDatabase.java:40)
                at com.example.brian.myapplication.MainActivity.onCreate(MainActivity.java:50)
                at android.app.Activity.performCreate(Activity.java:5933)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                at android.app.ActivityThread.access0(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
        Suppressed: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils
                at java.lang.Class.classForName(Native Method)
                at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                ... 21 more
         Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

您需要添加 commons-lang3 作为依赖项。

'org.apache.commons:commons-lang3:3.0'

其中包含缺失的 class org/apache/commons/lang3/StringUtils