view raw
Heribert Heribert - 7 months ago 52
Android Question

Duplicate files during packaging of APK or UnsatisfiedLinkError Android

I have a Android project which uses libraries with interdependencies:

Android Application depends on the following native libraries

  • module tess-two (containing tesseract and leptonica)

  • module opencv4android

  • module mylibrary

mylibrary also depends on tess-two and opencv4android

The following is the build.gradle file of my Android Application:

apply plugin: ''

dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile project(':libraries:opencv4android')
compile project(':libraries:tess-two')
compile project(':libraries:mylibrary')

android {
compileSdkVersion 19
buildToolsVersion "22.0.1"

defaultConfig {
minSdkVersion 14
targetSdkVersion 19

// packagingOptions {
// exclude 'lib/armeabi/'
// exclude 'lib/armeabi/'
// exclude 'lib/armeabi-v7a/'
// exclude 'lib/armeabi-v7a/'
// exclude 'lib/mips/'
// exclude 'lib/mips/'
// exclude 'lib/x86/'
// exclude 'lib/x86/'
// }

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']


A gradle build on this configuration fails with
duplicate files during packaging of APK
error due to
being copied twice.

When I uncomment the packaging options I can build successfully, however, I get an
at runtime since
are not contained in the apk at all.

How can I get
to be included in the apk exactly once?


I think I found the solution, however, I do not quite understand it.

The build.gradle files of both modules mylibrary and tess-two contained a definition for

android.sourceSets.main.jniLibs.srcDirs = ['libs']

Commenting this definition in tess-two resulted in libtess.soand being copied to the apk only once.

If someone understands this solution please leave a comment or answer.


In the meantime I found a better solution: I added the following code to my main applications file:

packagingOptions { // otherwise and are copied to apk twice resulting in an error