# How can I add mock location permission to Android jobs?

**URL:** <https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162>\
**Category:** Languages\
**Tags:** android\
**Created:** [November 27, 2019, 10:56am UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162 "2019-11-27T10:56:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![freakyblue](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/freakyblue/32/3279_2.png) [@freakyblue](https://travis-ci.community/u/freakyblue)\
**Post date:** [November 27, 2019, 10:56am UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/1 "2019-11-27T10:56:36Z")

</div>

My Android app uses location. To test it on my physical phone with mock locations I added `android.permission.ACCESS_MOCK_LOCATION` to debug manifest. To use mock locations I activated `adb shell appops set com.example.my.package android:mock_location allow`.

How do I do that in travis?  
I tried the same command in the `before_script:`-section, but it does not work.

```auto
  - android-wait-for-emulator
  - adb shell input keyevent 82 &
  - adb shell appops set com.example.my.package android:mock_location allow

```

Here the error message:  
`V/InstrumentationResultParser: java.lang.SecurityException: Requires ACCESS_MOCK_LOCATION secure setting`

Of course the tests run fine in local mode and the rest of my `.travis.yml` is set up correct. (Travis tests that do not require location permission run fine as well)

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [November 28, 2019, 10:34pm UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/2 "2019-11-28T22:34:32Z")

</div>

Please link to an affected build or I can’t say anything for sure.

From what I can see, you need to add this permission into your app’s `AndroidManifest.xml`. The error suggests that it’s not produced by emulator’s security but by some check that validates your source code even before building it.

> <https://stackoverflow.com/questions/39204953/mock-location-does-not-work-in-android-studio-emulator>

---

<div class="post-metadata">

**Author:** ![freakyblue](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/freakyblue/32/3279_2.png) [@freakyblue](https://travis-ci.community/u/freakyblue)\
**Post date:** [November 29, 2019, 4:26pm UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/3 "2019-11-29T16:26:34Z")

</div>

I added the mock location permission already (otherwise the test could not run on a physical device). And no, building does not produce any error. The error occurs during the execution of the regression test.

I’m creating a demo repo to inspect this error…

---

<div class="post-metadata">

**Author:** ![freakyblue](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/freakyblue/32/3279_2.png) [@freakyblue](https://travis-ci.community/u/freakyblue)\
**Post date:** [November 30, 2019, 4:19am UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/4 "2019-11-30T04:19:32Z")

</div>

@native-api here you can see the error:  
[https://travis-ci.com/freakyblue/GPSDemo/builds/138770728](https://travis-ci.com/freakyblue/GPSDemo/builds/138770728)

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [November 30, 2019, 1:21pm UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/5 "2019-11-30T13:21:50Z")

</div>

Googling this message invariably says that you need to enable Mock Locations for your specific app in Development options in emulator’s GUI (the same as for a real device). Not very helpful for an automated run.

Finally, I guessed to search if the same is possible with ADB (googled “adb enable mock locations command line”):

> <https://stackoverflow.com/questions/36339817/is-there-an-adb-command-to-toggle-allow-mock-locations>

---

<div class="post-metadata">

**Author:** ![freakyblue](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/freakyblue/32/3279_2.png) [@freakyblue](https://travis-ci.community/u/freakyblue)\
**Post date:** [November 30, 2019, 7:28pm UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/6 "2019-11-30T19:28:30Z")

</div>

I know. That’s the point of my question: As you see in my initial post, I found the command as well and on a virtual device this works fine. But using it in the `.travis.yml` does not have the wished effect (as you can see in the linked repository).

I am not sure if I added the command to the correct block. But the position where I put it, is my best guess. Any suggestion where to put this line to get the intended effect?

---

<div class="post-metadata">

**Author:** ![native-api](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/native-api/32/430_2.png) [@native-api](https://travis-ci.community/u/native-api)\
**Post date:** [November 30, 2019, 9:23pm UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/7 "2019-11-30T21:23:10Z")

</div>

I don’t see this command anywhere in the linked build so I assumed you are not running it 'cuz showing what exactly you are doing to trigger the problem is the entire purpose of providing a reproducing example.

Since this command is equivalent to setting the permission in GUI manually, you need to run it at the same moment that you would do that. That is, it’s very likely that you need to run it _after_ you install the app – i.e. from inside Gradle.

> <https://stackoverflow.com/questions/38586678/grant-access-mock-location-permission-for-testing-using-adb-fails-in-marshmallow/38587260#38587260>

---

<div class="post-metadata">

**Author:** ![freakyblue](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/freakyblue/32/3279_2.png) [@freakyblue](https://travis-ci.community/u/freakyblue)\
**Post date:** [December 1, 2019, 11:56am UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/8 "2019-12-01T11:56:07Z")

</div>

[Here](https://travis-ci.com/freakyblue/GPSDemo/jobs/261753496/config) you see the config with the line  
`adb shell appops set de.freakyblue.gpsdemo android:mock_location allow`.

The linked example is a ruby repo. Can you tell (or show) me where to put the gradle block?

---

<div class="post-metadata">

**Author:** ![freakyblue](https://sea1.discourse-cdn.com/flex015/user_avatar/travis-ci.community/freakyblue/32/3279_2.png) [@freakyblue](https://travis-ci.community/u/freakyblue)\
**Post date:** [December 2, 2019, 11:15am UTC](https://travis-ci.community/t/how-can-i-add-mock-location-permission-to-android-jobs/6162/9 "2019-12-02T11:15:32Z")

</div>

Does anyone has an idea why the `android:mock_location` does not appear in AppOps of adb?  
I tried every kind of this appops argument but adb answers every time with  
`adb shell appops set de.freakyblue.gpsdemo ACCESS_MOCK_LOCATION allow` or something similar.  
[Here](https://travis-ci.com/freakyblue/GPSDemo) you can see the travis logs.
