Android OS 11+ pushing data files to an applications scoped storage

Viewed 137

I have an Android algorithm library that I want to test inside an application that can run on real device. In order to run this test, I need to be able to push data files from a computer to the phone, to a location that can be read by the application being tested. Before OS 11 this could be done using the adb push ... command but the new scoped storage privacy feature will not allow me to do this anymore.

Whatever the solution is, it can not require manual human intervention as the intention of this test is to run automatically as part of a CI/CD pipeline.

Does anyone have any solutions? Thanks in advance for your suggestions!

1 Answers

You can add this permission to the Manifest:

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

To grant this permission you need to enable it, but as far as I know this cannot be changed programatically, but you can use this adb command to change it via UI (Or you can write an UI Automator script to be more robust):

adb shell "am start -a android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION \
       -d package:<REPLACE_WITH_YOUR_PACKAGE_NAME> ; \
       sleep 2 ; \
       input keyevent KEYCODE_DPAD_DOWN ; \
       input keyevent KEYCODE_DPAD_DOWN ; \
       input keyevent KEYCODE_DPAD_CENTER"

Now you can read your files normally:

@Test
fun myTest() {
    val content = File("/sdcard/Download/myFile.txt")
        .useLines(Charsets.UTF_8) {
            it.joinToString("\n")
        }
    Log.d("TAG", content)
}
Related