Compiling opencv4.3 for Android in Windows

Time:2021-1-13

Compiling opencv4.3 for Android in Windows

After three or four days, I finally got through today. It turned out to be the problem of toolchain. Most of the tutorials on the Internet use toolchain in opencv source, which will lead to all kinds of strange mistakes (STD not a member of STD, canot find iostram, etc.)

Environment configuration

The compilation environment is as follows:

  1. You need to download it firstOpencv 4.3.0 source codeAccording to the demand, plusOpenCV_ Contrib 4.3.0 source code, unzip and put it in the folder you remember

  2. downloadcmake 3.18It is convenient to select the installation package directlycmake-3.18.0-rc1-win64-x64.msi

  3. downloadMingw(it is recommended to use the installation fool type installation) select the following components to be installed, right-click to selectMark for Installation, and then selectMenu -> Installation -> Apply Changes

    After installation, add MinGW to the system environment variable (win key to search the environment variable)Restart the system(effective)

  4. Download Android NDK and SDK, you can download them directly inOfficial websiteHowever, it is recommended to arrange it directly in Android studio. Open any item of as and select it in the toolbarTools -> SDK Maneger

    After opening, select SDK tools, check show package details in the lower right corner, check a NDK version and apply.

    Similarly, open the SDK after installationProject Structure -> SKD LocationFind the SDK path.

    Also write to environment variables


    NDK is in theSdk\ndk\21.2.6472646, it’s OK not to write in the environment variables. We’ll configure them in cmake later

  5. To install JDK, you need at least 88u251(too high version may not be compatible)

    You also need to configure environment variables

    Still need to join

    Just hit it. After restart, you can use CMD to test whether the configuration is successful

  6. downloadANT 1.10.7Put it in the directory you remember

Compile configuration

Create a new storeCompilation resultsFor example, I put it in theG:\opencv\android_buildAfter decompression, the source code is placed in theG:\opencv\source

Run cmake GUI (press win if you can’t find it)

Fill in the source code and output address here

clickAdd Entry, add the following properties

  • ANDROID_NDKType is path, fill in your NDK path (the as download is on theSdk\ndk\21.2.6472646
  • ANDROID_SDKPath, fill in your SDK path
  • ANDROID_ABIString, set the platform, do not fill in the defaultarmeabi-v7a
  • ANDROID_NATIVE_API_LEVELString, the default API is 21
  • ANT_EXECUTABLEPath, fill in bin under ant path (used for Java encapsulation, convenient for as to import module)
  • ANDROID_STLString, write as requiredc++_staticorc++_shared(the default is gnustl_ static)

At this time, you can see a red, do not panic, this is on behalf of the new, not an error. Let’s click Configure below.

Select this option, next.

Here you need to configure the toolchain path,Don’t use toolchain in the source code package, use toolchain in your own NDK directory, otherwise it may lead to a series of header file loss errors, blood lessons=_ =。

Wait for configure to complete. If everything is OK, it should be like this

If you need to, you can now add the opencv contrib module to searchextra, inOPENCV_EXTRA_MODULES_PATHAdd path....../opencv_contrib-4.3.0/modules。 (need to add NDK path again)

Parameter adjustment

The following parameters are adjusted according to your own needs

  • BUILD_ANDROID_PROJECTSYou can cancel the check directly without sample items

  • BUILD_ANDROID_EXAMPLES = OFF

  • BUILD_PERF_TESTS = OFF

  • BUILD_TESTS = OFF

  • BUILD_opencv_world=Off (generate. So library libopencv)_ world.so , which is a collection of unclear and default compiled libopencv_ What’s the difference between java3.so? It’s not turned on by default. If it’s turned on, it won’t copy Java layer functions. In other words, the GUI interface of cmake will display Java wrappers: no)

  • BUILD_SHARED_LIBS=Off (if on, only the so library will be compiled)

  • WITH_CUDA=Off (CUDA is a parallel computing architecture introduced by NVIDIA, which is recommended to be added when compiling non Android SDK)

  • WITH_OPENCL=On (since we are compiling Android SDK, it is recommended to add parallel architecture support for mobile terminal)

  • WITH_OPENCL_SVM=On (it is recommended to turn on shared virtual memory)

After adjustment, click Configure again, and then click generate.

Perform compilation

When finished, enter your build folder through the command line tool CMD (administrator), or right-click in the buid folder and select gitbash

Input commandmingw32-make.exe

If there is no error, enter it againmingw32-make.exe install

After compiling, open the as project and select it on the toolbarFile -> New -> Import_Module, put build/install/sdk/javaImport can (if you have ant configuration) or directly copy compiled.aperhaps.soConnect in as and write later.

After summarizing my mistakes, I can post them to the comment area or ask me in private.