As you begin your journey into native Android development with Salesforce, you'll want to get started with Mobile SDK for Android. This powerful toolset enables you to build Salesforce-enabled apps on the world's most popular mobile platform. In this article, we'll dive into the fundamental features of the Android native SDK and explore how to create a connected app, set up your Android development environment, and run your first Swift app development project.

Learning Objectives

By the end of this guide, you'll be able to:

  • Describe two primary features of Mobile SDK for Android
  • Install Mobile SDK for Android
  • Create an Android app and specify application options
  • Import and build your app in Android Studio

Native Android Development with Salesforce

Mobile SDK for Android gives you the tools and resources needed to build Salesforce-enabled apps on the world's most popular mobile platform. With this powerful toolset, you'll be able to automate the OAuth2 authentication flow and access the Salesforce REST API with utility classes that simplify that access.

Before diving in, make sure your development environment is properly configured by completing the Set Up Your Mobile SDK Development Environment project. Once you're ready, start by creating a simple native Android app. You'll then hone your Android development chops by modifying the app's REST interactions and user interface.

Creating a Connected App

To connect to the Salesforce service, every mobile app requires a Salesforce connected app. This connected app authorizes your app to communicate with Salesforce and securely access Salesforce APIs. To set up authentication in your app, you'll need to copy the Callback URL and Consumer Key values from your connected app. Don't worry about the consumer secret – Mobile SDK apps don't use it.

Creating an Android Project

Now that you've set up your Android development environment and created a Salesforce connected app, you're ready to create your first Swift app development project. Use the forcedroid command-line tool to create a new project:

  • Change to the directory where you want to store your project
  • Type forcedroid create
  • Enter the following values:

+ Application type: native

+ Application name: MyTrailNative

+ Package name: com.mytrail.android

+ Organization name: MyApps, Inc.

+ Output directory: TrailAndroidApps

After a sequence of log messages and a congratulatory message, you'll be returned to the command prompt. Congratulations! Your new app is now ready to run.

Running Your New Android App

The project created by forcedroid is ready to run immediately. It may not do much – just executing a simple SOQL query and displaying a table of names. But at least you know that the plumbing is in place and ready to do your bidding. Let's see it in action!

When creating a native app, forcedroid substitutes generic values for consumer ID and callback URI. Every app requires these values to connect to Salesforce servers and data. Before posting the app publicly, remember to replace the generic settings with values from your own connected app. So that you don't forget, let's do that now.

  • Open app/res/values/bootconfig.xml in a UTF-8 compliant text editor
  • Replace the remoteAccessConsumerKey value with the consumer key from your connected app
  • Similarly, replace the oauthRedirectURI value with the callback URL from your connected app
  • Save the file in UTF-8 encoding
  • Launch Android Studio
  • Import the project by selecting "Import project (Eclipse ADT, Gradle, etc.)" and browsing to your target directory

Android Studio will automatically build your workspace. This process can take several minutes. When the status bar reports "Gradle build successful", you're ready to run the project.

  • Click the "Run" button or press SHIFT+F10 (Windows) or ^R (Mac)
  • Select an emulator or connected device that's running Android SDK API 23 or later (up to Android 10 (API 29))

Android Studio will launch your app in the emulator or on your connected Android device.