- 26 Sep 2023
- Print
- DarkLight
How to integrate the SDK in your application
- Updated on 26 Sep 2023
- Print
- DarkLight
v3
Introducing the software documentation and the demo app!
The software documentation provides elaborated information on every class, function and member in the SDK. Please choose the relevant SDK version:
v3.1.5 | v3.1.4 | v3.1.3 | v3.1.2 | v3.1.1 | v3.1.0 | v3.0.6 | v3.0.5 | v3.0.4 | v3.0.3 | v3.0.2 | v3.0.1 | v3.0.0 |
---|
The demo app gives you a general idea on how you should implement the SDK in you app. To download, please follow those steps:
- Open the demo app repo.
- Download the content of the repo.
- Unzip the file and open the Android demo app.
Prerequisites
The following are required to install the SDK:
- Administrative access to the PerimeterX Portal to:
- Retrieve the PerimeterX application ID (AppID).
- Set the token expiration and validity.
- An active PerimeterX Enforcer.
Permissions and dependencies
Add the following permissions to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
Adding PerimeterX SDK to your project
You can add the SDK to your project with one of the following options:
JFrog
- Add the following repository to your
build.gradle
andsettings.gradle
:
maven { url 'https://perimeterx.jfrog.io/artifactory/px-Android-SDK/' }
maven { url = uri("https://perimeterx.jfrog.io/artifactory/px-Android-SDK/") }
- Add the following dependency to your
build.gradle
and set the PerimeterX Android SDK version:
implementation 'com.perimeterx.sdk:msdk:<Version>'
implementation("com.perimeterx.sdk:msdk:<Version>")
Manual
- Download the AAR file from https://perimeterx.jfrog.io/ui/repos/tree/General/px-Android-SDK or use the following command line:
curl -LO https://perimeterx.jfrog.io/artifactory/px-Android-SDK/com/perimeterx/sdk/msdk/3.1.5/msdk-3.1.5.aar
- Put the
PerimeterX-release.aar
in the libs folder of your app. - Add the following dependency to your
build.gradle
:
implementation files('libs/PerimeterX-release.aar')
- Add the following dependencies to your
build.gradle
file (please refer to the relevant SDK version):
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
implementation 'com.google.android.gms:play-services-instantapps:18.0.1'
implementation 'io.ktor:ktor-client-core:2.2.4'
implementation 'io.ktor:ktor-client-okhttp:2.2.4'
implementation 'com.fasterxml.uuid:java-uuid-generator:4.0.1'
From v3.1, you should also add the followingdependencies:
implementation "androidx.datastore:datastore-preferences:1.0.0"
- The SDK depends on few AndroidX libraries. If your project does not includes AndroidX, you should add the following dependencies:
implementation 'androidx.lifecycle:lifecycle-common-java8:2.5.1'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
- The "Doctor App" feature requires the AndroidX's fragment library. If your project does not includes AndroidX, you should add the following dependency:
implementation 'androidx.fragment:fragment:1.5.4'
When using this tool it could sometimes cause an issue with ktor library which included in the SDK.
To prevent this issue, we recommend addting the following rule in your proguard file:
-keepclassmembers class kotlinx.** {
volatile <fields>;
}
How to start the SDK
Starting the SDK should be the first thing that runs in your app. Therefore, you should start it in your Application
class:
- Import the SDK:
import com.perimeterx.mobile_sdk.PerimeterX
import com.perimeterx.mobile_sdk.PerimeterXDelegate
import com.perimeterx.mobile_sdk.main.PXPolicy
import com.perimeterx.mobile_sdk.PerimeterX;
import com.perimeterx.mobile_sdk.PerimeterXDelegate;
import com.perimeterx.mobile_sdk.main.PXPolicy;
- Make your
Application
class to implement thePerimeterXDelegate
(optional).
class MainApplication: Application(), PerimeterXDelegate
class MainApplication extends Application implements PerimeterXDelegate
- Create and configure the
PXPolicy
object, in theApplication
'sonCreate
function.
val policy = PXPolicy()
// configure the policy instacne
PXPolicy nativePolicy = new PXPolicy();
// configure the policy instacne
- It's recommended to tell the SDK which domains it should intercept. Not setting domains will cause the SDK to intercept URL requests from ALL domains, including 3rd party libraries.
policy.setDomains(arrayListOf("my-domain.com"), "<APP_ID>")
ArrayList<String> domains = new ArrayList<>();
domains.add("my-domain.com");
policy.setDomains(domains, "<APP_ID>");
- Call the
PerimeterX/start(application:appId:delegate:policy:)
function, with your application, AppID and the policy, in theApplication
'sonCreate
function.
try {
PerimeterX.start(this, "<APP_ID>", this, policy)
}
catch (exception: Exception) {
println("failed to start. error: ${exception.message}")
}
try {
PerimeterX.INSTANCE.start(this, "<APP_ID>", this, policy);
}
catch (Exception exception) {
Log.e("tag","failed to start. error: " + exception.getMessage());
}
- Add the PerimeterX's interceptor (
PXInterceptor
) to yourHttpClient
's application interceptors list at the end. This is required for the "Automatic Interception".
OkHttp:
private var okHttpClient: OkHttpClient = OkHttpClient.Builder()
.addInterceptor(MyInterceptor())
.addInterceptor(PXInterceptor()) // MUST BE THE LAST INTERCEPTOR IN THE CHAIN
.build()
private final OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(new MyInterceptor())
.addInterceptor(new PXInterceptor()) // MUST BE THE LAST INTERCEPTOR IN THE CHAIN
.build();
ktor:
private val httpClient: HttpClient = HttpClient(OkHttp) {
engine {
addInterceptor(MyInterceptor())
addInterceptor(PXInterceptor()) // MUST BE THE LAST INTERCEPTOR IN THE CHAIN
}
}
PerimeterX/start(appId:delegate:policy:)
function set up the session for a given AppID. It's essential to call this function as early as possible in your application and before any URL request to your server.Adding custom parameters
You may add custom parameters for additional configuration.
try {
PerimeterX.setCustomParameters(hashMapOf("custom_param1" to "hello", "custom_param2" to "world"))
}
catch (exception: Exception) {
println("error: ${exception.message}")
}
try {
PerimeterX.INSTANCE.setCustomParameters(customParameters, null);
}
catch (exception: Exception) {
Log.e("tag","failed to set custom parameters. error: " + exception.getMessage());
}
Summary
After writing the code above, the SDK will:
- Intercept your URL requests.
- Add SDK's HTTP headers to those URL requests.
- Handle block responses from the server by presenting a challenge to the user.
Next steps
Review the following topics:
- Integration verification and testing
- Manual integration
- Handle block responses from the server
- React Native support
- Hybrid App support
- Account Defender
- Multiple AppIDs support
- Migrating from earlier SDK versions