Android 集成firefox浏览器内核GeckoView

本文详细介绍了如何在Android应用中集成GeckoView,以替代性能不佳的原生WebKit内核。步骤包括添加Maven仓库、配置Java支持、添加依赖、设置GeckoView在XML布局和Java类中的使用,以及解决特定bug。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 集成firefox浏览器内核GeckoView

1.集成原因

Android手机原生的 webkit 内嵌的谷歌内核性能性能不太理想,即使使用更换腾讯X5内核性能的提升也很有限,不能很好的满足我们的需求,所以使用GeckoView。

2.如何集成

You need to add or edit four stanzas inside your module’s build.gradle file.`

在模块级build.gradle文件中配置

2.1设置GeckoView版本

GeckoView具有三个发布渠道:Stable(稳定版),Bate(测试版)和Nightly。浏览Maven存储库以查看当前可用的内部版本我现在使用的是"120.0.20231006092133"

ext {
    geckoviewChannel = "nightly"
    geckoviewVersion = "120.0.20231006092133"
}

2.2添加Mozilla的Maven存储库

repositories {
    maven {
        url "https://maven.mozilla.org/maven2/"
    }
}

2.3配置Java支持

 compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

2.4添加GeckoView依赖实现

dependencies {
 // ...
    implementation "org.mozilla.geckoview:geckoview-${geckoviewChannel}:${geckoviewVersion}"
}

2.5布局文件.xml中使用GeckoView

<org.mozilla.geckoview.GeckoView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/geckoview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

2.6在java类中添加使用

2.6.1在Activity中导入GeckoView类:
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView;
2.6.2创建一个static成员变量来存储GeckoRuntime实例。
private static GeckoRuntime sRuntime;
2.6.3在该activity的“onCreate”函数中,添加以下内容:
GeckoView view = findViewById(R.id.geckoview);
GeckoSession session = new GeckoSession();

// Workaround for Bug 1758212
session.setContentDelegate(new GeckoSession.ContentDelegate() {});

if (sRuntime == null) {
  // GeckoRuntime can only be initialized once per process
  sRuntime = GeckoRuntime.create(this);
}

session.open(sRuntime);
view.setSession(session);
session.loadUri("about:buildconfig"); // Or any other URL...

大功告成

参考文章:

Getting Started with GeckoView — Firefox Source Docs documentation (mozilla.org)

Android Tutorial for GeckoView: Getting Started | Kodeco

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值