Installing and Configuring the eG Mobile Agent for Android

Android applications are typically built using the Gradle build system. A build system transforms your source code into an executable application. Builds often involve multiple tools, to analyze, compile, link, and package your application or library. Gradle uses a task-based approach to organize and run these commands.

eG Enterprise offers two mobile agents for Android - one for Android Gradle version 7.x and below, and another for Android Gradle version 8.x and above.

Installing the eG Mobile Agent for Android Applications Using Gradle v8.x (or above)

To install the eG mobile agent for an Android application that was built using Gradle v8.x, do the following:

  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 Android application you want to monitor.

    Adding an Android App Using Gradle v8x Plugin for Monitoring

    Figure 2 : Adding the target Android 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 report the same to the eG manager.

  7. To install the eG mobile agent on Android, set Application type as Native.

  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, click on the Download button corresponding to Gradle version 8.x and above.

    Figure 3 : Downloading eG Mobile Agent for Android Gradle v8.x and above

  10. The eG Mobile agent will be downloaded as a zipped file. Extract the contents of the zip to any location on the application host.Figure 4 depicts the extracted contents.

    Figure 4 : Contents of the zipped file

  11. Copy the 'eg-agent' directory and paste it into the root folder of the target application's install directory.

  12. Then, you need to update the Android project's build configuration files with the changes introduced by the addition of the 'eg-agent' directory. A project in Android Studio contains everything that defines the workspace for an app, from source code and assets to test code and build configurations.

    The procedure to update the project's build configuration varies based on the project type (whether Java/Kotlin) and Gradle version.

    For Android Kotlin projects, follow the steps below:

    • Open the build.gradle.kts file located in the root directory of the Android application and append the following lines of code.

      Copy
      buildscript {
      repositories {
      flatDir {
      dirs("eg-agent")
      }
      }
      dependencies {
      classpath(fileTree(mapOf("include" to listOf("*.jar"), "dir" to "eg-agent")))
      }
      }
    • Finally, save the file.

    For Android Java applications or applications using Groovy DSL, follow the steps below:

    • Open the build.gradle file located in the root directory of the target application and append the following lines of code to it:

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

      .

    • Finally, save the file.

  1. After updating the project's build configuration, proceed to update the application's build configuration with the changes introduced by the addition of the eG mobile agent. This procedure again varies depending upon the project type and Gradle version:

    For Android Kotlin projects, follow the steps below:

    • Open the build.gradle.kts file located in your application module(app) directory and append the following lines of code to it:

      Copy
      plugins {
      id("EgAgentGradlePlugin")
      }
      android {
      .....
      repositories {
      mavenCentral() } ..... packagingOptions { resources.excludes.add("META-INF/*") } ……….
      }
      dependencies {
      implementation(fileTree(mapOf("dir" to "../eg-agent", "include" to listOf("*.jar"))))
      }
    • Then, save the file.

    For Android Java Projects or Projects Using Groovy DSL, follow the steps below:

    • Open the build.gradle file located in your application module(app) directory and append the following lines to it:

      Copy
      plugins {
      id 'com.android.application'
      id 'EgAgentGradlePlugin'
      }
      android {
      .....
      repositories {
      mavenCentral() } .... packagingOptions { resources.excludes.add("META-INF/*") } ……….
      }
      dependencies {
      implementation fileTree(dir: '../eg-agent' , include: ['*.jar'])
      }
    • Finally, save the file.

  2. Next, you need to inform Gradle to include the eG Mobile Agent when building the target application. The steps for achieving this vary based on the project type.

    For Android Kotlin projects, follow the steps below:

    • Open the settings.gradle.kts file located in the root directory of Android project and append the following lines of code to it:

      Copy
      dependencyResolutionManagement {
      repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
      repositories {
      google()
      mavenCentral()
      flatDir {
      dirs("../eg-agent")
      }
      }
      }

       

    • Finally, save the file.

    For Android Java Projects or Projects Using Groovy DSL, follow the steps below:

    • Open the settings.gradle file located in the root directory of Android project and append the following lines of code to it:

      Copy
      dependencyResolutionManagement {
      repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
      repositories {
      google()
      mavenCentral()
      flatDir {
      dirs '../eg-agent'
      }
      }
      }

       

    • Finally, save the file.

  3. 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.

    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"/>
    • 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

    • Finally, save the file.

  4. If you are encountering errors related to Jetifier, do the following:

    • Open the gradle.properties file in the Android folder and append the following lines to it:

      android.enableJetifier=true

    • Finally, save the file.

  5. Now, proceed to set the eG mobile agent to start when the application starts. For that, you need to edit the Main or Default activity of the application. An activity serves as the entry point for an app's interaction with the user. Often, an activity is implemented as a subclass of the Activity class. Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. So naturally, the eG mobile agent should be started as part of the main activity. To achieve this, follow the steps below:

    • Import the eG mobile agent class into your Main or default activity as shown below

      import com.eg.agent.android.eGAndroidAgent;

    • Then, login to the eG administrative interface, and proceed to modify the configuration of the Mobile RUM component you added for monitoring the target application. When Figure 3 appears, copy the line displayed against 'Include this line in the Application Main or Default activity....' field inFigure 3 to the Main or Default activity. This will inject the eG Mobile Agent into the target application and start the agent when the application starts.

  6. Finally, clean the project and build the application. While building the application, the console should log messages about transforming classes etc. After successfully building the app, run the application in an emulator or mobile device and login to the eG web console to start seeing data.

Installing the eG Mobile Agent for Android Applications Using Gradle v7.x (or below)

To install the eG mobile agent for an Android application that was built using Gradle v7.x (or below), do the following:

  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 5 will appear. In Figure 5, 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 typ

    Figure 5 : Selecting Mobile RUM as the Component type

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

    Adding an Android App Using Gradle Plugin v7x or below for Monitoring

    Figure 6 : Adding the target Android 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 report the same to the eG manager.

  7. To install the eG mobile agent on Android, set Application type as Native.

  8. Finally, click the Add button.

  9. Figure 7 will then appear. Proceed to download the eG mobile agent from the manager. For that, in the right panel of Figure 7, click on the Download button corresponding to Gradle version 7.x and below.

    Dowloading eG Mobile Agent for Gradle v7x or below

    Figure 7 : Downloading eG Mobile Agent for Android Gradle v7.x and below

  10. The eG Mobile agent will be downloaded as a zipped file. Extract the contents of the zip to any location on the application host.Figure 8 depicts the extracted contents.

    Contents of the eG Mobile Agent Zip File

    Figure 8 : Contents of the zipped file

  11. Copy the 'eg-agent' directory and paste it into the root folder of the target application's install directory.

  12. Then, you need to update the Android project's build configuration files with the changes introduced by the addition of the 'eg-agent' directory. A project in Android Studio contains everything that defines the workspace for an app, from source code and assets to test code and build configurations.

    The procedure to update the project's build configuration varies based on the project type (whether Java/Kotlin) and Gradle version.

    For Android Kotlin projects, follow the steps below:

    • Open the build.gradle.kts file located in the root directory of the Android application and append the following lines of code.

      Copy
      buildscript {
      repositories {
      flatDir {
      dirs("eg-agent")
      }
      }
      dependencies {
      classpath(fileTree(mapOf("include" to listOf("*.jar"), "dir" to "eg-agent")))
      }
      }
    • Finally, save the file.

    For Android Java applications or applications using Groovy DSL, follow the steps below:

    • Open the build.gradle file located in the root directory of the target application and append the following lines of code to it:

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

      .

    • Finally, save the file.

  1. After updating the project's build configuration, proceed to update the application's build configuration with the changes introduced by the addition of the eG mobile agent. For this, follow the steps below:

    • Open the build.gradle file located in your application module(app) directory and append the following lines to it:

      Copy
      repositories {
      flatDir {
      dirs '../eg-agent'
      }
      }
      apply plugin: 'android'
      apply plugin: 'eg' android { ………. packagingOptions { exclude 'META-INF/DEPENDENCIES' } ……….
      }
      dependencies {
      implementation fileTree(dir: '../eg-agent' , include: ['a*.jar'])
      }
    • Finally, save the file.

  2. 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.

    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"/>
    • 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

    • Finally, save the file.

  3. If you are encountering errors related to Jetifier, do the following:

    • Open the gradle.properties file in the Android folder and append the following lines to it:

      android.enableJetifier=true

    • Finally, save the file.

  4. Now, proceed to set the eG mobile agent to start when the application starts. For that, you need to edit the Main or Default activity of the application. An activity serves as the entry point for an app's interaction with the user. Often, an activity is implemented as a subclass of the Activity class. Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. So naturally, the eG mobile agent should be started as part of the main activity. To achieve this, follow the steps below:

    • Import the eG mobile agent class into your Main or default activity as shown below

      import com.eg.agent.android.eGAndroidAgent;

    • Then, login to the eG administrative interface, and proceed to modify the configuration of the Mobile RUM component you added for monitoring the target application. When Figure 7 appears, copy the line displayed against 'Include this line in the Application Main or Default activity....' field in Figure 7 to the Main or Default activity. This will inject the eG Mobile Agent into the target application and start the agent when the application starts.

  5. Finally, clean the project and build the application. While building the application, the console should log messages about transforming classes etc. After successfully building the app, run the application in an emulator or mobile device and login to the eG web console to start seeing data.

RUM-Enabling an Android Application that Integrates with ProGuard

ProGuard is a tool that shrinks, optimizes, and obfuscates an Android application's code. Often, it is implemented as part of the build process for releasing production-ready apps, offering benefits like reduced app size, improved performance, and enhanced security.

Where the target Android application integrates with ProGuard, you need to make sure that the application code-level changes introduced by the eG mobile agent are preserved and not obfuscated by the tool. For that, edit the Proguard config file (e.g., proguard.cfg, proguardandroid.txt, proguardrules.pro, etc.) to add the following:

  1. To support eG instrumentation, add the following entry:

    -keep class com.eg.** { *; }

  2. To disable warnings related to unresolved problems, add the following entry:

    -dontwarn com.eg.**

  3. Then, add the following entry:

    -keepattributes Exceptions, Signature, InnerClasses, LineNumberTable, SourceFile, EnclosingMethod

    Each argument the -keepattributes entry takes and its purpose has been described below:

    Argument

    Purpose

    Exceptions

    Enforces that the exceptions thrown by the methods are captured

    Signature

    Specifies the generic signature of the class, field, or method. Compilers may need this information to properly compile classes that use generic types from compiled libraries.

    InnerClasses

    Specifies the relationship between a class and its inner classes and outer classes. Other than this and the naming convention with a '\$' separator between the names of inner classes and outer classes, inner classes are just like ordinary classes. Compilers may need this information to find classes referenced in a compiled library. Code may access this information by reflection, for instance to derive the simple name of the class.

    LineNumberTable

    Specifies the line numbers of the method. If present, these line numbers are reported in stack traces.

    SourceFile

    Specifies the name of the source file from which the class file was compiled. If present, this name is reported in stack traces.

    EnclosingMethod

    Specifies the method in which the class was defined. Compilers may need this information to find classes referenced in a compiled library. Code may access this information by reflection, for instance to derive the simple name of the class.

  4. Clean and build your project.

  5. Run your application in an emulator or mobile device.