rest2mobile plugin for Android Studio & IntelliJ IDEA

The rest2mobile Android Studio plugin allows you to generate mobile api from REST services directly inside your android project. You can check out this short demo to see how it works.

Stable releases

The latest stable release is also published on the Jetbrains plugin repository

Prerequisites

The rest2mobile plugin for Android Studio & IntelliJ IDEA has these prerequisites:

Build

Use IntelliJ 13 or later to create the plugin project from the source:

Build the plugin zip using the Build->Prepare Plugin Module ... for deployment option.

Setup

The installation instructions are summarized here.

Be sure you include the rest2mobile Android SDK in your app by inserting this in your app's build.gradle by adding the following lines to it:

repositories {
    maven {
        url "http://repo.magnet.com:8081/artifactory/public/"
    }
    mavenLocal()
    mavenCentral()
}
dependencies {
    compile("com.magnet:r2m-sdk-android:[email protected]") {
        transitive = true
    }
}

STEP 1: Download

Go to the releases and download the latest installer.

Note: You can find the latest official release on the Magnet developer site too.

rest2mobile plugin main menu

STEP 2: Install plugin

In Android Studio or IntelliJ, go to Preferences -> Plugins and click on Install plugin from disk...

rest2mobile plugin main menu

It will ask you to choose the location of the installer zip, point to the package that you've just downloaded.

Note: the plugin is also distributed on the Jetbrains repository, so instead of clicking on Install plugin from disk..., you can choose Browse repositories... and then look for rest2mobile.

STEP 3: Restart your IDE after loading the plugin

rest2mobile plugin main menu

STEP 4: Load or create an android application

It is recommended to use a gradle-based application (if you are still using ant, go here).

Ensure that you use at a minimum the Android API level 15.

STEP 5: Add new API

Once the IDE has restarted, you should see the new R2M menu (or "Magnet" menu in earlier releases):

rest2mobile plugin main menu

Choose "Add new API" from the R2M menu ('Magnet' menu for earlier releases). The following describes the wizard options:

rest2mobile plugin wizard

Alternatively, instead of crafting your own REST invocations, you can load existing examples from the r2m-examples github repo by clicking on the browse icon next to the Class Name box:

rest2mobile plugin download menu

STEP 6: Generate code

Click on "Generate", this generates the code under your src directory. If you have a src/androidTest/java directory, a unit test file is also generated.

Ex: say you selected the GoogleDistance built-in example, you should see the following files in your project pane:

rest2mobile generated classes

STEP 7: Test

Use the generated unit test generated under src/androidtest/java/

Ex: if you generated the GoogleDistance native API, the unit test can be found atr src/androidTest/java/com/magnetapi/examples/controller/api/test/GoogleDistanceTest. Remove the code>@Supress</code annotation on testGoogleDistance, and invoke the API with real values:

  /**
    * Generated unit test for {@link GoogleDistance#googleDistance}
    */
  @SmallTest
  public void testGoogleDistance() throws SchemaException, ExecutionException, InterruptedException {
    String origins = "435 Tasso Street, Palo Alto CA";
    String destinations = "1 Embarcadero Street, San Francisco, CA";
    String sensor = "false";
    String mode = "driving";
    String language = "en";
    String units = "imperial";

    Call<GoogleDistanceResult> callObject = googleDistance.googleDistance(
      origins, 
      destinations, 
      sensor, 
      mode, 
      language, 
      units, null); 
    GoogleDistanceResult result = callObject.get(); // Or use a non-null 'StateChangedListener' in 'googleDistance' to be asynchronously called back, once result is avail.
    assertEquals("33.9 mi", result.getRows().get(0).getElements().get(0).getDistance().getText());// actual distance may vary
  }

Notice how the rest2mobile's java invocation allows you to safely navigate the JSON payload:

Strongly-typed in java:

   String distance = result.getRows().get(0).getElements().get(0).getDistance().getText());

Stringly-typed in JSON:

{
   "destination_addresses" : [ "1 Lombard Street, San Francisco, CA 94111, USA" ],
   "origin_addresses" : [ "435 Tasso Street, Palo Alto, CA 94301, USA" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "33.9 mi",
                  "value" : 54605
               },
               "duration" : {
                  "text" : "44 mins",
                  "value" : 2626
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

Feedback

We are constantly adding features and welcome feedback. Please, ask questions or file requests here.

Blog

Get latest tips, news, and useful information from our blog

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this software except in compliance with the License.

Copyright

Copyright © 2014-2015 Magnet Systems, Inc. All rights reserved.