How do I use the Dynatrace Gradle plugin for Android app auto-instrumentation?

The Dynatrace Gradle plugin does not support the instrumentation of Android App Bundles.

The Dynatrace Gradle plugin is based on the Android Gradle plugin. The Dynatrace Gradle plugin inserts auto-instrumentation at the end of the Android build task sequence. Auto-instrumentation gets triggered by the Android build process and instruments your newly build APK file. The instrumented APK file is also automatically signed with the specified certificate. The Dynatrace Gradle plugin stores the instrumented and signed APK file under the same name in the same location (<project>/<module>/build/outputs/apk/<productFlavor>/<buildType>/<app_name>.apk). The advantage of this approach is that the Run button from the Android Studio can use the instrumented APK file and can install it on your device/emulator.

Supported Android Gradle plugin versions

The Dynatrace Gradle plugin is hosted on JCenter and Maven Central. For details on Dynatrace Gradle plugin version and Android Gradle plugin version interoperability, please see supported Android Gradle plugin versions.

Run auto-instrumentation using Dynatrace Gradle plugin

To run auto-instrumentation using Dynatrace Gradle plugin:

Click Applications in the navigation menu.

Select your mobile application.

Click the browse button (...) in the top right corner and select Edit.

Click Instrumentation in the left menu.

Select Google Android.

Select the Gradle tab. In this tab, you will find the following Gradle snippet that is already populated with the configuration values from your mobile app:

Or deactivate the auto-start injection feature from the auto-instrumentor with the DTXAutoStart property. In this case you don't need to specify the DTXApplicationID, DTXAgentEnvironment, DTXClusterURL and DTXManagedCluster properties, because you have to specify these values in the manual startup call. You have to add the startup call manually into the Application.onCreate method and load the auto-instrumentation configuration with the DynatraceConfigurationBuilder.loadDefaultProperties(Context) method. For more information, see the OneAgent SDK for Mobile section.

Dynatrace.startup(this, new DynatraceConfigurationBuilder("YourApplicationID", "YourEnvironmentID", "ProvidedClusterUrl")
	.loadDefaultProperties(context)
	...
	.buildConfiguration());

Copy this Gradle snippet into your module's build.gradle file and the Dynatrace Gradle plugin will automatically instrument your mobile app on every build. Of course, if you are familiar with Gradle, you can customize the Gradle snippet. In this case, you have to ensure that the Dynatrace Gradle plugin dependency is added to your build script. Additionally, the apply statement and the dynatrace extension block must be added to the same build.gradle file that also contains the apply statement of the Android Gradle plugin.

You can optionally perform additional configuration.

Start your next build.

Signing

The Dynatrace Gradle plugin uses the signing configuration and logic from the Android Gradle plugin. You don't have to add any additional settings to the Dynatrace Gradle plugin.

android {
    ...
    defaultConfig {  }
    signingConfigs {
        release {
            storeFile file("my-release-key.jks")
            storePassword "password"
            keyAlias "my-alias"
            keyPassword "password"
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
            ...
        }
    }
}

Limitations

  • Do NOT use whitespaces in your project path.
  • Do NOT use the Android Studio feature Instant Run. With Android Studio version 2 and later, Android introduced the new feature Instant Run. This feature significantly reduces the app update time. The Dynatrace Gradle plugin for Android does not support Instant Run APKs and the corresponding Gradle build process. You should disable the Instant Run setting. To do this, open the Settings and navigate to Build, Execution, Deployment > Instant Run. Then clear the Enable Instant Run check box.

See the auto-instrumentation limitations for more information.