1

I'd like to mock locations in release build type of Android app. I've created a special flavour "mockable" manifest in app/src/mockable/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
</manifest>

But it seems that Android lint blocks from using this permissions in release build type. For command:

 ./gradlew assembleRelease

I got output:

:app:lintVitalMockableRelease                 
/home/fr/app/src/mockable/AndroidManifest.xml:3: Error: Mock locations should only be requested in a debug-specific manifest file (typically src/debug/AndroidManifest.xml) [MockLocation]
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   Explanation for issues of type "MockLocation":
   Using a mock location provider (by requiring the permission
   android.permission.ACCESS_MOCK_LOCATION) should only be done in debug
   builds. In Gradle projects, that means you should only request this
   permission in a debug source set specific manifest file.

   To fix this, create a new manifest file in the debug folder and move the
   <uses-permission> element there. A typical path to a debug manifest
   override file in a Gradle project is src/debug/AndroidManifest.xml.

1 errors, 0 warnings

Is it possible to generate release app with location mocking permission?

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103

3 Answers3

7

Disable the MockLocation lint check for that particular permission, i.e.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <uses-permission
        android:name="android.permission.ACCESS_MOCK_LOCATION"
        tools:ignore="MockLocation" />  
</manifest>

(Refer to http://tools.android.com/tips/lint-checks)

user3761063
  • 81
  • 1
  • 2
4

The only solution I know for sure is to let lint report errors but don't let that abort the build process:

To enable that just add

lintOptions {
   abortOnError false
}

inside the android block.

You will still get lint errors and warnings but the build will continue.

I am not sure if you can put that inside the flavor definition or use a custom buildType for that. I have no project where I could test that.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
0

This is a caution. If you want a user to "mock location" then the user must enable mock locations in Android.

A production release is definitely possible, I have done this. You need to prompt the user to allow for "Mock Locations" like a developer/debug would.

Jim
  • 10,172
  • 1
  • 27
  • 36