3

The issue I have is that if I try to run my app on different devices that have different Android version I have to clean every time before changing Android versions. If I don't clean, I get the following error:

Zip file '/long path to app/app/build/outputs/app/apk/my_app_name-my_variant_name-verionName.apk' already contains entry 'some file.dex', cannot overwrite

After having this error for a long time I finally found the piece of code that is causing the problem when Android Studio Arctic Fox started telling me exactly which file had the conflict.

The gradle code that causes the problem is this:

android.applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    outputFileName = new File(path + "my_app_name-${variant.name}-${variant.versionName}.apk")
                }
            }

One solution I've found that saves me a clean is to delete the build/outputs/app directory.

So is there some way to accomplish the same thing without having this issue?

casolorz
  • 8,486
  • 19
  • 93
  • 200
  • Seems like an elevation issue. Can you try running Android Studio in admin mode or check if ransomware protection is causing any issues? – Abishek Stephen Aug 27 '21 at 09:03
  • Sorry this is the first time I've heard of that and a quick search on Google didn't help, how do I do this on OSX? – casolorz Aug 27 '21 at 16:37
  • I am not well versed with OSX unfortunately. You use sudo command I think? Need someone with knowledge on this matter... In windows you simply set the program to run as administrator... – Abishek Stephen Aug 27 '21 at 17:59
  • So this ransomware protection is something of Android Studio or the OS? – casolorz Aug 28 '21 at 21:04
  • It is for Windows OS. It interferes with file read/write operations in protected folders. See https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/controlled-folders?view=o365-worldwide#:~:text=On%20your%20Windows%2010%20device,need%20to%20turn%20it%20on. – Abishek Stephen Aug 29 '21 at 06:14
  • This is probably because Android builds different APKs for different ABIs or screen densities and expect them to be placed under different file names. See for example: https://developer.android.com/studio/build/configure-apk-splits#build-apks-filename – Hampus Sep 02 '21 at 20:17
  • You are probably down the right path but when I comment my line it doesn't name the apks that way, just names it `app-google-debug.apk` where `google` I think is coming from my variant name. Another odd thing is that I have this line that causes issues under `release` in `buildTypes` so it shouldn't even be running when doing a `debug` build but that is when it gives me an issue. – casolorz Sep 03 '21 at 14:32

1 Answers1

1

This isn't exactly an answer as to why it happens. As someone pointed out, it might have to do with Android Studio trying to build apks per architecture, however the solution I found here https://stackoverflow.com/a/28992851/704836 does not make different apks per architecture, so I don't know why this fixes it but it does.

Basically I removed the code from the original post:

android.applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    outputFileName = new File(path + "my_app_name-${variant.name}-${variant.versionName}.apk")
                }
            }

And replaced it with:

setProperty("archivesBaseName", "MyApp-$versionName")

And that was enough to solve the issue. I can not switch phone targets and it works just fine. The new apk that is generated is called MyApp-version-variant.apk

casolorz
  • 8,486
  • 19
  • 93
  • 200