OpenCV, Android Studio 1.4.1, gradle-experimental plugin 0.2.1
None of the other answers helped me. Here's what worked for me. I'm using the tutorial-1 sample from opencv but I will be doing using the NDK in my project so I'm using the gradle-experimental plugin which has a different structure than the gradle plugin.
Android studio should be installed, the Android NDK should be installed via the Android SDK Manager, and the OpenCV Android SDK should be downloaded and unzipped.
This is in chunks of bash script to keep it compact but complete. It's also all on the command line because on of the big problems I had was that in-IDE instructions were obsolete as the IDE evolved.
First set the location of the root directory of the OpenCV SDK.
export OPENCV_SDK=/home/user/wip/OpenCV-2.4.11-android-sdk
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 what to label the library as and where to find it.
cat > $OPENCV_SDK/samples/tutorial-1-camerapreview/settings.gradle <<'==='
include ':openCVLibrary2411'
project(':openCVLibrary2411').projectDir = new File('../../sdk/java')
===
Create 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.
Build the tutoral on the command line with the following command:
./gradlew assembleDebug
It should build your apk, putting it in ./build/outputs/apk