# File already exists error

**URL:** <https://discuss.bitrise.io/t/file-already-exists-error/344>\
**Category:** Builds\
**Created:** [January 18, 2017, 4:12pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344 "2017-01-18T16:12:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabien\_hinge](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fabien_hinge/32/108_2.png) [@fabien\_hinge](https://discuss.bitrise.io/u/fabien_hinge)\
**Post date:** [January 18, 2017, 4:12pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/1 "2017-01-18T16:12:35Z")

</div>

Hello,

I am trying to create two different Android builds (APK), by running the Gradle task on two different flavors.  
For some reasons it creates 3 APKs, one with a timestamp.  
Note that I am trying to trigger two Gradle tasks specific to 2 specific variants, which I couldn’t get via the more generic gradle tasks available (I would get useless builds).

It looks like something goes wrong during the copy of files.

**1st task:**

```
............
Move apk files...
  find "." "-name" "*.apk" "!" "-name" "*-unaligned.apk"
  copy ./app/build/outputs/apk/app-back-generic-development-debug.apk to /bitrise/deploy/app-back-generic-development-debug.apk
  The apk path is now available in the Environment Variable: $BITRISE_APK_PATH (value: /bitrise/deploy/app-back-generic-development-debug.apk)
............

```

**2nd task:**

```
............
Move apk files...
  find "." "-name" "*.apk" "!" "-name" "*-unaligned.apk"
  copy ./app/build/outputs/apk/app-knee-generic-development-debug.apk to /bitrise/deploy/app-knee-generic-development-debug.apk
  1 attempt failed:
  file already exists at: /bitrise/deploy/app-back-generic-development-debug.apk
  Retrying...
  copy ./app/build/outputs/apk/app-back-generic-development-debug.apk to /bitrise/deploy/app-back-generic-development-debug20170118145054.apk
  The apk path is now available in the Environment Variable: $BITRISE_APK_PATH (value: /bitrise/deploy/app-back-generic-development-debug20170118145054.apk)
............

```

Can we prevent this unnecessary APK to be created?

Maybe it’s similar to this issue: [Support for multiple apk in android workflows](http://discuss.bitrise.io/t/support-for-multiple-apk-in-android-workflows/155)

Cheers!

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 18, 2017, 5:45pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/2 "2017-01-18T17:45:52Z")

</div>

Hi,

Thanks for creating the report here!

> [@fabien\_hinge](#):
>
> It looks like something goes wrong during the copy of files.

If you’re referring to

> [@fabien\_hinge](#):
>
> file already exists at: /bitrise/deploy/app-back-generic-development-debug.apk  
> Retrying…

That’s the expected behavior. If the file already exists at the target path, the step will append a timestamp to the filename, to prevent accidental overwrite of an existing APK.

> Can we prevent this unnecessary APK to be created?

Absolutely!

- You can either tweak your Gradle configs to not to generate that APK for the task you run (I guess you run the `assemble` gradle task, which generates both a Debug and a Release APK by default)
- Or tweak/set the **Gradle Runner** step’s **APK file include filter** input. The default value is `*.apk`, to copy all generated APKs into the deploy directory, but you can change this to e.g. `*/app-knee-generic-development-debug.apk` (based on your log) to only copy the `app-knee-generic-development-debug.apk` to the deploy directory.

If you’d have any questions, just let us know!

Happy Building! 🙂

---

<div class="post-metadata">

**Author:** ![fabien\_hinge](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fabien_hinge/32/108_2.png) [@fabien\_hinge](https://discuss.bitrise.io/u/fabien_hinge)\
**Post date:** [January 19, 2017, 5:33pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/3 "2017-01-19T17:33:29Z")

</div>

Hi,

Thanks for the reply!  
I understand the config should probably be refined.

One detail though, if you look at the log there:

```
copy ./app/build/outputs/apk/app-knee-generic-development-debug.apk to /bitrise/deploy/app-knee-generic-development-debug.apk
  1 attempt failed:
  file already exists at: /bitrise/deploy/app-back-generic-development-debug.apk

```

We try to copy the file app-knee-generic-development-debug.apk, but it then complains about app-back-generic-development-debug.apk.  
I am not sure to understand how those two files would create a conflict.

Cheers!

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 19, 2017, 7:12pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/4 "2017-01-19T19:12:23Z")

</div>

> [@fabien\_hinge](#):
>
> I am not sure to understand how those two files would create a conflict.

I might misunderstood what you do:

> Note that I am trying to trigger two Gradle tasks specific to 2 specific variants

Don’t you run two gradle tasks **during the same build** , where both tasks generate `app-back-generic-development-debug.apk`?

---

<div class="post-metadata">

**Author:** ![fabien\_hinge](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fabien_hinge/32/108_2.png) [@fabien\_hinge](https://discuss.bitrise.io/u/fabien_hinge)\
**Post date:** [January 20, 2017, 10:18am UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/5 "2017-01-20T10:18:23Z")

</div>

> Don’t you run two gradle tasks during the same build, where both tasks generate app-back-generic-development-debug.apk?

No, I run two Gradle Runner blocks, one after the other, one for app-back-generic-development-debug.apk and one for app-knee-generic-development-debug.apk. That’s why it’s a bit weird.

Can I send you the link over Intercom so you can have a closer look maybe?

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 20, 2017, 10:29am UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/6 "2017-01-20T10:29:32Z")

</div>

> [@fabien\_hinge](#):
>
> No, I run two Gradle Runner blocks, one after the other,

Ahh, I know what the issue is!

1. You run the first **Gradle Runner** step which generates `app1.apk`, which is **copied** into the deploy dir, but it’s not deleted from your `build` directory, where it’s generated by `gradle`.
2. Then you run a second **Gradle Runner** step which generates `app2.apk` _into the same `build` directory_, so `app1.apk` will still be in the `build` directory when the second **Gradle Runner** step looks through the dirs for generates `apk`s, and it will find `app1.apk` again.

So, you can either delete `app1.apk` after the first Gradle Runner from the build dir (if you don’t need it there), or specify an **APK file include filter** to not to copy `app1.apk` **again** (e.g. by setting it to the name of `app2.apk` with a wildcard prefix `*/app2.apk`, as advised at [File already exists error - #2 by viktorbenei](http://discuss.bitrise.io/t/file-already-exists-error/344/2)) when the second **Gradle Runner** finishes.

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 20, 2017, 10:32am UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/7 "2017-01-20T10:32:00Z")

</div>

Not sure if we should change the behavior of **Gradle Runner** , e.g. to _move_ the apk into the deploy dir instead of _copy_…

I believe the current default is better, as you can run it locally on your Mac with our [Bitrise CLI](https://www.bitrise.io/cli) and get the expected output (that the `apk` can be found in the `build` directory - as well as in the _deploy_ directory).

---

<div class="post-metadata">

**Author:** ![fabien\_hinge](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fabien_hinge/32/108_2.png) [@fabien\_hinge](https://discuss.bitrise.io/u/fabien_hinge)\
**Post date:** [January 20, 2017, 10:38am UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/8 "2017-01-20T10:38:11Z")

</div>

OK nice, thanks for the reply!  
I will have to refine the flow indeed. Now that I have played more with the editor, I don’t think it’s really the natural way to do this anyway.

Cheers!

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 20, 2017, 10:48am UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/9 "2017-01-20T10:48:24Z")

</div>

If you have any questions just let us know! 😉

Note: you can specify multiple gradle tasks for a single Gradle Runner, space separated (eg: `assembleX assembleY`), that would solve the issue in your case I think.

---

<div class="post-metadata">

**Author:** ![fabien\_hinge](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fabien_hinge/32/108_2.png) [@fabien\_hinge](https://discuss.bitrise.io/u/fabien_hinge)\
**Post date:** [January 23, 2017, 10:58am UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/10 "2017-01-23T10:58:07Z")

</div>

Oooooh OK!  
Thanks!

---

<div class="post-metadata">

**Author:** ![fabien\_hinge](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fabien_hinge/32/108_2.png) [@fabien\_hinge](https://discuss.bitrise.io/u/fabien_hinge)\
**Post date:** [January 23, 2017, 5:24pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/11 "2017-01-23T17:24:05Z")

</div>

> Note: you can specify multiple gradle tasks for a single Gradle Runner, space separated (eg: assembleX assembleY), that would solve the issue in your case I think.

Works like a charm!

Another question:  
If I do `assembleX assembleY` via a Gradle Runner and then call Deploy to Bitrise, is there any way I can get an array of build URLs into $BITRISE\_PUBLIC\_INSTALL\_PAGE\_URL, instead of a single APK?

I understand I could also “Compress the artifacts into one file” but I would like to plug in a Slack Message callback where I could say:

- Build: [http://buildurl](http://buildurl)… (I know how to do this)
- APK X: http://…
- APK Y: http://…

Thanks!

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 23, 2017, 5:45pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/12 "2017-01-23T17:45:40Z")

</div>

> [@fabien\_hinge](#):
>
> is there any way I can get an array of build URLs into $BITRISE\_PUBLIC\_INSTALL\_PAGE\_URL, instead of a single APK?

Not right now, unfortunately, but it’s definitely planned - see:

> [@Support for multiple apk in android workflows](http://discuss.bitrise.io/t/support-for-multiple-apk-in-android-workflows/155):
>
> From @motorro on Thu Oct 13 2016 08:12:07 GMT-0400 (EDT) As an android programmer I’m eager to get support for multiple apk builds in Android workflow. Say I have a multi-variant application or a platform apk split and want them signed and deployed. Let’s take the latter case for example. When build is performed I have a set of apk (one per platform): app-x86-release-unsigned.apk app-x86\_64-release-unsigned.apk ... app-armeabi-release-unsigned.apk The problem is only the single file is put…

---

<div class="post-metadata">

**Author:** ![fabien\_hinge](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fabien_hinge/32/108_2.png) [@fabien\_hinge](https://discuss.bitrise.io/u/fabien_hinge)\
**Post date:** [January 23, 2017, 5:53pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/13 "2017-01-23T17:53:11Z")

</div>

Awesome thanks!

---

<div class="post-metadata">

**Author:** ![viktorbenei](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/viktorbenei/32/18_2.png) [@viktorbenei](https://discuss.bitrise.io/u/viktorbenei)\
**Post date:** [January 27, 2017, 5:02pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/14 "2017-01-27T17:02:03Z")

</div>

One note, **Gradle Runner** v1.5.3 changed how the filter works, and instead of a _file name_ filter it uses a _path_ filter now.

If you used a wildcard prefixed pattern before, like `*.apk`, this should not affect you at all, but if you previously used a full file name like `app1.apk` you should change the pattern to `*/app1.apk`, or specify the full path (can be relative, e.g. `./app/build/outputs/apk/app1.apk` or `*/app/build/outputs/apk/app1.apk`) or part of the path with a wildcard prefix (e.g. `*/outputs/apk/app1.apk`).

Basically you now specify a pattern _for the path_, instead of for the _file name_.

Related discussion:

> [@Android apk not being found anymore](http://discuss.bitrise.io/t/android-apk-not-being-found-anymore/484/7):
>
> OK, we found the issue and it’s indeed related to the new Gradle Runner step version. Fortunately the issue is really simple to fix: just add a \* prefix to the filter. E.g. instead of: app-stage-release.apk use: \*app-stage-release.apk or \*/app-stage-release.apk Basically the change was that the filter is no longer applied on the file name, but instead on the path, so if you specify an exact value without \* then the full path have to match that. The advantage of the new filter method is th…

Related #Changelog :

> [@Gradle Runner v1.5.3 - change in filter input!](http://discuss.bitrise.io/t/gradle-runner-v1-5-3-change-in-filter-input/487):
>
> Gradle Runner v1.5.3 was just released and it changed how the include and exclude filters works; instead of a file name filter it uses a path filter now. If you used a wildcard prefixed pattern before, like \*.apk, this should not affect you at all, but if you previously used a full file name like app1.apk you should change the pattern to \*/app1.apk, or specify the full path (can be relative, e.g. ./app/build/outputs/apk/app1.apk or \*/app/build/outputs/apk/app1.apk) or part of the path with a wi…

_Edited my previous comments above in this discussion to reflect this change._

---

<div class="post-metadata">

**Author:** ![fabien\_hinge](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.bitrise.io/fabien_hinge/32/108_2.png) [@fabien\_hinge](https://discuss.bitrise.io/u/fabien_hinge)\
**Post date:** [January 27, 2017, 5:48pm UTC](https://discuss.bitrise.io/t/file-already-exists-error/344/15 "2017-01-27T17:48:41Z")

</div>

Thanks for the update! I don’t have any filter in place so it’s fine for me.  
Cheers
