Skip to content

Commit 7038de1

Browse files
committed
👷 Add Spotless Java linting to CI pipeline
Add automated Java code style checking using Spotless with: - Google Java Format (AOSP style) for Android-friendly formatting - Gradle project in pythonforandroid/bootstraps/ for linting - New 'spotless' job in GitHub Actions workflow - Makefile targets: java-lint, java-lint-fix, docker/java-lint - Excludes third-party code (org/kamranzafar/jtar/) Downstream CI jobs now depend on both flake8 and spotless checks.
1 parent be1daa7 commit 7038de1

File tree

7 files changed

+166
-5
lines changed

7 files changed

+166
-5
lines changed

.github/workflows/push.yml

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,26 @@ jobs:
3030
pip install tox>=2.0
3131
tox -e pep8
3232
33+
spotless:
34+
name: Java Spotless check
35+
runs-on: ubuntu-latest
36+
steps:
37+
- name: Checkout python-for-android
38+
uses: actions/checkout@v5
39+
- name: Set up Java 17
40+
uses: actions/setup-java@v4
41+
with:
42+
distribution: 'temurin'
43+
java-version: '17'
44+
- name: Set up Gradle
45+
uses: gradle/actions/setup-gradle@v4
46+
- name: Run Spotless check
47+
working-directory: pythonforandroid/bootstraps
48+
run: ./common/build/gradlew spotlessCheck
49+
3350
test:
3451
name: Pytest [Python ${{ matrix.python-version }} | ${{ matrix.os }}]
35-
needs: flake8
52+
needs: [flake8, spotless]
3653
runs-on: ${{ matrix.os }}
3754
strategy:
3855
fail-fast: false
@@ -61,7 +78,7 @@ jobs:
6178

6279
ubuntu_build:
6380
name: Build test APP [ ${{ matrix.runs_on }} | ${{ matrix.bootstrap.name }} ]
64-
needs: [flake8]
81+
needs: [flake8, spotless]
6582
runs-on: ${{ matrix.runs_on }}
6683
strategy:
6784
matrix:
@@ -120,7 +137,7 @@ jobs:
120137

121138
macos_build:
122139
name: Build test APP [ ${{ matrix.runs_on }} | ${{ matrix.bootstrap.name }} ]
123-
needs: [flake8]
140+
needs: [flake8, spotless]
124141
runs-on: ${{ matrix.runs_on }}
125142
strategy:
126143
matrix:
@@ -194,7 +211,7 @@ jobs:
194211

195212
ubuntu_rebuild_updated_recipes:
196213
name: Test updated recipes for arch ${{ matrix.android_arch }} [ ubuntu-latest ]
197-
needs: [flake8]
214+
needs: [flake8, spotless]
198215
runs-on: ubuntu-latest
199216
# continue on error to see failures across all architectures
200217
continue-on-error: true
@@ -227,7 +244,7 @@ jobs:
227244
228245
macos_rebuild_updated_recipes:
229246
name: Test updated recipes for arch ${{ matrix.android_arch }} [ ${{ matrix.runs_on }} ]
230-
needs: [flake8]
247+
needs: [flake8, spotless]
231248
runs-on: ${{ matrix.runs_on }}
232249
# continue on error to see failures across all architectures
233250
continue-on-error: true

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,9 @@ coverage.xml
3636
# testapp's build folder
3737
testapps/build/
3838

39+
# Gradle build artifacts (Java linting)
40+
pythonforandroid/bootstraps/.gradle/
41+
pythonforandroid/bootstraps/build/
42+
3943
# Dolphin (the KDE file manager autogenerates the file `.directory`)
4044
.directory

CONTRIBUTING.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,60 @@ latest python-for-android release that supported building Python 2 was version
6666
On August 2021, we added support for Android App Bundle (aab). As a
6767
collateral benefit, we now support multi-arch apk.
6868

69+
## Code Quality
70+
71+
### Python Linting
72+
73+
Python code is linted using flake8. Run it locally with:
74+
75+
```bash
76+
tox -e pep8
77+
```
78+
79+
### Java Linting
80+
81+
Java source files in the bootstrap directories are linted using
82+
[Spotless](https://github.com/diffplug/spotless) with Google Java Format
83+
(AOSP style). The CI runs this check automatically.
84+
85+
**Local execution** (requires Java 17+):
86+
87+
```bash
88+
# Check for violations
89+
make java-lint
90+
91+
# Auto-fix violations
92+
make java-lint-fix
93+
```
94+
95+
The Makefile uses the Gradle wrapper (`gradlew`), which automatically downloads
96+
the correct Gradle version on first run. No manual Gradle installation is required.
97+
98+
**Using Docker** (if you don't have Java 17):
99+
100+
```bash
101+
# Check for violations
102+
make docker/java-lint
103+
104+
# Auto-fix violations
105+
make docker/java-lint-fix
106+
```
107+
108+
The Docker approach builds the project's Docker image (which includes Java 17)
109+
and runs the linting inside the container.
110+
111+
**What gets linted:**
112+
113+
- All `.java` files in `pythonforandroid/bootstraps/*/build/src/main/java/`
114+
- Excludes third-party code (`org/kamranzafar/jtar/`)
115+
116+
**Formatting rules applied:**
117+
118+
- Google Java Format with AOSP style (Android-friendly indentation)
119+
- Removal of unused imports
120+
- Trailing whitespace trimming
121+
- Files end with newline
122+
69123
## Creating a new release
70124

71125
(These instructions are for core developers, not casual contributors.)

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ virtualenv: $(VIRTUAL_ENV)
2424
test:
2525
$(TOX) -- tests/ --ignore tests/test_pythonpackage.py
2626

27+
# Java linting using Spotless (requires Java 17+, uses Gradle wrapper)
28+
java-lint:
29+
cd pythonforandroid/bootstraps && ./common/build/gradlew spotlessCheck
30+
31+
java-lint-fix:
32+
cd pythonforandroid/bootstraps && ./common/build/gradlew spotlessApply
33+
34+
# Java linting via Docker (no local Java required)
35+
docker/java-lint: docker/build
36+
docker run --rm -v $(CURDIR):/home/user/app -w /home/user/app/pythonforandroid/bootstraps $(DOCKER_IMAGE) ./common/build/gradlew spotlessCheck
37+
38+
docker/java-lint-fix: docker/build
39+
docker run --rm -v $(CURDIR):/home/user/app -w /home/user/app/pythonforandroid/bootstraps $(DOCKER_IMAGE) ./common/build/gradlew spotlessApply
40+
2741
# Also install and configure rust
2842
rebuild_updated_recipes: virtualenv
2943
. $(ACTIVATE) && \
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Java Lint Configuration for python-for-android
2+
// This file configures Spotless to lint Java source files across all bootstraps
3+
4+
plugins {
5+
id 'java'
6+
id 'com.diffplug.spotless' version '6.25.0'
7+
}
8+
9+
// Repositories for plugin dependencies (e.g., google-java-format)
10+
repositories {
11+
mavenCentral()
12+
}
13+
14+
// Define the root directory for bootstrap Java sources
15+
def bootstrapsDir = "${rootProject.projectDir}"
16+
17+
// Collect all Java source directories from all bootstraps
18+
def javaSourceDirs = []
19+
file(bootstrapsDir).eachDir { bootstrapDir ->
20+
def srcDir = new File(bootstrapDir, 'build/src/main/java')
21+
if (srcDir.exists()) {
22+
javaSourceDirs.add(srcDir.absolutePath)
23+
}
24+
}
25+
26+
sourceSets {
27+
main {
28+
java {
29+
srcDirs = javaSourceDirs
30+
}
31+
}
32+
}
33+
34+
spotless {
35+
java {
36+
// Target all Java files from the source directories
37+
target fileTree(bootstrapsDir) {
38+
include '**/build/src/main/java/**/*.java'
39+
// Exclude third-party vendored code
40+
exclude '**/org/kamranzafar/jtar/**'
41+
}
42+
43+
// Use Google Java Format with AOSP style (Android-friendly, slightly relaxed)
44+
googleJavaFormat('1.19.2').aosp()
45+
46+
// Remove unused imports
47+
removeUnusedImports()
48+
49+
// Trim trailing whitespace
50+
trimTrailingWhitespace()
51+
52+
// Ensure files end with a newline
53+
endWithNewline()
54+
}
55+
}
56+
57+
// Disable compilation - we only want to lint, not build
58+
tasks.withType(JavaCompile).configureEach {
59+
enabled = false
60+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Gradle properties for Java lint project
2+
# Disable daemon for CI environments
3+
org.gradle.daemon=false
4+
5+
# Use parallel execution where possible
6+
org.gradle.parallel=true
7+
8+
# Configure JVM memory
9+
org.gradle.jvmargs=-Xmx512m
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Java Lint Project Settings
2+
// This project is used for linting Java source files in CI
3+
rootProject.name = 'p4a-java-lint'

0 commit comments

Comments
 (0)