title: Android Studio, OpenCV jar, java.lang.ClassNotFoundException: org.opencv.R$styleable
body: <p>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. </p>
  
  <p>So these are the steps I am doing for my app called 'app'.</p>
  
  <p>A. Create a libs directory under /Project/apps. Put the OpenCV jar there. Also put the opencv native libs there, in sub-directories.</p>
  
  <p>B. In /Project/app/build.gradle do the following</p>
  
  <p>Put these lines at the top:</p>
  
  <pre><code>repositories { flatDir { dirs 'libs'    } }
  </code></pre>
  
  <p>Put these lines in the android section:</p>
  
  <pre><code>sourceSets.main.jniLibs.srcDirs = ['libs']
  </code></pre>
  
  <p>Put this line in the dependencies section:</p>
  
  <pre><code>    compile fileTree(dir: 'libs', include: ['*.jar'])
  </code></pre>
  
  <p>C. Add these lines to AndroidManifest.xml, after the application tag.</p>
  
  <pre><code>    &lt;uses-sdk android:minSdkVersion="9"/&gt;
      &lt;uses-permission android:name="android.permission.CAMERA"/&gt;
      &lt;uses-feature android:name="android.hardware.camera" android:required="false"/&gt;
      &lt;uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/&gt;
      &lt;uses-feature android:name="android.hardware.camera.front" android:required="false"/&gt;
      &lt;uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>        &lt;uses-library android:name="org.opencv.android.JavaCameraView" /&gt;
  </code></pre>
  
  <p>E. Finally here is my layout xml:</p>
  
  <pre><code>    &lt;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" &gt;
  
      &lt;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" /&gt;
  
      &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>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.</p>
  
  <p>If it was just a rendering problem perhaps I could ignore it, however a similar error occurs when I run it.</p>
  
  <p>What am I missing?</p>
  
  <p>(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.)</p>