OpenCV for Android 2.4.11, gradle-experimental, and Android Studio 1.4.1

The last few days I have been trying to get up to speed on ROS and OpenCV in the context of Android. I have to say that it’s been really frustrating. There has been a big transition in the Android world from Eclipse+Ant to Intellij+Gradle. Documentation has not been kept up, and downstream OpenCV has not followed the transition, in code or documentation. ROS Android is even further behind. It all adds up to a mess for someone like me who is new to java and Android.

Anyway, here’s my recipe for getting the OpenCV Android samples to work with Gradle, Android Studio 1.4.1, the Android NDK, and the gradle-experimental plugin. I don’t pretend to understand all of it and I have no idea if it’s best practice, but it seems to work.

  1. Android studio should be installed.
  2. The Android NDK should be installed via the Android SDK Manager
  3. The OpenCV Android SDK should be downloaded and unzipped

I’ve done the recipe as a bash script because I like being able to cut and paste. You can cut and paste each code section into a bash shell and it should do the right thing…

First set the location of the root directory of the OpenCV SDK.

export OPENCV_SDK=/home/teh/wip/ocv24
cd $OPENCV_SDK

Create your gradle build files…

First the OpenCV library

cat > $OPENCV_SDK/sdk/java/build.gradle <<'==='


apply plugin: 'com.android.model.library'
model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            minSdkVersion.apiLevel = 8
            targetSdkVersion.apiLevel = 23
        }
    }

    android.buildTypes {
        release {
            minifyEnabled = false
        }
        debug{
            minifyEnabled = false
        }
    }
   android.sources {
       main.manifest.source.srcDirs +=  "."
       main.res.source.srcDirs +=  "res"
       main.aidl.source.srcDirs +=  "src"
       main.java.source.srcDirs +=  "src"
   }
 
}


===

Then tell the tutorial sample where to find the library

cat > $OPENCV_SDK/samples/tutorial-1-camerapreview/settings.gradle <<'==='


include ':openCVLibrary2411'
project(':openCVLibrary2411').projectDir = new File('../../sdk/java')


===

Finally, the build file for the tutorial.

cat > $OPENCV_SDK/samples/tutorial-1-camerapreview/build.gradle <<'==='


buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.2.1'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "org.opencv.samples.tutorial1"
            minSdkVersion.apiLevel = 8
            targetSdkVersion.apiLevel = 23
        }
    }

    android.sources {
       main.manifest.source.srcDirs +=  "."
       main.res.source.srcDirs +=  "res"
       main.aidl.source.srcDirs +=  "src"
       main.java.source.srcDirs +=  "src"
    } 

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles += file('proguard-rules.pro')
        }
        debug {
             minifyEnabled = false
        }
    }
}

dependencies {
    compile project(':openCVLibrary2411')
}


===

Your build tools version needs to be set correctly. Here’s an easy way to see what you have installed. (You can install other versions via the Android SDK Manager). Change buildToolsVersion if you don’t have 23.0.2.

echo "Your buildToolsVersion is one of: "
ls $ANDROID_HOME/build-tools

Change the environment variable on the first line to your version number

REP=23.0.2 #CHANGE ME
sed -i.bak s/23\.0\.2/${REP}/g $OPENCV_SDK/sdk/java/build.gradle
sed -i.bak s/23\.0\.2/${REP}/g $OPENCV_SDK/samples/tutorial-1-camerapreview/build.gradle

Finally, set up the correct gradle wrapper. Gradle needs a clean directory to do this.

pushd $(mktemp -d)
gradle wrapper --gradle-version 2.5
mv -f gradle* $OPENCV_SDK/samples/tutorial-1-camerapreview
popd

You should now be all set. You can now browse to this directory with Android Studio and open up the project.

You can build the tutoral on the command line with the following command:

./gradlew assembleDebug

It should build your apk, putting it in ./build/outputs/apk

Written on November 14, 2015