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.
Before You Start: Create an External Client App
Before you complete any steps in this badge, make sure you complete the hands-on challenges (HOCs) in the Set Up Your Mobile SDK Development Environment and the Salesforce Mobile SDK Basics badge, using the same Trailhead Playground or Developer Edition. You can’t complete the challenges in this badge without configuring your mobile development environment and creating an external client app in those badges first.
To connect to the Salesforce service, every mobile app requires a Salesforce external client app. An external client app authorizes your app to communicate with Salesforce and securely access Salesforce APIs.
After you create and save your external client 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.
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 badge, 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! Remember, you must complete the Set Up Your Mobile SDK Development Environment project and the Salesforce Mobile SDK Basics badgebefore moving on in this unit. If you haven’t, make a quick detour now. Once you’re ready to move forward, you start by creating a simple native Android app. After a quick look under the hood, you home your Android development chops by modifying the app’s REST interactions and user interface.
Create an Android Project
Now that you’ve set up your Android development environment and created a Salesforce external client 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 name: MyTrailNative
- Package name: com.mytrail.android
- Organization name: MyApps, Inc.
- Output directory: TrailAndroidApps
- OAuth consumer key: YOUR_CONSUMER_KEY
- OAuth callback URL: YOUR_CALLBACK_URL
- Login server URL: https://login.salesforce.com
After a sequence of log messages, a congratulatory message appears, and you’re returned to the command prompt. Congratulations! Your new app is now ready to run.
Run 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!
- Launch Android Studio.
- Select File | New | Import Project.
- 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 Run | Run app, or press SHIFT+F10 (Windows) or ^R (Mac).
- Select an emulator or connected device that’s running Android 12 (API 31) or later.
Android Studio launches your app in the emulator or on your connected Android device.
Resources