I use Android Studio and when I commit to a repo on GitHub and view git diff I see a red marker at end of line for few files with a warning 'No newline at end of file'. How can I configure Android Studio to ensure that it does add a newline at file of file for every file. Thank you.
4 Answers
Yes, you can configure Android Studio to automatically add a new line at the end of a file.
File | Settings...
menu, then Editor
under IDE Settings
, then check the Ensure line feed at file end on Save
option box.
This will only take effect on freshly edited files after the setting has been enabled.
UPDATE
On newer versions of Android Studio, the preference location has changed to
File > Preferences > Editor > General > Ensure line feed at file end on Save

- 1,307
- 2
- 16
- 30

- 1,144
- 1
- 9
- 5
-
1It is `File → Settings → ..`. – creativecreatorormaybenot Jun 16 '20 at 19:13
-
Thanks for the answer. I was looking for justification of _why_ to include a newline at the end of a file but couldn't find it. Why do we do that and why does Github call it out? Is it something with *nix? Edit: Sorry [here's the discussion](https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline) I'm looking for – Brady Dowling Aug 07 '20 at 18:58
-
@creativecreatorormaybenot He's probably referring to Mac. Though on Mac it would be `Android Studio > Preferences > ...` – Henri Langenhoven Oct 14 '20 at 06:55
-
7Option label now is `Ensure every saved file ends with a line break`. Also, for it to work properly, you have to uncheck `Keep trailing spaces on caret line`. – Cesar Castro Jul 19 '21 at 18:28
In the latest Android Studio (2.3.3), the setting has moved.
Android Studio -> Preferences -> Editor -> General -> Ensure line feed at file end on Save

- 841
- 8
- 11
-
In Android Studio 3.5 `File` -> `Settings` -> `Editor` -> `General` -> `Ensure line feed at file end on Save` – Marlon Oct 03 '19 at 19:05
On Android Studio Bumblbee open Preferences -> General -> enable option "Ensure every saved file ends with a line break"

- 452
- 1
- 6
- 16
The previous changes for Android Studio (53630 - 52407 and 54132 - 52407: should add a newline at the end of autogenerated xml files) seem to suggest there is no global setting to add a newline for every file.
It may have changed depending on the Android Studio you are using (eclipse-based or the more recent IntellJ IDEA-based), but you can check the formatters associated with the different types of files: they usually have that option "add newline at the end of a (certain type of) file".

- 1,262,500
- 529
- 4,410
- 5,250