okreplay icon indicating copy to clipboard operation
okreplay copied to clipboard

WRITE_EXTERNAL_STORAGE Permission on newer devices

Open christopherperry opened this issue 7 years ago • 19 comments

When trying to run the sample tests on my Nexus 6P running Android 7.1.2 I get the following RuntimeException:

We need WRITE_EXTERNAL_STORAGE permission for OkReplay. Please add adbOptions { installOptions "-g" } to your build.gradle file.

This option is already in the build.gradle file of the sample.

christopherperry avatar Jun 23 '17 22:06 christopherperry

Are you running from the command line or Android Studio?

felipecsl avatar Jun 23 '17 22:06 felipecsl

Android Studio, from the test class.

christopherperry avatar Jun 23 '17 22:06 christopherperry

Hmm that may be why. Does it work if you run from the command line with ./gradlew connectedAndroidCheck?

felipecsl avatar Jun 23 '17 22:06 felipecsl

./gradlew connectedAndroidTest is able to run the tests; however, the tests fail. Perhaps the IdlingResource isn't working correctly?

christopherperry avatar Jun 23 '17 22:06 christopherperry

Well that's gonna depend on why it's failing, but that's out of the scope of this issue, it seems. Regarding the original problem, I think it's expected that running from Android Studio doesn't respect adbOptions unfortunately

felipecsl avatar Jun 23 '17 22:06 felipecsl

It seems to respect the adbOptions for me, as long as you have the permission defined in the AndroidManifest

athkalia avatar Jul 09 '17 22:07 athkalia

@athkalia I still see the same issue. Were you able to get the test running from Android Studio?

niranjani23 avatar Jul 31 '17 18:07 niranjani23

@felipecsl I too noticed the error: We need WRITE_EXTERNAL_STORAGE permission for OkReplay. Do you mind updating the readme if the test can be invoked only via command line and not via Android Studio?

niranjani23 avatar Jul 31 '17 18:07 niranjani23

I'll give it another bash over the weekend and update here

athkalia avatar Aug 16 '17 10:08 athkalia

indeed I've tried it once more and it doesn't seem to work, sorry

athkalia avatar Aug 20 '17 17:08 athkalia

Setup is here btw if you want to experiment a bit https://github.com/athkalia/Just-Another-Android-App

athkalia avatar Aug 20 '17 17:08 athkalia

I've been running usually on API25 emulators with no problems. Are you running on physical devices?

felipecsl avatar Aug 21 '17 17:08 felipecsl

Confirmed that I ran into the same problem trying to run a test against the LG v20 from A.S.

malachid avatar Dec 13 '17 00:12 malachid

I was able to get past the permission problem by using the GrantPermissionRule.

    @ClassRule
    public static GrantPermissionRule grantPermissionRule = GrantPermissionRule.grant(
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    );

malachid avatar Dec 13 '17 15:12 malachid

I'm unable to get past the permissions issue with the GrantPermissionRule. I couldn't use the @ClassRule as the compiler complained about it not being static 😞

@Rule
@JvmField
    public val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)

scottyab avatar Feb 19 '18 15:02 scottyab

@scottyab did you put that in a companion object?

malachid avatar Feb 19 '18 19:02 malachid

Thanks @malachid 👍 I'd forgotten the need for companion object. The below allows me to run Espresso tests from Android Studio.

companion object {
        @ClassRule
        @JvmField
        val grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    }

scottyab avatar Feb 20 '18 09:02 scottyab

After going in circles for the better part of a day, I've found that I need the following tools value in order for the GrantPermissionRule to work (in addition to the @ClassRule annotation)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>

cbuche avatar Jun 28 '18 00:06 cbuche

After going in circles despite above suggestions, using ClassRule in companion object worked, but this gave me a hint that it's an rules ordering issue so also this approach below works using just RuleChain and regular @Rule annotation:

    var permissionRule: GrantPermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    var activityTestRule = ActivityTestRule(ExampleActivity::class.java, false, false)
    val okReplayRule: TestRule = OkReplayRuleChain(configuration, activityTestRule).get()

    @Rule
    @JvmField
    val ruleChain: RuleChain = RuleChain.outerRule(activityTestRule).around(permissionRule).around(okReplayRule)

ddudek avatar Oct 11 '21 13:10 ddudek