Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Android Studio uses gradle as the build system for Android projects. Until the OpenCV Android SDK ships with support for gradle out-of-the-box, I find it easiest to first convert the SDK to a gradle project from Eclipse before using it from Android Studio. To do this:

  1. If you don't have it installed already, install the Eclipse Android Developer Tools bundle from http://developer.android.com/sdk/index.html.
  2. Make sure you have the latest version of the Eclipse ADT plugin installed.
  3. Download and extract the OpenCV Android SDK (e.g. OpenCV-2.4.6-android-sdk-r2.zip)
  4. Open Eclipse and import the OpenCV Android SDK using the instructions on the OpenCV website. Note: When selecting the root directory to import, use the OpenCV-2.4.6-android-sdk/sdk/java sub-directory and not the top-level OpenCV-2.4.6-android-sdk directory as the instructions on that site suggest.
  5. Select File > Export. In the window that appears, open Android and select Generate Gradle build files.

Your copy of the OpenCV Android SDK should now be properly configured to be built by gradle. To use it from a new project in Android Studio:

  1. Create the Android app project using the New Project wizard in Android Studio. Make sure you set the minimum Android SDK to at least version 8 (this is what the OpenCV Android SDK declares as its minimum). For the following instructions, I'll assume the project is using the default name of "MyApplicationProject".
  2. Copy/move your copy of the OpenCV Android SDK into the new project directory (e.g. copy the OpenCV-2.4.6-android-sdk/sdk/java directory to MyApplicationProject/opencv-android).
  3. Open MyApplicationProject/settings.gradle and add ':opencv-android' to the include line. The new line should look similar to this:

    include ':MyApplication', ':opencv-android'
    
  4. Open MyApplicationProject/MyApplication/build.gradle and add "compile project(':opencv-android')" to the dependencies section. Make sure this is the top-level dependencies section and not the one inside of the buildscript section at the top of the file. The new dependencies section should look similar to this:

    dependencies {
        compile 'com.android.support:support-v4:13.0.+'
        compile project(':opencv-android')
    }
    
  5. In Android Studio, select Tools > Android > Sync Project with Gradle Files.

Your new project can now use the OpenCV Android SDK from Android Studio. For example, you should be able to implement the "Hello World" example from the OpenCV Android tutorial (skip all of the Eclipse setup instructions and just make the layout, Android manifest, and main activity changes inside MyApplication).

Android Studio uses gradle as the build system for Android projects. Until the OpenCV Android SDK ships with support for gradle out-of-the-box, I find it easiest to first convert the SDK to a gradle project from Eclipse before using it from Android Studio. To do this:

  1. If you don't have it installed already, install the Eclipse Android Developer Tools bundle from http://developer.android.com/sdk/index.html.

  2. Make sure you have the latest version of the Eclipse ADT plugin installed.

  3. Download and extract the OpenCV Android SDK (e.g. OpenCV-2.4.6-android-sdk-r2.zip)

  4. Open Eclipse and import the OpenCV Android SDK using the instructions on the OpenCV website. Note: When selecting the root directory to import, use the OpenCV-2.4.6-android-sdk/sdk/java sub-directory and not the top-level OpenCV-2.4.6-android-sdk directory as the instructions on that site suggest.

  5. Select File > Export. In the window that appears, open Android and select Generate Gradle build files.

Your copy of the OpenCV Android SDK should now be properly configured to be built by gradle. To use it from a new project in Android Studio:

  1. Create the Android app project using the New Project wizard in Android Studio. Make sure you set the minimum Android SDK to at least version 8 (this is what the OpenCV Android SDK declares as its minimum). For the following instructions, I'll assume the project is using the default name of "MyApplicationProject".

  2. Copy/move your copy of the OpenCV Android SDK into the new project directory (e.g. copy the OpenCV-2.4.6-android-sdk/sdk/java directory to MyApplicationProject/opencv-android).

  3. Open MyApplicationProject/settings.gradle and add ':opencv-android' to the include line. The new line should look similar to this:

    include ':MyApplication', ':opencv-android'
    
  4. Open MyApplicationProject/MyApplication/build.gradle and add "compile project(':opencv-android')" to the dependencies section. Make sure this is the top-level dependencies section and not the one inside of the buildscript section at the top of the file. The new dependencies section should look similar to this:

    dependencies {
        compile 'com.android.support:support-v4:13.0.+'
        compile project(':opencv-android')
    }
    
  5. In Android Studio, select Tools > Android > Sync Project with Gradle Files.

Your new project can now use the OpenCV Android SDK from Android Studio. For example, you should be able to implement the "Hello World" example from the OpenCV Android tutorial (skip all of the Eclipse setup instructions and just make the layout, Android manifest, and main activity changes inside MyApplication).