7

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:

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!

Community
  • 1
  • 1
David Miguel
  • 12,154
  • 3
  • 66
  • 68
  • Why you want to set the CompileSdk to such a old State? Set it to 24 and since you have a MinSdk of 9 the old devices will still be supported (but you need to make sure on runtime that you dont call any camera2 apis) – GoneUp Sep 29 '16 at 12:41
  • 1
    The `compileSdkVersion` in both `build.gradle` files is 24. I want to give support from API 19 (KitKat). In the last commit I changed `minSdkVersion` from 19 to 9 just to try (because OpenCV is supposed to give support from API 8, but with `appcompat-v7:24.2.1` the minimum API is 9). But nothing changed. – David Miguel Sep 29 '16 at 12:49
  • Possible duplicate of [Error: package android.hardware.camera2 does not exist OpenCV](http://stackoverflow.com/questions/36204781/error-package-android-hardware-camera2-does-not-exist-opencv) – Yuliia Ashomok Sep 30 '16 at 21:24
  • @Iuliia As I have explained, the solution proposed in the question that you mention does not solve my problem. I do not have any problem when I am using a device with Android≥5 (`camera2` works well, not as in his case). The problem arrives when I use a device with Android<5, in particular KitKat. `camera2` does not have to be used in that case (because is only for API≥21), but for some reason it is used and that is why my error is thrown. – David Miguel Oct 01 '16 at 13:29
  • I had the same problem and [this](http://stackoverflow.com/a/34061729/323149) answer helped me – Javi Mollá Oct 03 '16 at 08:26

1 Answers1

6

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.

David Miguel
  • 12,154
  • 3
  • 66
  • 68
  • So, there was an error during Instant Run preparations and when you switch off Instant Run the error disappears. Whoa! Surprise! -1 for a non-solution. – AndreKR Oct 04 '16 at 07:58
  • @AndreKR Thanks for your better solution. If you read my answer you could see "temporal solution", if you find anything better just tell me, meanwhile I cannot stop working. – David Miguel Oct 04 '16 at 11:52
  • I don't have one, but I suspect it's [this bug](https://code.google.com/p/android/issues/detail?id=212672). I wasn't suggesting that you stop working, I was suggesting that you delete your "answer" and leave the question open for the time being. Since it's quite obvious that an error in `transformClassesWithInstantRunForDebug` will go away when you disable InstantRun, your "answer" does not add any value. – AndreKR Oct 04 '16 at 14:36
  • @AndreKR Thank you for adding info. I've unchecked my answer as correct but I'll leave it there because It's the only temporal solution that I have found for the moment (obvious or not). I will follow up that issue and update my answer as soon as they make any improvement. – David Miguel Oct 05 '16 at 09:47
  • @AndreKR thanks for sharing. That solution worked for me using an Android 4.1.2 device. – Thor_Bux Oct 11 '16 at 21:32
  • @DavidMiguel Nice! – Kevin Crain Nov 03 '16 at 08:14
  • @DavidMiguel It worked on 4.4.4 KTU84 P Redmi HM2LTE-IN – ashishdhiman2007 Dec 26 '16 at 07:37