Configure Dynatrace Android Gradle plugin for instrumentation processes
The following configuration options allow you to customize the instrumentation process of the Dynatrace Android Gradle plugin.
Variant-specific configurations
The plugin allows you to specify multiple variant-specific configurations, where each variant-specific configuration can be applied to multiple Android build variants. You need to provide a variant-specific configuration for every variant. If the plugin is unable to find a variant-specific configuration for a certain Android build variant, it cancels the build and throws an error. This protection feature can be deactivated with the strictMode
property.
The association is determined by the regex, that is specified in the variantFilter
property, and the variant name of the Android build variant. The regex is case-sensitive and if no product flavor is defined, the build type in the variant name is lower case. If multiple variant-specific configurations match the same variant, the first configuration is selected and applied.
The following sample shows how you can specify a variant-specific configuration in the dynatrace
block:
For example, you can have an app that has two product flavors, demo
and paid
, and two default build types, debug
and release
. The plugin can be used to specify a variant-specific configuration for all debug build variants, demoDebug
and paidDebug
, and another two variant-specific configurations for the two variants, demoRelease
and paidRelease
.
The association between variant-specific configurations from the plugin and the Android build variants can be printed with the printVariantAffiliation
task. For example, the following sample shows the console output of the above example:
> Task :app:printVariantAffiliationVariant 'demoDebug' will use configuration 'dev'Variant 'demoRelease' will use configuration 'demo'Variant 'paidDebug' will use configuration 'dev'Variant 'paidRelease' will use configuration 'prod'
Separate development and production monitoring data
If you don't want to pollute your production monitoring data with monitoring data from your debug
builds, separate the development data from the production monitoring data.
To do this, create two mobile apps in Dynatrace. Generate two variant-specific configurations and use the provided applicationId
and beaconUrl
values from the Instrumentation
page.
If you prefer to not monitor your debug
builds, disable the variant-specific configuration that is responsible for your debug
builds.
Deactivate auto-instrumentation
You can deactivate auto-instrumentation for all variants or for a specific variant.
Deactivate all variants
To deactivate auto-instrumentation for all variants, deactivate the plugin with the pluginEnabled
property. The plugin still adds OneAgent SDK as Gradle dependency to ensure that the compiler can compile the manually instrumented source files.
With this setting, the verification step of the plugin is deactivated. Also, the auto-start feature is deactivated and the app stops being monitored. If you start the agent manually, the app is monitored, but OneAgent can capture only mobile user experience data that is instrumented by manual calls to the OneAgent SDK.
You can deactivate auto-instrumentation for all variants or for a specific variant.
Deactivate one variant
If you want to deactivate auto-instrumentation for a specific variant, explicitly deactivate the variant-specific configuration via the enabled
property. The following example shows how you can deactivate all debug
build types (variant demoDebug
and paidDebug
):
You can deactivate the strictMode
property and only define a variant-specific configuration for the variant you want to instrument. The plugin doesn’t instrument Android build variants when no variant-specific configuration matches the given Android build variant.
If you don't deactivate the strictMode
property, the plugin cancels the build and throws an error.
We don't recommend this approach, because the strict mode feature should protect you from building variants with no variant-specific configuration. Deactivate auto-instrumentation via the enabled
property of your variant-specific configuration.
Automatic OneAgent startup
To obtain the correct startup parameters, go to Instrumentation wizard in Dynatrace.
- Go to Mobile.
- Select the mobile application that you want to configure.
- Select More (…) > Edit in the upper-right corner of the tile with your application name.
- From the application settings, select Instrumentation wizard.
- Select Android, and then go to either the Groovy (build.gradle) or the Kotlin (build.gradle.kts) tab.
- Use the preconfigured snippet (see the step called Apply the Dynatrace plugin and add the plugin configuration) that is already populated with the configuration values from your mobile app.
All OneAgent start-up related properties are part of the AutoStart DSL and must be configured via the autoStart
block:
The automatic OneAgent startup feature starts OneAgent in the Application.onCreate
method. If your app supports Direct Boot, you have to deactivate the automatic OneAgent startup feature. You should also read Adjust communication with OneAgent SDK for Android to ensure that OneAgent is able to transmit the data to the cluster.
Deactivate automatic OneAgent startup
You can disable the automatic OneAgent startup with the enabled
property. You cannot specify the applicationId
and the beaconUrl
properties because these values must be used in the manual startup call.
It is recommended to add the startup call to the Application.onCreate
method. For more information, see OneAgent SDK for Android > Start OneAgent.
Additional configuration changes specified via DynatraceConfigurationBuilder
override the configuration values defined via the Dynatrace Android Gradle plugin DSL.
Exclude certain classes and methods
By default, the Dynatrace Android Gradle plugin instruments all packages. If you want to exclude certain classes, you have two options:
- Exclude a list of packages, classes, and methods
- Use a custom exclude filter
Exclude a list of packages, classes, and methods via the packages
, classes
, and methods
properties
These properties contain some additional features:
packages
automatically excludes sub-packagesclasses
automatically excludes inner classesmethods
automatically excludes all methods with the same name (regardless of the method signature)
Escape the $
character for inner classes as shown in the above example.
Use the custom exclude filter
This option allows you to define a finer granular exclusion logic via additional filter rules. In the filter you can define a regex for className
, methodName
, and methodDescription
.
- For the
className
property, the fully qualified name is used. - A class name matches when the specified expression is found somewhere in the class name
- A method name matches when the specified expression is found somewhere in the method name
- A method description matches when the specified expression is found somewhere in the method description
Adjust test case instrumentation
The plugin only executes the auto-instrumentation step when the app is built. Therefore, the behavior of local unit tests and instrumented unit tests is different.
Local unit tests
Local unit tests use instrumented classes from your source folder, but uninstrumented libraries. There should be no impact on the unit tests, as the plugin mostly instruments Android-specific classes and method calls are ignored when OneAgent is not started. This is also valid for method calls to OneAgent SDK.
Local unit tests with Robolectric
Robolectric allows you to simulate Android in your local unit tests. Robolectric uses instrumented classes from your source folder, but uninstrumented libraries.
If you have disabled the automatic OneAgent startup and started OneAgent manually in the Application
class, OneAgent runs and monitors your Robolectric test cases. In this case, adjust your configuration by either separating development and production monitoring data or using a different build type for unit tests.
Instrumented unit tests
To run instrumented unit tests on a device or emulator, the Android build generates two APK files:
- The APK file of the app you want to test. The plugin is part of this build process and auto-instruments the APK file.
- A test APK file that contains the class for testing the other APK. The plugin is also part of this build process, but it doesn't auto-instrument these test APKs.
Because the APK is instrumented, every instrumented unit test is monitored. In this case, you should adjust your configuration by either separating development and production monitoring data or using a different build type for unit tests.
Use a different build type for unit tests
For a customized instrumentation behavior for your unit tests, generate a new build type for your unit tests with the plugin.
For example, if you want to monitor your debug
builds used by the developers and you don't want to monitor the unit test execution in the CI, use the following configuration:
When you execute the unit tests with the debug
variant, unit tests are monitored because the instrumentation is deactivated only for the CI
variant.