Setting up the Connect SDK for Android app development
To set up the Connect SDK in Android Studio so that its API can be used in Android mobile apps that are being developed, follow the instructions provided in the sections below.
- Prerequisites
- Create an Android Application project
- Add the Connect SDK library
- Verify that the Connect SDK library is configured (optional)
Prerequisites
Before you start developing an Android app that uses the Connect SDK, make sure that you have access to the following items:
- Android Studio 1.2 or later
- Android SDK with API level 14 or later
- Android device or Android emulator
Additionally, do the following tasks:
- Read the Connect SDK overview article.
- Read the Connect SDK JavaDocs, which describe the API.
Create an Android application project
To create an Android application project that is able to use the Connect SDK, do the following steps:
- In Android Studio, click to start the New Project wizard.
- Select the
Phone and Tablet
platform. - Select
Minimum SDK API 14 or higher
.
Add the Connect SDK library
To set up the Connect SDK in the Android Studio environment, do the following steps:
- Copy the Connect SDK library file,
connectsdk.pegaapi-3.1.0.0.aar
, that you have received from Pegasystems to the/libs
folder in your Android module. By default, the module name is 'app' and the path that is relative to your project root directory consists ofapp/libs
. - Open the
build.gradle
file in the Android module (not the one at the top level of the project) and add the following code to it. Additionally, append the Connect SDK and its dependencies to the 'dependencies' section at the top level of the file:repositories { (...) flatDir { dirs 'lib' } } (...) dependencies { (...) compile 'com.pega:connectsdk.pegaapi-3.2.0.0@aar' compile 'com.android.support:support-v4:26.1.0' compile 'com.android.support:appcompat-v7:26.1.0' compile 'com.google.android.gms:play-services-gcm:11.8.0' compile 'com.fasterxml.jackson.core:jackson-databind:2.1.4' compile 'com.google.dagger:dagger:2.0' compile 'com.squareup.okhttp3:okhttp:3.8.1' }
- Compile and run the application.
Verify that the Connect SDK library is configured
You can now test whether the Connect SDK has been correctly configured in Android Studio by adding the following code to the Android app that you are creating.
- Create the code for the
PegaSingleton
class. Set the Pega API URL in an instance of aConfiguration
object by replacing the <server URL> string with your Pega Platform application instance URL.import com.pega.connectsdk.pegaapi.Pega; public class PegaSingleton { private static final String PEGA_URL = "https://server_url_address"; private static final PegaSingleton INSTANCE = new PegaSingleton(); private Pega pega; private PegaSingleton() { this.pega = Pega.create(); this.pega.configuration().setServerUrl(PEGA_URL); } public static Pega getInstance() { return INSTANCE.pega; } }
- In the
MainActivity
class, initialize this new object inside theonCreate()
method. Also make sure to invoke theauthenticate()
method.< br/>@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); authenticate(); }
- Define the
authenticate()
method in theMainActivity
class. Remember that before you call the Connect SDK, you must authenticate by replacing the <user name> and <password> strings in the following code with your Pega Platform application credentials:private void authenticate() { Pega pega = PegaSingleton.getInstance(); if(!pega.authentication().isLoggedIn()) { Authentication.LoginCallback loginCallback = new Authentication.LoginCallback() { @Override public void onSuccess(){ // pega instance is ready to use } @Override public void onFailure(ErrorResponse errorResponse) { // handle authentication error here } } pega.authentication().login("", "", loginCallback); } }