Friday, July 18, 2014

How to create a Salesforce.com Android Mobile app(hybrid_local)

This post assumes that you have already set up the development environment for Salesforce.com Mobile development. If you don't have, visit here and set it up first (for windows)

To connect the mobile application with your Salesforce.com instance, we need to create a some trusted connection between the app and target environment. Salesforce.com connected apps helps you here.
First we'll configure a connected app and then create the mobile app.

1). Login to your Salesforce.com instance and navigate into Setup ->  Create -> Apps,
2). Under Connected apps click on New
3). Fill the fields as required.
4). Under "API (Enable OAuth Settings)" tick the checkbox "Enable OAuth Settings".
5). Select the required OAuth scopes and click Save.
You should see the newly created Connected app something like below
Note that this Consumer key is going to do the main role to keep the connection between Salesforce.com and the app. Now it's time to create the mobile app. This post explain developing a hybrid_local app.

6). open a command prompt and run "forcedroid create"
7). Choose your options for each prompt. Below is a snap shot of what it has been chosen for this demo.

The command prompt output is itself describing the next steps.
After importing the projects into eclipse, open the MyFirstSFDCMobile/assets/www/bootconfig.json file. You have to update this file as below.
                        remoteAccessConsumerKey = Consumer key in your connected app
                        oauthRedirectURI = Call back URL of your connected app

Below is sample of bootconfig.json
{
    "remoteAccessConsumerKey": "my_consumer_key",
    "oauthRedirectURI": "sfdc://success",
    "oauthScopes": ["api refresh_token"],
    "isLocal": true,
    "startPage": "index.html",
    "errorPage": "error.html",
    "shouldAuthenticate": true,
    "attemptOfflineLoad": false,
    "androidPushNotificationClientId": ""
}

Done. Right click on MyFirstSFDCMobile and select Run As Android Project.
Have a look at index.html and inline.js inside www directory. These are the key files you have to modify to build your own application.

Cheers!

6 comments:

  1. Hi I followed similar steps based on the new instructions in mobile sdk.pdf.
    1. npm install forcedroid –g
    2. npm -g install cordova
    3. forcedroid create
    4. Build the project in Eclipse
    5. modify bootconfig to use the Consumer key for the remote app in my developer org
    set callback to same which is testsfdc:///mobilesdk/detect/oauth/done

    When I run the project by selecting run or run an android app, I select an AVD i set up which is Andoird TV ARM(armeabi-v7a) and target is Android 5.1.1(API Level 22), I only see a black screen with controls on the right side, but on the left side, it's blank and eventually displays "Android". I don't get the salesforce login window. Do you know this might be the case?

    ReplyDelete
    Replies
    1. Looks like not related to SFDC setting up, but issue with AVD. This is a pain in android development. Better you just create an AVD and run it first and make sure you can see home screen. Then try running the project by selecting that running AVD.(use run configuration so that you can select the device to be deployed)

      Delete
  2. Hi I followed the same steps to create sample hybrid app using salesforce sdk.
    When I run the project it's not working on emulator only getting the error message
    application has stoped.But the same app is working in emulator after removing
    salesforce plugin.
    Is is possible for you to share your working code.
    I am using Cordova 4.0.0.
    Android API 23
    ant 1.9.6
    java jdk 1.7.0_55

    ReplyDelete
    Replies
    1. I haven't use the plugin separately. Are you using npm to create the project?

      Delete
    2. I tried both ways.
      1. Using cordova npm package to create sample app and manually add the plugin.
      2. Using forcedroid npm package .

      Delete
    3. Sorry Avinash, I've not tried them recently and what I've used is cordova 2.3.0. Try to see logs to identify any errors

      Delete