Status Update
Comments
he...@gmail.com <he...@gmail.com> #2
here is the profier picture.
ra...@google.com <ra...@google.com>
he...@gmail.com <he...@gmail.com> #3
help asap please..
jd...@google.com <jd...@google.com> #4
What device and version of android are you testing with?
What version of webview are you using?
Does this issue also occur with older versions of webview?
Are you able to share which app this occurs in or a sample test app to reproduce the issue?
jd...@google.com <jd...@google.com> #5
bu...@google.com <bu...@google.com> #6
he...@gmail.com <he...@gmail.com> #7
just try to call WebSettings.getDefaultUserAgent
anywhere in your app, it takes 400ms the first time,
I have read the source code, this method will try to init webview zygote process the first time, after init. it will just take several milliseconds.
but I need call this method when I open my app immediatly.
My android system webview version is: 114.0.5735.130
an...@gmail.com <an...@gmail.com> #8
bu...@google.com <bu...@google.com>
nt...@google.com <nt...@google.com> #9
This behavior is working as intended. WebSettings.getDefaultUserAgent()
is intentionally designed to load WebView code (as you found, this happens through WebViewFactory.getProvider()
). It's necessary to load the code because this is how we determine what the full user agent string actually is. To be clear: this slow performance will only happen the first time you call WebSettings.getDefaultUserAgent()
; if you call this API a second time then the second time will execute much faster.
I understand this may seem very slow for your use case, but this is not something which we can optimize much further. WebView has a large amount of code because it contains a full copy of the chromium browsing engine, so this requires a lot of time to load and initialize.
However there are some alternative options you could consider, depending on your exact requirements:
- If you only need to know the WebView version, then we have a dedicated API for this which is much faster. Please review
https://developer.android.com/develop/ui/views/layout/webapps/managing-webview#version for how you can do this. - If you need the user agent string but you don't need it immediately, then you can spawn a background thread early during your app startup which calls
WebSettings.getDefaultUserAgent()
. This will force WebView to load its code. Then you can just callWebSettings.getDefaultUserAgent()
a second time whenever you need the user agent string. Like I said above, the second time you call this will be much faster. However you should be aware that callingWebSettings.getDefaultUserAgent()
will still do some (not all) of its work on the UI thread, so you may want to initialize your app's UI before doing this. - Unfortunately if you really do need the full user agent string immediately during startup, then you will need to live with the slow performance.
I hope this helps!
ma...@gmail.com <ma...@gmail.com> #10
nt...@google.com <nt...@google.com> #11
Please see my comment above. I gave suggestions for two different workarounds which will not cause ANRs.
Description
I need to get User-agent by calling
WebSettings.getDefaultUserAgent
when I open my app, but it takes 400ms on my device. I check the source code, WebViewFactory.getProvider().getStatics() , and try to relfect to "preload" it, but I failed as the method is marked with @UnsupportedAppUsage.can you help me to get ueser-agent quickly? 400ms is too long