5

I have a WebView in the layout xml of my MainActivity, to which I setWebViewClient(new WebViewClient()), followed by loadUrl(...) in onCreate.

Most of the time the app runs fine and the Web content is displayed correctly.

But in some cases, opening the app causes a crash. I've noticed that it happens when the app scheduled a PendingIntent broadcast with AlarmManager, which triggers a Notification whose contentIntent is a PendingIntent.getActivity set to launch MainActivity. But it happens only in the case when the user has removed the app from the stack of active apps in the meantime (Notification is visible, not yet clicked, and stack if apps cleared. So, app process probably stopped?).

Seemingly no other system modifications in between (in particular no app/system update, no playing around with user profiles or Chrome app.)

Stack trace:

java.lang.RuntimeException: 
  at android.webkit.WebViewDelegate.getPackageId (WebViewDelegate.java:164)
  at yj.a (PG:16)
  at xH.run (PG:14)
  at java.lang.Thread.run (Thread.java:764)

Occurs with Android 7.0 thru 9. Also, seems to have started to occur when I upgraded target SDK to 28.

I don't use explicitly a WebViewDelegate. It must be internal system code (hence the obfuscation).

By reading the source code of AOSP, it seems that the WebView fails to retrieve the package to which it belongs -- but why sometimes only!?

Any help appreciated! Thanks.

Gabriel
  • 1,401
  • 13
  • 21
  • Did you find an answer to this? I've been investigating this issue for days now, and can't find the cause or how to fix it. My app runs perfectly on Android 27 and below, but 28 or 29 crashes with this error. – Darren Taft Oct 11 '19 at 23:00
  • Hi @Darren, no, I didn't find an answer. I filed a bug with the Android team here: https://issuetracker.google.com/issues/138327342 , and if you have any additional details and/or a piece of code for them to reproduce, it could help a lot! Thanks – Gabriel Oct 13 '19 at 09:08
  • 1
    Follow-up: I have found that the problem only shows up when I forcibly modify the application's context's config (locale). I needed to do that because some activities must show in a particular language (different from the system's). But after acting on each activity's context instead of the one of the Application instance, the crash disappeared... – Gabriel Feb 27 '20 at 13:09

2 Answers2

1

It has taken weeks of investigation on and off, but I've finally found why I'm seeing this issue. For me, it was just because I'd overridden the getResources() method in my application scope to use the current activity. Something like this:

public class MyApplication extends MultiDexApplication {
    private static MyApplication sInstance = null;
    private WeakReference<Activity> mCurrentActivity;

    public static MyApplication getInstance() {
        return sInstance;
    }

    public void setCurrentActivity(Activity activity) {
        mCurrentActivity = new WeakReference<>(activity);
    }

    public Activity getCurrentActivity() {
        return mCurrentActivity == null ? null : mCurrentActivity.get();
    }

    @Override
    public Resources getResources() {
        // This is a very BAD thing to do
        Activity activity = getCurrentActivity();
        if (activity != null) {
            return activity.getResources();
        }
        return super.getResources();
    }
}

This was done as a shortcut as I often wanted to get strings that were activity-specific, so I was calling MyApplication.getInstance().getResources().getString(). I now know this was a bad thing to do - removing my override of this method instantly fixed it.

So the key takeaway from this for me is that when the WebView is initialising, it MUST be able to get hold of the application context, so that the resources passed into WebViewDelegate.getPackageId() are at the application level - the activity context isn't enough, and causes this error.

As a side note - I wasn't even trying to add a WebView to my application. I was only actually using the following:

String userAgent = WebSettings.getDefaultUserAgent(this);

I was then passing this value into a custom media player that I'm using. Passing "this" as either application or activity scope always failed, due to my override.

Darren Taft
  • 198
  • 1
  • 8
  • Thanks @Darren for your insight. I'm glad you could track up the root cause for your particular case -- but this is different for me, as I don't override the application's getResources nor façade the access to the application's context. So, this might be yet a different issue. Thanks. – Gabriel Nov 26 '19 at 19:33
  • when messing with context via updateConfiguration it causes this issue – Raghav Satyadev Dec 04 '19 at 15:05
0

Looking through documentation,you can see that error is thrown when package can't be found.Check your syntax ,package name and try again.

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/webkit/WebViewDelegate.java (Line 164)

 /**
     * Returns the package id of the given {@code packageName}.
     */
    public int getPackageId(Resources resources, String packageName) {
        SparseArray<String> packageIdentifiers =
                resources.getAssets().getAssignedPackageIdentifiers();
        for (int i = 0; i < packageIdentifiers.size(); i++) {
            final String name = packageIdentifiers.valueAt(i);
            if (packageName.equals(name)) {
                return packageIdentifiers.keyAt(i);
            }
        }
        throw new RuntimeException("Package not found: " + packageName);
    }
n3dx
  • 109
  • 6
  • Thanks. But I don't think so. As I said, all goes perfectly well most of the time (99%). Only in some very specific conditions of creation of the activity, does the RuntimeException show up. So it proves that the package name is fine (and has been around unchanged for about 5 years in the Store). Additionally, this piece of code indicates that the Package that is being searched, is not *mine* but that of the WebViewDelegate class implementor itself. (android.webkit?) – Gabriel Jun 07 '19 at 15:42
  • Ok,no problem :) – n3dx Jun 07 '19 at 15:45
  • But bring more ideas please! :) – Gabriel Jun 07 '19 at 15:46
  • Did you handle android lifecycle in your MainActivity properly? – n3dx Jun 07 '19 at 15:47