安装Android应用程序时是否有允许adb命令行选项的gradle插件?我希望在Android M设备上运行robotium测试,同时忽略弹出要求摄像头,麦克风等权限的权限对话框。
答案 0 :(得分:8)
task grantPermissions(type: Exec, dependsOn: 'installDebugAndroidTest') {
def permissions = ['INTERNET',
'GET_ACCOUNTS',
'WAKE_LOCK',
'VIBRATE',
'READ_CONTACTS',
'RECORD_AUDIO',
'CAMERA',
'WRITE_EXTERNAL_STORAGE',
'READ_EXTERNAL_STORAGE']
permissions.each {
commandLine "\$ANDROID_HOME/platform-tools/adb shell pm grant com.singlewire.cirrus android.permission.${it}".split(' ')
}}
tasks.whenTaskAdded { task ->
if (task.name.startsWith('connectedDebugAndroidTest')) {
task.dependsOn grantPermissions
}
}
答案 1 :(得分:-1)
android.productFlavors.all { flavour ->
def applicationId = flavour.applicationId
def adb = android.getAdbExe().toString()
def grantPermissionsTask = tasks.create("grant${flavour.name.capitalize()}Permissions") << {
"${adb} shell pm grant ${applicationId} android.permission.ACCESS_FINE_LOCATION".execute()
"${adb} shell pm grant ${applicationId} android.permission.ACCESS_COARSE_LOCATION".execute()
}
grantPermissionsTask.description = "Grants permissions for Marshmallow"
tasks.whenTaskAdded { theTask ->
def assemblePattern = ~"assemble${flavour.name.capitalize()}DebugAndroidTest"
if (assemblePattern.matcher(theTask.name).matches()) {
theTask.dependsOn grantPermissionsTask.name
}
}
}
您可以仅针对检测测试进行单元测试。 Reference