当前位置: 代码网 > it编程>App开发>Android > Android WebView升级详细操作指南

Android WebView升级详细操作指南

2024年09月07日 Android 我要评论
一、项目背景1.1需求项目需要使用webview加载合作方公司的url(里面功能比较多):使用了es6(ecmascript 6.0(以下简称 es6)是 javascript 语言的下一代标准,已经

一、项目背景

1.1 需求

项目需要使用webview加载合作方公司的url(里面功能比较多):

  • 使用了es6(ecmascript 6.0(以下简称 es6)是 javascript 语言的下一代标准,已经在 2015 年 6 月正式发布了(所以也被叫作es2015))

  • javascript 使用的函数,要求 chrome 最低版本是85。

一系列原因导致webview加载该链接报错,合作方那边因为某些原因,不想改变现有的代码实现等。需要我们自行解决,那就开整呗。

1.2 设备

  • android 8.1, webview 版本 60.xxxx.xxxx.xx(好吧,忘记版本了,页面打不开))

  • android 8.1, webview 版本 61.0.3163.98(页面打不开)

  • android 11,  webview 版本 83.0.4103.120(视频无法播放,厂商给的答复是不支持,具体原因没说)

查看版本号:

  • 可以进入设置-开发者模式-webview实现可以看到 webview 内核版本号。

  • adb 命令: adb shell pm dump com.android.webview|grep version

adb shell pm dump com.android.webview|grep version
    versioncode=410412053 minsdk=21 targetsdk=30
    versionname=83.0.4103.120
    signatures=packagesignatures{48d4530 version:3, signatures:[63b96fc5], past signatures:[]}

1.3 解决方案

1.腾讯x5内核,还是加载失败,以为是集成错误。特意使用微信打开该链接,也是加载失败。

2.更新安装包,因为内嵌webview的是com.android.webview。

  • 2.1 下载了包名为 95.0.4638.50-463805003_minapi21版本的包,进行覆盖安装->报错,提示签名文件不一致。

  • 2.2 下载com.android.google.weview_95.0.4638.74_apkpure,安装成功了,加载还是报错。而且在webview实现->不可选择com.android.google.webview。

3.修改framework-res.apk中配置,允许webview实现选择com.android.google.webview。这种操作不太合适2c,需要root权限,修改系统文件,一不留神就可能产生其他问题。

4.使用 webviewupgrade,这个还是比较强大的,对webview内核进行升级。已解决低版本无法加载url、视频无法播放的问题。

二、webviewupgrade 接入

webviewupgrade的创建者遇到的问题:华为机上因为webview内核的chromium版本低于107无法播放h265视频的情况,为了解决上述问题可以用js实现h265播放,但是会比较卡,就想不能让webview用应用内的apk作为内核。

2.1 支持版本

  • com.google.android.webview:122.0.6261.64

  • com.android.webview:113.0.5672.136

  • com.huawei.webview:14.0.0.331

  • com.android.chrome:122.0.6261.43

  • com.amazon.webview.chromium:118-5993-tv.5993.155.51

当然,你也可以找到其他版本下载安装即可。例如 com.android.webview,我觉得113版本太高,那么你可以下载一个95版本的。或者你不想使用com.android.webview 改用 com.google.android.webview也是可以的。

2.2 接入

2.2.1 导入项目(我使用的是这个)

将demo的core和download-source模块直接导入到自己项目,修改core中 implementation 'androidx.appcompat:appcompat:1.6.1'的版本。

2.2.2 implementation

implementation 'io.github.jonanorman.android.webviewup:core:0.1.0'// 不需要下载apk时使用
implementation 'io.github.jonanorman.android.webviewup:download-source:0.1.0'// 需要下载apk使用

如果是低版本,可能会报错。core中 implementation 'androidx.appcompat:appcompat:1.6.1'的版本比较高,需要兼容的sdk版本也比较高。

解决方案:

  • 1.提高项目 sdk 版本。

  • 2.使用exclude。

以上两种方式都可以。

2.3 使用

设置

    private void setwebview(){
        upgradeinfo upgradeinfo = new upgradeinfo(
                "com.android.webview",
                "113.0.5672.136",
                "https://mirror.ghproxy.com/https://raw.githubusercontent.com/jonanorman/sharefile/main/com.android.webview_113.0.5672.13_armeabi-v7a.zip",
                "网络");
        string  systemwebviewpackagename = webviewupgrade.getsystemwebviewpackagename();
        if (systemwebviewpackagename != null &&systemwebviewpackagename.equals(upgradeinfo.packagename)
                && versionutils.compareversion( webviewupgrade.getsystemwebviewpackageversion(),upgradeinfo.versionname) >= 0) {
            //toast.maketext(getapplicationcontext(), "system webview is larger than the one to be upgraded, so there is no need to upgrade", toast.length_long).show();
            return;
        }
        upgradesource upgradesource = upgradeinfo.toupgradesource(robotapplication.getinstance());
        if (upgradesource == null) {
            return;
        }
        webviewupgrade.upgrade(upgradesource);
        updateupgradewebviewstatus();
    }
}

这里我使用的是网络下载:

  • 包名:com.android.webview;

  • 版本号:113.0.5672.136

  • 下载内核地址:https://mirror.ghproxy.com/https://raw.githubusercontent.com/jonanorman/sharefile/main/com.android.webview_113.0.5672.13_armeabi-v7a.zip

    //更新状态
    private void updateupgradewebviewstatus() {
        if (webviewupgrade.isprocessing()) {
            logutils.e("webviewupgrade","upgrading...");
        } else if (webviewupgrade.isfailed()) {
            logutils.e("webviewupgrade","fail...");
        } else if (webviewupgrade.iscompleted()) {
            logutils.e("webviewupgrade","complete...");
        }
        int process = (int) (webviewupgrade.getupgradeprocess() * 100);
        logutils.e("webviewupgrade","processing:-----"+process + "%");
        throwable throwable = webviewupgrade.getupgradeerror();
        if (throwable != null) {
            logutils.e("webviewupgrade","throwable.message:" + throwable.getmessage() + "\nstacktrace:" + log.getstacktracestring(throwable));
        }
    }
public class upgradeinfo {
    public string title;
    public string url;
    public string packagename;

    public string versionname;
    public string extrainfo;
    public upgradeinfo(string packagename, string versionname, string url, string extrainfo) {
        this.title = packagename + "\n" + versionname;
        this.extrainfo = !textutils.isempty(extrainfo) ? extrainfo : "";
        if (!extrainfo.isempty()) {
            this.title = this.title + "\n" + extrainfo;
        }
        this.url = url;
        this.packagename = packagename;
        this.versionname = versionname;
    }

    public upgradeinfo(string packagename, string versionname, string url) {

        this(packagename, versionname, url, "");
    }
    @nullable
    public upgradesource toupgradesource(context context) {
        upgradesource upgradesource = null;
        if (this.extrainfo.equals("网络")) {
            upgradesource = new upgradedownloadsource(
                    context,
                    this.url,
                    new file(context.getfilesdir(), this.packagename + "/" + this.versionname + ".apk")
            );
        } else if (this.extrainfo.equals("内置")) {
            upgradesource = new upgradeassetsource(
                    context,
                    this.url,
                    new file(context.getfilesdir(), this.packagename + "/" + this.versionname + ".apk")
            );
        } else if (this.extrainfo.equals("安装包")) {
            upgradesource = new upgradepackagesource(
                    context,
                    this.packagename
            );
        }
        return upgradesource;
    }
    
public class mainactivity extends baseactivity implements , upgradecallback {

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setwebview();
    }
    private void setwebview(){
        ......
    }
    //更新中
    @override
    public void onupgradeprocess(float percent) {
        updateupgradewebviewstatus();
    }
    //更新完成
    @override
    public void onupgradecomplete() {
        updateupgradewebviewstatus();
    }
    //更新失败
    @override
    public void onupgradeerror(throwable throwable) {
        updateupgradewebviewstatus();
    }
}

注意:这个代码尽量前置,如果无法前置,需要保证,其他地方先不要对webview进行操作。如果操作了,会导致初始化失败。

可以先试用demo尝试功能是否可用,再集成到自己的项目中。

2.4 webview

执行完上面的操作后,你就正常操作webview即可。上面我只是列举了网上下载的例子(第一次下载,只要文件存在,无需重复下载),还支持内置/安装包的形式,我这是应用包体太大,不适合内置。

这种方式是可行的,但是也有一些小的前置条件需要注意,如果无法前置会导致使用失败。怎么前置才能做到万无一失,就需要根据实际情况来判断了。

总结

到此这篇关于android webview升级的文章就介绍到这了,更多相关android webview升级内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com