Fix "permission denied for this window type" when adding React Native to an Android app

React Native

If you have followed the react-native Integration With Existing Apps tutorial with a target SDK version > 23, you may get the "permission denied for this window type" error when you open the react-native view. The problem is the new android permission system.

To get rid of this error, you must request the new permissions from the user.

In the previous Activity, you may have something like this to go to your react Activity.

@Override
public void onClick(View v) {
    Intent intent = new Intent(this, ReactDemoActivity.class);
    startActivity(intent);
}

Instead of going directly on your new Page, you must request for permission first:

    private static final int OVERLAY_PERMISSION_CODE =5463 ; // can be anything

    @Override
    public void onClick(View v) {

        if (Build.VERSION.SDK_INT >= 23) {
            if (!Settings.canDrawOverlays(this)) {

                // Open the permission page
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                        Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, OVERLAY_PERMISSION_CODE);
                return;
            }
        }

        // if not needed, go directly on the new Activity
        goToReactActivity();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {

        // If the permission has been checked
        if (requestCode == OVERLAY_PERMISSION_CODE) {
            if (Settings.canDrawOverlays(this)) {
                goToReactActivity();
            }
        }
    }

    private void goToReactActivity() {
        Intent intent = new Intent(this, ReactDemoActivity.class);
        startActivity(intent);
    }

To have a complete example of a react-native integration in an Android existing application, you can see this commit.

Other common issues

If your application still crashes on the react-native Activity on release, you may have forgotten to Bundle your javascript.

Last but not least, you may also have some problems with 32-bit architecture, like this:

java.lang.UnsatisfiedLinkError: dlopen failed: "/data/data/com.bamlab.reactIntegration/lib-main/libgnustl_shared.so" is 32-bit instead of 64-bit

In that case, add this to your build.gradle :

android {
    defaultConfig {
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
}

You will also need to edit your gradle.properties

android.useDeprecatedNdk=true