如何仅为UI测试项目添加其他Android权限? [英] How can I add additional Android permissions for my UI test project only?

查看:72
本文介绍了如何仅为UI测试项目添加其他Android权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于纯测试目​​的,我尝试使用InstrumentationTestCase2派生的测试用例将文件写入外部SD卡.当在被测应用程序的AndroidManifest.xml文件中配置了android.permission.WRITE_EXTERNAL_STORAGE时,这一切正常,但如果仅在测试项目的AndroidManifest.xml文件中存在此设置,则该功能不起作用.

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?

推荐答案

简而言之,您应该为应用程序的清单和测试项目的清单添加相同的android:sharedUserId,并声明测试项目的必要权限.

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.

此解决方法来自以下事实:Android实际上是将权限分配给linux用户帐户(uid),而不是分配给应用程序本身(默认情况下,每个应用程序都有自己的uid,因此看起来每个应用程序都设置了权限).

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

但是,使用相同证书签名的应用程序可以共享相同的uid.因此,它们具有一组通用的权限.例如,我可以有一个请求WRITE_EXTERNAL_STORAGE权限的应用程序A和一个请求INTERNET权限的应用程序B. A和B都由相同的证书签名(例如,调试一个).在AndroidManifest.xml中,用于A和B的文件在<manifest>标签中声明了android:sharedUserId="test.shared.id".然后,A和B都可以访问网络并写入sdcard,即使它们仅声明所需权限的一部分,因为权限是按uid分配的.当然,只有在同时安装了A和B的情况下,此方法才有效.

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.

这里是在测试项目中如何进行设置的示例.适用于AndroidManifest.xml的应用程序:

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>

还有用于测试项目的AndroidManifest.xml

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.

有关共享UID的一些其他信息,可以在 http://developer.android.com中找到/guide/topics/security/permissions.html#userid .

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

这篇关于如何仅为UI测试项目添加其他Android权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆