Skip to main content

Get Started with Native Android Development

Learning Objectives

After completing this unit, 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

Mobile SDK for Android gives you the tools and resources for building Salesforce-enabled apps on the world’s most popular mobile platform. In this module, you learn about the fundamental features of the Android native SDK:

  • Automation of the OAuth2 authentication flow, making it easy to integrate the process with your app.
  • Access to the Salesforce REST API, with utility classes that simplify that access.

Let’s get going! You must complete the Set Up Your Mobile SDK Development Environment project before moving on in this unit. If you haven’t, make a quick detour to that project to make sure that your development environment is properly configured. Once you’re ready to move forward, you start by creating a simple native Android app. After a quick look under the hood, you hone your Android development chops by modifying the app’s REST interactions and user interface.

Creating a Connected App

Important

Important

The Salesforce Mobile SDK Basics module, which includes connected app instructions, is a prerequisite to this module. You can’t complete this unit's challenge without creating a connected app.

To connect to the Salesforce service, every mobile app requires a Salesforce connected app. A connected app authorizes your app to communicate with Salesforce and securely access Salesforce APIs.

After you create and save your connected app, notice its details.
  • Copy the Callback URL and Consumer Key values. You use these values to set up authentication in your app.
  • Mobile SDK apps do not use the consumer secret, so you can ignore this value.

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 forcedroid app.

  1. At the command prompt, change to the directory where you want to store your project, then type:
    forcedroid create
  2. As forcedroid prompts you, 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, a congratulatory message appears, and 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 doesn’t do a lot—it just executes a simple SOQL query and displays 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 it creates 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.

  1. From your project directory, open app\res\values\bootconfig.xml in a UTF-8 compliant text editor. (Windows hint: The factory default ANSI encoding of notepad.exe isn’t going to work. You can, however, use the Notepad Save As dialog box to choose UTF-8 encoding.)
  2. Replace the remoteAccessConsumerKey value with the consumer key from your connected app.
  3. Similarly, replace the oauthRedirectURI value with the callback URL from your connected app.
  4. Save the file in UTF-8 encoding.
You’ve done it! You’ve successfully created and personalized an Android native app that uses Mobile SDK. Time to build and run it with Android Studio.
  1. Launch Android Studio.
  2. From the welcome screen, select Import project (Eclipse ADT, Gradle, etc.). Or, if Android Studio is already running, select File | New | Import Project.
  3. Browse to your target directory—TrailAndroidApps—and click OK.

    Android Studio automatically builds your workspace. This process can take several minutes. When the status bar reports “Gradle build successful”, you’re ready to run the project.

  4. Click Run | Run app, or press SHIFT+F10 (Windows) or ^R (Mac).
  5. Select an emulator or connected device that’s running Android SDK API 23 or later (up to Android 10 (API 29)).

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

在 Salesforce 帮助中分享 Trailhead 反馈

我们很想听听您使用 Trailhead 的经验——您现在可以随时从 Salesforce 帮助网站访问新的反馈表单。

了解更多 继续分享反馈