đź“Ł Attention Salesforce Certified Trailblazers! Link your Trailhead and Webassessor accounts and maintain your credentials by December 14th. Learn more.
close
trailhead

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 project 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! We assume you’ve completed the Set Up Your Mobile SDK Development Environment project. 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

Are you ready to create your first forcedroid app? This video shows how to create and build a Mobile SDK native project on Android:

  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 whole 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. In a UTF-8 compliant text editor, open \<appname>\res\values\bootconfig.xml from your project’s target directory. (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 MyTrailNative, or press SHIFT+F10.
  5. Select an emulator or connected device that’s running Android SDK API 19 or later (up to Android Oreo (API 27)).

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

retargeting