question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android is crashing when "WebView" is wrapped by "View"

See original GitHub issue

Bug description: I have an autosized webview that works perfectly on ios:

import React, { useState } from "react";
import { View } from "react-native";
import { WebView } from "react-native-webview";
import PropTypes from "prop-types";

const BODY_TAG_PATTERN = /<\/ *body>/;

const code = `
    <style>
        body, html {
            margin: 0;
            padding: 0;
        }
    </style>

    <script>
        window.ReactNativeWebView.postMessage(document.body.scrollHeight, '*');
    </script>
`;

const injectCode = (html) => {
    return html.replace(BODY_TAG_PATTERN, `${code}</body>`);
};

const WebViewAutoHeight = ({ html, style }) => {
    const [contentHeight, setContentHeight] = useState(1);

    const adjustHeight = (event) => {
        setContentHeight(parseInt(event.nativeEvent.data));
    };

    if (!html) {
        throw new Error("At this moment, WebViewAutoHeight only supports html files");
    }

    if (!BODY_TAG_PATTERN.test(html)) {
        throw new Error(`Cannot find </body> from: ${html}`);
    }

    return (
        <View style={[style, { height: contentHeight }]}>
            <WebView
                source={{ html: injectCode(html) }}
                onMessage={adjustHeight}
            />
        </View>
    );
};

WebViewAutoHeight.propTypes = {
    html: PropTypes.string.isRequired,
    style: PropTypes.any
};

WebViewAutoHeight.defaultProps = {
    style: null
};

export default WebViewAutoHeight;

However, when I try to use it on android, the app crashes without a message. Then I decided to remove everything and replaced the render method with the following:

<View>
   <WebView
        source={{ uri: 'https://infinite.red' }}
        style={{ marginTop: 20 }}
   />
</View>

but it was still crashing. Then I removed the outer view element and it worked!

Expected behavior: The android app should not crash

Environment:

  • OS: macOS Mojave
  • OS version: 10.14.5
  • react-native version: 0.59.8
  • react-native-webview version: 6.9.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:28

github_iconTop GitHub Comments

154reactions
jonsaichcommented, Jun 5, 2020

Just spent far too long working on a solution for this. I didn’t get any of the above solutions to work, however setting androidHardwareAccelerationDisabled={true} on the WebView did work.

92reactions
CodingBitsDevcommented, Jan 4, 2020

@Rendfold I managed to come across this link in my research: https://www.gitmemory.com/issue/react-native-community/react-native-webview/430/493976111

Apperently this bug can be fixed by changing the Opacity of the webviwe to 0.99 instead of the default value of 1. Setting the overflow value to “hidden” prevents that the background will be visible.

This worked for me as a fix Altho the androud crash did only appeare when I pressed the back button and by this dismissed the screen. I am also using react navigation which probably played a role as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webview causing react-native app to crash on navigation
The app crashes on multiple Android devices I tested on. When I go from the welcome screen to a screen containing nothing but...
Read more >
PSA: Update Android System WebView to fix crashing apps
PSA: If your Android apps keep crashing, update 'WebView' and Chrome [Permanent fix] · Open the Play Store app · Search for “Android...
Read more >
Here's how to fix those mysterious Android WebView crashes
Tap the three-dot menu at the top right of this screen, then select "Uninstall updates." This won't uninstall Android System WebView completely— ...
Read more >
How To Fix APPS Constantly Crashing But System Webview ...
How To Fix APPS Constantly Crashing But System Webview is Disabled Watch Here To FIX it Now!!#AppsCrashing #SystemWebviewfix ...
Read more >
Android apps crashing? Android System Webview is to ...
Click the app and on the next screen, you should see a three-dot menu at the top right. Click that menu and select...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found