You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android Studio中调整Kotlin代码排序规则以匹配Java风格?

Fixing Kotlin Method Override & Code Formatting Order in Android Studio (2019)

Hey there! I totally get how frustrating this formatting inconsistency between Java and Kotlin in Android Studio can be—especially when you’re used to the predictable ordering from Java and it throws off your workflow. Let’s walk through the exact settings tweaks to fix this:

Step 1: Access Kotlin Code Style Settings

Open Android Studio’s Settings (shortcut: Ctrl+Alt+S on Windows/Linux, Cmd+, on Mac), then navigate to Editor > Code Style > Kotlin.

Step 2: Configure Overridden Method Order

Switch to the Arrangement tab—this is where all code ordering rules live:

  • Look for the rule labeled Override methods (it might be near the top by default). Drag it to your preferred position (e.g., after constructors, before your custom class methods).
  • Make sure the checkbox Keep overridden methods in the same order as in superclass/interface is checked. This will mimic Java’s behavior, keeping your overridden methods aligned with the interface/superclass definition order.

Step 3: Adjust Order of Properties, Constructors, and Other Elements

Still in the Arrangement tab, you can reorder other code components to match your Java habits:

  • Drag Constructors to the position you want (usually near the top, before properties or methods)
  • Rearrange Properties (your global variables) to sit where you prefer relative to constructors and methods
  • Tweak any other rules to match your personal workflow

Step 4: Apply Settings and Test Formatting

Click Apply then OK to save your changes. Now use the code formatting shortcut (Ctrl+Alt+L on Windows/Linux, Cmd+Option+L on Mac) on a Kotlin class—your overridden methods should now stay in the correct order, and all code elements should sort according to your rules.

Bonus: Fix Persistent Cache Issues

If you’ve already tried deleting settings and reinstalling, the problem might be stuck in Android Studio’s cache. Try this:

  1. Go to File > Invalidate Caches...
  2. Select Invalidate and Restart
  3. Once Studio reopens, reapply your code style settings (they might reset after invalidation)

Final Check: Update Kotlin Plugin

Make sure your Kotlin plugin is up to date (Settings > Plugins > Kotlin > Check for updates). Older plugin versions in 2019 sometimes had bugs with code arrangement rules, so updating could resolve lingering issues.

I hope this gets your Kotlin code formatting back to the predictable workflow you love!

内容的提问来源于stack exchange,提问作者Serhii Pokrovskyi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 03:46:01