Question

I'm trying use JavaCV with Android Studio and Gradle. I wrote such code fragment:

   repositories {
    mavenCentral()
    maven {
        url "http://maven2.javacv.googlecode.com/git/"
    }
}

dependencies {
    compile files('libs/android-support-v4.jar')
    compile group: 'com.googlecode.javacpp', name: 'javacpp', version: '0.5'
    compile group: 'com.googlecode.javacv', name: 'javacv', version: '0.5'
}

and I see imported libraries in dir External Libraries. So I tried to run this:

...
  IplImage zdjecie=cvLoadImage(Environment.getExternalStorageDirectory().getPath()+ "/1.bmp");
    cvSaveImage(Environment.getExternalStorageDirectory().getPath()
    + "/2.bmp", zdjecie);
...

and I got error:

Caused by: java.lang.UnsatisfiedLinkError: Couldn't load jniopencv_core: findLibrary returned null

...because I don't know what should I do with opencv's (and others) .so files.

So how should we use JavaCV in Android Studio?

Was it helpful?

Solution

1.WARNING: That's not enough!:

  dependencies { compile group: 'org.bytedeco', name: 'javacv', version: '0.9'}

2.EDIT: Sorry for mistake, my recent solution which I posted here and which told only about line above was wrong. But I checked it out and this works for me:

a)Add dependencies

dependencies {
    compile group: 'org.bytedeco', name: 'javacv', version: '0.9'
    compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.9-0.9', classifier:    'android-arm'
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.3-0.9', classifier: 'android-arm'    
}

b) Create jniLibs dir inside your project (on the same level as normal libs dir. EDIT: If you have some troubles try moving jniLibs to app/src/main).
c) Add required .so files extracted from opencv-android-arm.jar and ffmpeg-android-arm.jar (or only this files which you really need) to created jniLibs dir. (If you don't know about what I'm talking you can download javacv-0.9-bin.zip from JavaCV page and inside it you can find these 2 .jars).

OTHER TIPS

Inserting inside the build.gradle dependencies worked for me:

compile ‘org.bytedeco:javacv:+’
compile group: ‘org.bytedeco.javacpp-presets’, name: ‘opencv’, version:   ‘2.4.10–0.10', classifier: ‘android-arm’ 
compile group: ‘org.bytedeco.javacpp-presets’, name: ‘ffmpeg’, version:   ‘2.5.1–0.10', classifier: ‘android-arm’

For an up-to-date solution (JavaCV 1.1 and Android Studio 2.1.1) please see my step-by-step instructions in my Gist. JavaCV 1.2 currently doesn't like Android 6.

This tut will use JavaCV 1.1, version 1.2 currently has SIGSEGV issue. JavaCV 1.1 comes with FFmpeg 2.8.1.

  1. Obtain the prebulit binaries here.
  2. Extract it and copy these files:
    • javacpp.jar (essential).
    • javacv.jar (essential).
    • ffmpeg.jar (essential if you use FFmpeg).
    • ffmpeg-android-arm.jar (for arm CPU).
    • ffmpeg-android-x86.jar (optional for x86 CPU). into the libs folder of your project (app/libs in my case).
  3. Click on app -> F4 -> Dependencies -> + -> File dependencies -> choose all the previous .jar.
  4. Temporarily, because of this issue, we need to set targetSdkVersion 22 the app's build.gradle.
  5. Possibly need to set these packagingOptions in the build.gradle:

android { compileSdkVersion 23 buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.example.test"
    minSdkVersion 16
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

packagingOptions {
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.properties'
    exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.xml'

    //might need these if you use openCV
    //exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.properties'
    //exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.xml'
}

}

Unfortunately, I had some problems with user2645214's solution (still getting java.lang.UnsatisfiedLinkError), but I found another one, so I decided to share it with those who would have the same problem.

Since release 0.7.3 there is another way to include your *.so files - you can just put them in /src/main/jniLibs (just create jniLibs directory if you don't have it) and it should work.

Also my build.gradle file looks like:

apply plugin: 'android'

repositories {
    mavenCentral()
    maven { url 'http://maven2.javacv.googlecode.com/git/' }
}

dependencies {
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'com.googlecode.javacpp:javacpp:0.7'
    compile 'com.googlecode.javacv:javacv:0.7'
}

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'

    defaultConfig {
        applicationId "com.example.agp.testapplication2"
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

I faced this issue on Windows 7 x64. Here is a section from my gradle.build file which resolved the problem:

compile("org.bytedeco:javacv:0.11")
compile("org.bytedeco.javacpp-presets:opencv:2.4.11-0.11:windows-x86_64")
compile("org.bytedeco.javacpp-presets:ffmpeg:2.6.1-0.11:windows-x86_64")

EDIT. Solution in this post is deprecated and may contain some errors. Please see recent, correct post at the bottom of the page.

Ok, I did it on my own :). So here is what we should do to use JavaCV in Android Studio:

1) First we should add such dependencies to build.gradle inside project:

dependencies {
    compile group: 'com.googlecode.javacpp', name: 'javacpp', version: '0.7'
    compile group: 'com.googlecode.javacv', name: 'javacv', version: '0.7'}

(I mean: build.gradle inside our module directory)

Thanks that we get javacv.jar and javacpp.jar inside external libraries (if not, we should restart Android Studio).

2) If we follow the Eclipse instruction on javacv official site we are now in point 4.:

Extract all the *.so files rom javacv-android-arm.jar, opencv-2.4.8-android-arm.jar, and ffmpeg-2.1.1-android-arm.jar directly into the newly created "libs/armeabi" folder, without creating any of the subdirectories found in the JAR files.

We have to extract all *.so files which we need, but then we should join them to our project in other special way: 3rd answer here says about it, but generally, we should:

  1. Create new 'armeabi' directory
  2. Put interesting .so files into 'armeabi' directory
  3. Create armeabi.zip
  4. Change extension of armeabi.zip to armeabi.jar
  5. Put armeabi.jar to 'libs' directory
  6. Make sure that build.gradle of our module contains such dependency:
    compile fileTree(dir: 'libs', include: ['*.jar'])

3) Enjoy your OpenCV and JavaCv in Android Studio (and don't forget to add

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

to your AndroidManifest.xml if you want to read/save image from/to sdcard and try thanks this, that your project works well).

/////////////////////////////////////////////////////////
If something is wrong, please write about it in comments.

If you try out with javacv 1.2, we have to do some additional steps so at to get this working. Here is my build.gradle file

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"

    defaultConfig {
        applicationId "org.audiorecording"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.properties'
        exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.xml'

        //might need these if you use openCV
        exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.properties'
        exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.xml'
    }
}

repositories {
    mavenCentral()
}

configurations {
    all*.exclude group: 'org.bytedeco', module: 'javacpp-presets'
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'org.bytedeco:javacv:1.2'
    compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.1.0-1.2', classifier: 'android-arm'
    compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '3.0.2-1.2', classifier: 'android-arm'

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top