87

In the latest versions of IntelliJ IDEA it seems that it isn't possible to import code style files like intellij-java-google-style.xml.

I would like use the Google Java Code Style in this IDE but it seems there isn't a trivial solution.

Lorenzo Lerate
  • 3,552
  • 3
  • 26
  • 25

6 Answers6

100

This problem can be solve installing the google-java-format Plugin.

  1. Open plugins window (CTRL+Shift+A): plugins

  2. Click on browse repositories.

  3. Search for google-java-format

  4. Install the plugin.

  5. Restart the IDE.

  6. Enable the plugin executing the action (Ctrl+Shift+A): Reformat with google-java-format Update: in the newer version of the plugin select Reformat since the plugin overrides / enhances the functionality of the standard "Reformat" command.

Additional notes from the google-java-format README

The plugin will be disabled by default. To enable it in the current project, go to File→Settings...→google-java-format Settings (or IntelliJ IDEA→Preferences...→Other Settings→google-java-format Settings on macOS) and check the Enable google-java-format checkbox. (A notification will be presented when you first open a project offering to do this for you.)

To enable it by default in new projects, use File→Other Settings→Default Settings....

izogfif
  • 6,000
  • 2
  • 35
  • 25
Lorenzo Lerate
  • 3,552
  • 3
  • 26
  • 25
  • 8
    Just wondering: can the plugin be configured, like: changing the indenting from 2 spaces to 4 spaces? – GhostCat Aug 17 '18 at 09:10
  • 1
    @GhostCat Nope. The plugin uses https://github.com/google/google-java-format which is not configurable unless you copy the sources and modify them yourself of course. – Jonas Gröger Oct 03 '18 at 13:40
  • 1
    on the Mac, this is CMD+SHIFT+A. – Brad Parks Jun 27 '19 at 14:05
  • 2
    it worth mentioning that currently import ordering is not supported using the plugin, whilst manually importing the xml file config adding a new scheme style does it. – kekko12 Apr 23 '20 at 18:25
  • 1
    for enable it by default for all new projects: File -> New Project Settings -> Preference for new project settings. Then the rest is the same. – RoundPi Apr 27 '20 at 09:00
  • How to use this plugin after enabling it? – Evandro Coan Jul 16 '20 at 22:51
  • @user As explained in step 6: Ctrl+Shift+A => Reformat with google-java-format – Lorenzo Lerate Jul 18 '20 at 19:53
  • I found that sticking to IntelliJ's Default Code Style (which is configurable) is better. @GhostCat – Melvin Feb 20 '21 at 17:43
  • We work in a global team, with people using different tools. So a single tool solution won't work for our context. – GhostCat Feb 20 '21 at 19:56
  • 5
    When doing Ctrl+Shift+A, there is no "Reformat with google-java-format" option available. – Hector Ricardo Apr 27 '21 at 00:07
  • 1
    @HectorRicardo [It says](https://github.com/google/google-java-format#intellij-android-studio-and-other-jetbrains-ides) "When enabled, **it will replace the normal Reformat Code action**, which can be triggered from the Code menu or with the Ctrl-Alt-L (by default) keyboard shortcut." So choose "Reformat code" from Ctrl+Shift+A. – izogfif Apr 11 '22 at 16:51
  • how do we add specific version of the plugin to intelliJ eg(1.7)? – pujan kc Apr 19 '22 at 14:49
59

As mentioned Here, Download the intellij-java-google-style.xml file from the This Link.

Then in Intellij, go Under Settings -> Editor -> Code Style. there in Scheme settings (settings icon on right side) -> import schemes-> intellij idea code style xml. Select the xml downloaded in first step. then in Scheme dropdown select the GoogleStyle IDE (newly added style). Click on apply and close.

Denis Abakumov
  • 355
  • 3
  • 11
rahul maindargi
  • 5,359
  • 2
  • 16
  • 23
  • 2
    I had the same problem, and was about to add exactly this answer here. Well, upvoting an existing answer is less work though ;-) – GhostCat Aug 17 '18 at 09:07
  • 4
    Please note that using the xml code style file will not be 100% match against code formatted with the actual google code formatter. This will become an annoyance if (as you maybe should) you will format the code with google formatter during e.g. a git hook or on CI server. – Raipe Nov 06 '18 at 09:25
  • 1
    For some reason the xml file downloaded from the above, couldn't be imported into IntelliJ. As mentioned [here](https://github.com/google/google-java-format/issues/325), the xml file at the below link worked: https://raw.githubusercontent.com/google/styleguide/gh-pages/intellij-java-google-style.xml – Melvin Feb 20 '21 at 16:36
6

After doing above steps mentioned by Lorenzo, for applying the setting in the current project :- Intellij>Preferences>google-java-format setting and enable it in intellij; as it wont be enabled by default after doing restart as well.

If you want to have this setting for the projects which will you create after installing this plugin , then Go to File >Other Settings> Default Settings> google-java-format-setting and enable it

Rancho
  • 976
  • 1
  • 8
  • 16
  • Here's a way to enable the code style for all existing IntelliJ projects: https://stackoverflow.com/a/65634361/5752730 – DV82XL Jan 08 '21 at 21:07
1

on a mac this was the easiest for me:

wget https://raw.githubusercontent.com/google/styleguide/gh-pages/intellij-java-google-style.xml

then cmd+, code style > Java > Scheme (select the gear icon) > import scheme > IntelliJ IDEA code style XML select the intellij-java-google-style.xml wherever you stored it on your system.

Zach Folwick
  • 893
  • 10
  • 18
0

You can also simply add this xml file to Intellij configuration folder

/home/USERNAME/.IntelliJIdea17/config/codestyles

Then you can make this code style the default one.

File/Settings/Editor/Code Style ==> Scheme = GoogleStyle

Kamiel Ahmadpour
  • 1,215
  • 12
  • 16
0

For the latest version(2020.3.2), as mentioned by Gob00st, to enable it by default in new projects, use File>New Project Settings>Settings for New Projects>Other Settings>google-java-format Settings

lecranek
  • 11
  • 1