I want to execute JUnit tests against my lib and want to ask user to grant all required permissions before the test starts (because I need to read some files from device storage during the automated tests for example).
I know how to make it by adding a task to gradle and run it from cmd, it is working well. But I need to ask user (or do it automatically) when I ran my tests using IDE.
I've tried to add permission request to MainActivity.onCreate() of test app but no luck because MainActivity starts not for all tests.
Do anyone have any ideas?
Also, please do not talk about adding gradle grant task to execution configuration. it is working but unusable, needs something more unified.
I found a solution to my problem. It was easy:
needs to create a new task in app-level build.gradle
file like this:
android.applicationVariants.all { variant ->
def applicationId = variant.applicationId
def adb = android.getAdbExe().toString()
def variantName = variant.name.capitalize()
def grantPermissionTask = tasks.create("create${variantName}Permissions") << {
println "Granting permissions"
"${adb} shell pm grant ${applicationId} android.permission.ACCESS_FINE_LOCATION".execute()
"${adb} shell pm grant ${applicationId} android.permission.WRITE_EXTERNAL_STORAGE".execute()
"${adb} shell pm grant ${applicationId} android.permission.READ_EXTERNAL_STORAGE".execute()
}
}
then add the following dependency:
preBuild.dependsOn "createDebugPermissions"
after that, all required permissions will be granted when you run any test