The Mobile Protect SDK has a dedicated integration for for Flutter supports the iOS and Android apps built using Flutterplatforms. This document will take you through the steps to integrate the SDK into your build system.
This will involve some steps on the Flutter side as well as some steps on both iOS and Android.
Flutter Integration
Step 1
Download the SDK from the Mobile Protect in the Portal.
Step 2
Copy project. For other guides, check out https://datatheorem.atlassian.net/plugins/servlet/servicedesk/customer/confluence/shim/spaces/PKB/pages/1800142885
In order for Mobile Protect Flutter to correctly fetch its dependencies a Mobile Protect API key is needed. If you haven’t received an API key for Mobile Protect, please contact us.
Installation
Some steps are done in the pubspec.yaml
file while some of them are done in the native iOS and Android configuration files.
Step 1: add MobileProtect as a dependency
Mobile Protect is most commonly added as a dependency via pub.dev, in which case the plugin will be fetched automatically. This is the recommended approach. It is also possible to add the dependency via a local copy of the plugin. Please choose only one of the following options:
Option 1: adding Mobile Protect via pub.dev
Add the following entry to the pubspec.yaml
file in your project:
Code Block |
---|
dependencies:
//...
mobile_protect: ^24.0.0 |
Note: the latest version of the SDK can be seen here:
Alternatively, you can run the following command in your project’s root folder:
Code Block |
---|
$ flutter pub add mobile_protect |
Option 2: adding Mobile Protect via a local copy of the plugin
Download the Mobile Protect Flutter SDK from the Data Theorem portal, unzip it and copy the flutter_mobile_protect
directory to the root of the Flutter project. Then add the following entry to the pubspec.
Step 3
Add yaml
file in your project:
Code Block |
---|
dependencies: //... mobile_protect: path: flutter_mobile_protect |
...
Alternatively, you can run the following command in your project’s root folder:
Code Block |
---|
$ flutter pub add mobile_protect --path flutter_mobile_protect/ |
...
The project’s pubspec.yaml
should then contain the following entry:
Code Block | ||
---|---|---|
| ||
dependencies:
flutter_mobile_protect:
path: flutter_mobile_protect |
Step 4
In your main.dart
file
Import the Mobile Protect for Flutter library:
import 'package:flutter_mobile_protect/flutter_mobile_protect.dart';
Add the following initialiser method:
Code Block Future<void> initMobileProtect() async { try { await FlutterMobileProtect.init; } on PlatformException { print("Failed to initialize"); } }
Call
initMobileProtect()
when initialising your main App widget.
Android Integration
Step 1
Declare dependencies within your project’s build.gradle
as follows:
Code Block |
---|
buildscript {
dependencies {
classpath files('../flutter_mobile_protect/android/mobileprotect/gradle{VERSION}+_plugin/mobile-protect-gradle-plugin.jar')
classpath 'org.aspectj:aspectjtools:1.9.4'
}
} |
Note: replace {VERSION}
with one of the provided versions in the SDK that matches the version of Gradle your project is using.
Step 2
Activate the plugin in your App module's build.gradle
file, by adding the following statement after the apply plugin: 'com.android.application'
statement.
Code Block |
---|
apply plugin: 'com.dtplugin.mobileprotect' |
Step 3
In the Android project, create the xml
directory (flutter_app/android/app/src/main/res/xml
) if it does not exist. Then, copy the mobileprotect.xml
config file into the xml
resources folder.
iOS Integration
...
Step 2: configure for Android
Follow steps 1-4 in Mobile Protect SDK for Android via Maven | Installing Mobile Protect SDK with Maven for v23 and above. Please note:
the version of Mobile Protect for Android can and usually does differ from the version of Mobile Protect for Flutter. Thus the version specified in the
pubspec.yaml
may be different from that specified in the gradle build files. Please use theMaven for v23 and above
section of the Android installation instructions.the settings.gradle of your flutter project is located at
android/settings.gradle
, while the build.gradle of your flutter project is located atandroid/build.gradle
, both relative to the flutter project root.
Step 3: configure for iOS
The API key is necessary to access the Podfile upon which Mobile Protect depends. This API key should be added to your netrc file, which will be used automatically by CocoaPods,
curl
andgit
when fetching the SDK.Add the following entry to the file at
~/.netrc
:Code Block machine mobile-protect-repos.securetheorem.com password {MOBILEPROTECT_REPO_API_KEY}
Replacing
{MOBILEPROTECT_REPO_API_KEY}
with the Mobile Protect API key.
Please note: this entry should be placed as the first entry in.netrc
, otherwise cocoapods may fail to read itIn the iOS project copy the
MobileProtect.plist
config file into the Flutter project's Xcode project, placing it in the project root, and then add it to your xcode project - see Add Existing Files and Folders to a Project for instructions.
Installation of the SDK is now complete. No initialization of MobileProtect is necessary for flutter as the SDK will be initialized automatically.
Results
Please visit https://www.securetheorem.com/mobile/protect to see the list of your apps and the state of protection, along with the individual protection item details.
Setup callbacks
MobileProtect supports 2 types of callbacks: protections and events. Protection callbacks are triggered before closing the app and event callbacks are triggered on security events.
They can be setup using FlutterMobileProtect.onProtectionCallback((reason) async { TODO(); });
and FlutterMobileProtect.onEventsCallback((reason) async { TODO(); });
For example, if you want to listen to emulator and jailbreak protection events only, you can use it this way:
Code Block |
---|
FlutterMobileProtect.onEventsCallback((reason) async {
if(reason == "EmulatorProtection") {
// do something
} else if (reason == "JailbreakDetection") {
// do something (else)
} else {
// do nothing
}
}); |
...
Upgrading from earlier versions (prior to v23.0.0)
In order to update the Mobile Protect Flutter version from an earlier version to 23.0.0 or later, the following changes must be made:
update the Flutter plugin version in the project’s
pubspec.yaml
file, or if installing from a local copy of the plugin, update that local copy to the new plugin. For example, we may change:Code Block dependencies: //... mobile_protect: ^22.3.14 # OLD VERSION!
to:
Code Block dependencies: //... mobile_protect: ^24.0.0
Remove references to the previous Android installation:
remove the old remove the reference to the old android plugin by removing this line from
android/app/build.gradle
:Code Block apply plugin: 'com.dtplugin.mobileprotect
remove any references to
mobileprotect
andaspectj
from the dependencies section ofandroid/build.gradle
Follow the steps outlined in the section Step 2: configure for Android above. Note that no changes are necessary for iOS configuration.
Common errors during upgrade
If the following error occurs when running on Android
Code Block |
---|
> A problem occurred configuring project ':app'.
> Cannot add task 'aopWeaveDebug' as a task with that name already exists. |
The cause is likely that the (new) plugin is correctly applied in android/build.gradle
, but the (old) plugin apply plugin: 'com.dtplugin.mobileprotect'
was not removed from android/app/build.gradle
.
If a pod-related error occrs while running on iOS, such as the following:
Code Block Error: CocoaPods's specs repository is too out-of-date to satisfy dependencies. To update the CocoaPods specs, run: pod repo update
it may be necessary to remove the file
ios/Podfile.lock
, then cleaning the project usingflutter clean
and finally rebuilding for iOS.