Skip to main content

Android Elements SDK

Android Elements SDK

basistheory-android

The Basis Theory Android SDK makes it easy to build secure Android applications that collect or reveal sensitive data using Elements.

Get started with our guide, explore our example application, or continue reading the reference docs.

Before You Begin

This SDK requires the use of an API Key associated with a Public Application, which only allows token:create or token:update permissions to mitigate the risk that these API keys may be publicly exposed within your frontend applications.

To create one, login into our Portal and create a new "Public" Application with the permissions you require.

Installation

Requirements

  • Android 5.0+ (API level 21+)
  • AndroidX

Gradle

Add the SDK dependency to your project's build file:

repositories {
maven { url 'https://jitpack.io' }
}

dependencies {
implementation 'com.github.basis-theory:basistheory-android:<version>'
}

The latest release version can be found in GitHub.

If your build configuration enables code shrinking or obfuscation, these ProGuard rules are needed to use this SDK.

Additionally, the usage of anonymous objects to call some SDK methods such as tokenize, might lead to unexpected behavior. Creating dedicated classes along with ProGuard rules to preserve them solves the problem.

Basic Usage

Simply include one or more elements within your application's views:

<com.basistheory.android.view.CardNumberElement
android:id="@+id/card_number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<com.basistheory.android.view.CardExpirationDateElement
android:id="@+id/expiration_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<com.basistheory.android.view.CardVerificationCodeElement
android:id="@+id/cvc"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Then tokenize the user input by referencing these elements. This can be wired up in response to a button click, or any other user action.

val cardNumberElement = findViewById(R.id.card_number)
val cardExpirationDateElement = findViewById(R.id.expiration_date)
val cardVerificationCodeElement = findViewById(R.id.cvc)

val bt = BasisTheoryElements.builder()
.apiKey(myPublicApiKey)
.build()

runBlocking {
val tokenizeResponse = bt.tokenize(object {
val type = "card"
val data = object {
val number = cardNumberElement
val expiration_month = cardExpirationDateElement.month()
val expiration_year = cardExpirationDateElement.year()
val cvc = cardVerificationCodeElement
}
})

// send the tokens within tokenizeResponse to your backend
}

A full example Android app is included within the example module within the GitHub repo or explore all the supported Element Types.