142

In Lollipop (API 22) every time in my application I show a webview the application crashes. I have multiple crashes in my android developer console related to this event.

No need to say that it works on Android 4, 6 and 7.

Reading the stack trace (posted at the end of this post), something bugs me

Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003

I searched in the generated R.java without any luck, obviously because the ID does not exists, but it was worth a try.

Googling around the problem seems to be related to how lollipop handles the webview. I started a fresh AVD with lollipop based on a device I found on the crash reporter in GDC, and I can reproduce the problem.


Full stack trace:

android.view.InflateException: Binary XML file line #7: Error inflating class android.webkit.WebView
                  at android.view.LayoutInflater.createView(LayoutInflater.java:633)
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                  at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67)
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5254)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
               Caused by: java.lang.reflect.InvocationTargetException
                  at java.lang.reflect.Constructor.newInstance(Native Method)
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:607)
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                  at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67) 
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) 
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682) 
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
               Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2040003
                  at android.content.res.Resources.getText(Resources.java:299)
                  at android.content.res.Resources.getString(Resources.java:385)
                  at com.android.org.chromium.content.browser.ContentViewCore.setContainerView(ContentViewCore.java:684)
                  at com.android.org.chromium.content.browser.ContentViewCore.initialize(ContentViewCore.java:608)
                  at com.android.org.chromium.android_webview.AwContents.createAndInitializeContentViewCore(AwContents.java:631)
                  at com.android.org.chromium.android_webview.AwContents.setNewAwContents(AwContents.java:780)
                  at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:619)
                  at com.android.org.chromium.android_webview.AwContents.<init>(AwContents.java:556)
                  at com.android.webview.chromium.WebViewChromium.initForReal(WebViewChromium.java:311)
                  at com.android.webview.chromium.WebViewChromium.access$100(WebViewChromium.java:96)
                  at com.android.webview.chromium.WebViewChromium$1.run(WebViewChromium.java:263)
                  at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.drainQueue(WebViewChromium.java:123)
                  at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue$1.run(WebViewChromium.java:110)
                  at com.android.org.chromium.base.ThreadUtils.runOnUiThread(ThreadUtils.java:144)
                  at com.android.webview.chromium.WebViewChromium$WebViewChromiumRunQueue.addTask(WebViewChromium.java:107)
                  at com.android.webview.chromium.WebViewChromium.init(WebViewChromium.java:260)
                  at android.webkit.WebView.<init>(WebView.java:554)
                  at android.webkit.WebView.<init>(WebView.java:489)
                  at android.webkit.WebView.<init>(WebView.java:472)
                  at android.webkit.WebView.<init>(WebView.java:459)
                  at java.lang.reflect.Constructor.newInstance(Native Method) 
                  at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
                  at android.view.LayoutInflater.createView(LayoutInflater.java:607) 
                  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55) 
                  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:682) 
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741) 
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
                  at it.artecoop.ibreviary.WebViewFragment.onCreateView(WebViewFragment.java:67) 
                  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) 
                  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) 
                  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
                  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682) 
                  at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:541) 
                  at android.os.Handler.handleCallback(Handler.java:739) 
                  at android.os.Handler.dispatchMessage(Handler.java:95) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5254) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Dharman
  • 21,838
  • 18
  • 57
  • 107
Valerio
  • 2,472
  • 3
  • 20
  • 31
  • This looks like a system `WebView` problem. I don't know if/when the emulators get updates to that. Have you tried on hardware? – CommonsWare Dec 07 '16 at 19:17
  • @commonsware no I don't have any device right now with lollipop. But the crash reporter is showing multiple entries for this. – Valerio Dec 07 '16 at 19:48
  • 21
    This question was from 2016 but this issue is surfacing again in late 2019. If you arrived here from a web search, make sure to pick answers that applies to you. – user1032613 Sep 30 '19 at 01:45
  • I don't have physical device of API level 21. I tried running app on Genymotion and Android emulators with API level 21 but it crashes! Anyone here let me know whether it will work on Physical Device! Now my app is at risk! – Sumit Shukla Oct 01 '19 at 13:52
  • For me, Google Play reported the problem on a Huawei P8 Lite running Android 5.0 but it didn't surface on a real Samsung Note 4 running Android 6.0.1 or a Samsung Note 8 tablet running Android 4.4.2. – FractalBob Dec 26 '19 at 13:38
  • Recently I get the crash from Android 10, mostly from Samsung user. Anyone have the same trouble ? – neobie Mar 10 '20 at 03:59

20 Answers20

188

If you use androidx.appcompat:appcompat:1.1.0, try androidx.appcompat:appcompat:1.0.2 instead. it seems that 1.1.0 doesn't fix the bug with WebView in Android 5.1.1.

Feb-2020 update: Reverting to 1.0.2 stopped working for many people (including my app), but using the current version of androidx.appcompat:appcompat:1.2.0-alpha02 did fix the crash. (I was seeing it on a Huawei P8 Lite running Android 5.0 during Google's automated "Pre-launch report" testing).

Jun-2020 update: There are newer releases available than the one mentioned in the Feb-2020 update, you can see the currently available versions here:

Manuel
  • 11,849
  • 3
  • 39
  • 93
戴文锦
  • 1,935
  • 1
  • 5
  • 5
  • 3
    Is there a bug fix for keeping 1.1.0 and fixing this? We are reliant on some things not present in 1.0.2 and it seems like some alpha or beta version of 1.1.0 don't have this bug. – Roudi Sep 17 '19 at 23:39
  • Also am not sure that's the problem, I have a branch with beta01 that works fine with 5.1 and my branch with 1.1.0 doesn't, if i revert my 1.1.0 to beta01 it still doesn't work, other than that the gradle files are the same but i'm not sure what is the main difference since its quite a big branch – Roudi Sep 17 '19 at 23:48
  • @PampaZiya I tried to use beta01 version. It's OK. About what you said “if i revert my 1.1.0 to beta01 it still doesn't work” , try clean? What we can do is wait patiently for Google to release a new version. – 戴文锦 Sep 18 '19 at 03:30
  • 3
    Yes a clean fixed it thank you ! I am using beta01 and everything seems fine. I'll keep using it cause there's no obvious benefit of using 1.1.0 anyway. – Roudi Sep 18 '19 at 06:15
  • 11
    There's an issue related with this regresion: https://issuetracker.google.com/issues/141132133 – Brais Gabin Sep 18 '19 at 08:19
  • Thanks, this also seems to fix the `getDefaultVideoPoster` crash. – casolorz Oct 20 '19 at 15:02
  • Having just finished migrating to androidx and facing this issue for API 22 only, it does work. – the_dude_abides Oct 25 '19 at 18:45
  • androidx.appcompat:appcompat:1.1.0-beta01 works for me too – CodeSmith Nov 01 '19 at 16:47
  • yeah works well in android Lollipop without using https://stackoverflow.com/a/56168774/1089841 workaround which causing other issues and also https://stackoverflow.com/a/58684213/1089841 one works but some condition don't know what are the issue downgrading to beta01 – Girish Nov 05 '19 at 13:30
  • 3
    As today, the downgrade it's not working, in my case, this solution from https://issuetracker.google.com/issues/141132133 works: "Maybe the most simple way to solve this issue is to force Gradle to set the version to 1.1.0-rc01. Add this to your gradle file. configurations.all { resolutionStrategy { force 'androidx.appcompat:appcompat:1.1.0-rc01' } }" – Talu Dec 12 '19 at 00:16
  • 1
    @戴文锦 @Talu considering 2 options: using `androidx.appcompat:appcompat:1.2.0-alpha02` or `androidx.appcompat:appcompat:1.1.0-rc01`, which one is more stable and less risky? personally I think `rc` versions are more stable comparing to `alpha` versions, and so I decided to use `androidx.appcompat:appcompat:1.1.0-rc01`. – Soheil Feb 26 '20 at 12:21
  • @Soheil have you tried overriding the `getAssets()` in the Webview's Activity to return `getResources().getAssets()` while still using `androidx.appcompat:appcompat:1.1.0`? – Prawira Feb 27 '20 at 18:05
  • 3
    `androidx.appcompat:appcompat:1.2.0-alpha03` works too – Artyom Mar 27 '20 at 07:46
  • 3
    upgrading to implementation 'androidx.appcompat:appcompat:1.2.0-beta01 solved the issue, is there any side effects of it on the above versions(6.0 and above)? – Girish Apr 07 '20 at 14:21
  • after updating androidx.appcompat:appcompat the issue was gone for me for sometime but today the issue reappeared after updating the dependency to androidx.appcompat:appcompat:1.3.0-alpha02 – Ibtisam Asif Nov 18 '20 at 06:52
51

If you are using androidx.appcompat:appcompat:1.1.0 and don't want to downgrade to androidx.appcompat:appcompat:1.0.2 or upgrading to androidx.appcompat:appcompat:1.2.0-alpha03, there's another solution which is described in this comment on the Google Issue Tracker.

I noticed that after calling applyOverrideConfiguration, Context.getAssets() and Context.getResources().getAssets() are not returning the same AssetManager object. The AssetManager returned from Context.getAssets() can't access resources in other packages (including the system WebView package), causing WebView to crash. If I override Context.getAssets() to return getResources().getAssets(), the problem is gone.

Based on that comment, you can override the getAssets() in the WebView's Activity so that it returns getResources().getAssets() instead to solve the issue.

Java

@Override
public AssetManager getAssets() {
    return getResources().getAssets();
}

Kotlin

override fun getAssets(): AssetManager {
    return resources.assets
}
Prawira
  • 511
  • 3
  • 6
  • 3
    Great answer +1 – Shirane85 Feb 12 '20 at 13:29
  • Hi, if this was true, how would this be fixed with a release of appCompact which is unrelated to the AssetManager and the webview itself? – Sinapse Jul 17 '20 at 04:14
  • Looking at [this changelist](https://android-review.googlesource.com/c/platform/frameworks/support/+/1177665) it seems that some webview versions are having issues with the `applyOverrideConfiguration` calls inside the AppCompat library. – Prawira Jul 17 '20 at 04:56
46

Warning: this workaround might also break some things; see the comments for details

If you'd like to inflate the WebView from an XML layout, you can wrap it in a nice little subclass (based on ikostet's answer):

public class LollipopFixedWebView extends WebView {
    public LollipopFixedWebView(Context context) {
        super(getFixedContext(context));
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, boolean privateBrowsing) {
        super(getFixedContext(context), attrs, defStyleAttr, privateBrowsing);
    }

    public static Context getFixedContext(Context context) {
        return context.createConfigurationContext(new Configuration());
    }
}

EDIT: now even nicer with Kotlin

class LollipopFixedWebView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : WebView(context.createConfigurationContext(Configuration()), attrs, defStyleAttr, defStyleRes)
SpaceBison
  • 2,094
  • 1
  • 16
  • 31
  • 1
    I found the same problem and used this solution to fix it, thank you – itsa04g9 Mar 28 '19 at 04:17
  • Updating the Android WebView on the device also fixes the issue – display name Aug 20 '19 at 19:12
  • @displayname Might not be an option for those running it on the AVD – TheRealChx101 Sep 14 '19 at 17:01
  • 2
    Still a problem in 2019 and it works, but I don't have idea why/how, it would be good if this is explained. Also I get the warning Is never used on the 4th constructor "public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {" – David Sep 17 '19 at 12:29
  • 4
    Just a word of caution about this workaround: instantiating a WebView with a blank Context can lead to unintended side effects. For example, ActionModes from the WebView will probably no longer work (i.e. long-pressing to highlight text). – Dmitry Brant Sep 27 '19 at 14:25
  • @displayname Are you sure it is working fine on pysical device!Because I don't have physical device of API level 21! – Sumit Shukla Oct 01 '19 at 13:46
  • Yes. I am. I don't have a device with with OS 5/ API21. I setup an VD with google play and it worked fine on OS 5 and OS 6. – display name Oct 01 '19 at 17:51
  • 6
    As @DmitryBrant said be very careful when implementing this because this breaks a few things, including **drop down fields** and **long pressing to highlight text**! – user5507535 Oct 03 '19 at 08:49
  • This workaround breaks ` – G00fY Oct 12 '19 at 15:42
  • FYI on at least one Samsung 4.1.2 device this can lead to a `NoSuchMethodError` for `android.content.Context.createConfigurationContext` – casolorz Oct 21 '19 at 15:47
  • Just realized that is because it was added on API 17. – casolorz Oct 21 '19 at 17:13
  • @DmitryBrant Correct now Dropdown crashing in webview , Is there any fix for this https://stackoverflow.com/questions/58710401/android-webview-drop-down-crashing-only-on-android-lollipop5-0-5-1 – Girish Nov 05 '19 at 12:46
  • This cause that keyboard is not snown when clicked into an input. – hrach Dec 13 '19 at 10:15
37

my advice is to use custom/new Configuration only when "original one" is causing problems, so on Lollipop only. @SpaceBizon code work well till Android 8.x, on 9 and Q (currenly beta) every select/dropdown press won't show AlertDialog picker, instead of that memory leak occurs... below fixed getFixedContext method with "iffed" proper version code

public class LollipopFixedWebView extends WebView {

    public LollipopFixedWebView(Context context) {
        super(getFixedContext(context));
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs) {
        super(getFixedContext(context), attrs);
    }

    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(getFixedContext(context), attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public LollipopFixedWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(getFixedContext(context), attrs, defStyleAttr, defStyleRes);
    }

    private static Context getFixedContext(Context context) {
        if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) // Android Lollipop 5.0 & 5.1
            return context.createConfigurationContext(new Configuration());
        return context;
    }
}
snachmsm
  • 10,975
  • 1
  • 22
  • 52
22

If you are using androidx.appcompat:appcompat:1.1.0, either change to androidx.appcompat:appcompat:1.0.2 or if you want to use DayNight Theme, override applyOverrideConfiguration in your activity as follows. (Note: This requires app restart while switching from Dark Theme to Light Theme and vice versa).

override fun applyOverrideConfiguration(overrideConfiguration: Configuration?) {
        if (Build.VERSION.SDK_INT in 21..25 && (resources.configuration.uiMode ==  applicationContext.resources.configuration.uiMode)) {
                return
        }
        super.applyOverrideConfiguration(overrideConfiguration)
}
  • This workaround works, but shouldn't it be for `21..22`, instead of `21..25`? The problem doesn't seem to occur in API 23+. – Dmitry Brant Sep 27 '19 at 14:26
  • 5
    This is the revision that caused this crash - https://android.googlesource.com/platform/frameworks/support/+/26079d87c79a64829f036236353fac1dae4e0613%5E%21/#F2 . So, according to the code, the crash will occur in API 21..25 in the devices in which Google Play Store is not installed and Android Webview Version is <50. – A R Mythili Saran Sep 29 '19 at 16:56
  • Downgrading to androidx.appcompat:appcompat:1.0.2 do the trick, thanks – Talu Sep 30 '19 at 18:14
  • 3
    My workaround in java that seems to work for me is like this (sorry for the horrible formatting but it's not a new answer just a port): ```@Override public void applyOverrideConfiguration(Configuration overrideConfiguration) { if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT <= 25) { return; } super.applyOverrideConfiguration(overrideConfiguration); }``` – Mike Hardy Oct 18 '19 at 04:38
  • @ARMythiliSaran why do you say that this only occurs in "the devices in which Google Play Store is not installed and Android Webview Version is <50" ? – andrei Dec 17 '19 at 09:10
  • @andrei I have verified on physical devices(Android 5.1) I own. On devices with google play store, if i update webview app, the crash wouldn't occur. On the other devices there is no way to update the webview unless you install latest webview apk yourself. The lowest webview apk i could get my hands on was version 54 and it didn't crash in that version either. So, I decided to go into production with the above code applied if webview<50. Haven't received any crash reports related to this till date. Seems like it was fixed recently - https://issuetracker.google.com/issues/141351441. – A R Mythili Saran Dec 17 '19 at 09:29
  • What is `AppConstants.appContext.resources.configuration.uiMode` ? – Mr-IDE Mar 26 '20 at 11:24
  • @Mr-IDE Sorry for not keeping it clear. AppConstants.appContext is the application context. Edited the answer accordingly. – A R Mythili Saran Mar 30 '20 at 02:00
20

If you are not relying on DayNight theme switching (or other UiMode events), you can add android:configChanges="uiMode" to the webview activity manifest to prevent AppCompatDelegate updating the Resources configuration and thereby messing up the webview inflation.

S. Gissel
  • 1,027
  • 1
  • 9
  • 23
17

Try use for creating webview:

mWebView = new WebView(getActivity().createConfigurationContext(new Configuration()));
ikostet
  • 171
  • 3
10

And one more try to fix the problem. Should override this method in your activity:

    @Override
    public void applyOverrideConfiguration(final Configuration overrideConfiguration) {
        if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 25) {
            overrideConfiguration.uiMode &= ~Configuration.UI_MODE_NIGHT_MASK;
        }
        super.applyOverrideConfiguration(overrideConfiguration);
    }
HotIceCream
  • 2,193
  • 2
  • 17
  • 27
6

I was able to reproduce the crash on API 21 in the emulator.

So I tried to add this to the implementation, as described in the docs:

dependencies {
    def appcompat_version = "1.1.0"

    implementation "androidx.appcompat:appcompat:$appcompat_version"
    // For loading and tinting drawables on older versions of the platform
    implementation "androidx.appcompat:appcompat-resources:$appcompat_version"
}

Adding appcompat-resources did not fix the issue.

Maybe a future version will, but I wanted to mention that there seems to be a complementary resource library that is supposed to address this issue. So when trying to fix this with a new version of appcompat, add the appcompat-resources library with the same version.

Reverting to androidx.appcompat:appcompat:1.0.2 did fix the issue as a workaround.

Manuel
  • 11,849
  • 3
  • 39
  • 93
6

The below code will fix the problem. Please add it in your Activity:

@Override 
public AssetManager getAssets() {
    return getResources().getAssets(); 
}
CoolMind
  • 16,738
  • 10
  • 131
  • 165
avinasha
  • 61
  • 2
  • 1
4

I used the latest version from this source. Please check and use your own based on what you think is best.

In case you want to skip, below implementation solves the issue:

implementation 'androidx.appcompat:appcompat:1.2.0'
F.Mysir
  • 581
  • 7
  • 17
3

Just as a side note, I struggled for a while even though I downgraded to 1.0.2, just because I seemed to have a transitive dependency to appcompat:1.1.0 via androidx.constraintlayout:constraintlayout:2.0.0-beta3. Once I downgraded that to constraintlayout:1.1.3 everything worked fine

(According to https://issuetracker.google.com/issues/141351441 issue will be fixed soon with appcompat:1.2.0-alpha02)

2

戴文锦 was right for me too. But downgrading just androidx.appcompat:appcompat:1.1.0 to 1.0.2 did not succeed.

I downgraded all my previously upgraded androidx versions from

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha10'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta04'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta04'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.core:core:1.2.0-alpha04'

back to

implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.1.0-alpha09'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta03'
implementation 'androidx.viewpager2:viewpager2:1.0.0-beta03'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.vectordrawable:vectordrawable:1.1.0-rc01'
implementation 'androidx.preference:preference:1.1.0-rc01'
implementation 'androidx.core:core:1.2.0-alpha03'

A reminder that Googles code is by far not bug free and well tested and that someone should never upgrade the versions flippantly.

S. Gissel
  • 1,027
  • 1
  • 9
  • 23
  • Did you downgraded androidx.appcompat:appcompat to 1.1.0-rc01? Not to 1.0.2? – Zhebzhik Babich Sep 27 '19 at 08:25
  • Tryed it with 1.0.2 but no success. Must be one of the other dependencies in combination with appcompat. – S. Gissel Sep 27 '19 at 09:44
  • Simply downgrading `androidx.appcompat:appcompat` worked for me. To future readers: make sure to "gradle sync" and confirm it's not sufficient for you, before downgrading more stuff. – user1032613 Sep 30 '19 at 01:48
  • After downgrading it will not be superfluous to check the versions of dependencies in the project https://stackoverflow.com/a/39020703/6055194 – Zhebzhik Babich Oct 10 '19 at 05:59
2
/**
 * Customized WebView to avoid crashing on Android 5 and 6 (API level 21 to 23)
 * If the Android System WebView is old and is not updated, the WebView view inflation fails.
 * To reproduce the issue, try on OS 5 or 6 with Android System WebView 72.0.3626.76 (this version is just a reference point from being which we saw no crashes)
 */
class BrilliantWebView : WebView {

   companion object {
       private fun getBrilliantContext(context: Context?) =
            if (!OSUtils.hasNougat()) // OS < 24 or OS < 7.0
                context?.createConfigurationContext(Configuration())
            else
                context
    }

   constructor(context: Context?) : super(getBrilliantContext(context))
   constructor(context: Context?, attrs: AttributeSet?) : super(getBrilliantContext(context), attrs)
   constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(getBrilliantContext(context), attrs, defStyleAttr)
}
display name
  • 799
  • 9
  • 18
  • @sumit-shukla posting the code snippet that helped us resolve the issue. But make sure you do this first https://stackoverflow.com/a/57715955/2344192 – display name Oct 01 '19 at 18:00
  • 2
    Be careful, this workaround breaks drop down fields and selecting text in the WebView. – user5507535 Oct 03 '19 at 08:58
2

AppCompat 1.2.0-alpha02 was released today, so you can use

implementation 'androidx.appcompat:appcompat:1.2.0-alpha02'

which will fix WebView issues on Lollipop.

  • it fixes the issue but since it is alpha version it may cause other bugs. if you have tried it, haven't you faced any other issue in your app? – Soheil Feb 26 '20 at 12:27
  • @Soheil No, I haven't experienced any glitches. Alpha versions are always a bit risky, of course. For example, a bug for alpha02 is mentioned here: https://issuetracker.google.com/issues/141351441#comment48 – Dimitris Pattas Feb 27 '20 at 13:27
2

I was able to reproduce this issue on a Nexus 7 API 22. The issue is fixed by updating the tablet's version of Android System WebView from version 39 (2237560-arm) to version 79.0.3945.136

This isn't in my control for a user to do this, but it is a solution if you need to help someone use your app immediately.

TGruenwald
  • 61
  • 6
1

The issue was solve for my by downgrading to implementation 'androidx.appcompat:appcompat:1.0.2'

Ash
  • 81
  • 7
1

In my case a problem was in method

fun getUserId(): Int = userId

of a MainActivity. I don't know why it affects WebView. getUserId() is neither overriden, nor public method of the Activity class. If I rename or remove this method, WebView starts to open. Also trying to change

private var userId: Int = 0

to

var userId: Int = 0
    private set

leads to the same exception. I understood that when had seen an exception: Caused by: java.lang.SecurityException: Permission Denial: null asks to run as user 123456 but is calling from user 0; this requires android.permission.INTERACT_ACROSS_USERS_FULL or android.permission.INTERACT_ACROSS_USERS. In that case user=123456 was in fact my authorized user, that was used in some requests, not in WebView. I suspect Android uses userId as a local user_id for its processes.

As many other people noticed we could use appcompat:1.1.0 or 1.0.2 and extend WebView class. In emulator 21 without Google Play Services this WebView will crash on long touch over text labels, but on typical devices everything is allright.

CoolMind
  • 16,738
  • 10
  • 131
  • 165
0

Instead of changing version of any dependency, I implemented programmatically generation of webView as a solution.

The problem actually looks like starting with reading layout from xml file. So if programmatic way is applicable solution for you please check bellow sample.

private WebView generateWebView(){
    WebView wv = new WebView(YourContext);
    wv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    wv.setFitsSystemWindows(false); // your preferences
    wv.setVerticalScrollBarEnabled(false); // your preferences
    wv.setPadding(15,15,15,15); // your preferences
    return wv;
}

Than just add into your parent view:

LinearLayout scrollContainer = findViewById(R.id.scrollContainer);
scrollContainer.addView(generateWebView());
-4

Check version. Androidx appcompat and Google material version keep same always.

implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation "androidx.cardview:cardview:1.0.0"

enter image description here