# An exercise in frustration

**URL:** <https://forum.opencv.org/t/an-exercise-in-frustration/12964>\
**Category:** Android/Java\
**Created:** [April 26, 2023, 5:07pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964 "2023-04-26T17:07:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtch](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mtch](https://forum.opencv.org/u/mtch)\
**Post date:** [April 26, 2023, 5:07pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/1 "2023-04-26T17:07:05Z")

</div>

Hi guys,  
I have not been so frustrated in long time. After a full day, I am still unable to compile an hello world in Android.

OpenCV for android 4.7.0 as downloaded  
Android Studio Flamingo | 2022.2.1  
Installed multiple SDK version, SDK built tools, NDK, command line tools, platform tools + some irrelevant extras.  
Successfully tested a simple camera capture program using only android (not opencv).

Followed the tutorial:

> **[A Beginner’s Guide to Setting up OpenCV Android Library on Android Studio](https://medium.com/android-news/a-beginners-guide-to-setting-up-opencv-android-library-on-android-studio-19794e220f3c)**
>
> I recently started a project that involved working with OpenCV on Android. Most of the guides on setting up the library on Android were…

However, I am unable to get it compiled:  
error: package org.opencv.engine does not exist  
import org.opencv.engine.OpenCVEngineInterface;

I found hundred of “solutions” on the internet. NONE of them works.  
Ended up copying all the opencv java into the project,  
i have the whole org/opencv/ tree in main, i have added the jniLibs folder, fixed version inconsistencies in gradle files etc…

Still, nothing works.  
I was assuming that maybe I need to compile the opencv sdk.  
Attempting to import the opencv/sdk in Android Studio to see if it compile. It does not:

A problem occurred evaluating root project ‘sdk’.

> Plugin with id ‘com.android.library’ not found.

Questions:

- The android packet is ready to go or need compiled ?
- How to get OpenCV to compile in Android Studio, if this is the problem?
- If no, any hints to alleviate a nerve-racking experience ?

Tnx.

---

<div class="post-metadata">

**Author:** ![HenryHao](https://avatars.discourse-cdn.com/v4/letter/h/43a26b/32.png) [@HenryHao](https://forum.opencv.org/u/HenryHao)\
**Post date:** [April 27, 2023, 9:35am UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/2 "2023-04-27T09:35:47Z")

</div>

You could have a try to add “android.defaults.buildfeatures.aidl=true” to the gradle.properties. to enable the aidl buildfeature.  
ref: [BuildFeatures &nbsp;|&nbsp; Android Developers](https://developer.android.com/reference/tools/gradle-api/7.0/com/android/build/api/dsl/BuildFeatures)

---

<div class="post-metadata">

**Author:** ![mtch](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mtch](https://forum.opencv.org/u/mtch)\
**Post date:** [April 27, 2023, 9:43pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/3 "2023-04-27T21:43:33Z")

</div>

Thank you very much Henry.  
That is a very good piece of info. It fixedthat point,

Now, I advanced to the next frustration 😁

error: cannot find symbol import org.opencv.BuildConfig;

No advice found by Google helped so far either.

---

<div class="post-metadata">

**Author:** ![mtch](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mtch](https://forum.opencv.org/u/mtch)\
**Post date:** [May 1, 2023, 12:12pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/4 "2023-05-01T12:12:51Z")

</div>

Just in case somebody else have the org.opencv.BuildConfig; issue, I was able to pass that by adding:

```
buildFeatures {
    buildConfig true
}

```

in build.gradle for opencv.

Android is so frustrating … ☹

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 4, 2023, 7:48am UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/5 "2023-05-04T07:48:46Z")

</div>

recent updates, please take a look:

> <https://github.com/opencv/opencv/pull/23447>
>
> Hello,
> 
> Android Gradle Plugin version 8.0 is asking for namespace. This is bec…ome mandatory and after I update my AGP to 8.0, I got this error 
> 
> 
> \`\`\`
> Namespace not specified. Please specify a namespace in the module's build.gradle file like so:
> 
> android {
> namespace 'com.example.namespace'
> }
> 
> If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.
> \`\`\`
> 
> This change fix this future releases. However I am not sure how opencv wants to user namespace I used "org.opencv" if there is a different namespace please let me know so I can changed that too. Also should I add namepsace into "opencv/modules/java/android\_sdk/android\_gradle\_lib/build.gradle" here ?
> 
> 
> \### Sources
> 
> Android developer link: https://developer.android.com/studio/preview/features#namespace-dsl
> Issue Tracker Google: https://issuetracker.google.com/issues/191813691?pli=1#comment19
> 
> 
> 
> 
> 
> 
> \### Pull Request Readiness Checklist
> 
> See details at https://github.com/opencv/opencv/wiki/How\_to\_contribute#making-a-good-pull-request
> 
> \- \[x\] I agree to contribute to the project under Apache 2 License.
> \- \[x\] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
> \- \[x\] The PR is proposed to the proper branch
> \- \[x\] There is a reference to the original bug report and related work
> \- \[\] There is accuracy test, performance test and test data in opencv\_extra repository, if applicable
> Patch to opencv\_extra has the same branch name.
> \- \[\] The feature is well documented and sample code can be built with the project CMake

---

<div class="post-metadata">

**Author:** ![YohannesTz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/yohannestz/32/7835_2.png) [@YohannesTz](https://forum.opencv.org/u/YohannesTz)\
**Post date:** [May 7, 2023, 2:25pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/6 "2023-05-07T14:25:44Z")

</div>

> [@mtch](#):
>
> ` buildConfig true`

thank you so much. This saved me after 3 hours of searching all over.

---

<div class="post-metadata">

**Author:** ![ankit\_sachan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ankit_sachan/32/8539_2.png) [@ankit\_sachan](https://forum.opencv.org/u/ankit_sachan)\
**Post date:** [August 14, 2023, 12:36pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/7 "2023-08-14T12:36:29Z")

</div>

if somebody still needs help

> **[Android Studio + Kotlin + Open CV](https://medium.com/@ankitsachan/android-studio-kotlin-open-cv-16d75f8d9969)**
>
> so this is if you are getting frustrated with how to integrate OpenCV 4.x.x with Android Studio.

---

<div class="post-metadata">

**Author:** ![9dubs](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/9dubs/32/8725_2.png) [@9dubs](https://forum.opencv.org/u/9dubs)\
**Post date:** [October 23, 2023, 8:43pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/8 "2023-10-23T20:43:06Z")

</div>

that was a life saver holy hell thank you guys

---

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://forum.opencv.org/u/rajmb)\
**Post date:** [November 25, 2023, 2:09pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/9 "2023-11-25T14:09:41Z")

</div>

Thank you @ankit_sachan for documenting this. I followed the documentation and the build went smoothly. But when I try to run the app, I get an error that “OpenCV Manager” package not installed and it redirects me to Google Playstore. Any idea what needs to be done to tackle this?

---

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://forum.opencv.org/u/rajmb)\
**Post date:** [November 25, 2023, 3:42pm UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/10 "2023-11-25T15:42:26Z")

</div>

OK, just after I posted this, I happened to look at the OpenCV build.gradle file that had this comment:

```auto
// Load OpenCV native library before using:
//
// - avoid using of "OpenCVLoader.initAsync()" approach - it is deprecated
// It may load library with different version (from OpenCV Android Manager, which is installed separatelly on device)
//
// - use "System.loadLibrary("opencv_java4")" or "OpenCVLoader.initDebug()"

```

So, now, I just have:

```auto
    static {
        System.loadLibrary("opencv_java4");
    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {

        binding = FragmentFirstBinding.inflate(inflater, container, false);
        Log.d(TAG, "OpenCV loaded: "+ OpenCVLoader.initDebug());
        if (OpenCVLoader.initDebug()) {
            binding.mainCamera.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_BACK);
            binding.mainCamera.setCameraPermissionGranted();
            binding.mainCamera.enableView();
        }
        return binding.getRoot();

    }

```

And the preview pane opened 🙂

Only issue I now have is that the preview pane is in landscape mode. Any idea if I can get it to portrait mode?

---

<div class="post-metadata">

**Author:** ![rajmb](https://avatars.discourse-cdn.com/v4/letter/r/f14d63/32.png) [@rajmb](https://forum.opencv.org/u/rajmb)\
**Post date:** [November 28, 2023, 5:58am UTC](https://forum.opencv.org/t/an-exercise-in-frustration/12964/11 "2023-11-28T05:58:49Z")

</div>

The code that I have used for portrait view:

```auto
        @Override
        public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {

            Imgproc.cvtColor(inputFrame.rgba(), rgb, Imgproc.COLOR_RGBA2RGB);
             /*
             --- Your logic
                // Do your processing before rotating the image so that the corner/pose estimations are not affected
             */
             Mat localRGB;
             // Ref: https://stackoverflow.com/a/28601634
            Core.rotate(rgb, localRGB, Core.ROTATE_90_CLOCKWISE);
            //ref: https://stackoverflow.com/a/37972813
            Imgproc.resize(localRGB, rgb, rgb.size());
            return rgb;
        }

```
