Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing permissions and an Android apps Manifest file

I'd like to know if there is a way to add certain permissions (or anything) to an android manifest file, but so that its only used during test runs - not production. I'm looking for something programmatic, not cutting and pasting when I'm testing.

Here's the context:

I'm reading this article: http://developer.android.com/training/location/location-testing.html, the best practice for test running an app used to be creating a 'test-app' however with android studio we now are not meant to create a new app - all testing should be done through the app. (Thank you gradle)

The issue is that this article is written with a testing permission (ACCESS_MOCK_LOCATION) in it, and I don't want that sitting in my app - and if there's a good way of doing it, I'd like to do that.

UPDATE: The reason I had this problem was because of a misunderstanding of the set up of android studio architecture since the migration to Gradle. I didn't realize that the build types shared the 'androidTest' and 'main' source folders. And so when testing or running the unfinished app, it takes the debug files (if any) and adds all the production stuff to it. So in my case, I added a empty manifest file in debug and simply added the two permissions to it. When I run or test, gradle adds all of my apps things from its other manifest to it this skeletal file (or vice versa, I'm uncertain).

So in the end we don't need to modify the androidTest folder (in fact I don't think we are allowed to add a manifest here) as its completely generated based off of whether a user is running on debug or deployment. Cheers! :-)

like image 717
Jono Avatar asked Aug 07 '14 00:08

Jono


2 Answers

Let's say you use the default debug and release build types and you run your tests against the debug build type. In this case you can create a src/debug/AndroidManifest.xml and add the additional permissions you need in your debug builds:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.package">

    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

</manifest>
like image 127
rciovati Avatar answered Oct 10 '22 03:10

rciovati


If your project does not follow the default folder hierarchy and you want to add permissions to the debug build add the following block.

sourceSets {
    debug {
        manifest.srcFile '<your folder>/AndroidManifest.xml'
    }
}
like image 34
Reid Baker Avatar answered Oct 10 '22 04:10

Reid Baker