Android – Fix for Duplicate files copied in APK

If you are an android developer you may already familiar with this issue.

com.android.build.api.transform.TransformException: 
    com.android.builder.packaging.DuplicateFileException: 
    Duplicate files copied in APK *****************

Usually you’re getting this for some text files or license files like META-INF/notice.txt , META-INF/license.txt , META-INF/ASL2.0

In such scenarios we can exclude those files from APK packaging without any hesitation because we definitely know those will not affect our application functionality. We can do so using gradle packagingOptions .

packagingOptions {
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/ASL2.0'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

What can we do when we get like

com.android.build.api.transform.TransformException: 
    com.android.builder.packaging.DuplicateFileException: 
    Duplicate files copied in APK lib/arm64-v8a/librealm-jni.so

or

com.android.build.api.transform.TransformException: 
    com.android.builder.packaging.DuplicateFileException: 
    Duplicate files copied in APK lib/x86/librealm-jni.so

or

com.android.build.api.transform.TransformException: 
    com.android.builder.packaging.DuplicateFileException: 
    Duplicate files copied in APK lib/armeabi/librealm-jni.so

We cannot exclude those files since they required for app functionality. So we need another way to handle such scenarios.

We can use pickFirst in packagingOptions to avoid duplicate file copying to APK as below.

packagingOptions {
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        pickFirst 'lib/armeabi/librealm-jni.so'
        pickFirst 'lib/x86/librealm-jni.so'
        pickFirst 'lib/x86_64/librealm-jni.so'
        pickFirst 'lib/arm64-v8a/librealm-jni.so'
        pickFirst 'lib/armeabi-v7a/librealm-jni.so'
        pickFirst 'lib/mips/librealm-jni.so'
    }



Leave a Reply

Your email address will not be published. Required fields are marked *