How can I add mock location permission to Android jobs?

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.

  - 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)

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.

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…

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

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”):

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?

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.

Here 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?

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 you can see the travis logs.