Skip to content

Instantly share code, notes, and snippets.

@fangzhzh
Last active October 3, 2017 06:46
Show Gist options
  • Save fangzhzh/01e1bb59712eb2fa7d9b7a8136aeea43 to your computer and use it in GitHub Desktop.
Save fangzhzh/01e1bb59712eb2fa7d9b7a8136aeea43 to your computer and use it in GitHub Desktop.
gradle task to grant permission to device
android.applicationVariants.all { variant ->
def applicationId = variant.applicationId
def adb = androidadbExe. as String
def variantName = variant.name.capitalize()
def grantPermissionTask = task.create("create${variantName}Permissions") << {
"${adb} devices".execute().text.eachLine {
if(it.endsWith("device")){
def device = it.split()[0]
println "Granting permissions on devices ${device}"
"${adb} -s ${devices} shell pm grant ${applicationId} android.permission.ACCESS_FINE_LOCATION".execute()
"${adb} -s ${devices} shell pm grant ${applicationId} android.permission.ACCESS_COARSE_LOCATION".execute()
"${adb} -s ${devices} shell pm grant ${applicationId} android.permission.READ_EXTERNAL_STORAGE".execute()
"${adb} -s ${devices} shell pm grant ${applicationId} android.permission.WRITE_EXTERNAL_STORAGE".execute()
}
}
}
}
@letroll
Copy link

letroll commented May 19, 2016

thank for sharing, a little mistake :

android.permission.READ_EXTERNAL_STORAGE

@fangzhzh
Copy link
Author

fangzhzh commented Jun 2, 2016

@letroll thanks, fixed

@lalfcesar
Copy link

There are some mistakes on line 5:

def grantPermissionTask = task.create("create${variantName}Permissions") << {

Should be:

def grantPermissionTask = tasks.create("grant${variantName}Permissions") << {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment