相关疑难解决方法(0)

如何在运行连接Android测试时启用读/写联系人权限?

任务:

让连接的Android测试在Android M上运行良好.

题:

如何在运行连接Android测试时启用读/写联系人权限?

问题:

我知道pm命令可以启用apk的权限.

adb shell pm grant <PACKAGE_NAME> <PERMISSION_NAME>
Run Code Online (Sandbox Code Playgroud)

我想运行可以在真正的apis和mock apis上运行的测试.如果我无法在gradle DSL中触发pm命令,则出于安全原因,测试代码无法触及真正的api.

我尝试将该步骤添加为第一项connectedAndroidTest (connectedInstrumentTest)任务.它不适用于目标apk还没有安装.使用错误代码调用命令行.

android.testVariants.all { variant ->
    variant.connectedInstrumentTest.doFirst {
        def adb = android.getAdbExe().toString()
        exec {
            commandLine 'echo', "hello, world testVariants"
        }
        exec {
            commandLine adb, 'shell', 'pm', 'grant', variant.testedVariant.applicationId, 'android.permission.READ_ACCOUNTS'
         }
     }
 }
Run Code Online (Sandbox Code Playgroud)

我尝试添加步骤作为安装任务的最后一步.我开始时没有打电话connectedAndroidTest.

android.applicationVariants.all { variant ->
    if (variant.getBuildType().name == "debug") {
        variant.install.doLast {
            def adb = android.getAdbExe().toString()

            exec {
                commandLine 'echo', "hello, world applicationVariants"
            }
            exec {
                commandLine adb, …
Run Code Online (Sandbox Code Playgroud)

android

4
推荐指数
1
解决办法
2563
查看次数

标签 统计

android ×1