User:Dcallahan/GeckoView: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
m (Add a ToC back in)
m (Java.)
Line 67: Line 67:
Inside a layout <code>.xml</code> file, add the following:
Inside a layout <code>.xml</code> file, add the following:
<syntaxhighlight lang="XML">
<syntaxhighlight lang="XML">
<org.mozilla.gecko.GeckoView
<org.mozilla.geckoview.GeckoView
     android:id="@+id/geckoview"
     android:id="@+id/geckoview"
     android:layout_width="fill_parent"
     android:layout_width="fill_parent"
Line 74: Line 74:


=== Initialize GeckoView in an Activity ===
=== Initialize GeckoView in an Activity ===
''Note: These snippets are written in [http://kotlinlang.org/ Kotlin]''


'''1. Import the GeckoView classes inside an Activity:'''
'''1. Import the GeckoView classes inside an Activity:'''
<syntaxhighlight lang="Kotlin">
<syntaxhighlight lang="Java">
import org.mozilla.geckoview.GeckoRuntime
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView
import org.mozilla.geckoview.GeckoView;
</syntaxhighlight>
</syntaxhighlight>


'''2. In that activity's <code>onCreate</code> function, add the following:'''
'''2. In that activity's <code>onCreate</code> function, add the following:'''
<syntaxhighlight lang="Kotlin">
<syntaxhighlight lang="Java">
val view : GeckoView = findViewById(R.id.geckoview)
GeckoView view = findViewById(R.id.geckoview);
val session = GeckoSession()
GeckoSession session = new GeckoSession();
val runtime = GeckoRuntime.create(this)
GeckoRuntime runtime = GeckoRuntime.create(this);


session.open(runtime)
session.open(runtime);
view.session = session
view.setSession(session);
session.loadUri("https://mozilla.org")
session.loadUri("about:mozilla");
</syntaxhighlight>
</syntaxhighlight>



Revision as of 15:18, 3 October 2018

GeckoView is Firefox Quantum's engine, packaged as a reusable Android library.

Mozilla uses GeckoView to power Firefox Reality, Firefox Focus, and more.

GeckoView serves a similar purpose to Android's built-in WebView, but it has its own APIs and is not a drop in replacement.

Get Started

Building a browser? Check out Android Components, our collection of ready-to-use supporting libraries!

Configure Gradle

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

1. Set the GeckoView version

Like Firefox, GeckoView has three release channels: Stable, Beta, and Nightly. Browse the Maven Repository to see currently available builds.

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

2. Add Mozilla's Maven repository

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

3. Configure build flavors

Note: Until we resolve Bug 1485045, you must configure a separate build variant for each CPU architecture.

android {
    // ...

    flavorDimensions "abi"

    productFlavors {
        x86     { dimension "abi" }
        x86_64  { dimension "abi" }
        arm     { dimension "abi" }
        aarch64 { dimension "abi" }
    }
}

4. Add GeckoView Implementations

dependencies {
    // ...

    x86Implementation     "org.mozilla.geckoview:geckoview-${geckoviewChannel}-x86:${geckoviewVersion}"
    x86_64Implementation  "org.mozilla.geckoview:geckoview-${geckoviewChannel}-x86_64:${geckoviewVersion}"
    armImplementation     "org.mozilla.geckoview:geckoview-${geckoviewChannel}-armeabi-v7a:${geckoviewVersion}"
    aarch64Implementation "org.mozilla.geckoview:geckoview-${geckoviewChannel}-arm64-v8a:${geckoviewVersion}"
}

Add GeckoView to a Layout

Inside a layout .xml file, add the following:

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

Initialize GeckoView in an Activity

1. Import the GeckoView classes inside an Activity:

import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoView;

2. In that activity's onCreate function, add the following:

GeckoView view = findViewById(R.id.geckoview);
GeckoSession session = new GeckoSession();
GeckoRuntime runtime = GeckoRuntime.create(this);

session.open(runtime);
view.setSession(session);
session.loadUri("about:mozilla");

You're done!

Your application should now load and display a webpage inside of GeckoView.

To learn more about GeckoView's capabilities, review GeckoView's JavaDoc or the reference application.

Documentation and Examples

APIs

Building / Contributing / Bugs

Products / Examples