Installing and Configuring the eG Mobile Agent for Flutter Applications

Flutter is an open-source UI software development kit created by Google. It allows developers to build high-performance, natively compiled applications for mobile (iOS, Android), web, and desktop (Windows, macOS, Linux) using a single code-base and the Dart programming language.

To monitor a Flutter application, you first need to install an eG mobile agent on the application host. The steps for the same are discussed hereunder:

  1. Login to the application host.

  2. Then, open the browser, connect to the eG management console, and then login to the eG administrative interface.

  3. Follow the Infrastructure -> Add /Modify menu sequence. Figure 1 will appear. In Figure 1, pick Mobile RUM as the Component type, and click the Add New Component button to add a new Mobile RUM component.

    Selecting Mobile RUM as Component Type

    Figure 1 : Selecting Mobile RUM as the Component type

  4. Figure 2 will then appear. Provide a Nick name for the Flutter application you want to monitor.

    Managing Flutter App as Mobile RUM Component

    Figure 2 : Adding the target Flutter application as a Mobile RUM component

  5. Then, select the Mobile RUM collector to which the eG Mobile Agent should send the performance metrics it gathers from the target application.

  6. Next, assign a Remote agent to the Mobile RUM component. This is the agent that will pull metrics from the Mobile RUM collector and reports the same to the eG manager.

  7. To install the eG Mobile Agent on Flutter, set Application type as Hybrid.

  8. Finally, click the Add button.

  9. Figure 3 will then appear. Proceed to download the eG mobile agent from the manager. For that, in the right panel of Figure 3, first click on the Flutter button. Then, click on the Download button against Download eG Agent for Flutter.

    Figure 3 : Downloading eG Mobile Agent for Flutter

  10. The eG Mobile agent will be downloaded as a zipped file.
  11. Now, create a folder named eg-agent in the android directory of the target Flutter application. You will find this directory directly at the root level of the your Flutter project folder.

  12. Next, extract the contents of the downloaded zip file to any location on the target application host. Browse the extracted contents to locate and open the <Extract_Location>\Hybrid_Flutter_Agent\eginnovations\android\eg-agent directory. Copy the android-agent-2.1.3 and EgAgentGradlePlugin-1.0.0 files from this directory to the eg-agent folder that you manually created (at step 11).
  13. Next, edit the pubspec.yaml file in the root (top-level) directory of your Flutter project tree. This YAML file is the central configuration hub and manifest for any Flutter or Dart project. It defines the configuration rules, external packages, and static assets your application needs to build and run properly. To make sure that the eG mobile agent's classes and libraries are compiled and run by the target Flutter application, you should include the following lines in the YAML file:

    eginnovations:

    path: ..<Plugin Path>/

    After inserting the above-mentioned lines, make sure that you replace the <Plugin_Path> with the full path to the EgAgentGradlePlugin-1.0.0 that you copied at step 12 above.

  14. Next, proceed to install the eG mobile agent on the target application host. For that, run the following command at the application's root directory:

    flutter pub get

  15. Once the eG mobile agent is installed, proceed to configure the target application to start the mobile agent when it starts. For this, follow the steps below: you need to edit the main.dart file located inside the lib/ directory of the target application. This file is the absolute execution entry point for every Flutter application. When you build or launch your application on any platform (iOS, Android, Web, or Desktop), the underlying engine explicitly looks for this file and executes the code written inside it.

    1. In the main.dart file, include the following lines:

      import 'package:eginnovations/eginnovations.dart';

      void main() {

      FlutterError.onError = (FlutterErrorDetails details) {

      FlutterError.presentError(details);

      EginnovationsPlugin.reportFlutterError(details); // Dart Errors Monitoring

      };

      runZonedGuarded(() {

      WidgetsFlutterBinding.ensureInitialized();

      runeGInnovaions();

      runApp(const MyApp());

      }, (Object error, StackTrace stackTrace) {

      });

      }

    2. Next, login to the eG administrative interface of the eG manager where the Flutter application has been managed.

    3. Follow the Admin -> Infrastructure -> Add/Modify menu sequence.

    4. Select Mobile RUM as the Component type in the page that appears. All managed mobile RUM components will be displayed.

    5. In the list, locate the component that you added for the target Flutter application and click the Modify button corresponding to it.
    6. In the page that appears next, click Flutter in the right panel.Figure 4 will then appear. Click on the Copy to Clipboard button above the code snippet displayed against 'Include the following code...'.

      Copying the eG RUM Code Snippet to Clipboard

      Figure 4 : Copying the code snippet to be inserted in the main.dart file of the Flutter application

    7. Now, edit the main.dart file (in the lib/ directory) once again. Locate the code block you added to this file at step (a) above. Paste the code snippet you copied at step (f) below this code block.

    8. Finally, save the file.
  16. If the underlying native platform is Android, then you have to make the following configuration changes additionally:

    • First, you need to update the project-level build.gradle file (located in the root directory of the target Android project) with dependencies related to the eG mobile agent. For that, you need to edit the file and include the following lines in it:

      Copy
      buildscript {
      repositories {
      flatDir {
      dirs 'eg-agent'
      }
      }
      dependencies {
      classpath fileTree(include: ['*.jar'], dir: 'eg-agent')
      }
      }
    • Then, save the file.

    • Similarly, you will have to update the application-level build.gradle file (located in the app/ directory) with the dependencies related to the eG mobile agent. For that, you need to edit that file and include the following lines in it:

      Copy
      apply plugin: 'EgAgentGradlePlugin' //Gradle 8
      repositories {
      flatDir {
      dirs '../eg-agent'
      }
      }
      dependencies {
      implementation fileTree(dir: '../eg-agent', include: ['a*.jar'])
      }
    • Next, save the file.

    • Now, proceed to edit the gradle.properties file (in the Android project's root directory), and add the eG mobile agent to the 'ignorelist' of the file, as shown below:

      Copy
      android.jetifier.ignorelist= EgAgentGradlePlugin-1.0.0.jar //Gradle 8
      hybrid =true

      This instructs the Android Gradle Plugin (AGP) to skip the automatic Jetifier migration process for eG mobile agent libraries. Its because this process may sometimes spew errors and cause application crashes.

    • Then, save the gradle.properties file.

    • Next, you need to allow the target application to perform network operations. For that, you need to set the following permissions for the application:

      • android.permission.INTERNET - This permission allows the application to perform network sockets.

      • android.permission.ACCESS_NETWORK_STATE - This permission allows the application to access information about networks.

      • android.permission.ACCESS_WIFI_STATE - This permission allows your application to access information about Wi-Fi networks, such as whether Wi-Fi is enabled, the RSSI (signal strength), and connected Wi-Fi networks

      To allow these permissions, do the following:

      • Edit the AndroidManifest.xml file in the root of the project source set.

      • Under the manifest tag of the file, specify the following entries:

        Copy
        <uses-permission android:name="android.permission.INTERNET" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
      • Also, ensure that `android:usesCleartextTraffic="true"` is added in the <application> tag, as shown by the entry highlighted in Bold below:

        <application android:name=".MyApplication" android:theme="@style/AppTheme" android:usesCleartextTraffic="true">

        This allows the application to transmit over unencrypted HTTP connections

    • Next, proceed to edit the MainActivity.java file (in the [YourProjectFolder]/android/app/src/main/java/[your]/[package]/[name]/ directory), and include the following 'import' statements in the file. These statements instruct the compiler to load the eG mobile agent and all its dependent classes and packages.

      Copy
      import com.eg.agent.android.AppPlatform
      import com.eg.agent.android.eGAndroidAgent
    • Next, inside the onCreate method, initialize the eG Mobile Agent by inserting the following code:

      @Override

      public void onCreate(Bundle savedInstanceState){

      super.onCreate(savedInstanceState)

      eGAndroidAgent.withCollectorHost(‘<APP_TOKEN>,'<URL>’).usingSsl(false).start(getApplicationContext());

      eGAndroidAgent.setAppliactionPlatform(AppPlatform.Flutter)

      }

      After inserting, make sure you replace the variables highlighted in Bold in the code above with the following values:

      • <APP_TOKEN>: Replace this with the value displayed against appToken in the code snippet of Figure 4.

      • <URL>: Take the value displayed against host and port in the code snippet of Figure 4. Then, build the URL by combining the host and port values in the format: host:port. Then, replace the <URL> with host:port.

    • Finally, save the file.

    • Clean and build the project in Android Studio. Run it on a simulator or mobile device:

      flutter run

  17. If the underlying native platform is iOS, then you have to make the following configuration changes additionally:

    • First, navigate to the ios folder in your Flutter app’s root folder and update it by running this command from the terminal in your project directory.

      Pod Install

      This command is used to download, install, and configure the native iOS dependencies (plugins) required by your project.

    • If the app is being built using the Swift programming language, then proceed as follows:

      • Open the AppDelegate.Swift and add the below import statement:

        import eGMobileAPM

        This command will import the eG mobile agent into your application code. This way, your code will be able to use the eG mobile agent's classes, functions, and variables

      • Then, in the file, search for the application(_:didFinishLaunchingWithOptions:) method.
      • Inside this method, provide the following initialization code:

        egAPM.sharedInstance().start(appToken:"APP_TOKEN", collectorHost:"URL”)

        In the above lines of code, replace the entries in Bold with the following values:

        • <APP_TOKEN>: Replace this with the value displayed against appToken in the code snippet of Figure 4.
        • <URL>: Take the value displayed against host and port in the code snippet of Figure 4. Then, build the URL by combining the host and port values in the format: host:port. Then, replace the <URL> with host:port.

      • Finally, save the file.

    • If the app is being built using the Objective-C programming language, then proceed as follows:

      • Open the AppDelegate.m file; this file will reside inside the root project folder or the primary app target folder directly inside your project directory.

      • At the top of the file (outside of any implementation blocks), include the following statement:

        #import <eGMobileAPM/eGMobileAPM.h>

        This statement will import the eG mobile agent into your application code, so that your code will be able to use the eG mobile agent's classes, functions, and variables.

      • Next, inside the didFinishLaunchingWithOptions method in the file, add the following initialization code:

        [egAPM.sharedInstance startWithApptoken:(@"APP_TOKEN") collectorHost:(@"URL")];

        In the above line of code, replace the entries in Bold with the following values:

        • <APP_TOKEN>: Replace this with the value displayed against appToken in the code snippet of Figure 4.
        • <URL>: Take the value displayed against host and port in the code snippet of Figure 4. Then, build the URL by combining the host and port values in the format: host:port. Then, replace the <URL> with host:port.

        • Finally, save the file.

    • Then, clean the project, build the application, run the application on an iOS device or simulator, and finally, log into the eG management console to verify data collection by the eG mobile agent.