# Run samples of open cv in android studio

**URL:** <https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453>\
**Category:** Android/Java\
**Tags:** build\
**Created:** [December 21, 2020, 6:05pm UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453 "2020-12-21T18:05:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![supriya\_pd](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@supriya\_pd](https://forum.opencv.org/u/supriya_pd)\
**Post date:** [December 21, 2020, 6:05pm UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453/1 "2020-12-21T18:05:45Z")

</div>

Hi, I recently downloaded open cv for android (version 4.0.1 ), I have successfully integrated open cv with android. Now I wish to run one of the samples that are provided. I tried to do it in a different number of ways but I am still stuck. I think I am missing something. Please can anyone tell me/guide me as to how can I import the provided samples in android studio? Thanks in advance!

---

<div class="post-metadata">

**Author:** ![vjeranc](https://avatars.discourse-cdn.com/v4/letter/v/3da27b/32.png) [@vjeranc](https://forum.opencv.org/u/vjeranc)\
**Post date:** [January 1, 2021, 8:34pm UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453/2 "2021-01-01T20:34:15Z")

</div>

You need to build the project successfully with CMake. You have to set the BUILD\_JAVA and ANDROID flags when configuring with CMake to get the android studio project files generated.

You also have tohave ANDROID\_NDK discoverable and usable by CMake.

---

<div class="post-metadata">

**Author:** ![matti.vuori](https://avatars.discourse-cdn.com/v4/letter/m/e79b87/32.png) [@matti.vuori](https://forum.opencv.org/u/matti.vuori)\
**Post date:** [January 2, 2021, 7:57am UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453/3 "2021-01-02T07:57:16Z")

</div>

You seem to describe building OpenCV, not projects that use OpenCV, which here is successfully integrated. OpenCV is just a set of Android libraries in that case, just like any other libraries inside an Android Studio project.

---

<div class="post-metadata">

**Author:** ![supriya\_pd](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@supriya\_pd](https://forum.opencv.org/u/supriya_pd)\
**Post date:** [January 2, 2021, 8:17am UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453/4 "2021-01-02T08:17:52Z")

</div>

Thanks for replying! I didn’t know about CMake, I’ll try building with it and keep the results posted here.

---

<div class="post-metadata">

**Author:** ![vjeranc](https://avatars.discourse-cdn.com/v4/letter/v/3da27b/32.png) [@vjeranc](https://forum.opencv.org/u/vjeranc)\
**Post date:** [January 2, 2021, 1:45pm UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453/5 "2021-01-02T13:45:00Z")

</div>

If you just want to use OpenCV as an android library and then try out samples, it’s easier to just copy the code. Getting the libraries build from source is more complicated.

They need to be built because `build.gradle` and similar files are transformed during the build process.

---

<div class="post-metadata">

**Author:** ![supriya\_pd](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@supriya\_pd](https://forum.opencv.org/u/supriya_pd)\
**Post date:** [January 2, 2021, 2:42pm UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453/6 "2021-01-02T14:42:49Z")

</div>

Yeah, I did that only, but still, I wanted to know if it was possible to build directly from them. I just thought knowing the stuff would help me broaden my understanding of file structures in Android.

---

<div class="post-metadata">

**Author:** ![vjeranc](https://avatars.discourse-cdn.com/v4/letter/v/3da27b/32.png) [@vjeranc](https://forum.opencv.org/u/vjeranc)\
**Post date:** [January 2, 2021, 5:19pm UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453/7 "2021-01-02T17:19:17Z")

</div>

If you want to build from source you can take the whole OpenCV repository:

> **[opencv/opencv](https://github.com/opencv/opencv)**
>
> Open Source Computer Vision Library. Contribute to opencv/opencv development by creating an account on GitHub.

You need to have CMake and Android-NDK. If you’re using Android Studio, it can manage NDK for you (you just go to SDK manager and you will see NDK checkbox in SDK Tools tab). NDK will be in the `ANDROID_SDK_LOCATION/ndk/22.0.7026061` or a different version.

Inside `opencv/` directory you can create a new dir called `build/` (or any name you like).

Inside the `build/` directory you can invoke the `cmake` command.

```bash
#!/usr/bin/env bash
export ANDROID_NDK="$ANDROID_SDK/ndk/22.0.7026061"
# android.toolchain.cmake in opencv source does not work so
# `.cmake` file included in the NDK is used.
cmake .. \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
  -DBUILD_EXAMPLES=OFF \
  -DBUILD_ANDROID_EXAMPLES=ON \
  -DBUILD_TESTS=OFF \
  -DBUILD_PERF_TESTS=OFF

```

After `build/` directory is configured you can just run `make` inside it.

After that finishes you should have a `build/opencv_android` directory that you can open inside Android Studio and build each separate sample and install it on your phone.

You might only need DNN or other modules that are shown in the samples and you can build only with them by adding a `-DBUILD_LIST=dnn` to script above.  
If you don’t care about the build time you can just do a build with everything.

---

<div class="post-metadata">

**Author:** ![supriya\_pd](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@supriya\_pd](https://forum.opencv.org/u/supriya_pd)\
**Post date:** [January 3, 2021, 4:31am UTC](https://forum.opencv.org/t/run-samples-of-open-cv-in-android-studio/453/8 "2021-01-03T04:31:01Z")

</div>

Thanks for taking the effort to clarify the steps! I will definitely try out the suggested approach.
