Adding Glean to your Kotlin project
This page provides a step-by-step guide on how to integrate the Glean library into a Kotlin project.
Nevertheless this is just one of the required steps for integrating Glean successfully into a project. Check you the full Glean integration checklist for a comprehensive list of all the steps involved in doing so.
Currently, this SDK only supports the Android platform.
Setting up the dependency
The Glean Kotlin SDK is published on maven.mozilla.org.
To use it, you need to add the following to your project's top-level build file,
in the allprojects
block (see e.g. Glean SDK's own build.gradle
):
repositories {
maven {
url "https://maven.mozilla.org/maven2"
}
}
Each module that uses the Glean Kotlin SDK needs to specify it in its build file, in the dependencies
block.
Add this to your Gradle configuration:
implementation "org.mozilla.components:service-glean:{latest-version}"
Pick the correct version
The
{latest-version}
placeholder in the above link should be replaced with the version of Android Components used by the project.
The Glean Kotlin SDK is released as part of android-components. Therefore, it follows android-components' versions. The android-components release page can be used to determine the latest version.
For example, if version 33.0.0 is used, then the include directive becomes:
implementation "org.mozilla.components:service-glean:33.0.0"
Size impact on the application APK
The Glean Kotlin SDK APK ships binary libraries for all the supported platforms. Each library file measures about 600KB. If the final APK size of the consuming project is a concern, please enable ABI splits.
Dependency for local testing
Due to its use of a native library you will need additional setup to allow local testing.
First add a new configuration to your build.gradle
, just before your dependencies
:
configurations {
jnaForTest
}
Then add the following lines to your dependencies
block:
jnaForTest "net.java.dev.jna:jna:5.6.0@jar"
testImplementation files(configurations.jnaForTest.copyRecursive().files)
testImplementation "org.mozilla.telemetry:glean-forUnitTests:${project.ext.glean_version}"
Note: Always use org.mozilla.telemetry:glean-forUnitTests
.
This package is standalone and its version will be exported from the main Glean package automatically.
Setting up metrics and pings code generation
In order for the Glean Kotlin SDK to generate an API for your metrics, two Gradle plugins must be included in your build:
- The Glean Gradle plugin
- JetBrains' Python envs plugin
The Glean Gradle plugin is distributed through Mozilla's Maven, so we need to tell your build where to look for it by adding the following to the top of your build.gradle
:
buildscript {
repositories {
// Include the next clause if you are tracking snapshots of android components
maven {
url "https://snapshots.maven.mozilla.org/maven2"
}
maven {
url "https://maven.mozilla.org/maven2"
}
dependencies {
classpath "org.mozilla.components:tooling-glean-gradle:{android-components-version}"
}
}
}
Important
As above, the
{android-components-version}
placeholder in the above link should be replaced with the version number of android components used in your project.
The JetBrains Python plugin is distributed in the Gradle plugin repository, so it can be included with:
plugins {
id "com.jetbrains.python.envs" version "0.0.26"
}
Right before the end of the same file, we need to apply the Glean Gradle plugin.
Set any additional parameters to control the behavior of the Glean Gradle plugin before calling apply plugin
.
// Optionally, set any parameters to send to the plugin.
ext.gleanGenerateMarkdownDocs = true
apply plugin: "org.mozilla.telemetry.glean-gradle-plugin"
Rosetta 2 required on Apple Silicon
On Apple Silicon machines (M1/M2/M3 MacBooks and iMacs) Rosetta 2 is required for the bundled Python. See the Apple documentation about Rosetta 2 and Bug 1775420 for details.
You can install it withsoftwareupdate --install-rosetta
Offline builds
The Glean Gradle plugin has limited support for offline builds of applications that use the Glean SDK.