'Android Studio: Save format file to the repo

We are having an issue on a team involving several team members using Android Studio. Basically, I am not sure why, but the formatting is different across different Android Studio versions. This is of course annoying for version control, so I would like to know what is idiomatic way of synchronizing the formatting between several users. My preference would be to commit a format file to the repo to ensure that everybody is using exactly the same formatting.

Our team has a lot of experience working with C++ projects, where we would typically just push a clang-format file to the repo so that we are all using the same formatting. However, Android Studio doesn't seem to support clang-format.



Solution 1:[1]

I assume you are looking for Editor Configuration.

In your Editor settings (Preferences > Editor > Code Style) choose the Project option for the Scheme (default is IDE) and export the Configuration file as shown on the screenshot below. This will create .editorconfig file in your project root folder so you could share it in repo.

To apply created Config over the custom studio editor style please make sure to Enable EditorConfig support in the Preferences > Editor > Code Style.

enter image description here

Solution 2:[2]

What you could do is just add contents of the .idea\codeStyles\ like Project.xml and codeStyleConfig.xml to version control. Also exclude it from .gitignore with:

#IntelliJ
  ...
  .idea/*
  !.idea/codeStyles/*

codeStyleConfig.xml should have this option inside:

       <option name="USE_PER_PROJECT_SETTINGS" value="true" />

so that code style is taken from the project rather than from IDE.

And then go to File -> Settings -> Editor -> Code Style and check if Scheme is Project(Stored in Project) instead of Default(Stored in IDE). If it is, then your team members should just need to pull the changes, check in the Code Style section that all the options are same, restart the IDE and that should be it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DmitryArc
Solution 2 Torima