JavaCV库上的NoClassDefFoundError和ClassNotFoundException

时间:2015-10-19 15:27:33

标签: java android javacv

我正在尝试在Android Studio中使用JavaCV,但每次我尝试使用该库并运行它时,它都会崩溃并且logcat会显示此错误..

01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime: FATAL 

EXCEPTION: main
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime: java.lang.NoClassDefFoundError: java.lang.ClassNotFoundException: org.bytedeco.javacpp.opencv_core
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at org.bytedeco.javacpp.Loader.load(Loader.java:392)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at org.bytedeco.javacpp.Loader.load(Loader.java:358)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at org.bytedeco.javacpp.opencv_imgcodecs.<clinit>(opencv_imgcodecs.java:13)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at com.example.user.dmr.MainActivity.onCreate(MainActivity.java:20)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5122)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1084)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread.access$600(ActivityThread.java:162)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:107)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:194)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5392)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:  Caused by: java.lang.ClassNotFoundException: org.bytedeco.javacpp.opencv_core
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at java.lang.Class.classForName(Native Method)
01-03 09:16:32.242 15233-15233/com.example.user.dmr E/AndroidRuntime:     at java.lang.Class.forName(Class.java:217)

这是我正在尝试运行的Android Studio中的java代码:

package com.example.user.dmr;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView v = (TextView) findViewById(R.id.textview1);

        IplImage image = cvLoadImage("drawable/face1.JPG"); //crashes when it read this line

        if (image == null) {
            v.setText("Fail");
        } else {
            v.setText("Succeed");
        }
    }
}

我已经在外部库中添加了javacv库。这是我在build.gradle中添加的依赖项。

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile group: 'org.bytedeco', name: 'javacv', version: '1.0'
}

知道为什么会一直发生以及如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:0)

我认为你也需要javacpp-presets。

在您的gradle依赖项中尝试此操作:

compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.0.0' + '-1.0'

compile group: 'org.bytedeco', name: 'javacv', version: '1.0'