Why does having a custom name for apk leads to an error when changing target device to one with a different Android version?

Viewed 232

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?

1 Answers

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

Related