mobile-java
FreeNo executable scriptsNot checkedWrites, reviews and modernizes Java in Android apps: Java 17 and 21 features that D8 supports, immutability, JSpecify nullability for Kotlin interop…
by ahmadhussain0335267 installs in other catalogsAbout this skill
Java on Android
Keep Java Android code correct, leak-free and Kotlin-friendly while it lives, and move it to Kotlin in small safe steps when the team decides to.
When to use
- Editing, fixing or reviewing
.javafiles in an Android app or library. - Bumping the Java language level, or using a JDK API that may not exist on the app's
minSdk. - Java code that Kotlin code calls (or will call) and needs nullability and interop annotations.
- Leaks, ANRs or crashes caused by Handlers, executors, Fragments, adapters or unclosed resources.
- Planning or executing an incremental Java-to-Kotlin migration.
Not for: Kotlin-first code (use mobile-kotlin), architecture choices (use mobile-mvvm, mobile-mvi).
Workflow
- Read the module's build file:
compileOptionssource and target level,minSdk, and whetherisCoreLibraryDesugaringEnabledis on. These decide which features and APIs are safe. - Load the reference that matches the task: Threading, Migration to Kotlin, Testing.
- If the project already builds Kotlin and the team has not said otherwise, write new classes in Kotlin. Change existing Java minimally and in place.
- Add nullability annotations to every public signature you touch.
- Apply the rules below. Prefer fixing leaks and threading over stylistic rewrites.
- Run lint (
./gradlew lint) forNewApi, threading annotation and resource-leak checks, then the tests. - Keep conversion or modernization commits separate from behaviour changes.
Toolchain: what Java you can use
| Feature | Java | On Android |
|---|---|---|
| Lambdas, method references, default and static interface methods | 8 | Desugared by D8 for every API level |
var, switch expressions, text blocks, instanceof patterns |
10-16 | Compile-time features, every API level |
| Records | 16 | Supported by current AGP (D8 desugars them). Check reflection-based JSON libraries |
| Sealed classes and interfaces | 17 | Only on recent AGP / D8. Verify before relying on them |
Pattern matching for switch, record patterns |
21 | Needs Java 21 source level and AGP / D8 support. Verify first |
java.time (API 26), java.util.stream, Optional, java.util.function (API 24) |
library | Below those API levels only with core library desugaring |
Newer JDK methods (List.of, List.getFirst(), Stream.toList()) |
library | Tied to newer API levels. Trust lint NewApi; desugaring covers only part |
The JDK that runs Gradle (17+ for current AGP) is separate from the language level and from the APIs available at runtime. The runtime APIs come from the device's Android version.
Rules
1. Language level is free, library APIs are not
// BAD: compiles against a recent compileSdk, crashes on older devices (added to Android in API 35)
Device first = devices.getFirst();
// GOOD
Device first = devices.get(0);
// GOOD: java.time on minSdk below 26 through core library desugaring (app/build.gradle.kts)
android {
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
dependencies { coreLibraryDesugaring(libs.desugar.jdk.libs) }
Never suppress NewApi without a Build.VERSION.SDK_INT check or confirmed desugaring.
2. Immutable by default
// BAD: public mutable fields, internal list shared with callers
public class Device {
public String name;
public List<String> tags;
}
// GOOD: record with a defensive copy (works on every API level)
public record Device(String id, String name, List<String> tags) {
public Device {
Objects.requireNonNull(id);
tags = Collections.unmodifiableList(new ArrayList<>(tags));
}
}
- No records?
finalclass,private finalfields, no setters, copies in and unmodifiable views out. static finalconstants only for truly immutable values. Astatic final Listis still mutable.
3. Nullability is declared for Kotlin callers
// package-info.java: everything in the package is non-null unless marked
@NullMarked
package com.example.devices;
import org.jspecify.annotations.NullMarked;
// BAD: Kotlin sees String! (platform type) and skips null checks, NPE at runtime
public String findName(String id) { return names.get(id); }
// GOOD: inside @NullMarked, only nullable values are annotated
public @Nullable String findName(String id) { return names.get(id); }
- JSpecify
@Nullableis a type-use annotation:List<@Nullable String>,String @Nullable []. - Packages not yet
@NullMarkeduse AndroidX@NonNull/@Nullableon every public parameter and return. - Recent Kotlin compilers report JSpecify violations as errors, so wrong annotations break the Kotlin build.
4. Nothing long-lived references an Activity, Fragment or View
// BAD: static field
Install mobile-java in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
FAQ
What does the mobile-java skill do?
Writes, reviews and modernizes Java in Android apps: Java 17 and 21 features that D8 supports, immutability, JSpecify nullability for Kotlin interop, leak-free threading without static Context or inner-class Handlers, ExecutorService, LiveData, ViewBinding, ListAdapter and DiffUtil, tests with JUnit, Mockito and Espresso, and incremental migration to Kotlin. Use when editing, reviewing or migrating .java files in an Android project.
How do I install the mobile-java skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the mobile-java skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
theme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
by Anthropicaccess
Manage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropicconfigure
Set up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
by Anthropicaccess
Manage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
by Anthropic