How to Integrate the Kount SDK 4.2.x into an Android App

Integrate the Kount Analytics SDK into your Android application to collect additional end-user information. This assists in detecting suspicious devices and preventing fraud. Software for this integration can be found in this repository. Contact Kount support if you do not have access.

Note: This SDK’s minimum supported version is Android 5 (Android Lollipop). Refer to Kount documentation for more information.

Using the SDK as a library in your app

  1. In Android Studio, copy the JAR file provided with this documentation into the libs folder of your application (“Project Path”\app\libs).

  2. In the Project viewer, right-click the JAR file, and then click Add as library.

Using the SDK as a gradle plugin in your app

  1. In your root build.gradle, at the end of repositories, add the following maven dependency:

        allprojects {
           repositories {
         ...
              maven {
         url "https://jitpack.io"
            }
              }
        }
        
  2. In the gradle file for your app, add the following dependencies:

        dependencies {
           ...
           implementation 'com.github.Kount:kount-android-sdk-source:4.2.1'}
        

Integrating the Kount SDK into your Android App

The following steps are common for both scenarios mentioned above.

  1. In the build.gradle file of your app add the dependencies listed below:

        dependencies {
             ...
                  implementation 'com.google.android.instantapps:instantapps:1.1.0' implementation 'com.google.code.gson:gson:2.8.6'
                  implementation group: 'com.android.volley', name: 'volley', version: '1.1.1' implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.72'
                  implementation "com.google.android.material:material:1.4.0"
             }
        
  2. To enable Location Collection support to detect devices in suspicious locations, add the following permissions to your application’s AndroidManifest.xml under the <manifest> tag:

        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        

    Note

    While preferred, not all apps are approved in the app store to use the ACCESS_FINE_LOCATION. If your app has been rejected for having this permission, replace ACCESS_FINE_LOCATION (uses GPS) with ACCESS_COARSE_LOCATION (uses cellular triangulation).

  3. There are two ways to enable SDK support: without custom application class and with custom application class.

    • For customers who do not use custom application class: add the following to your application’s AndroidManifest.xml under the <application> tag to enable SDK support:

              <application
                   android:name="com.kount.api.analytics.KountAnalyticsApplication"
                        ...>
                        ...
              </application>
              
    • For customers who use custom application class: add Application.ActivityLifecycleCallbacks in your application class to enable to enable SDK support.

      import com.kount.api.analytics.AnalyticsApplication
      import com.kount.api.analytics.enums.EventEnums
      class CustomerApplication: Application(), Application.ActivityLifecycleCallbacks {
          override fun onCreate() {
              AnalyticsApplication.init()
              registerActivityLifecycleCallbacks(this)
              super.onCreate()
          }
          override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle ? ) {
      
          }
          override fun onActivityStarted(activity: Activity) {
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_STARTED, activity)
          }
          override fun onActivityResumed(activity: Activity) {
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_RESUMED, activity)
          }
          override fun onActivityPaused(activity: Activity) {
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_PAUSED, activity)
          }
          override fun onActivityStopped(activity: Activity) {
              AnalyticsApplication.registerKountEvents(EventEnums.EVENT_STOPPED, activity)
          }
          override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {}
          override fun onActivityDestroyed(activity: Activity) {}
      }
  4. In the main activity of your application, you must initialize the collector and assign the<MERCHANT_ID> you received from Kount. Optionally, you can provide a sessionID if you need to generate your own (if not, the SDK generates one for you). You must send this sessionID back to your severs in order to make calls to the Kount API (see the Kount API section below).

    Add the following initialization in your MainActivity.

    Your MainActivity must implement ActivityCompat.OnRequestPermissionsResultCallback and override method onRequestPermissionsResult(requestCode, permissions, grantResults), as listed below.

    Kotlin

        import com.kount.api.analytics.AnalyticsCollector
        …
        override fun onCreate(savedInstanceState: Bundle?) {
        ...
        // REQUIRED SECTION
        AnalyticsCollector.setMerchantId(<MERCHANT_ID>)
        // END REQUIRED SECTION
        
        //This turns the alpha collections on(true)/off(false). It defaults to true.
        AnalyticsCollector.collectAnalytics(true)
        
        //For production need to add AnalyticsCollector.ENVIRONMENT_PRODUCTION.
        AnalyticsCollector.setEnvironment(AnalyticsCollector.ENVIRONMENT_TEST)
        
        // OPTIONAL SESSION_ID SECTION
        val deviceSessionID = UUID.randomUUID().toString()
        AnalyticsCollector.setSessionId(deviceSessionID)
        //END OPTIONAL SESSION_ID SECTION
        
        //Request location permission for Android 6.0 & above
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        Utils.requestLocationPermission(this)
        else
        AnalyticsCollector.collectDeviceDataForSession(this)
        ...
        }
        
        override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        if (requestCode == AnalyticsCollector.REQUEST_PERMISSION_LOCATION) {
        AnalyticsCollector.collectDeviceDataForSession(this)
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        }
        

    Java

        import com.kount.api.analytics.AnalyticsCollector;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        ...
        // REQUIRED SECTION
        AnalyticsCollector.setMerchantId(<MERCHANT_ID>);
        // END REQUIRED SECTION
        
        //This turns the alpha collections on(true)/off(false). It defaults to true.
        AnalyticsCollector.collectAnalytics(true);
        
        //For production need to add AnalyticsCollector.ENVIRONMENT_PRODUCTION 
        AnalyticsCollector.setEnvironment(AnalyticsCollector.ENVIRONMENT_TEST);
        
        // OPTIONAL SESSION_ID SECTION
        final String deviceSessionID UUID.randomUUID().toString(); 
        AnalyticsCollector.setSessionId(deviceSessionID);
        // END OPTIONAL SESSION_ID SECTION
        
        //Request location permission for Android 6.0 & above
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        Utils.requestLocationPermission(this);
        else{
        AnalyticsCollector.collectDeviceDataForSession(this);
        }
        ...
        }
        
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == AnalyticsCollector.REQUEST_PERMISSION_LOCATION) {
        AnalyticsCollector.collectDeviceDataForSession(this);
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
        }
        
  5. To support Location Collection in Android API 23 and above, add the following permissions to your main activity, onCreate method.

    Kotlin

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
        ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), AnalyticsCollector.REQUEST_PERMISSION_LOCATION)
        } else {
        ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), AnalyticsCollector.REQUEST_PERMISSION_LOCATION)
        }
        }
        else{
        AnalyticsCollector.collectDeviceDataForSession(activity)
        }
        

    Java

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AnalyticsCollector.REQUEST_PERMISSION_LOCATION);
        } else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, AnalyticsCollector.REQUEST_PERMISSION_LOCATION);
        }
        }
        else{
        AnalyticsCollector.collectDeviceDataForSession(activity)
        }
        
  6. To track login session data, add successful logins:

    Kotlin

    AnalyticsCollector.trackLoginEvent(true)

    Java

    AnalyticsCollector.trackLoginEvent(true);
  7. Almost all Kount API calls require your Kount assigned merchantID and a sessionID. You must send the sessionID created earlier back to your servers. This can be done at any time prior to the Kount API calls being made (from your servers to ours). For example, if you are using Kount Control and plan to evaluate logins, you would include the sessionID along with the user's login credentials to your servers. This sessionID is accessible as a global variable.

    This sessionID is accessible as a global variable:

    Kotlin

    …
    val sessionId = AnalyticsCollector.getSessionId()
    // TODO add sessionId in posting to server here
    …

    Java

    …
    
    String sessionId = AnalyticsCollector.getSessionId();
    
    // TODO add sessionId in posting to server here
    
    …
  8. Build and run.

Was this article helpful?
1 out of 4 found this helpful