Integrate the Flutter SDK

更新时间:
复制 MD 格式

This guide describes how to integrate and configure the Alibaba Cloud mobile monitoring Flutter SDK. It covers downloading, starting, and verifying the SDK for both Android and iOS platforms.

I. Prerequisites

  • You have created an application in EMAS and obtained the necessary credentials.

    • Log on to the EMAS console.

    • Create an Android or iOS application and obtain the AppKey, AppSecret, and AppRsaSecret.

    • Refer to the quick start guide.

  • Development environment requirements

    Android

    • Android 5.0+

    iOS

    • Xcode 14.0+

    • iOS 12.0+

    • CocoaPods 1.12.0+

II. Sample project

  • For a sample project integrating the mobile monitoring Flutter SDK, see the Example project.

III. Add the SDK dependency

  • In your project's pubspec.yaml file, add the alicloud_apm dependency:

    dependencies:
      alicloud_apm: ${ApmVersion}
    Important
  • Run flutter pub get to download the dependency.

IV. Use the SDK

Method 1 (Recommended)

import 'dart:io' show Platform;
import 'package:alicloud_apm/alicloud_apm.dart';

Future<void> main() async {
  final options = buildOptions();
  if (options != null) {
    await Apm.start(options);
  }
  
  runApp(const YourApp());
}

ApmOptions? buildOptions() {
  if (Platform.isIOS) {
    const appKey = 'Your iOS AppKey';
    const appSecret = 'Your iOS AppSecret';
    const appRsaSecret = 'Your iOS AppRsaSecret';

    return const ApmOptions(
      appKey: appKey,
      appSecret: appSecret,
      appRsaSecret: appRsaSecret,
      // Optional configurations. Replace the values as needed.
      channel: 'Your Channel',
      userNick: 'Your UserNick',
      userId: 'Your UserId',
    );
  }

  if (Platform.isAndroid) {
    const appKey = 'Your Android AppKey';
    const appSecret = 'Your Android AppSecret';
    const appRsaSecret = 'Your Android AppRsaSecret';

    return const ApmOptions(
      appKey: appKey,
      appSecret: appSecret,
      appRsaSecret: appRsaSecret,
      // Optional configurations. Replace the values as needed.
      channel: 'Your Channel',
      userNick: 'Your UserNick',
      userId: 'Your UserId',
    );
  }

  // Do not start APM on other platforms such as Web, Windows, macOS, and Linux.
  return null;
}

Method 2

  • Start the app in a guarded zone.

import 'dart:io' show Platform;
import 'package:alicloud_apm/alicloud_apm.dart';

Future<void> main() async {
  await Apm.runGuarded(() async {
    final options = buildOptions();
    if (options != null) {
      await Apm.start(options);
    }

    runApp(const YourApp());
  });
}

ApmOptions? buildOptions() {
  if (Platform.isIOS) {
    const appKey = 'Your iOS AppKey';
    const appSecret = 'Your iOS AppSecret';
    const appRsaSecret = 'Your iOS AppRsaSecret';

    return const ApmOptions(
      appKey: appKey,
      appSecret: appSecret,
      appRsaSecret: appRsaSecret,
      // Optional configurations. Replace the values as needed.
      channel: 'Your Channel',
      userNick: 'Your UserNick',
      userId: 'Your UserId',
    );
  }

  if (Platform.isAndroid) {
    const appKey = 'Your Android AppKey';
    const appSecret = 'Your Android AppSecret';
    const appRsaSecret = 'Your Android AppRsaSecret';

    return const ApmOptions(
      appKey: appKey,
      appSecret: appSecret,
      appRsaSecret: appRsaSecret,
      // Optional configurations. Replace the values as needed.
      channel: 'Your Channel',
      userNick: 'Your UserNick',
      userId: 'Your UserId',
    );
  }

  // Do not start APM on other platforms such as Web, Windows, macOS, and Linux.
  return null;
}

V. Configure the Gradle plugin (Android)

The Gradle plugin for performance analysis works by injecting code into the bytecode to collect data. To enable this, you must add the required Maven repository and Gradle plugin.

Add the Maven repository

In your <project>/android/build.gradle.kts file, add the Alibaba Cloud Maven repository.

allprojects {
    repositories {
        google()
        mavenCentral()
        // Add the Alibaba Cloud Maven repository
        maven {
            url = uri("https://maven.aliyun.com/nexus/content/repositories/releases/")
        }
    }
}

In your <project>/android/settings.gradle.kts file, add the Alibaba Cloud Maven repository.

pluginManagement {
    ...

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
        // Add the Alibaba Cloud Maven repository
        maven {
            url = uri("https://maven.aliyun.com/nexus/content/repositories/releases/")
        }
    }
}

plugins {
    ...
    // Add the mobile monitoring Gradle plugin
    id("com.aliyun.emas.apm") version "3.3.0" apply false
}

Apply the Gradle plugin

In your <project>/android/app/build.gradle.kts file, apply the plugin.

plugins {
    ...
    // Apply the mobile monitoring Gradle plugin
    id("com.aliyun.emas.apm")
}
Important

To collect cold start data, the APM plugin instruments the Application class. Ensure that your project has a custom Application.

VI. Configure ProGuard rules (Android)

If your project uses code obfuscation, add the following keep rules to your <project>/android/app/proguard-rules.pro file.

-keep class com.aliyun.emas.apm.**{*;}

-dontwarn com.google.auto.value.AutoValue
-dontwarn com.google.auto.value.AutoValue$Builder
-dontwarn com.alibaba.motu.crashreporter.MotuCrashReporter
-dontwarn com.alibaba.sdk.android.networkmonitor.NetworkMonitorManager
-dontwarn com.taobao.tao.log.TLogInitializer

VII. Verify the integration

Android

  • For Android, check the Logcat output in Android Studio for the following log messages:

    Apm                     I  Device unlocked: initializing Apm
    Apm-CrashAnalysis       I  Initializing Apm Crash Analysis 3.6.1 for com.aliyun.emas.apm.flutter
    Apm-CrashAnalysis       I  Initializing CrashAnalysis blocked main for 10 ms
    Apm                     I  Apm initialization successful

    This log shows the crash analysis SDK started successfully at the native layer.

iOS

[alicloud_apm] Alicloud APM started. Package version=1.1.0

[AlicloudApmCore] Started Successfully
[AlicloudApmSetting] Fetched Successfully

This log shows the SDK started successfully on both the Flutter and native layers and fetched its configuration.

VIII. Additional resources