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.)
The compilation environment is as follows:
- cmake 3.18
- Android Studio 4.0
- Java JDK 8(8 + required, too high version may not be compatible)
- Apache ant1.10.7(compile only. So library, not required)
downloadcmake 3.18It is convenient to select the installation package directly
downloadMingw(it is recommended to use the installation fool type installation) select the following components to be installed, right-click to select
Mark for Installation, and then select
Menu -> Installation -> Apply Changes
After installation, add MinGW to the system environment variable (win key to search the environment variable)Restart the system(effective)
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 toolbar
Tools -> 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 installation
Project Structure -> SKD LocationFind the SDK path.
Also write to environment variables
NDK is in the
Sdk\ndk\21.2.6472646, it’s OK not to write in the environment variables. We’ll configure them in cmake later
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
downloadANT 1.10.7Put it in the directory you remember
Create a new storeCompilation resultsFor example, I put it in the
G:\opencv\android_buildAfter decompression, the source code is placed in the
Run cmake GUI (press win if you can’t find it)
Fill in the source code and output address here
Add Entry, add the following properties
ANDROID_NDKType is path, fill in your NDK path (the as download is on the
ANDROID_SDKPath, fill in your SDK path
ANDROID_ABIString, set the platform, do not fill in the default
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 required
c++_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 search
....../opencv_contrib-4.3.0/modules。 (need to add NDK path again)
The following parameters are adjusted according to your own needs
BUILD_ANDROID_PROJECTSYou can cancel the check directly without sample items
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.
When finished, enter your build folder through the command line tool CMD (administrator), or right-click in the buid folder and select gitbash
If there is no error, enter it again
After compiling, open the as project and select it on the toolbar
File -> New -> Import_Module, put
build/install/sdk/javaImport can (if you have ant configuration) or directly copy compiled
.soConnect in as and write later.
After summarizing my mistakes, I can post them to the comment area or ask me in private.