Solved – DuplicateFileException: Duplicate files copied in APK META-INF/..

It is more often that you see DuplicateFileException: Duplicate files copied in APK META-INF/.. errors in the Gradle build system. The concept is simple, In a Gradle build, you are not allowed to include a file more than once with the same path.

Thus the same file, when included more than once, makes a duplicate error. For example, If you were using two jars which contain common META-INF files, then you are duplicating the META-INF files. Since you don’t need these duplicate files in your application, the simplest way of doing is to tell the build system to exclude the files altogether.

Few other build systems are more lenient compared to the Gradle build system. In Java, it is typical that if you include the same class file twice or more in a classpath, the duplicates are automatically ignored. For example, the first copy of the class file will be considered by the build system and will be used, and the subsequent same class files appearing for the second time are ignored.

Even if this looks easier, there are a number of drawbacks in such builds. The biggest of them is the subtle errors. When it happens without you knowing it’s indeed a creepy topic. It can be hard to figure out why you ended up with these errors and where you did that. Once you do figure it correctly, you can easily solve them by simply writing in the order in which things need to be included. For the reason that Gradle system doesn’t need any ordering of things, it becomes one of the most compatible build systems.

 

If the packager is informing that the file META-INF/XXX appears in more than one jar file, it shall be resolved by simply adding these lines of code to your app’s build.gradle.

 

 

Also, make sure that you’re not duplicating the dependencies.

For example,

Here in the first line of code, you are including all the jar files in the libs directory, and then in the second line again httpmine.jar is included which is also from the same libs directory, which makes a duplication.

To avoid such duplication, comment out those duplicate lines.

 

More common issue and its solution

If you are getting “DuplicateFileException: Duplicate files copied in APK”, just make sure that you are not repeatedly calling same dependencies.

For example, here’s how generally files get duplicated

Here httpclient is already in the group httpcomponents, so basically you’re duplicating httpclient.

So it should be written this way:

 

Don’t forget to clean, rebuild after making changes!

 

CONCLUSION

You might have encountered another problem similar to this, the “Multiple dex files defined” problem. This also happens because of the duplicate class files. So as a conclusion Gradle build is so strict in handling duplicate resources, and you should avoid such duplications.

 

Leave a Reply

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