Skip to content

Initializing the SDK

Narendra Prabhu Gurusiddappa edited this page Apr 23, 2019 · 13 revisions

In Getting started with the Community Android SDK, we describe how to create an API app key and secret and other required credentials in Community Admin. You pass these credentials during initialization to make the SDK available to your app. Initialization is required to use our core features and our Support UI components.

The SDK includes the following utility method on LiSDKManager for initialization:

  • LiSDKManager.isInitialized() - To check if the the SDK is initialized correctly

Initialization steps include:

Adding your credentials

Add the credentials listed in Community Admin > System > API Apps and your community URL to /res/values/credentials.xml. LiAppCredentials uses these strings when you initialize the SDK. If you are using Community v19.1 and above, there is an option to download these credentials from the Community Admin > System > API Apps page as credentials.xml, which can be placed directly under res/values folder of your Android project.

    <string name="li_sender_id"><!--The project ID from the Firebase project settings --></string>
    <string name="li_client_id"><!--Client ID from Api Apps--></string>
    <string name="li_client_secret"><!--Client Secret from Api Apps--></string>
    <string name="li_community_url"><!--Community URL--></string>
    <string name="li_tenant_id"><!--Community tenant ID--></string>
    <string name="li_client_name"><!--The app name from Api Apps--></string>

Adding initialization code

Every app that uses the Community Android SDK must initialize the SDK using LiAppCredentials.

Here is an example of app initialization. To initialize the SDK, use something like the following snippet in the Main Activity or wherever other app dependencies are initialized. Notice that the snippet uses a method called getInstanceId(). We will talk more about this method in the next section.

try {
    LiAppCredentials credentials = new LiAppCredentials(
            getString(R.string.li_client_name),
            getString(R.string.li_client_id),
            getString(R.string.li_client_secret),
            getString(R.string.li_tenant_id),
            getString(R.string.li_community_url),
            getInstanceId() /* check ahead for more information on `getInstanceId(): String` */
    ); 
    LiSDKManager.initialize(this, credentials);
    LiSDKManager.getInstance().syncWithCommunity(this);
} catch (LiInitializationException e) {
    Log.e(MainApplication.class.getSimpleName(), "Lithium SDK initialization failed.");
    e.printStackTrace();
}

About getInstanceId()

Our example initialization code uses a method called getInstanceId(). This method is a unique static identifier for the device. Use a globally-unique identifier (GUID) or instance ID scoped to 'Single app' with 'Install-reset' resettability and persistence. Example: UUID.randomUUID().toString() persisted in a Shared Preferences.

Here is a reference implementation:

String getInstanceId() {
    String instanceId;

    // get it from the shared preference
    SharedPreferences preferences = getSharedPreferences("default", MODE_PRIVATE);
    instanceId = preferences.getString(INSTANCE_ID, null);

    if (TextUtils.isEmpty(instanceId)) { // if it is null
        instanceId = UUID.randomUUID().toString();  // create a new instance id
        preferences.edit().putString(INSTANCE_ID, instanceId).apply(); // save it in Shared Preferences
    }

    return instanceId;
}

Syncing the app with Community

After successful initialization, and before making any REST calls to the Community API, you must update the SDK settings with Community server using:

LiSDKManager.getInstance().syncWithCommunity(context);

Note: If you are using a Community Android SDK version earlier than 1.2.5, synching with the Community is done as part of initialization and calling syncWithCommunity() is not required.

Clone this wiki locally