如何启用运行时连接的Andr​​oid测试读/写权限的接触?权限、测试、Andr、oid

2023-09-06 06:42:26 作者:花落相依

任务:

让连接的Andr​​oid上测试版本的Andr​​oid M很好地工作。

Let connected Android tests work well on Android M.

问:

在运行Android的连接测试如何启用读/写权限的接触?

How to enable read/write contacts permission when run connected Android test?

问题:

我知道PM命令可能会启用APK的许可。

I know pm command could enable the apk's permission.

adb shell pm grant <PACKAGE_NAME> <PERMISSION_NAME>

我要运行它可以在两个真正的API和模拟API的运行测试。如果我失败的gradle在DSL触发点的命令,测试code是不能够触摸真正的API出于安全原因。

I want to run the tests which could run on both real apis and mock apis. If I fail to trigger pm command in gradle DSL, test code is not able to touch real api for security reason.

我尝试添加步骤作为第一个 connectedAndroidTest(connectedInstrumentTest)的任务。它不工作目标APK一直没有安装呢。命令行调用错误code。

I try to add the step as first of connectedAndroidTest (connectedInstrumentTest) task. It doesn't work for the target apk has not been install yet. The command lines are called with error code.

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'
         }
     }
 }

我尝试添加步骤为安装任务的最后一步。当我开始它不叫 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, 'shell', 'pm', 'grant', variant.applicationId, 'android.permission.READ_ACCOUNTS'
            }
        }
    }
}

我的计划是使权限启动测试之前。我不知道哪个任务是正确的。它看起来像 connectedVariantAndroidTest 不依赖于 installVariant ,虽然它们都调用 ADB安装

My plan is to enable permissions before tests are launched. I don't know which task is proper one. It looks like connectedVariantAndroidTest doesn't depend on installVariant, though they both call adb install.

我尝试从测试用例执行下午拨款。它未能如预期。

I try to run the pm grant from test cases. It fails as expected.

我会接受其他解决方案来运行Android测试好。

I will accept other solutions to run the android tests well.

推荐答案

我认为你需要创建依赖于 installDebug 你自己的任务,然后让 connectedDebugAndroidTest 取决于你的任务。

I think that you need to create your own task depending on installDebug and then make connectedDebugAndroidTest depend on your task.

人们它禁用动画和作品,将强制安装的应用程序和Android测试都是这样执行之前授予特定的权限:

People does it to disable animations and works, you force the app installation and grant your specific permission before the android tests are executed like this:

def adb = android.getAdbExe().toString()

task nameofyourtask(type: Exec, dependsOn: 'installDebug') { // or install{productFlavour}{buildType}
    group = 'nameofyourtaskgroup'
    description = 'Describe your task here.'
    def mypermission = 'android.permission.READ_ACCOUNTS'
    commandLine "$adb shell pm grant ${variant.applicationId} $mypermission".split(' ')
}

tasks.whenTaskAdded { task ->
    if (task.name.startsWith('connectedDebugAndroidTest')) { // or connected{productFlavour}{buildType}AndroidTest
        task.dependsOn nameofyourtask
    }
}

您可以添加此code到一个新的 yourtask.gradle 文件,并在的build.gradle的底部添加下一行文件:

You can add this code to a new yourtask.gradle file and add the next line at the bottom of the build.gradle file:

apply from: "yourtask.gradle"

和在适当的舱单申报您的许可

And declare your permission in the proper manifest

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

更新:

固定命令行命令在您的版本多个变种,谢谢。

Fixed commandLine command like you did on your version for multiple variants, thanks.

android.applicationVariants.all { variant ->
    if (variant.getBuildType().name == "debug") {
        task "configDevice${variant.name.capitalize()}" (type: Exec){
            dependsOn variant.install

            group = 'nameofyourtaskgroup'
            description = 'Describe your task here.'

            def adb = android.getAdbExe().toString()
            def mypermission = 'android.permission.READ_ACCOUNTS'
            commandLine "$adb shell pm grant ${variant.applicationId} $mypermission".split(' ')
        }
        variant.testVariant.connectedInstrumentTest.dependsOn "configDevice${variant.name.capitalize()}"
    }
}