Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding .so files to jniLibs in Android Studio: duplicate files during packaging of APK

Following various sets of instructions, it seems that I should add native libraries to app/src/main/jniLibs and they'll be linked up automagically. However when I do this (for OpenCV) I get the following:

Error:duplicate files during packaging of APK /.../app/build/outputs/apk/app-debug-unaligned.apk
    Path in archive: lib/armeabi/libopencv_java.so
    Origin 1: /.../app/src/main/jniLibs/armeabi/libopencv_java.so
    Origin 2: /.../build/intermediates/exploded-aar/AppName/.libraries/opencv/unspecified/jni/armeabi/libopencv_java.so
You can ignore those files in your build.gradle:
    android {
      packagingOptions {
        exclude 'lib/armeabi/libopencv_java.so'
      }
    }
Error:Execution failed for task ':app:packageDebug'.
> Duplicate files copied in APK lib/armeabi/libopencv_java.so
    File 1: /.../app/src/main/jniLibs/armeabi/libopencv_java.so
    File 2: /.../app/src/main/jniLibs/armeabi/libopencv_java.so

Adding the exclude section doesn't work. There's only one copy of the file, but somehow it's being referenced twice, and it seems the second (build) include is something auto-generated. What can I do?

like image 672
fredley Avatar asked Jun 20 '14 15:06

fredley


2 Answers

In case of duplicate libraries (*.so) files, exclude option will not help as we cannot completely exclude the native binaries. There is one more option in packagingOptions. It is 'pickFirst'. We can avoid duplicate files error and include the first one the compiler encounters.

packagingOptions {    
    pickFirst 'lib/armeabi/libopencv_java.so'
}
like image 174
Chandra Lakkimsetty Avatar answered Oct 21 '22 07:10

Chandra Lakkimsetty


ndk-build was generating duplicates of all of the packages from OpenCV. I removed all of the .so libraries from my app, apart from my actual app library, and then it packaged up fine.

like image 21
fredley Avatar answered Oct 21 '22 08:10

fredley