Getting Started in Mobile Development with Android

The Salesforce Mobile SDK for Android is an open source toolkit that provides a collection of native libraries to enable developers to rapidly build Android applications that securely connect to the Salesforce Platform. The SDK abstracts the underlying OAuth 2.0 and REST API calls needed to connect an Android application with the Salesforce Platform, allowing developers to focus on their app's functionality.

The SDK also provides a hybrid container based on the open source Apache Cordova (PhoneGap) project that enables HTML5-based applications to leverage device features like the camera and microphone. The hybrid SDK extends the core Cordova platform to provide additional libraries for key enterprise requirements such as OAuth 2.0 authentication and secure offline storage, effectively providing an enterprise-ready hybrid application container.

Select Platform

Select Architecture

Choose Native to develop native Android applications that can access all of the rich features and hardware on the device. Choose Hybrid to convert a web application developed using HTML5, JavaScript and CSS3 into a native Android binary. A hybrid approach combines the portability of HTML5 with access to device features like the camera and microphone.

Sign up for a free Salesforce Platform Developer Edition (DE), if you don’t already have one. This will act as your own personal test environment. You should not use this tutorial with a trial, sandbox, or production org.

Create a Connected App in Salesforce

Log into your Developer Edition.

Open the Setup menu by clicking [Your Name] > Setup.

Create a new Connected App by going to App Setup > Create > Apps.

Click the ‘New’ button in the Connected Apps list.

Fill out all required fields and click ‘Save’:

Connected App: MySampleApp

Developer Name: MySampleApp

Contact Email: Your email

Callback URL: sfdc://success.

Selected all available OAuth scopes

From the detail page of the connect app you just created, copy the Consumer Key and Callback URL as you’ll need these later.

Install the Mobile SDK for Android

For the fastest, easiest route to Android development, install Node.js and NPM. Then use NPM to install the forcedroid package. This package installs the Salesforce Mobile SDK for Android.

Run the downloaded installer and accept all prompts asking for permissions to install.

Open a command prompt and type npm and press ENTER to make sure your installation was successful. If you don’t see a page
of usage information, revisit the previous step to find out what’s missing.

Install the Mobile SDK: npm install forcedroid -g

Note: With the -g option, you run npm install from any directory. In Windows environments,
global packages are installed in %APPDATA%\npm\node_modules, and binaries are linked in %APPDATA%\npm.
In non-Windows environments, use sudo npm install forcedroid -g.
The NPM utility installs the package under /usr/local/lib/node_modules, and links binary modules in /usr/local/bin. Most
users need the sudo option because they lack read-write permissions in /usr/local.

Create a Native Android Project

It's easy to create a new app by entering information on the command line.

On the command line, enter forcedroid and you'll see a list of options.

Now enter forcedroid create and enter the appropriate information for each prompt.

Note that you can also enter the app options right on the command line:

Run the Project Template App

Before building the new application, build the SalesforceSDK project by running the following commands at the command
prompt:cd $SALESFORCE_SDK_DIR/native/SalesforceSDK
$ANDROID_SDK_DIR/tools/android update project -p . -t <id>
ant clean debug

Next Steps

Getting Started with Android Hybrid

This quick start creates a hybrid Android mobile application using the Salesforce Mobile SDK. A hybrid mobile architecture converts a web application developed using HTML5, JavaScript and CSS3 into a native Android binary. A hybrid approach combines the portability of HTML5 with access to device features like the camera and microphone.

Using this app, users can view a list of Account and Contact records from Salesforce as well as a list of local device contacts.

Sign up for a free Salesforce Platform Developer Edition (DE), if you don’t already have one. This will act as your own personal test environment. You should not use this tutorial with a trial, sandbox, or production org.

Create a Connected App in Salesforce

Log into your Developer Edition.

Open the Setup menu by clicking [Your Name] > Setup.

Create a new Connected App by going to App Setup > Create > Apps.

Click the ‘New’ button in the Connected Apps list.

Fill out all required fields and click ‘Save’:

Connected App: MySampleApp

Developer Name: MySampleApp

Contact Email: Your email

Callback URL: sfdc://success.

Selected all available OAuth scopes

From the detail page of the connect app you just created, copy the Consumer Key and Callback URL as you’ll need these later.

Install the Mobile SDK for Android

For the fastest, easiest route to Android development, install Node.js and NPM. Then use NPM to install the forcedroid package. This package installs the Salesforce Mobile SDK for Android.

Run the downloaded installer and accept all prompts asking for permissions to install.

Open a command prompt and type npm and press ENTER to make sure your installation was successful. If you don’t see a page
of usage information, revisit the previous step to find out what’s missing.

Install the Mobile SDK: npm install forcedroid -g

Note: With the -g option, you run npm install from any directory. In Windows environments,
global packages are installed in %APPDATA%\npm\node_modules, and binaries are linked in %APPDATA%\npm.
In non-Windows environments, use sudo npm install forcedroid -g.
The NPM utility installs the package under /usr/local/lib/node_modules, and links binary modules in /usr/local/bin. Most
users need the sudo option because they lack read-write permissions in /usr/local.

Create a Hybrid Android Project

It's easy to create a new app by entering information on the command line.

On the command line, enter forcedroid and you'll see a list of options.

Now enter forcedroid create and enter the appropriate information for each prompt.

When specifying the apptype, use -—apptype=”hybrid_local” for a hybrid app with all code in the local project (your HTML and JavaScript
files go in ${target.dir}/assets/www/). Use -—apptype=”hybrid_remote” for a hybrid app with code in a Visualforce app on the server.

Note that you can also enter the app options right on the command line: