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)