Getting Started

Quickstart guide for Android projects using Keyless.

β€‹πŸš€ Lets get started!

In this Getting Started guide, we will add the Keyless SDK to your Android mobile application project. This will allow you to enroll and authenticate users with the Keyless protocol.

Prerequisites

  • Android Studio 3.5 or later

  • Your project must target API level 23 or later

  • Set up a physical Android device for running your app.

Once you're ready with the prerequisites, let's get started.

β€‹πŸ‘¨πŸ”§ Installation

Add the following within the repositories section of the build.gradle file of your Android application. Replace the YOUR_CLOUDSMITH_TOKEN text with the CloudSmith token for partners provided to you by Keyless:

allprojects {
repositories {
google()
jcenter()
​
maven {
url "https://dl.cloudsmith.io/YOUR_CLOUDSMITH_TOKEN/keyless/partners/maven/"
}
}
}

Add the following to the dependencies block of your project build.gradle file (typically app/build.gradle):

app/build.gradle
dependencies {
// ...
implementation 'io.keyless:keyless-sdk:+'
}

Add the following to the android block of the same file:

app/build.gradle
android {
// ...
aaptOptions {
noCompress "tflite"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
​
// add the following only if you're using Kotlin
kotlinOptions {
jvmTarget = "1.8"
}
}

β€‹πŸ›Έ Implementation

The final step is to prepare your application to interact with the Keyless SDK. To do this, first create a new Kotlin class that will implement the methods of the KeylessDelegate used by the Keyless SDK to communicate progress back to your application. Through this quickstart, we will implement the application login within the methods of this delegate.

// Mandatory methods
​
fun enrollmentFinish(
keylessID: String?,
enrollmentResponse: String?,
error: KeylessException?
)
fun authenticationFinish(token: String?, error: AuthenticationFailed?)
fun deEnrollmentFinish(deEnrollmentResponse: String?, error: DeEnrollmentFailed?)
​
// Optional methods
​
fun authenticationStart()
fun deEnrollmentStart()
fun enrollmentProgress(progress: Float)
fun enrollmentStart()

An example implementation of a KeylessDelegateCustom class is here.

KeylessDelegateCustom.kt
import android.util.Log
import io.keyless.sdk.api.AuthenticationFailed
import io.keyless.sdk.api.DeEnrollmentFailed
import io.keyless.sdk.api.KeylessDelegate
import io.keyless.sdk.api.KeylessException
​
class KeylessDelegateCustom : KeylessDelegate {
​
private var TAG = "KEYLESS_DELEGATE"
​
// Mandatory methods
​
override fun authenticationFinish(token: String?, error: AuthenticationFailed?) {
if (error != null) {
Log.e(TAG, "Authentication failed: " + error.message)
return
}
​
// Handle successful authentication
}
​
override fun deEnrollmentFinish(deEnrollmentResponse: String?, error: DeEnrollmentFailed?) {
if (error != null) {
Log.e(TAG, "DeEnrollment failed: " + error.message)
return
}
​
// Handle successful de-enrollment
}
​
override fun enrollmentFinish(
keylessID: String?,
enrollmentResponse: String?,
error: KeylessException?
) {
if (error != null) {
Log.e(TAG, "Enrollment failed: " + error.message)
return
}
​
// Handle successful enrollment
}
​
// Optional methods
​
override fun authenticationStart() {
super.authenticationStart()
Log.i(TAG, "Authentication started.")
}
​
override fun deEnrollmentStart() {
super.deEnrollmentStart()
Log.i(TAG, "DeEnrollment started.")
}
​
override fun enrollmentProgress(progress: Float) {
super.enrollmentProgress(progress)
Log.i(TAG, "Enrollment progress: $progress.")
}
​
override fun enrollmentStart() {
super.enrollmentStart()
Log.i(TAG, "Enrollment started.")
}
}

β€‹πŸ“± Initialisation

Now you are ready to interact with the Keyless SDK from your Activity.

Import the Keyless SDK module:

import io.keyless.sdk.api.Keyless

Configure the Keyless class, typically in your Application’s onCreate method, or in onCreate of any Activity. Pass in YOUR_KEYLESS_API_KEY and the class that will conform to KeylessDelegate the KeylessDelegate protocol (for instance, the KeylessDelegateCustom class you created before).

private var delegate = KeylessDelegateCustom()
​
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
​
try {
Keyless.configure(this, "YOUR_KEYLESS_API_KEY", delegate, 0)
} catch (e: Exception) {
e.printStackTrace()
}
}

Example implementation:

package io.keyless.keylesssdkintegration
​
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
​
import io.keyless.sdk.api.Keyless
​
class MainActivity : AppCompatActivity() {
private var delegate = KeylessDelegateCustom()
​
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
​
try {
Keyless.configure(
this, "YOUR_KEYLESS_API_KEY",
delegate, 0
)
} catch (e: Exception) {
e.printStackTrace()
}
​
setContentView(R.layout.activity_main)
​
}
}