Mobile/GeckoView

From MozillaWiki
< Mobile
Revision as of 07:31, 11 September 2018 by Cpeterson (talk | contribs) (→‎Add GeckoView to your build.gradle: Describe GV version numbers)
Jump to navigation Jump to search

What is GeckoView

GeckoView is an Android library project that allows third-party developers to use Gecko as an Android View in their own applications. GeckoView is similar to Android's built in WebView, but it is not a drop in replacement for WebView.

Note that GeckoView is not quite ready to be used in a production environment.

Current API documentation can be viewed here: https://mozilla.github.io/geckoview/javadoc/mozilla-central/

Testing GeckoView

GeckoView is a library, not a standalone app, but you can test GeckoView by installing Focus Nightly "track" (channel). Focus 6.0 and earlier use Android's WebView, but Focus 7.0 is switching to Mozilla's homegrown Gecko. Unfortunately, Google Play only allows you to install one track (per Google Play account) at a time. Installing Focus Nightly will uninstall the regular Focus and replace it (and your settings) with Focus Nightly. If you have multiple Google Play accounts, then you can install a different Focus track in each account.

If you joined the Focus beta program before subscribing to the focus-klar-nightly Google Group, Google Play might not upgrade you to the new Focus Nightly right away. Google Play treats the Focus beta and nightly tracks as different flavors of beta and can forget to switch you from the regular beta to the nightly "beta" track. In this case, try:

  1. Leave the Focus beta program (by clicking the "LEAVE" button in the "You're a beta tester" section of Focus's Google Play page)
  2. Uninstall Focus
  3. Re-join the beta program
  4. Re-install Focus
  5. Open the Play Store app, open the "My apps & games" screen, select the "BETA" tab, and check for "Firefox Focus: The privacy browser" UPDATE button.

Using GeckoView

Add GeckoView to your build.gradle

GeckoView builds are published in a Maven repository: maven.mozilla.org with three release channels:

  • Nightly: geckoview-nightly-armeabi-v7a
  • Beta: geckoview-beta-armeabi-v7a
  • Stable: geckoview-armeabi-v7a

And three different architectures:

  • armeabi-v7a (32-bit ARM)
  • arm64-v8a (64-bit ARM aka AArch64)
  • x86 (32-bit)
  • Coming soon in bug 1480834: x86-64

New Nightly builds are released every day from Firefox's mozilla-central repository. New Beta builds are released once or twice a week from Firefox/Gecko's mozilla-beta repository. New major versions of the Stable channel are released every 6-8 weeks when a new major version of Firefox is released, but dot releases with security fixes may be released more frequently. The Stable channel is built from a release branch ("relbranch") in the [mozilla-release repository] with a branch name like GECKOVIEW_62_RELBRANCH.

GeckoView build numbers use the format: MAJOR.MINOR.BUILD_ID, e.g. 63.0.20180904170936.

repositories {
    maven {
       url 'https://maven.mozilla.org/?prefix=maven2/'
    }
}

Add geckoview to dependencies

Again, in build.gradle

dependencies {
    implementation 'org.mozilla:geckoview:geckoview-nightly-armeabi-v7a:+'
}

This will always use the latest Nightly in the repository. As GeckoView development continues, we will have Beta and Release repositories that have the expected version names (62.0.0, etc).

Loading a page in GeckoView

You can now use GeckoView your app by including the following in a layout XML file:

<org.mozilla.gecko.GeckoView
  android:id="@+id/geckoview"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" />

You can then load a page in your Activity with:

onCreate(...) {
    // Find the GeckoView in our layout
    GeckoView geckoView = (GeckoView) findViewById(R.id.geckoview);

    // Attach the GeckoView to a new GeckoSession
    GeckoSession session = new GeckoSession();
    session.open();
    geckoView.setSession(session);

    // Load a URL
    session.loadUri("http://mozilla.com");
}

Example App

GeckoViewExample is an example app that lives in mozilla-central and lets you easily exercise GeckoView. You can build/install it with the following gradle command:

mach gradle geckoview_example:installLocalWithGeckoBinariesNoMinApiDebug

You may find it easier to do this from Android Studio, where geckoview_example appears as a module.

Firefox Focus also has a build variant that uses Gecko. To build, check out the Focus code from https://github.com/mozilla-mobile/focus-android and follow the instructions. The only difference is you need to select one of the Gecko build variants from the Android Studio 'Build' menu.

The Gecko-related code for Focus lives in WebViewProvider.java

Bugs

GeckoView bugs 🐛