• December 14 2015
  • Small edit
Changed Getting Started: Mobile SDK for Android

Getting Started with the Mobile SDK for Android

Easily integrate LinkedIn into your Android applications

  • Download Mobile SDK for Android

Overview

The mobile SDK for Android increases your app's time to market by providing out-of-box support for LinkedIn natively inside your Android applications. This allows you to boost your sign in conversion rates and provides you more time to work on the things that matter.

The SDK provides:

  • Single sign-on (SSO) authentication, in conjunction with the LinkedIn mobile app.
  • A convenient wrapper for making authenticated calls to LinkedIn's REST APIs.
  • "Deep linking" to additional member data in the LinkedIn mobile app.
  • Sample application that demonstrate best-practice implementations of all of the SDK's features.

This guide will walk you through the key concepts behind the new Mobile SDK and the steps required to get the included sample application up and running so you can start experimenting with the SDK as soon as possible.

Download required resources

Get the Mobile SDK for Android

Download the latest release of LinkedIn's Mobile SDK for Android here.

Get the LinkedIn APK

The Mobile SDK for Android requires the official LinkedIn Android application is also installed to support the SDK's capabilities.

Due to Google Play store limitations on emulated Android devices, you will very likely want to acquire the LinkedIn APK file and manually install on virtual devices it for a smoother development and debugging experience.  Download the latest release of the LinkedIn APK.

Setting up your development environment

Download and install an IDE

There are several popular IDE choices available for Android development, including Eclipse with the Android Developer Tools plugin, JetBrains IntelliJ IDEA, and Google's Android Studio.   

Install the required Android SDK dependencies

Wroking with the SDK and sample applications requires that you have the Android SDK and build tools installed in your environment.  The minimum supported version is Android 4.4.2 (API 19).

Building the Sample Application

The following instructions detail how to build the LinkedIn Mobile SDK and associated sample projects within the Android Studio environment.  If you choose to use a different IDE for your development, you will have to adapt these steps accordingly:

  1. Launch Android Studio and select "Import Non-Android Studio project"
  2. Select the build.gradle file in the root of the unzipped Mobile SDK for Android archive.
  3. From the top navigation menu, select Build -> Make Project

You should now be able to run the Sample App and Events App projects.  Remember that the Mobile SDK-enabled applications require the official LinkedIn Android app to be installed on the device to function properly.  If you're running the sample apps on a connected physical device, you can get the LinkedIn app from your app store.  If you want to run the sample apps on an emulated Android device, use your emulated device's browser to surf to the developer website and download the LinkedIn APK file.

For your convienence, there is a pre-built APK version of the Events App that is included in the SDK available for download for you to see without needed to build it yourself.

Associate your Android app with your LinkedIn app

You must identify your specific Android app with LinkedIn by adding some information about it to your LinkedIn application's settings.

If you have not already done so, create an application. If you have an existing application, select it to modify its settings.  Along with the other required values, ensure that you have filled out the "Android Package Names and Hashes" field, using the following information:

There are two types of key hashes: debug (i.e. development) and release (i.e. production).

  • Debug keys are required for LinkedIn to verify the authenticity of your application while it interacts with our APIs during your development cycle.  
  • A release key is required because all Android applications must be signed with one before they can be uploaded to the Play store for distribution.

We strongly encourage you to read Google's official Signing Your Applications documentation, specifically the sections regarding "Signing Considerations" and "Securing Your Private Key", to ensure that you fully understand the Android application signing process.

Generating a debug key hash value

Use the following command to generate a debug key hash value.  The location of your debug key store will differ depending on whether you are developing on a Windows or Mac platform.  If you do not have OpenSSL installed, you can download it for Windows or Mac/Unix.

Windows
keytool -exportcert -keystore %HOMEPATH%\.android\debug.keystore -alias androiddebugkey | openssl sha1 -binary | openssl base64
Mac/Unix
keytool -exportcert -keystore ~/.android/debug.keystore -alias androiddebugkey | openssl sha1 -binary | openssl base64

Generating a release key hash value

To generate a release key hash value, use the following command:

keytool -exportcert -keystore YOUR_RELEASE_KEY_PATH -alias YOUR_RELEASE_KEY_ALIAS | openssl sha1 -binary | openssl base64

Configure the values

In the Mobile->Android Settings->"Package "Package Name & Package Hash" section field of your LinkedIn application's configuration, add your application's package and hash values: configuration under "Android Settings", provide one or more values in the following comma-separated format:

Android.Package.Name,Key-Hash-Value

For example:

Authenticating mobile users

Before you can use any of the functionality in the mobile SDK, you must create an authenticated LinkedIn session.  Learn more about how to authenticate LinkedIn users in the Android environment in the Authenticating with the Mobile SDK for Android guide.

Making Authenticated REST API calls

Once your users are authenticated, you can easily make calls to LinkedIn's REST API on their behalf.  The APIHelper object provides helper methods for all of the necessary HTTP verbs.  For complete details on all of the available API calls that can be made, consult the developer documentation.

APIHelper.getRequest()

Make an HTTP GET request to LinkedIn's REST API using the currently authenticated user's credentials.  If successful, a LinkedIn ApiResponse object containing all of the relevant aspects of the server's response will be returned.

This method takes the following arguments:

  • context - The Android Context that the API call is originating from. This value acts as a reference that will allow you to cancel this request in the future.
  • url - The URL for the LinkedIn REST API endpoint that you wish to call.
  • ApiListener - An implementation of ApiListener to handle the response from the API call.  The onApiSuccess() and onApiError() methods should be overridden to handle the result of the call in a manner that makes sense to your application.
java
String url = "https://api.linkedin.com/v1/people/~:(id,first-name,last-name)";
                
APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
apiHelper.getRequest(this, url, new ApiListener() {
    @Override
    public void onApiSuccess(ApiResponse apiResponse) {
        // Success!
    }

    @Override
    public void onApiError(LIApiError liApiError) {
        // Error making GET request!
    }
});

APIHelper.postRequest()

Make an HTTP POST request to LinkedIn's REST API using the currently authenticated user's credentials.  If successful, a LinkedIn ApiResponse object containing all of the relevant aspects of the server's response will be returned.

This method takes the following arguments:

  • context - The Android Context that the API call is originating from.  This value acts as a reference that will allow you to cancel this request in the future.
  • url - The URL for the LinkedIn REST API endpoint that you wish to call.
  • body - A JSONObjector String containing the body of the request.
  • ApiListener - An implementation of ApiListener to handle the response from the API call.  The onApiSuccess() and onApiError() methods should be overridden to handle the result of the call in a manner that makes sense to your application.
java
String url = "https://api.linkedin.com/v1/people/~/shares";

JSONObject body = new JSONObject("{" +
    "\"comment\": \"Sample share\"," +
    "\"visibility\": { \"code\": \"anyone\" }," +
    "\"content\": { " +
        "\"title\": \"Sample share\"," +
        "\"description\": \"Testing the mobile SDK call wrapper!\"," +
        "\"submitted-url\": \"http://www.example.com/\"," +
        "\"submitted-image-url\": \"http://www.example.com/pic.jpg\"" +
    "}" +
"}");
               
APIHelper apiHelper = APIHelper.getInstance(getApplicationContext()); 
apiHelper.postRequest(this, url, body, new ApiListener() {
    @Override
    public void onApiSuccess(ApiResponse apiResponse) {
        // Success!
    }

    @Override
    public void onApiError(LIApiError liApiError) {
        // Error making POST request!
    }
});

APIHelper.putRequest()

Make an HTTP PUT request to LinkedIn's REST API using the currently authenticated user's credentials.  If successful, a LinkedIn ApiResponse object containing all of the relevant aspects of the server's response will be returned.

This method takes the following arguments:

  • context - The Android Context that the API call is originating from.  This value acts as a reference that will allow you to cancel this request in the future.
  • url - The URL for the LinkedIn REST API endpoint that you wish to call.
  • body - A JSONObjector String containing the body of the request.
  • ApiListener - An implementation of ApiListener() to handle the response from the API call.  The onApiSuccess() and onApiError() methods should be overridden to handle the result of the call in a manner that makes sense to your application.
java
String url = "https://api.linkedin.com/v1/companies/1234/updates/key=ABCDE-123456/update-comments-as-company/";

JSONObject body = new JSONObject("{" +
    "\"comment\": \"Sample share\" +
"}");
               
APIHelper apiHelper = APIHelper.getInstance(getApplicationContext()); 
apiHelper.putRequest(this, url, body, new ApiListener() {
    @Override
    public void onApiSuccess(ApiResponse apiResponse) {
        // Success!
    }

    @Override
    public void onApiError(LIApiError liApiError) {
        // Error making POST request!
    }
});

Cancelling in-progress requests

During your application's workflow, you may wish to cancel any in-progress API requests.  This is done by calling APIHelper.cancelCalls() method.

This call is generally used when a user closes or otherwise modifies a UI element that is in the process of looking up LinkedIn member data.  When the current fragment or context is dismissed, your application's performance is increased by remembering to close any pending API requests it's waiting on that are no longer relevant to the new state of the application.

This method takes the following argument:

  • context - The Android Context that was provided to the original API call helper.  All in-progress API requests associated with this application context will be cancelled.
java
APIHelper apiHelper = APIHelper.getInstance(getApplicationContext());
apiHelper.cancelCalls(this);

Using ProGuard with your application

If you intend to use ProGuard on the release build of your mobile application, you will need to add the following lines to your project's proguard-project.txt file to preserve information required for the SDK to function properly:

proguard configuration
-keep class com.linkedin.** { *; }
-keepattributes Signature

"Deep Linking" to content inside the LinkedIn mobile application

The mobile SDK provides you the capability to "deep link" to a specific member's profile directly within the official LinkedIn mobile application.

Use one of the following two methods to trigger the profile to open:

DeepLinkHelper.openCurrentProfile()

This method will open the currently authenticated user's LinkedIn profile directly within the official LinkedIn mobile application. Users have the right to see more information about themselves than of other members, so you should use this method rather than openOtherProfile() with the current user's member ID, when trying to link a member to his or her own LinkedIn profile.

This method takes the following arguments:

  • activity - The Android Activity to return the user to if the user presses the "back" button on their device while inside the official LinkedIn application.
  • callback - An implementation of DeeplinkListener that will handle the response from the call.  The onDeepLinkSuccess() and onDeepLinkError() methods should be overridden to handle the result of the call in a manner that makes sense to your application.
java
// Get a reference to an activity to return the user to
final Activity thisActivity = this;

DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance();

// Open the current user's profile
deepLinkHelper.openCurrentProfile(thisActivity, new DeeplinkListener() {
    @Override
    public void onDeepLinkSuccess() {
        // Successfully sent user to LinkedIn app
    }

    @Override
    public void onDeepLinkError(LiDeepLinkError error) {
        // Error sending user to LinkedIn app
    }
});

DeepLinkHelper.openOtherProfile()

This method will open a specific member profile within the official LinkedIn application.  It takes the following arguments:

  • activity - The Android activity to return the user to if the user presses the "back" button on their device while inside the official LinkedIn application.
  • memberID - A String representation of the ID of the member whose profile you want to send the user to.  If you are using the currently authenticated member's ID, you should consider calling openCurrentProfile() to enable the display of additional information from the member's own profile, instead.
  • callback - An implementation of DeeplinkListener that will handle the response from the call.  The onDeepLinkSuccess() and onDeepLinkError() methods should be overridden to handle the result of the call in a manner that makes sense to your application.

The memberID is a value that is unique to your particular LinkedIn application, representing a specific LinkedIn member. You can retrieve the current user's id by making a REST API call to LinkedIn's Profile API using the SDK's API wrapper method.

Storing member ID values on your own server allows your application to direct users to the official LinkedIn profiles for any other users that have authorized your mobile application.

java
// Get a reference to an activity to return the user to
final Activity thisActivity = this;

// A sample member ID value to open
final String targetID = "abcd1234";

DeepLinkHelper deepLinkHelper = DeepLinkHelper.getInstance();

// Open the target LinkedIn member's profile
deepLinkHelper.openOtherProfile(thisActivity, targetID, new DeeplinkListener() {
    @Override
    public void onDeepLinkSuccess() {
        // Successfully sent user to LinkedIn app
    }

    @Override
    public void onDeepLinkError(LiDeepLinkError error) {
        // Error sending user to LinkedIn app
    }
});