Bob Woodley
Bob Woodley

Reputation: 1304

Android Studio, OpenCV jar, java.lang.ClassNotFoundException: org.opencv.R$styleable

Most examples on the web regarding how to use OpenCV with Android Studio involve importing the OpenCV module into your project. However, I am trying to use OpenCV as a jar so that I avoid cluttering my source control repository with copies of OpenCV.

So these are the steps I am doing for my app called 'app'.

A. Create a libs directory under /Project/apps. Put the OpenCV jar there. Also put the opencv native libs there, in sub-directories.

B. In /Project/app/build.gradle do the following

Put these lines at the top:

repositories { flatDir { dirs 'libs'    } }

Put these lines in the android section:

sourceSets.main.jniLibs.srcDirs = ['libs']

Put this line in the dependencies section:

    compile fileTree(dir: 'libs', include: ['*.jar'])

C. Add these lines to AndroidManifest.xml, after the application tag.

    <uses-sdk android:minSdkVersion="9"/>
    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature android:name="android.hardware.camera" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front" android:required="false"/>
    <uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>

D. Not sure if this is required or not, I've tried with and without - seems to make no difference: I add the following line inside the application tag in AndroidManifest.xml:

        <uses-library android:name="org.opencv.android.JavaCameraView" />

E. Finally here is my layout xml:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:opencv="http://schemas.android.com/apk/lib/org.opencv.android.JavaCameraView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <org.opencv.android.JavaCameraView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone"
        android:id="@+id/main_activity_surface_view"
        opencv:show_fps="true"
        opencv:camera_id="any" />

    </LinearLayout>

This all works fine. I can reference OpenCV in my activity without errors. However the layout editor gives the following error: The following classes could not be instantiated: org.opencv.android.JavaCameraView. java.lang.ClassNotFoundException: org.opencv.R$styleable.

If it was just a rendering problem perhaps I could ignore it, however a similar error occurs when I run it.

What am I missing?

(I am aware of a somewhat similar question in SO regarding this error, but his solution involves importing the whole module which does indeed work but is precisely what I'm trying to avoid.)

Upvotes: 3

Views: 2373

Answers (1)

Scott Barta
Scott Barta

Reputation: 80020

An error message of:

The following classes could not be instantiated: org.opencv.android.JavaCameraView. java.lang.ClassNotFoundException: org.opencv.R$styleable

indicates that it isn't finding OpenCV resources when doing the build. If you're including OpenCV as a jar library, that will be the source of the problem -- jars don't have resources in them.

The solution is to build OpenCV as an AAR, because AARs do have resources. Construct a library module for it, compile it to AAR, and reuse that archive everywhere you need to.

Upvotes: 3

Related Questions