Rakatan
Rakatan

Reputation: 461

Using JavaCV and Realm together causes "java.lang.UnsatisfiedLinkError"

I have recently been getting the following error by attempting to start an instance of JavaCV's FFmpegFrameGrabber:

java.lang.UnsatisfiedLinkError: org.bytedeco.javacpp.avutil
    at java.lang.Class.classForName(Native Method)
    at java.lang.Class.forName(Class.java:324)
    at org.bytedeco.javacpp.Loader.load(Loader.java:413)
    at org.bytedeco.javacpp.Loader.load(Loader.java:381)
    at org.bytedeco.javacpp.avformat$AVFormatContext.<clinit>(avformat.java:2597)
    at org.bytedeco.javacv.FFmpegFrameGrabber.startUnsafe(FFmpegFrameGrabber.java:386)
    at org.bytedeco.javacv.FFmpegFrameGrabber.start(FFmpegFrameGrabber.java:380)...

While solutions to this problem exist, none worked for me.

Through many trials i have discovered that weirdly enough, if i do not include Realm in my project, i no longer receive this error.

Here is the part of my build.gradle file in which I include all of these libraries:

compile group: 'org.bytedeco', name: 'javacv', version: '1.1'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.0.0-1.1', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.0.0-1.1', classifier: 'android-x86'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.8.1-1.1', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.8.1-1.1', classifier: 'android-x86'

// ORM
compile 'io.realm:realm-android:0.87.2' // Tested NOT OK - Causes JavaCV to crash
//

I am thinking that there may be a solution to this problem that i am not aware of. I found no mention anywhere on the internet about library incompatibility or why this behaviour may occur.

I will edit this post with any additional details that anyone might need.

Any help would be greatly appreciated.

EDIT

I attempted to apply the fix described here. Now my packaging options look like this:

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'
    exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.properties'
    exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.xml'

    exclude "lib/arm64-v8a/librealm-jni.so"
}

Unfortunately, this change has no effect. I'm still stuck.

Upvotes: 9

Views: 2474

Answers (2)

Maher Abuthraa
Maher Abuthraa

Reputation: 17813

Firstly this issue occurs because of Gradle doesn't resolve dependencies properly from maven profile ..In my case only x86 depdendecies shipped to APK. that means code above works only on x86 cpu architecture. solution should be done on Android Studio. but as workaround I did this:

  1. Download binary javacv-platform-1.3.1-bin.zip. It's from: https://github.com/bytedeco/javacv
  2. Inside directory javacv-bin copy these jars to new directory

    1. ffmpeg-android-arm.jar
    2. opencv-android-arm.jar
    3. ffmpeg-android-x86.jar
    4. opencv-android-x86.jar
  3. For 2.1 2.2 files, Extract these jars and go to lib then or armeabi. Then copy all *.so files into your project under:

    1. app/src/main/jniLibs/armeabi/
    2. app/src/main/jniLibs/armeabi-v7a/
  4. you might do the same with 2.3 * 2.4 jars by copying dependencies into app/src/main/jniLibs/x86/ . just check your apk if it really doesn't have them.

That's it.

Upvotes: 0

Rakatan
Rakatan

Reputation: 461

With the help of one of my coleagues i have been able to solve this issue.

In adition to the steps described in the question, we:

  • Copyed all of the .so files in the app/src/main/jniLibs/armeabi and app/src/main/jniLibs/armeabi-v7a folders
  • Added

    ndk { abiFilters "armeabi-v7a" }

    to the defaultConfig part of the module's build.gradle file

  • Added

    lintOptions { abortOnError false }

    to the android part of the module's build.gradle file

I will try to provide further clarifications to anyone that needs them if i am able.

Upvotes: 6

Related Questions