ClassNotFoundException: android.hardware.camera2.CameraAccessException with OpenCV 3.1.0


I am working in an android project with Android Studio 2.2 that uses OpenCV 3.1.0 library. I imported the library according to this answer. The situation is the following:

  • When I compile the project for a device with API >= 21 (exactly API 23), everything goes well.
  • When I compile the project for a device with API < 21 (exactly API 19, the minimum API that I want to support), I get the following compilation error:

Gradle log

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithInstantRunForDebug'.
> java.lang.ClassNotFoundException: android.hardware.camera2.CameraAccessException

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesWithInstantRunForDebug'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(

I found these similar questions:

In both of them the solution was to set the compileSdkVersion to minimum 21 (the version where camera2 API was introduced) and make sure that build.gradle files of the project and OpenCV match.

Unfortunately, it does not work for me. I still get the same error. Any idea?

My code is available here: Github repository.

Thanks for your help!

Show source
| android   | opencv   | android-camera   | opencv4android   | opencv3.1   2016-09-29 14:09 1 Answers

Answers ( 1 )

  1. 2016-10-03 12:10

    Finally, I found a temporal solution thanks to @JavierMoll√°.

    Switching off Instant Run solved the issue. Go to File > Settings > Build, Execution, Deployment > InstantRun and uncheck Enable Instant Run to hot swap code....

    Disable instant run

    Instant Run is a major feature of Android Studio, but unfortunately it seems that it is still work-in-progress... As @AndreKR suggests, it can be related with this issue #212672.

◀ Go back