Library emulating Android APIs over GWT
Java Other
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
androidemu
demo
.gitignore
build.gradle
license.txt
readme.md
settings.gradle

readme.md

GWT Android Emu

GWT Android Emu helps porting Android Apps to HTML5 to make them work in any web browser keeping a lot of code in common.

It's also a framework to build web apps using Java an Android APIs.

Here is a running demo of the library:

http://mobialia.com/gwt-android-emu/

GWT is the perfect framework to migrate your Android Apps to the web because it compiles Java into Javascript.

The emulated Android APIs:

  • Activities with their life cycle
  • Services
  • Intents: you can pass data in a Bundle, launch activities and services, etc.
  • Handlers and Messages
  • AlertDialogs, ProgressDialogs, Toasts...
  • Views: each HTML element is mapped to a View type, see table below
  • ListViews with custom adapters
  • RecyclerViews
  • Menu and MenuItems (inflating menus from xml or from code)
  • Fragments with transactions, ViewPager, DrawerLayout, etc. (emulating the v4 support library)
  • AppCompatActivity (emulating the v7 support library)
  • SharedPreferences: implemented using HTML5's LocalStorage
  • Other utility classes: Log, FloatMath, SystemClock

The GWT entry point is a non-standard AndroidManifest class extending android.AndroidManifest. This class specifies the default Activity and creates the Resources and the Application objects.

This is a work in progress in continuous evolution. At Mobialia we used this library to port some of our Android apps to GWT (like http://chess.mobialia.com). It's far from complete and very fitted to our needs, but we make it public in the hope that it will be useful for other developers.

It's released under the MIT License, so feel free to use it anywhere.

Emulating Resources

We included a tool "GenerateResources" in the package "utils" to help with resource emulation generation.

There is a usage sample in the demo project: the generate-resources.sh script converts the resources from the source android project in the /demo/source_android_project/ folder.

Generated files:

  • /src/main/java//R.java: with the resource IDs for menus, drawable, colors....
  • /src/main/java//res/Resources.java: mapping IDs to resources
  • /src/main/java//res/Strings.java
  • /src/main/java//res/Arrays.java
  • /src/main/java//res/Strings.properties with language variants
  • /src/main/java//res/Arrays.properties with language variants
  • /src/main/java//res/Menus.java

Generated from the GWT code:

  • R.id.* for each id="xxxx" in the uibinder xml layouts at /src/main/resources//res/layout/
  • R.drawable.* for the images in /src/main/webapp/img/. Vector drawables can be used in SVG format.
  • R.layout.* for each method in the /src/main/java//res/Layouts.java class

Migrating Layouts

Layouts must be redesigned in HTML with the GWT UiBinder. You must create a "Layouts.java" class in the resource directory with a method to get each one of the GWT widgets. There is a sample Layouts.java file in the demo project.

HTML Elements to Android Widgets

This is how this library maps the HTML elements to Android widgets: (see also the ViewFactory class)

HTML Element Android Widget
<div class="ListView"> ListView
<div class="RecyclerView"> RecyclerView
<div class="ScrollView"> ScrollView
<div class="LinearLayout"> LinearLayout
<div class="ViewPager"> ViewPager
<div> TextView
<textarea> EditText
<input type="text"> EditText
<input type="number"> EditText
<input type="password"> EditText
<input type="button"> Button
<input type="radio"> RadioButton
<input type="checkbox"> CheckBox
<input type="image"> ImageButton
<select> Spinner
<img> ImageView

Using GWT Android Emu in other projects

This library's JARs are distributed via Mobialia's bintray repo (https://bintray.com/mobialia/maven). To use them, first include this repository in your gradle file:

repositories {
    maven { url 'https://dl.bintray.com/mobialia/maven' }
}

and then reference the gwt-android-emu libraries:

dependencies {
    compile 'com.mobialia:gwt-android-emu:0.4'
    compile 'com.mobialia:gwt-android-emu:0.4:sources'
}

Demo project

The demo project is a GWT app coded like an Android App, you can inspect the code at:

https://github.com/mobialia/gwt-android-emu/blob/master/demo/src/main/java/android/demo/

It includes Activities with Fragments, a Drawer, emulated Strings, Layouts as resources, Menus, Toasts, AlertDialogs, etc.

Building the demo project

cd demo
./generate-resources.sh
gradle clean compileGwt