Android Code Studio implements the Language Server Protocol (LSP) for Java, Kotlin, and XML, providing intelligent code features like completion, diagnostics, navigation, and refactoring directly on Android devices.
// Get compiler for current moduleval module = workspace.findModuleForFile(file)val compiler = JavaCompilerProvider.get(module)// Parse file for analysisval parseTask = compiler.parse(file)val compilationUnit = parseTask.root// Analyze codeval trees = parseTask.treesval elements = parseTask.elements
Each module has its own compiler instance to ensure proper classpath
isolation and accurate code analysis.
public class DefinitionProvider { public Location findDefinition( File file, int line, int column ) { ParseTask task = compiler.parse(file); // Find name at cursor FindNameAt visitor = new FindNameAt(line, column); visitor.scan(task.root, null); Tree nameTree = visitor.getName(); if (nameTree == null) return null; // Resolve to definition Element element = trees.getElement(nameTree); if (element == null) return null; // Get definition location TreePath path = trees.getPath(element); return getLocation(path); }}
public class ReferenceProvider { public List<Location> findReferences( File file, int line, int column ) { // Find element at cursor Element element = findElementAt(file, line, column); // Search all files in module List<Location> references = new ArrayList<>(); for (File sourceFile : module.getSourceFiles()) { FindReferences visitor = new FindReferences(element); ParseTask task = compiler.parse(sourceFile); visitor.scan(task.root, null); references.addAll(visitor.getReferences()); } return references; }}
Kotlin LSP implementation with compiler integration:
public class KotlinCompilerService { private final KotlinCoreEnvironment environment; private final ModuleProject module; public KotlinCompilerService(ModuleProject module) { this.module = module; this.environment = createEnvironment(module); } public KtFile parse(File file) { String content = Files.readString(file.toPath()); return KotlinPsiFactory.createFile( environment.getProject(), file.getName(), content ); } public AnalysisResult analyze(List<KtFile> files) { return AnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration( environment.getProject(), files, /* ... */ ); }}
The Kotlin LSP leverages the official Kotlin compiler for accurate analysis
and supports all Kotlin language features including coroutines and extension
functions.
public class XMLFormatter { public List<TextEdit> format(Document document) { List<TextEdit> edits = new ArrayList<>(); // Format elements DOMElementFormatter elementFormatter = new DOMElementFormatter(); elementFormatter.format(document, edits); // Format attributes DOMAttributeFormatter attrFormatter = new DOMAttributeFormatter(); attrFormatter.format(document, edits); return edits; }}
<!-- Before --><LinearLayout><TextView android:text="Hello"/></LinearLayout><!-- After --><LinearLayout> <TextView android:text="Hello" /></LinearLayout>
// Find variables in scopepublic class FindVariablesBetween extends TreePathScanner<Void, Void> { private final int startLine; private final int endLine; private final List<VariableElement> variables = new ArrayList<>(); @Override public Void visitVariable(VariableTree node, Void unused) { long line = getLine(node); if (line >= startLine && line <= endLine) { Element element = trees.getElement(getCurrentPath()); if (element instanceof VariableElement) { variables.add((VariableElement) element); } } return super.visitVariable(node, unused); } public List<VariableElement> getVariables() { return variables; }}
// Set language server in editorval languageServer = getLanguageServerForFile(file)editor.setLanguageServer(languageServer)// Request completionseditor.requireAutoComplete()// Navigate to definitioneditor.findDefinition()// Show signature helpeditor.signatureHelp()