Skip to main content
Android Code Studio provides comprehensive Java language support through an integrated Java Language Server Protocol (LSP) implementation powered by the OpenJDK compiler.

Language Server Features

The Java LSP provides intelligent code analysis and editing capabilities:

Code Completion

Smart Completions

Context-aware suggestions for classes, methods, fields, and variables with type information

Auto-Import

Automatic import statement generation when completing types from external packages

Snippet Support

Built-in code snippets for common patterns like loops, conditionals, and try-catch blocks

Parameter Hints

Method parameter information and signature help during invocation
The completion engine uses the FindCompletionsAt visitor to locate completion points in the AST and provides suggestions based on:
  • Local variables and method parameters
  • Class members (fields and methods)
  • Imported and classpath types
  • Java language keywords
Implementation: The completion provider analyzes the compilation unit tree at the cursor position to determine the appropriate completion context. Source: java/lsp/src/main/java/com/tom/rv2ide/lsp/java/visitors/FindCompletionsAt.java

Diagnostics

Real-time error detection and reporting:
  • Compilation Errors: Syntax errors, type mismatches, and undeclared variables
  • Semantic Analysis: Method signature conflicts, accessibility issues, and unresolved symbols
  • Quick Fixes: Automated fixes for common issues like missing imports or method implementations
The diagnostic system uses incremental compilation to minimize performance impact. Only changed methods are reparsed when possible, utilizing the PartialReparser for efficient updates.

Code Navigation

Jump to the declaration of any class, method, or field. The language server searches both source files and compiled classpath entries.
// Click on 'Button' to navigate to its definition
Button myButton = findViewById(R.id.button);

Code Examples

Basic Java Class

MainActivity.java
package com.example.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView = findViewById(R.id.textView);
        textView.setText("Hello, Android!");
    }
}

Advanced Features

// Java 8 lambda expressions
button.setOnClickListener(view -> {
    Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show();
});

// Method references
list.forEach(System.out::println);

Supported Language Features

Core Java Features

  • Java 8-17 Syntax: Full support for modern Java language features
  • Generics: Complete type parameter inference and validation
  • Annotations: Processing of standard and custom annotations
  • Inner Classes: Anonymous classes, local classes, and member classes
  • Enums: Enumeration types with methods and constructors

Android-Specific

  • Android API Completion: Context-aware suggestions for Android SDK classes
  • Resource References: Integration with R class for resource access
  • Manifest Awareness: Understanding of AndroidManifest.xml configurations

Code Rewrites and Refactoring

The language server provides automated code transformations:

Add Import

Automatically insert import statements for unresolved types

Implement Abstract Methods

Generate stubs for inherited abstract methods

Remove Unused

Clean up unused imports, methods, and classes

Generate Constructors

Create constructors for record types and regular classes
Available Rewrites (from java/lsp/src/main/java/com/tom/rv2ide/lsp/java/rewrite/):
  • AddImport: Insert missing import statements
  • ImplementAbstractMethods: Implement methods from interfaces/abstract classes
  • RemoveMethod, RemoveClass, RemoveException: Code cleanup operations
  • CreateMissingMethod: Generate method stubs for undefined references
  • AddSuppressWarningAnnotation: Add @SuppressWarnings annotations

Snippets

Built-in code snippets accelerate development:
// Type 'for' and press Tab
for(int i = 0; i < count; ++i) {
    // Loop body
}

// Type 'foreach' and press Tab
for(element : iterable) {
    // Loop body
}

// Type 'while' and press Tab
while(condition) {
    // Loop body
}
Snippets are defined in java/lsp/src/main/assets/data/editor/java/ and support placeholders, tab stops, and transformations.

Compiler Configuration

Source and Target Compatibility

The Java compiler service (JavaCompilerService) can be configured through the module’s build configuration:
public class JavaCompilerConfig {
    private String sourceCompatibility = "1.8";
    private String targetCompatibility = "1.8";
    private List<String> compileOptions = new ArrayList<>();
}

Classpath Management

The language server automatically manages classpaths:
  • Boot Classpath: Android SDK classes from android.jar
  • Compile Classpath: Project dependencies and library JARs
  • Source Classpath: Project source directories
The SourceFileManager handles file resolution across source paths, ensuring efficient lookup of Java source files during compilation and navigation.

Performance Optimizations

Incremental Compilation

The compiler uses several strategies to minimize compilation overhead:
  1. Cached Compilation: Reuses previous compilation results when source files haven’t changed
  2. Partial Reparsing: Only recompiles modified methods within a class (when possible)
  3. Synchronized Tasks: Manages compilation requests to prevent redundant work
// From JavaCompilerService.java
private boolean needsCompilation(Collection<? extends JavaFileObject> sources) {
    if (cachedModified.size() != sources.size()) {
        return true;
    }
    for (JavaFileObject f : sources) {
        if (!cachedModified.containsKey(f) || 
            f.getLastModified() != cachedModified.get(f)) {
            return true;
        }
    }
    return false;
}

Caching

Multiple cache layers improve responsiveness:
  • Type Declaration Cache: Caches parsed compilation units
  • Import Cache: Stores import statements per file
  • Word Search Cache: Optimizes text-based symbol searches

Limitations

The following features have limited or no support:
  • Multi-module Projects: Cross-module navigation may be incomplete
  • External Source Attachment: Source code for external libraries must be provided
  • Incremental Build: Full recompilation is required after structural changes
  • Code Generation: Some annotation processors may not be fully supported

API Reference

Key interfaces and classes:

CompilerProvider

The main interface for interacting with the Java compiler:
public interface CompilerProvider {
    // Get all public top-level types
    TreeSet<String> publicTopLevelTypes();
    
    // Find type declaration by fully qualified name
    Path findTypeDeclaration(String className);
    
    // Parse a Java source file
    ParseTask parse(Path file);
    
    // Compile source files
    SynchronizedTask compile(CompilationRequest request);
    
    // Find references to a type or member
    Path[] findTypeReferences(String className);
    Path[] findMemberReferences(String className, String memberName);
}

JavaCompilerService

The main implementation of CompilerProvider:
// Create a compiler service for a module
JavaCompilerService compiler = new JavaCompilerService(module);

// Parse a file
ParseTask task = compiler.parse(filePath);

// Compile sources
CompilationRequest request = new CompilationRequest(sources);
SynchronizedTask result = compiler.compile(request);

// Find type declaration
Path declaration = compiler.findTypeDeclaration("com.example.MyClass");

Kotlin Support

Learn about Kotlin language features

XML Support

Explore XML editing capabilities

Code Editor

Understand editor features and shortcuts

Build System

Configure build options and dependencies

Build docs developers (and LLMs) love