Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Duplicate Files

I am getting duplicate files in my source code:

enter image description here

This happens quite often with Android Studio and it happens will all types of files, not just gradle files. It typically happens when I checkout a new branch or swith branches.

What is causing this and how can I prevent it?

As a note, this question was originally posted with image files, thus the image answers. However, this is not limited to image files and I understand why there are multiple image files for different screen densities. I have updated to reflect that there are other files being duplicated.

like image 713
BlackHatSamurai Avatar asked Oct 19 '25 03:10

BlackHatSamurai


2 Answers

Do you have a Mac, and is your project stored in your "Documents" directory?

Filenames that include " 2" are generated by iCloud when it can't reconcile two versions of a file. E.g. if you have two computers, and both of them share their "Documents" directories via iCloud, and a file were to be changed simultaneously on both computers, iCloud would wind up creating e.g. a build 2.gradle file.

I found this was happening to me, even though I'm only using one computer to do my work. I suspect that Android Studio may be interacting with iCloud directly, not realizing that it's working on files that are already backed up on iCloud. (See Android Studio generating hundreds of duplicate " 2" files).

I seem to have fixed it by moving my project directory out of Documents. At any rate, I haven't had a problem since I did that.

(Credit to Mike M. for noticing the similarity between our problems.)

like image 65
Edward Falk Avatar answered Oct 22 '25 05:10

Edward Falk


Since different devices support different screen densities, all non-vector image drawables should have an image asset for each type of density. So, from a development standpoint, you're using the same Drawable but the app will use the correct image size based on the screen density of the device the app is installed in. I suggest you take a look at this article for more information.

like image 32
Omar Silva Avatar answered Oct 22 '25 05:10

Omar Silva