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

查看:12
本文介绍了如何仅为我的 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 都由相同的证书签名(假设是调试一个).在 A 和 B 的 AndroidManifest.xml 文件中,android:sharedUserId="test.shared.id" 标签中声明.然后 A 和 B 都可以访问网络并写入 SD 卡,即使它们只声明了所需权限的一部分,因为权限是按 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天全站免登陆