32

I try to write files to the external SD card in a from InstrumentationTestCase2 derived test case for pure testing purposes. This works all well when android.permission.WRITE_EXTERNAL_STORAGE is configured in the AndroidManifest.xml file of the application under test, but does not work if this setting is only present in the AndroidManifest.xml file of the test project.

Naturally, I don't want to add this permission to the main manifest, since I only need this capability during my functional tests. How can I achieve that?

Thomas Keller
  • 5,933
  • 6
  • 48
  • 80

2 Answers2

39

In short you should add the same android:sharedUserId for both application's manifest and test project's manifest and declare necessary permission for the test project.

This workaround comes from the fact that Android actually assigns permissions to linux user accounts (uids) but not to application themselves (by default every application gets its own uid so it looks like permissions are set per an application).

Applictions that are signed with the same certificate can however share the same uid. As a consequence they have a common set of permissions. For example, I can have application A that requests WRITE_EXTERNAL_STORAGE permission and application B that requests INTERNET permission. Both A and B are signed by the same certificate (let's say debug one). In AndroidManifest.xml files for A and B android:sharedUserId="test.shared.id" is declared in <manifest> tag. Then both A and B can access network and write to sdcard even though they declare only part of needed permissions because permissions are assigned per uid. Of course, this works only if both A and B are actually installed.

Here is an example of how to set up in case of the test project. The AndroidManifest.xml for application:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testproject"
    android:versionCode="1"
    android:versionName="1.0"
    android:sharedUserId="com.example.testproject.uid">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name="com.example.testproject.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And the AndroidManifest.xml for a test project

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testproject.test"
    android:sharedUserId="com.example.testproject.uid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.example.testproject" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

The drawback of this solution is that the application is able to write to external storage too when test package is installed. If it accidentally writes something to a storage it may remain unnoticed until release when the package will be signed with a different key.

Some additional information about shared UIDs can be found at http://developer.android.com/guide/topics/security/permissions.html#userid.

Darth Beleg
  • 2,657
  • 24
  • 26
  • Perfect and comprehensive answer! Now this is a very good use case for the otherwise "deprecated" shared user ids. I can live with the drawbacks you mentioned, the most important thing here is really to separate test from production permissions and this is accomplished. Thanks again! – Thomas Keller Jan 07 '13 at 15:36
  • nice workaround, but i still didn't get, when test.apk has all permission it required then why it was still throwing the error ? – Prateek Jain Jan 20 '16 at 17:59
  • 4
    Hi! Nice workaround, thank you! However, it seems that starting with Android 6 (API level 23) probably due to the new permission system, the permissions are not merged anymore. I personally didn't find any documentation explaining this, so my assumption might not be correct. Is there anybody experiencing this issue besides me? – Bianca Daniciuc Nov 08 '16 at 10:04
  • you saved my day! – ospider Feb 26 '17 at 14:40
  • 1
    Unfortunately, `sharedUserId` is [deprecated](https://developer.android.com/guide/topics/manifest/manifest-element#uid) as of API 29. I also get [an error](https://stackoverflow.com/q/15205159/238753) when installing the app with `sharedUserId` over an old version, which means I cannot deploy the new version of my app to Production since users won't be able to upgrade. – Sam Jun 19 '20 at 22:06
16

There is another easy answer.

Set the permission in src/debug/AndroidManifest.xml. If the file doesn't exist, create it.

By default, AndroidTest uses debug as BuildType, so if you define your testing permissions there, then the manifest merging process will add these permissions to your UI test build.

Here is the Manifest with new permissions. Note that I didn't include the package attribute because it will be inherited from lower priority level manifest.

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

Also, if you want to apply these permissions to a buildType other than debug, then just move your new AndroidManifest.xml to the folder you want, and use this:

android {
   ...
   testBuildType 'release' // or other buildType you might have like 'testUI'
}
Sam
  • 40,644
  • 36
  • 176
  • 219
Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • I have tried this solution, but no luck. I am still getting the error as "java.io.IOException: open failed: EACCES (Permission denied)" – mra419 Dec 06 '18 at 10:23
  • did you change testBuildType to release? – Maher Abuthraa Dec 06 '18 at 11:14
  • When I change my `testBuildType`, then my test code no longer builds. The testing libraries defined in `build.gradle` like `androidTestImplementation 'androidx.test:core:1.2.0'` can no longer be referenced from my Java testing code. Is there another step needed to get this working? – Sam Jun 19 '20 at 22:31