This is a fork of gradle-pitest-plugin which supports Android gradle projects.
- Android Gradle Plugin 8.5.1 or newer, including AGP 9 and the new Android DSL. The plugin depends on AGP 8.5.1, so builds using an older AGP get it upgraded to 8.5.1.
- The Gradle version required by your AGP version, Gradle 9 included.
- JDK 17 or newer.
Supported Android plugins: com.android.application, com.android.library, com.android.dynamic-feature,
com.android.test and com.android.kotlin.multiplatform.library.
With Gradle plugin portal
plugins {
id 'pl.droidsonroids.pitest' version '0.3.0'
}plugins {
id("pl.droidsonroids.pitest") version "0.3.0"
}Versions 0.2.13 to 0.2.27 were published only to the Gradle Plugin Portal. Use gradlePluginPortal() instead of
mavenCentral() for them.
buildscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath("pl.droidsonroids.gradle:gradle-pitest-plugin:0.3.0")
}
}
plugins {
id("com.android.application")
//or id("com.android.library")
//or id("com.android.test")
id("pl.droidsonroids.pitest")
}pitest<variant> tasks will be created for each build variant which has unit tests enabled
(eg. pitestProDebug for pro product flavor and debug build type).
Additionally pitest task will run tasks for all variants.
With AGP 9, unit tests and therefore pitest<variant> tasks exist only for the tested build type (debug by default).
Set android.onlyEnableUnitTestForTheTestedBuildType=false in gradle.properties to get them for all build types.
In Kotlin Multiplatform modules (com.android.kotlin.multiplatform.library) the task is named after the Android
compilation, eg. pitestAndroidMain.
After the measurements a report created by PIT will be placed in ${PROJECT_DIR}/build/reports/pitest/<variant> directory.
The pitest<variant> tasks are created after the project is evaluated, so tasks.named("pitestDebug") fails in a
build script. Configure them lazily instead:
tasks.withType<pl.droidsonroids.gradle.pitest.PitestTask>().configureEach {
// ...
}The plugin works only in Android modules. For pure Java/Kotlin JVM modules use the
upstream plugin (info.solidsoft.pitest).
For more information see README of source project
Apply pl.droidsonroids.pitest.aggregator in the root project and make the modules produce the files it needs:
pitest {
outputFormats = ['HTML', 'XML']
exportLineCoverage = true
timestampedReports = false
}The aggregator collects the reports of all pitest<variant> tasks that were run, it does not trigger them. To aggregate
only one build type, run just those tasks, eg. ./gradlew pitestDebug pitestReportAggregate.
The classpath is passed to PIT in a file (build/pitClasspath<Variant>) by default, to avoid the command line length limit on
Windows. To pass it on the command line instead, set useClasspathFile = false.
PIT 1.23.0 moved incremental analysis (historyInputLocation, historyOutputLocation, withHistory) into a separate
plugin. The default PIT version of this plugin still includes it. If you set pitestVersion to 1.23.0 or newer and use
history, add the plugin:
dependencies {
pitest 'org.pitest:pitest-history-plugin:0.0.1'
}This plugin by default adds mockable Android JAR (generated by Android Gradle Plugin) to classpath used under pitest tests.
If you are using alternative Android framework in tests, like Robolectric or
UnMock Gradle Plugin, you may want to add excludeMockableAndroidJar
to pitest configuration eg:
pitest {
targetClasses = ['com.myapp.*']
excludeMockableAndroidJar = true
}In such case default mockable Android JAR won't be added and alternative one will be used under tests.
This plugin tries to discover all the unit test dependencies and add them to the pitest classpath automatically.
But, this feature doesn't work with jetifier
(android.enableJetifier=true in gradle.properties).
If you are using jetifier and encounter NoClassDefFoundErrors in tests run under pitest, you may want to add
some dependencies manually to the pitestTestCompile configuration of the module. For example:
dependencies {
pitestTestCompile 'io.mockk:mockk-agent-jvm:1.14.11'
}Issue occurs when using Android API
without mocking it.
Pitest verbose logs may list exceptions like ExceptionInitializerError.
The fastest solution is to set android.testOptions.unitTests.returnDefaultValues = true.
See Local unit testing documentation
to see other consequences of this change.
The release process is automated using GitHub Actions and publishes to the Gradle Plugin Portal, Maven Central (via Sonatype Central Portal), and GitHub Releases.
Configure the following repository secrets under Settings > Secrets and variables > Actions:
GRADLE_PUBLISH_KEY: Gradle Plugin Portal API keyGRADLE_PUBLISH_SECRET: Gradle Plugin Portal API secretMAVEN_CENTRAL_USERNAME: Sonatype Central Portal User Token UsernameMAVEN_CENTRAL_PASSWORD: Sonatype Central Portal User Token PasswordSIGNING_IN_MEMORY_KEY: PGP private key (ASCII-armored-----BEGIN PGP PRIVATE KEY BLOCK-----)SIGNING_IN_MEMORY_KEY_PASSWORD: Passphrase for the PGP key (optional if unencrypted)SIGNING_IN_MEMORY_KEY_ID: 8-character hex PGP Key ID (optional)
- Run the Prepare release workflow on
masterwith the version to release (e.g.0.2.28, withoutvprefix). It verifies the version, generates release notes via GitHub API, updatesgradle.properties,README.md, andCHANGELOG.md, and pushes branchrelease/v<version>. - Open the pull request using the link provided in the workflow step summary, add the
ignore-for-releaselabel, review the notes and versions, and merge after CI passes. - Run the Release workflow on
masterwith the same version. It validates the merged branch and changelog, executes the full cross-platform CI matrix (Linux and Windows), creates git tagv<version>, creates the GitHub release, and publishes to the Gradle Plugin Portal and Maven Central.