When this series started in 2015, getting OpenCV into an Android app meant downloading the SDK, importing it as a module, fixing Gradle by hand and installing the separate OpenCV Manager app. None of that is needed any more. Since OpenCV 4.9, the OpenCV team publishes an official Android package on Maven Central, so adding OpenCV is now a single Gradle line.
Starting today? Use this part instead of the older setup parts of the series (downloading the SDK, the OpenCV Manager and importing the module). The rest of the series builds on the project you create here.
What you need
- The latest stable Android Studio.
- An Android phone with a camera (an emulator works, but its camera is a simulated scene).
- Basic Java knowledge. This series uses Java to stay consistent with the earlier parts.
Step 1: Create a new project
- In Android Studio choose File → New → New Project.
- Pick Empty Views Activity (not the Compose template).
- Set Language to Java, choose a package name such as
com.codeonion.opencvdemo, and set Minimum SDK to API 24 or higher. - Click Finish and wait for Gradle to sync.
Step 2: Add the OpenCV dependency
Open app/build.gradle.kts and add OpenCV to the dependencies block:
dependencies {
implementation("org.opencv:opencv:4.10.0")
// ...the dependencies Android Studio already added
}
If your project still uses the older Groovy build.gradle file, the line is implementation 'org.opencv:opencv:4.10.0'. Check Maven Central for the newest version number and click Sync Now.
Step 3: Load OpenCV when the app starts
OpenCV is a native (C++) library, so it has to be loaded before you call any OpenCV class. Add this to onCreate() in MainActivity.java:
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "OpenCVDemo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (OpenCVLoader.initLocal()) {
Log.i(TAG, "OpenCV loaded, version " + Core.VERSION);
} else {
Log.e(TAG, "OpenCV failed to load");
}
}
}
initLocal() loads the native library that is packaged inside your APK. It replaces the old initDebug() and initAsync() calls you will see in older tutorials.
Step 4: Declare the camera permission
The next parts use the camera, so add these lines to AndroidManifest.xml, just above the <application> tag:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
Step 5: Run and check Logcat
Run the app on your phone, open Logcat and filter by OpenCVDemo. You should see a line like OpenCV loaded, version 4.10.0. That is it: OpenCV is ready to use.
Keep the APK small
The OpenCV package contains native libraries for several CPU types, which makes the APK large. Almost every modern phone uses arm64-v8a, so you can keep only that one:
android {
defaultConfig {
ndk {
abiFilters += listOf("arm64-v8a")
}
}
}
If you also test on an x86_64 emulator, add "x86_64" to the list. When you publish an app bundle (AAB) to Google Play, Play already delivers only the right libraries to each device.
Troubleshooting
- Gradle cannot find org.opencv:opencv: make sure
mavenCentral()is listed in therepositoriesblock ofsettings.gradle.kts. - UnsatisfiedLinkError at runtime: you called an OpenCV class before
initLocal()returnedtrue, or you filtered out the ABI your device uses. - “OpenCV failed to load” on an emulator: add
"x86_64"toabiFilters.
In the next part we will show the live camera and add a menu to switch between the front and back cameras.





Leave a Reply