By following this guide, hopefully, you will have a working Android Studio project "template" from which you can build up on. Allowing you to create an AR app for Android using Java and/or c++ with JNI.
Credits goes to these links:
- https://stackoverflow.com/questions/40948953/building-opencv-for-android-and-using-it-with-the-ndk
- https://stackoverflow.com/questions/26016770/how-to-install-old-version-of-android-build-tools-from-command-line
- https://jeanvitor.com/cpp-opencv-windonws10-installing/
Tested on Windows 10 and MX Linux 18.2
| Item | Link |
|---|---|
| Cmake 3.11 or greater | https://cmake.org/download/ |
| Java JDK 1.8 | https://adoptopenjdk.net/?variant=openjdk8&jvmVariant=hotspot |
| Python 3.* preferred | https://www.python.org/downloads/ |
| latest OpenCV source | https://github.com/opencv/opencv |
| latest OpenCV contrib source | https://github.com/opencv/opencv_contrib |
| Android Studio 3.2 | https://developer.android.com/studio/ |
| android-ndk-r16b-windows-x86_64 | Windows: https://dl.google.com/android/repository/android-ndk-r16b-windows-x86_64.zip Linux: https://dl.google.com/android/repository/android-ndk-r16b-linux-x86_64.zip |
| tools-r25.2.5 | Windows: https://dl.google.com/android/repository/tools_r25.2.5-windows.zip Linux: https://dl.google.com/android/repository/tools_r25.2.5-linux.zip |
| Build tools | Windows: https://osdn.net/projects/mingw/releases/p15522 Linux: gcc, build-essential |
| latest Apache Ant | https://ant.apache.org/bindownload.cgi |
- Notes: For Linux users, check out your package manager for available applications
- Notes:
- If using MinGW install manager, choose:
mingw32-base-binandmsys-base-bin - Add JAVA_HOME environment variable pointing to JDK installation directory
- Add Python installation directory to PATH environment variable
- After creating environment variables, restart PC.
- If using MinGW install manager, choose:
- Setup project directory for building and easy access:
- Create the following directories:
../opencv_project(project root directory)../opencv_project/opencv../opencv_project/contrib../opencv_project/build../opencv_project/ndk-r16b../opencv_project/tools../opencv_project/ant
- Extract the following zip file contents into their directories:
opencv-master.zipinto../opencv_project/opencvopencv_contrib-master.zipinto../opencv_project/contribandroid-ndk-r16b-windows-x86_64.zipinto../opencv_project/ndk-r16btools-r25.2.5.zipinto../opencv_project/toolsapache-ant-1.10.5-bin.zipinto../opencv_project/ant
- Download build-tools and platform using Windows:
sdkmanager.bator Linux:sdkmanager:- Open PowerShell or Terminal
- Navigate to
../opencv_project/tools/bin - Execute Windows:
.\sdkmanager.bat "build-tools;28.0.3"or Linux:./sdkmanager "build-tools;28.0.3" - Execute Windows:
.\sdkmanager.bat "platforms;android-27"or Linux:./sdkmanager "platforms;android-27" - Execute Windows:
.\sdkmanager.bat "platform-tools"or Linux:./sdkmanager "platform-tools" - Note: To lookup package names use Windows:
.\sdkmanager.bat --listor Linux:./sdkmanager --list
- Open Cmake gui
- Click
Browse Source...find and add../opencv_project/opencv - Click
Browse Build...find and add.../opencv_project/build - Add the following entries by clicking
Add Entry:- Name:
ANDROID_NDKType:PathValue:../opencv_project/ndk-r16b - Name:
ANDROID_NATIVE_API_LEVELType:StringValue:16 - Name:
ANDROID_SDKType:PathValue:../opencv_project/ - Name:
ANDROID_NDK_HOST_X64Type:BoolValue:Ticked - Name:
ANT_EXECUTABLEType:File PathValue: Windows:../opencv_project/ant/bin/ant.bator Linux:../opencv_project/ant/bin/ant
- Name:
- Click
Configure - A window will pop up, make sure the following values are selected:
- Windows:
MinGW Makefilesor Linux:Unix Makefilesas selected generator Specify toolchain file for cross-compilingoption is selected- Click
Next - Specify toolchain file location:
../opencv_project/opencv/platforms/android/android.toolchain.cmake - Click
Finish
- Windows:
- Every thing is RED!
- Use
Search boxto check the following entries, if not found, add them:ANDROID_SDK_TARGETValue:android-27(the platform version that was downloaded in Prologue)OPENCV_EXTRA_MODULES_PATHValue:../opencv_project/contrib/modules- For Windows only:
CMAKE_MAKE_PROGRAMValue:../MINGW_INSTALL_DIR/bin/mingw32-make.exe WITH_CAROTENEValue:Not tickedBUILD_ZLIBValue:Ticked
- Click
Configure - If some entries are still RED, click
Configureagain - Lastly Click
Generate
- For Windows:
- Navigate to
../MINGW_INSTALL_DIR/msys/1.0 - Run msys.bat
- In the CMD window:
- Execute
cd "../opencv_project/build/" - Execute
mingw32-make(takes a while to finish ~40min) - Execute
mingw32-make install
- Execute
- Navigate to
- For Linux:
- Execute
cd "../opencv_project/build/" - Execute
make(takes a while to finish ~40min) - Execute
make install
- Execute
- Open Android Studio
- Create a new project with these parameters:
- Select
Include C++ support - Choose minimum
API 23 - Pick
Empty Activity - Select C++ standart:
C++11
- Select
- Note: During the following steps, Android Studio might ask you to install missing packages, do so
- Switch from
AndroidtoProjectview - Make Android Studio use the old ndk:
- Click
File-> Project Structure... - Select
SDK Location - Set the
Android NDK locationas../opencv_project/ndk-r16b - Click
OK
- Click
- Import the compiled opencv project as a module:
- Open
File-> New-> Import Module... - As Source directory select
../opencv_project/build/install/sdk/java - Leave the module name as it is (further in this guide
OPENCV_MOD) - Click
Next - Click
Finish
- Open
- Remove
uses-sdktag fromAndroidManifest.xmllocated inopencv_module_name/src/main - Add
OPENCV_MODas a dependency to the main app:- Open
File-> Project Structure...: - Select
appfrom the list - Select
Dependeciestab - Click the
+sign: SelectModule dependecy - Select
OPENCV_MODfrom the list - Click
OK - Click
OKin Project Structure
- Open
- Set the following values in
OPENCV_MOD/build.gradleto the same values as inapp/build.gradle: - Expand
OPENCV_MODandappdirectories in project view and open bothbuild.gradle - Check
compileSdkVersionis the same - Check
minSdkVersionis the same - Check
targetSdkVersionis the same - Click
Sync Now - Creating
jniLibsdirectory:- Expand
app/src/main - Right click
maindirectory - Select
New-> Folder-> JNI Folder - Select
Change folder Location - Input
src/main/jniLibsintoNew Folder Location - Click
Finish
- Expand
- Open
File Explorernavigate to../opencv_project/build/install/sdk/native/libs:- Copy the
armeabi-v7adirectory intoapp/src/main/jniLibs - Click
OK
- Copy the
- Add the following line in
app/build.gradlefile, insideexternalNativeBuild{...}block:ndk { abiFilter "armeabi-v7a" }- Click
Sync Now
- Linking the libraries, open
app/CMakeList.txt:- Note: Full path is required here; Remember to change
PROJECT_NAME - Add the following lines after
add_library(...)and beforefind_library(...): include_directories("../opencv_project/build/install/sdk/native/jni/include")link_directories("../AndroidStudioProjects/PROJECT_NAME/app/src/main/jniLibs/armeabi-v7a")- Add the following lines after
find_library(...)and beforetarget_link_libraries(...): file(GLOB PARTYLIBS "../opencv_project/build/install/sdk/native/3rdparty/libs/armeabi-v7a/*.a")file(GLOB CVLIBS "../opencv_project/build/install/sdk/native/staticlibs/armeabi-v7a/*.a")- Add the following lines inside
target_link_libraries(...)method betweennative-liband${log-lib}: ${CVLIBS}${PARTYLIBS}${CVLIBS}- Click
Sync Now
- Note: Full path is required here; Remember to change
- C++
- Navigate to
app/src/main/cppand opennative-lib.cpp:- Add
#include <opencv2/aruco.hpp> - Replace the contents of
stringFromJNIfunction with: std::string hello = "Hello from C++";cv::Mat test;hello += " test";return env->NewStringUTF(hello.c_str());
- Add
- If errors are present, high chance that the libraries are not linked:
- Check
CMakeList.txtfor typos - In project view, check if
libopencv_java{number}.sois underjniLibs/armeabi-v7adirectory - In explorer, check if
../opencv_project/build/install/sdk/native/staticlibs/armeabi-v7a/contains all the built libsaruco, etc
- Check
- Java
- Navigate to
app/src/main/java/com.example.me.PROJECT_NAMEand openMainActivity.java:- Add
import org.opencv.core.Mat; - Add
System.loadLibrary("opencv_java4");afterSystem.loadLibrary("native-lib"); - Replace the contents of
onCreatemethod with: super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Mat test = new Mat();TextView tv = (TextView) findViewById(R.id.sample_text);tv.setText(stringFromJNI() + " " + test.depth());
- Add
- If errors are present:
- Check if
OPENCV_MODis set as a dependency - Check if the
jniLibsfolder is used instead ofjni
- Check if