Task:
Let connected Android tests work well on Android M.
Question:
How to enable read/write contacts permission when run connected Android test?
Problem:
I know pm command could enable the apk's permission.
adb shell pm grant <PACKAGE_NAME> <PERMISSION_NAME>
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.
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'
}
}
}
I try to add the step as last step of install task. It isn't called when I start 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'
}
}
}
}
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.
I will accept other solutions to run the android tests well.