Repository navigation
Format the Java files of Android projects - #288
Merged
abashev merged 1 commit intoNov 28, 2026
Merged
Conversation
The plugins added the Spotless step and formatDiff only to a project with the java plugin, which the Android Gradle plugin never applies: in an Android module spotlessCheck passed whatever its files held, and there was no formatDiff (#284). Spotless's own default target finds nothing there either. AGP applies java-base, whose source sets stay empty, so a java format without a target formats no file. Both now also take com.android.application, library, dynamic-feature and test. In those the Spotless step's target is the java.srcDirs of every Android source set: main, test, androidTest, the build types and flavors. They are read by reflection, so the plugin depends on no Android Gradle plugin and needs no classes of the build's own. A target the build script sets replaces it, and generated directories stay out, as in a Java project. The tests apply AGP 9.1.1, the oldest that runs on Gradle 9.3.1, which the test projects resolve from Google's Maven repository themselves. They need no Android SDK.
abashev
deleted the
abashev/please-add-support-for-android-application-and-a
branch
November 28, 2026 16:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Based on
21.X.X. Fixes #284.Before this PR
The plugin added the Spotless step and
formatDiffonly to a project with thejavaplugin. The Android Gradle plugin never appliesjava, so in acom.android.applicationorcom.android.librarymodule the plugin did nothing:spotlessCheckpassed whatever the files held,spotlessApplychanged nothing, and there was noformatDiff. The IntelliJ plugin formatted the same files.Applying Spotless's
java {}by hand did not help either. AGP appliesjava-base, so the java extension is there with emptysourceSets, and Spotless's default target finds no file, without an error.After this PR
==COMMIT_MSG==
The plugin also takes
com.android.application,com.android.library,com.android.dynamic-featureandcom.android.test. In those modulesformatDiffis there, and the Spotless step's target is thejava.srcDirsof every Android source set: main, test, androidTest, the build types and flavors. Thekotlindirectories are left out. Atargetset in the build script still replaces it, and generated directories stay out, as in a Java project.The source sets are read by reflection (
android.getSourceSets(),getJava(),getSrcDirs()), so the plugin depends on no Android Gradle plugin and needs none of the build's AGP classes. If a future AGP changes these getters, the build fails with a message that namesandroid.sourceSets[*].java.srcDirs, instead of formatting nothing.==COMMIT_MSG==
Java projects keep what they had: the step's exclusions are computed from the same source sets as before, and the target stays Spotless's own.
Checked
AndroidTest(TestKit, with and without the configuration cache) applies AGP 9.1.1 without an Android SDK:spotlessCheckfails;srcDiris left alone;targetwins;formatDiffworks without Spotless;java-librarysubprojects are formatted in one parallel build, the layout of the report.All 16 fail on
21.X.X.AndroidSourcesTestchecks the error messages on stand-ins for the DSL.AGP 9.1.1 configured the reproducer, gave its source sets through these getters and ran Spotless on Gradle 9.3.1 with JDK 25 and on Gradle 9.8.0 with JDK 27, the far ends of CI's legs. I didn't have JDK 26 to check the 9.4.0 leg. The getters also work on AGP 9.5.3.
dglynch/OpenJavaFormatAndroid with the plugin built from this branch:
spotlessCheckfails on exactlyMainActivity.javaandIntermediateClass.java;spotlessApplychanges only those two files and leaves alone the ones the IntelliJ plugin formatted;:app:formatDiffand:androidlibrary:formatDiffformat only the changed lines../gradlew checkpasses.Possible downsides?
google()in their ownsettings.gradle, so the build itself gains no repository, but each CI job that runs:gradle-open-java-format:testdownloads it. That includes the native jobs on macOS and Windows; CI's Gradle cache keeps it afterwards. If those runners have an Android SDK installed, AGP should not touch it: Spotless's tasks depend on no Android task.spotless { java { target ... } }block, for example withapply plugin:further down the script, ours replaces that block's target. This can only happen in an Android project, where the plugin did nothing before.SpotlessInteropis no longer abstract. It had no abstract members left, which also removes an Error Prone warning.gradle.md,spotless.mdin openjavaformat/docs) will follow in a separate PR.