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
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.
- 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.
- At the command prompt, change to the directory where you want to store your project,
then type:
forcedroid create
- 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.
- 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.)
- 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.
- From the welcome screen, select Import project (Eclipse ADT, Gradle, etc.). Or, if Android Studio is already running, select .
- 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.
- Click , 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 launches your app in the emulator or on your connected Android device.