莱芜网站建设优化国内一家做国外酒店团购的网站
news/
2025/9/23 15:27:47/
文章来源:
莱芜网站建设优化,国内一家做国外酒店团购的网站,网站怎么建设?,做网站百度百科关于作者#xff1a;CSDN内容合伙人、技术专家#xff0c; 从零开始做日活千万级APP。 专注于分享各领域原创系列文章 #xff0c;擅长java后端、移动开发、商业变现、人工智能等#xff0c;希望大家多多支持。 未经允许不得转载 目录 一、导读二、概览三、问题过程源码追踪… 关于作者CSDN内容合伙人、技术专家 从零开始做日活千万级APP。 专注于分享各领域原创系列文章 擅长java后端、移动开发、商业变现、人工智能等希望大家多多支持。 未经允许不得转载 目录 一、导读二、概览三、问题过程源码追踪 四、 推荐阅读 一、导读
我们继续总结学习遇到的问题温故知新。
今天遇到一个线上问题启动就闪退比较坑在此做一个记录防止掉坑。
本文记录一次bug解决的过程
Using WebView from more than one process
二、概览
今天将 targetSdkVersion 的版升级到了29出现了一些奇怪的报错日志如下
Fatal Exception: java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported.
https://crbug.com/558377 : Current process com.xx.xxapp(pid 13862), lock owner com.xx.xx.xxAPP (pid 13559)at org.chromium.android_webview.AwDataDirLock.b(AwDataDirLock.java:27)at as0.i(as0.java:30)at Zr0.run(Zr0.java:2)at android.os.Handler.handleCallback(Handler.java:883)at android.os.Handler.dispatchMessage(Handler.java:100)at android.os.Looper.loop(Looper.java:224)at android.app.ActivityThread.main(ActivityThread.java:7520)at java.lang.reflect.Method.invoke(Method.java)at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
三、问题过程
我们查看文档发现 google 文档 在android 9.0系统上如果多个进程使用WebView需要使用官方提供的api在子进程中给webview的数据文件夹设置后缀
如果不设置则会报错不过这个影响范围有限影响范围 Android 9及以上 且targetSdkVersion 28 Starting Android Pie (API 28), Google isnt allowing using a single WebView instance in 2 different processes.WebView.setDataDirectorySuffix(suffix);官方提供方案
protected void attachBaseContext(Context base) {mApplicationContext base;webViewSetPath(this);} public void webViewSetPath(Context context) {if (Build.VERSION.SDK_INT Build.VERSION_CODES.P) {String processName SpecialUtils.getCurProcessName(context);// 根据进程名称设置多个目录if(!CommonConstant.NEW_PACKAGE_NAME.equals(processName)){WebView.setDataDirectorySuffix(getString(processName,这里隐藏名字自己设置个目录));}}
}public String getString(String processName, String defValue) {return TextUtils.isEmpty(processName) ? defValue : processName;
}通过使用官方提供的方法后实际在项目中运用 application中设置多个存储目录虽然能减少问题发生的次数但从bugly后台依然能收到此问题的大量崩溃信
源码追踪
那么这个问题发生的原因究竟是什么一起来分析下抛出这个异常的逻辑吧 https://chromium.googlesource.com/chromium/src//refs/heads/main/android_webview/java/src/org/chromium/android_webview/AwDataDirLock.java#126
从源码分析调用链最终调用到了AwDataDirLock类中的lock方法
abstract class AwDataDirLock {static void lock(final Context appContext) {try (ScopedSysTraceEvent e1 ScopedSysTraceEvent.scoped(AwDataDirLock.lock);StrictModeContext ignored StrictModeContext.allowDiskWrites()) {if (sExclusiveFileLock ! null) {我们已经调用了lock并在此过程中成功获取了锁return;}如果我们已经调用了lock但没有成功获得锁则可能应用程序捕获到异常进行自动重启。if (sLockFile null) {String dataPath PathUtils.getDataDirectory();File lockFile new File(dataPath, EXCLUSIVE_LOCK_FILE);try {// Note that the file is kept open intentionally.sLockFile new RandomAccessFile(lockFile, rw);} catch (IOException e) {throw new RuntimeException(Failed to create lock file lockFile, e);}}对webview数据目录中的webview_data.lock文件在for循环中尝试加锁16次for (int attempts 1; attempts LOCK_RETRIES; attempts) {try {sExclusiveFileLock sLockFile.getChannel().tryLock();} catch (IOException e) {}如果加锁成功会将该进程id和进程名写入到文件if (sExclusiveFileLock ! null) {writeCurrentProcessInfo(sLockFile);return;}if (attempts LOCK_RETRIES) break;try {Thread.sleep(LOCK_SLEEP_MS);} catch (InterruptedException e) {}}如果加锁失败则会抛出异常// Using WebView from more than one process String error getLockFailureReason(sLockFile);boolean dieOnFailure Build.VERSION.SDK_INT Build.VERSION_CODES.P appContext.getApplicationInfo().targetSdkVersion Build.VERSION_CODES.P;if (dieOnFailure) {throw new RuntimeException(error);} else {}}}
}分析了原因我们来看看解决思路我们可以在应用启动时对该文件尝试加锁如果加锁失败就删除该文件并重新创建加锁成功就立即释放锁这样当系统尝试加锁时理论上是可以加锁成功的。 通过检查目标目录的文件锁如果能够获得到锁就表明无异常如果获取不到文件锁再次重新设置存储目录。 public class WebViewUtil {public static void handleWebViewDir(Context context) {if (Build.VERSION.SDK_INT Build.VERSION_CODES.P) {return;}try {String suffix ;String processName getCurProcessName(context);if (!TextUtils.equals(context.getPackageName(), processName)) {//判断不等于默认进程名称suffix TextUtils.isEmpty(processName) ? context.getPackageName() : processName;WebView.setDataDirectorySuffix(suffix);suffix _ suffix;}tryLockOrRecreateFile(context,suffix);} catch (Exception e) {e.printStackTrace();}}TargetApi(Build.VERSION_CODES.P)private static void tryLockOrRecreateFile(Context context, String suffix) {String sb context.getDataDir().getAbsolutePath() /app_webviewsuffix/webview_data.lock;File file new File(sb);if (file.exists()) {try {FileLock tryLock new RandomAccessFile(file, rw).getChannel().tryLock();if (tryLock ! null) {tryLock.close();} else {createFile(file, file.delete());}} catch (Exception e) {e.printStackTrace();boolean deleted false;if (file.exists()) {deleted file.delete();}createFile(file, deleted);}}}private static void createFile(File file, boolean deleted){try {if (deleted !file.exists()) {file.createNewFile();}} catch (Exception e) {e.printStackTrace();}}public static String getCurProcessName(Context context) {int pid android.os.Process.myPid();ActivityManager activityManager (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);ListActivityManager.RunningAppProcessInfo appProcesses activityManager.getRunningAppProcesses();if (appProcesses null) {return null;}for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {if (appProcess null) {continue;}if (appProcess.pid pid) {return appProcess.processName;}}return null;}}但是这样上线后发现还有问题原因是不同机型目录可能不一样 我们自己使用debug包查看webview数据目录发现系统默认添加了进程名后缀这是由于用户更新了手机系统导致 使用华为mate20X测试调用 WebView.selDataDirecloySufx 自定义后缀已不生效会默认强制指定后缀为进程名 另外还发现部分华为手机直接将webview目录名app webview改为了app hws webview。 综上所述我们需要针对不同手机系统遍历可能的文件路径最新解决代码如下:
javapublic static void handleWebViewDir(Context context) {if (Build.VERSION.SDK_INT Build.VERSION_CODES.P) {return;}String webViewDir /app_webview;String huaweiWebViewDir /app_hws_webview;String lockFile /webview_data.lock;try {xxx} catch (Exception e) {e.printStackTrace();}}TargetApi(Build.VERSION_CODES.P)private static void tryLockOrRecreateFile(String path) {File file new File(path);if (file.exists()) {try {FileLock tryLock (new RandomAccessFile(file, rw)).getChannel().tryLock();if (tryLock ! null) {tryLock.close();} else {createFile(file, file.delete());}} catch (Exception e) {boolean deleted false;if (file.exists()) {deleted file.delete();}createFile(file, deleted);}}}private static void createFile(File file, boolean deleted) {try {if (deleted !file.exists()) {boolean var2 file.createNewFile();}} catch (Exception e) {e.printStackTrace();}}然后在application的oncreate方法中调用 handleWebViewDir();
参考文章 文章 1 文章 2 文章 3
四、 推荐阅读
Java 专栏
SQL 专栏
数据结构与算法
Android学习专栏
未经允许不得转载
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/912986.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!