Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(ExecuteActionsTaskExecuter.java:69)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)...

I found these similar questions:

  • Error: package android.hardware.camera2 does not exist OpenCV
  • Can't import import android.hardware.camera2 classes

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!

like image 692
David Miguel Avatar asked Sep 29 '16 12:09

David Miguel


1 Answers

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.

like image 118
David Miguel Avatar answered Nov 20 '22 14:11

David Miguel