adjust-icon

Flutter SDK integration guide

The Adjust Flutter SDK enables you to measure attribution, events, and much more in your Flutter app. Follow the steps in this guide to set up your app to work with the Adjust SDK. You can also check out the example apps on GitHub.

1. Add the SDK to your project

To use the Adjust Flutter SDK in your Flutter app, you need to add it to your project.

To import the Adjust SDK to your Flutter project, follow these steps:

  1. Add the following to your pubspec.yaml file:
pubspec.yaml
dependencies:
adjust_sdk: ^4.38.2
  1. Navigate to your project and run the following command. Visual Studio automatically runs this command after you edit the pubspec.yaml file.
Terminal window
$ flutter packages get

2. Set up Android devices

Add Google Play Services

Apps that target the Google Play Store must use the Google Advertising ID (gps_adid) to identify devices. To do this, add the following dependency to the dependencies section of your build.gradle file.

build.gradle
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'

Add permissions

The Adjust SDK requires the following permissions. Add them to your AndroidManifest.xml file if they’re not already present:

AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

The Adjust SDK includes the com.google.android.gms.AD_ID permission by default in version 4.32.0 and above. You can remove it by adding a remove directive if need to make your app COPPA-compliant or if you don’t target the Google Play Store.

AndroidManifest.xml
<uses-permission android:name="com.google.android.gms.permission.AD_ID" tools:node="remove"/>

Set up Proguard

If you are using Proguard, add the following rules to your custom Proguard file.

proguard.pro
-keep public class com.adjust.sdk.** { *; }
-keep class com.google.android.gms.common.ConnectionResult {
int SUCCESS;
}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient {
com.google.android.gms.ads.identifier.AdvertisingIdClient$Info getAdvertisingIdInfo(android.content.Context);
}
-keep class com.google.android.gms.ads.identifier.AdvertisingIdClient$Info {
java.lang.String getId();
boolean isLimitAdTrackingEnabled();
}
-keep public class com.android.installreferrer.** { *; }

If you aren’t publishing your app in the Google Play Store, add the following rule to your Proguard file.

proguard.pro
-keep public class com.adjust.sdk.** { *; }

Set up install referrer

The install referrer is a unique identifier which you can use to attribute an app install to a source. The Adjust SDK requires this information to perform attribution. There are three methods you can use to gather this information:

Google Play Referrer API

To support the Google Play Referrer API, add the following in your build.gradle file:

build.gradle
implementation 'com.android.installreferrer:installreferrer:2.2'

If you are using Proguard, make sure you have added the following setting in your Proguard file:

proguard.pro
-keep public class com.android.installreferrer.** { *; }

Huawei Referrer API

As of v4.22.0, the Adjust SDK supports install recording on Huawei devices using Huawei App Gallery v10.4 and later. You don’t need to make any changes to start using the Huawei Referrer API.

Meta referrer integration

The Adjust SDK supports the Meta Install Referrer in v4.36.0 and above. To enable this feature:

  1. Find your Meta app ID in your App Dashboard. See Meta’s App Dashboard documentation for more information.

  2. Assign your App ID to the fbAppId property on your AdjustConfig instance.

    AdjustConfig config = new AdjustConfig('{YourAppToken}', AdjustEnvironment.sandbox);
    config.fbAppId = "<FB_APP_ID_STRING>";

3. Add iOS frameworks

The Adjust SDK is able to get extra information when you include certain iOS frameworks in your app. These frameworks enable certain SDK features, but they’re not mandatory for the SDK to work normally. You can add the frameworks and then mark them as optional in Project Settings —> Build Phases —> Link Binary With Libraries.

4. Initialize the Adjust SDK

Make sure you initialize the Adjust SDK as soon as possible in your Flutter app. To do this, initialize your config object with your app token and the environment you want to run your application in.

AdjustConfig config = new AdjustConfig('{YourAppToken}', AdjustEnvironment.sandbox);
Adjust.start(config);

5. Set up session recording

You need to set up session recording so that the SDK can pass session information to the Adjust server.

To set up session recording for Android devices, call the Adjust.onResume() method when the app is running in the foreground and make a call to the Adjust.onPause() method when the app isn’t running in the foreground. You can do this globally or per widget.

class AdjustExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Adjust Flutter Example App',
home: new MainScreen(),
);
}
}
class MainScreen extends StatefulWidget {
@override
State createState() => new MainScreenState();
}
class MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
@override
initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
initPlatformState(); // <-- Initialise SDK in here.
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.inactive:
break;
case AppLifecycleState.resumed:
Adjust.onResume();
break;
case AppLifecycleState.paused:
Adjust.onPause();
break;
case AppLifecycleState.suspending:
break;
}
}
}

6. Build your app

Well done! You should now be able to build and run your Flutter app. Enable logging to check for any issues. Check your logs to see the Install tracked message.

You are ready to start attributing your users with the Adjust SDK.

7. Add the Adjust SDK signature

You can use the Adjust SDK signature to sign all communications sent by the Adjust SDK. This enables Adjust’s servers to detect and reject any install activity that’s not legitimate.

To get started with the Adjust SDK signature, contact your Technical Account Manager or support@adjust.com.

8. Test your integration

The Adjust SDK provides tools for testing and troubleshooting issues with your integration. To test your setup:

  • Set your environment to AdjustEnvironment.sandbox.
  • Add a sandbox filter to your Adjust dashboard results.
  • Set your log level to AdjustLogLevel.verbose.

Test Google Play Services integration

To test that the Adjust SDK can receive a device’s Google Advertising ID, set the log level to AdjustLogLevel.verbose and the environment to AdjustEnvironment.sandbox. Start your app and measure a session or an event. The SDK logs the gps_adid (Google Play Services Advertiser ID) parameter if it has read the advertising ID.

If you’re experiencing issues retrieving the Google Advertising ID, open an issue in the SDK GitHub repository or contact support@adjust.com.