Setup OpenCV4Android for Google Glass

OpenCV, Google Glass

This post introduce basic setup for opencv project dev in google glass, which can be taken as a tutorial for google glass opencv4android in android studio.

Environment: OpenCV4Android 2.4.9, android studio 0.8.9, java 1.8, java sdk API level 20, Glass Dev Kit Preview 19. We assume that environment has already been setup or downloaded.

Prepare OpenCV4Android

The current stable verion is 2.4.9. You can get it here.. After you extract it, you can see folder structures. In the apk folder, you need to install the OpenCV_2.4.9_Manager_2.18_armv7a-neon.apk which is the compatiable OpenCV Manager version for google glass.

adb install OpenCV_2.4.9_Manager_2.18_armv7a-neon.apk

This OpenCV Manager works as a service in the android system, to provide service to opencv apps. More explanations .

Create a Google Glass Project

Current version android studio provide a bootstrap dialog for you to create a google glass project, just remember to select the glass sdk at the second page, and you only select phone sdk when you need a phone module in your project. I suggest you to pick the Immersive rather than livecard for activity style.

At the time this post was written, the API level for google glass is Glass Development Kit Preview API level 19. Thus the minSdkVersion in your build.gradle should be 19, and the compileSdkVersion should be 'Google Inc.:Glass Development Kit Preview:19'(This should be automatic taken care of, if you have the latest android studio.).

This automatically generated code in the main activity may not suit your usage. You may need rewrite all of theHowever, the next step before you start to code your OpenCV project, is to import OpenCV library to your current project. You can do this by import module the path as /path to/OpenCV-2.4.9-android-sdk/sdk/java. After the import, you need manually edit the build.gradle file for this module openCVLibrary249, to make it smiliar as the main project module as following:

apply plugin: ''

android {
    compileSdkVersion "Google Inc.:Glass Development Kit Preview:19"
    buildToolsVersion "20.0.0"

defaultConfig {
    applicationId "org.opencv"
    minSdkVersion 19
    targetSdkVersion 19

buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

Then you need go to your main module setting to make the openCVLibrary249 to be the dependency of the project module. After this, you can see that the dependencies part in your main module should be

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':openCVLibrary249')

Now you can start to work on your opencv project on Google Glass. Have fun !

Update: some error may come with automatically generated top-level build.gradle file which is in the root level of the project. Please keep it clean with no top-level dependency declared.

Usefull projects