Skip to content

Format the Java files of Android projects - #288

Merged
abashev merged 1 commit into
21.X.Xfrom
abashev/please-add-support-for-android-application-and-a
Nov 28, 2026
Merged

abashev merged 1 commit into
21.X.Xfrom
abashev/please-add-support-for-android-application-and-a

Conversation

@abashev

@abashev abashev commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

Based on 21.X.X. Fixes #284.

Before this PR

The plugin added the Spotless step and formatDiff only to a project with the java plugin. The Android Gradle plugin never applies java, so in a com.android.application or com.android.library module the plugin did nothing: spotlessCheck passed whatever the files held, spotlessApply changed nothing, and there was no formatDiff. The IntelliJ plugin formatted the same files.

Applying Spotless's java {} by hand did not help either. AGP applies java-base, so the java extension is there with empty sourceSets, 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-feature and com.android.test. In those modules formatDiff is there, and the Spotless step's target is the java.srcDirs of every Android source set: main, test, androidTest, the build types and flavors. The kotlin directories are left out. A target set 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 names android.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:

    • the Java files of main, test, androidTest and debug are formatted, in an application and in a library;
    • spotlessCheck fails;
    • a generated srcDir is left alone;
    • a build script's target wins;
    • a file added later is formatted and the configuration cache entry is reused;
    • formatDiff works without Spotless;
    • Android and java-library subprojects are formatted in one parallel build, the layout of the report.

    All 16 fail on 21.X.X.

  • AndroidSourcesTest checks 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:

    • spotlessCheck fails on exactly MainActivity.java and IntermediateClass.java;
    • spotlessApply changes only those two files and leaves alone the ones the IntelliJ plugin formatted;
    • :app:formatDiff and :androidlibrary:formatDiff format only the changed lines.
  • ./gradlew check passes.

Possible downsides?

  • Every native job downloads AGP. The tests resolve AGP from google() in their own settings.gradle, so the build itself gains no repository, but each CI job that runs :gradle-open-java-format:test downloads 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.
  • Plugin order. The Android target is set when our plugin is applied. If a build applies our plugin after its own spotless { java { target ... } } block, for example with apply 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.
  • SpotlessInterop is no longer abstract. It had no abstract members left, which also removes an Error Prone warning.
  • Docs. The docs pages (gradle.md, spotless.md in openjavaformat/docs) will follow in a separate PR.

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
abashev merged commit f141709 into 21.X.X Nov 28, 2026
17 checks passed
@abashev
abashev deleted the abashev/please-add-support-for-android-application-and-a branch November 28, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Please add support for Android application and Android library subprojects in Gradle

1 participant