How to manage Runtime permissions android marshmallow espresso tests

后端 未结 12 926
孤独总比滥情好
孤独总比滥情好 2020-12-05 02:33

I\'m using espresso for testing but sometimes I try to get an image form external storage and with marshmallow I need a Runtime permission otherwise there will be an Excepti

12条回答
  •  Happy的楠姐
    2020-12-05 03:27

    You can create an Android gradle task to grant permission:

    android.applicationVariants.all { variant ->
        def applicationId = variant.applicationId
        def adb = android.getAdbExe().toString()
        def variantName = variant.name.capitalize()
        def grantPermissionTask = tasks.create("grant${variantName}Permissions") << {
            "${adb} devices".execute().text.eachLine {
                if (it.endsWith("device")){
                    def device = it.split()[0]
                    println "Granting permissions on devices ${device}"
                    "${adb} -s ${device} shell pm grant ${applicationId} android.permission.CAMERA".execute()
                    "${adb} -s ${device} shell pm grant ${applicationId} android.permission.ACCESS_FINE_LOCATION".execute()
                }
            }
        }
    }
    

    And this is the command to run the task: gradle grantDebugPermissions

提交回复
热议问题