抱歉,该网页目前还不存在中文版本,请继续浏览其他网页!

Android extensions

This tutorial explains how to write Crosswalk Android extensions using Java.

The application you'll build in the tutorial demonstrates a very simple "echo" extension. This simply returns the string passed to it, prefixed with "You said: ". The extension is deliberately trivial so that the tutorial can focus on explaining the principles behind Crosswalk Android extensions.

This tutorial doesn't cover best practices for web development. For example, it avoids grunt and bower and doesn't use any third party front-end libraries, for simplicity's sake. Instead, it focuses on helping you explore the pieces of a hybrid Crosswalk application and how they fit together.

This tutorial uses Ant and Ivy to build the extension. Alternatively, other Java tools, such as gradle, could be used.

By the end of the tutorial, you will be able to develop your own Java extensions for Crosswalk applications on Android.

Introduction to the tutorial

In this tutorial, you will build a Crosswalk Android application with a Java extension. This consists of two main pieces:

  1. A Crosswalk extension

    The extension consists of:

    • Java source code: Standard Android/Java classes, packaged into a jar file.
    • JavaScript wrapper: A JavaScript file which exposes the Java code to an app running on Crosswalk.
    • Configuration: A JSON file to wire up the JavaScript wrapper with the Java classes.

    The extension you'll write provides an echo service, which prefixes any string passed to it and returns it.

    Note that a Crosswalk application can use multiple extensions if desired.

  2. An HTML5 web application

    This is a self-contained web application which "lives inside" the Android application, but uses Crosswalk as its runtime. It consists of standard assets like HTML files, JavaScript files, images, fonts etc.

    The Crosswalk extension is invoked by code in the web application, via the JavaScript wrapper mentioned above. In the tutorial application, the response from the echo extension is rendered into a DOM element.

The project you create will also contain a few files to assist with packaging the above components into an .apk package file you can install on the Android target.

All of the source code for the tutorial is available as part of the crosswalk-samples download, or on github at https://github.com/crosswalk-project/crosswalk-samples (inside the extensions-android directory).