expo: Cannot get permissions using PermissionsAndroid on standalone app

I’m trying to request READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE on freshly detached Expo app using PermissionsAndroid API. The code was almost the same as Camera permission example code except changing PermissionsAndroid.PERMISSIONS.CAMERA to PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE. However I received following error:

Error: Tried to use permissions API but the host Activity doesn't implement PermissionAwareActivity.

I tried the same code on app created by react-cli and didn’t get any error. Seems like Expo SDK activity doesn’t extend ReactActivity nor PermissionAwareActivity. Is there any other method to manually request these permissions?

Additional info: “expo”: “^21.0.0”, “react”: “16.0.0-alpha.12”, “react-native”: “https://github.com/expo/react-native/archive/sdk-21.0.2.tar.gz”,

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (3 by maintainers)

Most upvoted comments

For those who need it, here is my edit to MainActivity.java to get things to work. There may be a better way to write this. I don’t know Java very well.

import android.os.Bundle;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.Nullable;

import com.facebook.react.ReactPackage;
import com.facebook.react.modules.core.PermissionAwareActivity;
import com.facebook.react.modules.core.PermissionListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.thehelperbees.helperhive.generated.ExponentBuildConstants;
import host.exp.expoview.ExponentActivity;

//public class MainActivity extends ExponentActivity {
// Original class def ----^
public class MainActivity extends ExponentActivity implements PermissionAwareActivity {

  @Nullable private PermissionListener mPermissionListener;

  @Override
  public String publishedUrl() {
    return "exp://exp.host/@timothykrell/helper-hive";
  }

  @Override
  public String developmentUrl() {
    return ExponentBuildConstants.DEVELOPMENT_URL;
  }

  @Override
  public List<String> sdkVersions() {
    return new ArrayList<>(Arrays.asList("21.0.0"));
  }

  @Override
  public List<ReactPackage> reactPackages() {
    return ((MainApplication) getApplication()).getPackages();
  }

  @Override
  public boolean isDebug() {
    return BuildConfig.DEBUG;
  }

  @Override
  public Bundle initialProps(Bundle expBundle) {
    // Add extra initialProps here
    return expBundle;
  }


  // Allow using PermissionsAndroid in react-native-background-location library
    // Didn't work with Expo. See https://github.com/expo/expo/issues/784
    // Copied solution from https://github.com/wix/react-native-navigation/pull/470/files
    @TargetApi(Build.VERSION_CODES.M)
    public void requestPermissions(String[] permissions, int requestCode, PermissionListener listener) {
        mPermissionListener = listener;
        requestPermissions(permissions, requestCode);
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (mPermissionListener != null && mPermissionListener.onRequestPermissionsResult(requestCode, permissions, grantResults)) {
            mPermissionListener = null;
        }
    }
}

Hey all,

In an effort to clean up and gain more clarity over our issues, we’re closing old issues that may no longer apply to our currently supported versions. If you are able to still repro this, please open a new issue with a repro case and we’ll be happy to investigate.

Cheers,

Adam

I am unable to use in expo version 29. Same issue. @AmolGslab Did you find Solution?

@TimothyKrell Getting same error “Tried to use permissions API but the host Activity doesn’t implement PermissionAwareActivity.” after updating MainActivity.java as provided by you. Expo V : 55 Android SDK 26

Thanks.

@tedkimzikto Agreed, I think it should be considered a bug for an ejected project with expokit to not have this already implemented in the MainActivity.

I too borrowed some parts from ReactActivity to fix this for my project, but it’s a shame, this is such a basic functionality compared to other complex things expokit already give us 🙂 .

This is still a problem with Expo SDK v31.0. @TimothyKrell thank you - your solution worked.

@mateusmedeiros I’ve managed to implement PemissionAwareActivity from MainActivity under android project. Refer to ReactActivity.java from RN (https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java)

I’m not sure why detached project is not handling these issues 😦

Chiming in just to say that I’m also having this problem with those exact same versions of everything, also right after ejecting a pure expo app and running on an Android 7.1.

@ide Tested on Lenovo Moto M Android 6.0